1 /***
2 * @(#)JDOServiceImpl.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.persistence;
26
27 import java.io.IOException;
28 import java.util.Collection;
29 import java.util.Map;
30 import java.util.Properties;
31
32 import javax.jdo.JDOException;
33 import javax.jdo.JDOHelper;
34 import javax.jdo.PersistenceManager;
35 import javax.jdo.PersistenceManagerFactory;
36 import javax.jdo.Query;
37 import javax.jdo.Transaction;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.huihoo.jfox.soaf.exception.DAOException;
42 import org.huihoo.jfox.soaf.util.resource.ResourceHelper;
43 import org.picocontainer.Startable;
44
45 /***
46 * <p>
47 * JDO persistence service wrapper implementation
48 * </p>
49 *
50 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
51 * @version $Revision:$ $Date:$
52 * @version Revision: 1.0
53 */
54
55 public class JdoServiceImpl implements JdoService, Startable {
56
57 private final Log logger = LogFactory.getLog(getClass());
58
59 private static final String JDO_CONFIG = "jpox.properties";
60
61 private PersistenceManagerFactory pmFactory;
62
63 private Transaction transaction;
64
65 private PersistenceManager pm;
66
67 /***
68 * Default Constructor.
69 */
70 public JdoServiceImpl() throws Exception {
71 }
72
73 /***
74 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getPersistenceManager()
75 */
76 public PersistenceManager getPersistenceManager() throws JDOException {
77 if (pm == null) {
78 pm = pmFactory.getPersistenceManager();
79 }
80 return pm;
81 }
82
83 /***
84 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#openTransaction()
85 */
86 public void beginTransaction() throws JDOException {
87 if (pm == null && transaction == null) {
88 pm = getPersistenceManager();
89 transaction = pm.currentTransaction();
90 transaction.begin();
91 }
92 }
93
94 /***
95 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#commit()
96 */
97 public void commit() throws JDOException {
98 if (pm != null && !pm.isClosed() && transaction != null
99 && transaction.isActive()) {
100 transaction.commit();
101 }
102 }
103
104 /***
105 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#rollback()
106 */
107 public void rollback() throws JDOException {
108 if (pm != null && !pm.isClosed() && transaction != null
109 && transaction.isActive()) {
110 transaction.rollback();
111 }
112 }
113
114 /***
115 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#close()
116 */
117 public void close() throws JDOException {
118 if (pm != null && !pm.isClosed()) {
119 pm.close();
120 pm = null;
121 }
122 }
123
124 /***
125 * @see org.picocontainer.Startable#start()
126 */
127 public void start() {
128 try {
129 Properties prop = ResourceHelper
130 .getResourceAsProperties(JDO_CONFIG);
131 pmFactory = JDOHelper.getPersistenceManagerFactory(prop);
132 } catch (IOException e) {
133 logger.error("Failed to start JDO service " + e.getMessage());
134 }
135 logger.info("Start jdo service successful.");
136 }
137
138 /***
139 * @see org.picocontainer.Startable#stop()
140 */
141 public void stop() {
142 }
143
144 /***
145 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object)
146 */
147 public Object getObjectById(Object oid) throws JDOException, DAOException {
148 Object obj;
149 PersistenceManager pm;
150 try {
151 pm = getPersistenceManager();
152 beginTransaction();
153 obj = pm.getObjectById(oid);
154 commit();
155 } catch (Exception e) {
156 rollback();
157 throw new DAOException(e);
158 } finally {
159 pm = null;
160 close();
161 }
162 return obj;
163 }
164
165 /***
166 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Class, Object)
167 */
168 public Object getObjectById(Class clazz, Object oid) throws JDOException,
169 DAOException {
170 Object obj;
171 PersistenceManager pm;
172 try {
173 pm = getPersistenceManager();
174 beginTransaction();
175 obj = pm.getObjectById(clazz, oid);
176 commit();
177 } catch (Exception e) {
178 rollback();
179 throw new DAOException(e);
180 } finally {
181 pm = null;
182 close();
183 }
184 return obj;
185 }
186
187 /***
188 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object, boolean)
189 */
190 public Object getObjectById(Object oid, boolean validate)
191 throws JDOException, DAOException {
192 Object obj;
193 PersistenceManager pm;
194 try {
195 pm = getPersistenceManager();
196 beginTransaction();
197 obj = pm.getObjectById(oid, validate);
198 commit();
199 } catch (Exception e) {
200 rollback();
201 throw new DAOException(e);
202 } finally {
203 pm = null;
204 close();
205 }
206 return obj;
207 }
208
209 /***
210 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object)
211 */
212 public Object getObjectId(Object pc) throws JDOException, DAOException {
213 Object obj;
214 PersistenceManager pm;
215 try {
216 pm = getPersistenceManager();
217 beginTransaction();
218 obj = pm.getObjectById(pc);
219 commit();
220 } catch (Exception e) {
221 rollback();
222 throw new DAOException(e);
223 } finally {
224 pm = null;
225 close();
226 }
227 return obj;
228 }
229
230 /***
231 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Class)
232 */
233 public Class getObjectIdClass(Class clazz) throws JDOException,
234 DAOException {
235 Class claz;
236 PersistenceManager pm;
237 try {
238 pm = getPersistenceManager();
239 beginTransaction();
240 claz = pm.getObjectIdClass(clazz);
241 commit();
242 } catch (Exception e) {
243 rollback();
244 throw new DAOException(e);
245 } finally {
246 pm = null;
247 close();
248 }
249 return claz;
250 }
251
252 /***
253 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectsById(Collection)
254 */
255 public Collection getObjectsById(Collection oids) throws JDOException,
256 DAOException {
257 Collection coll;
258 PersistenceManager pm;
259 try {
260 pm = getPersistenceManager();
261 beginTransaction();
262 coll = pm.getObjectsById(oids);
263 commit();
264 } catch (Exception e) {
265 rollback();
266 throw new DAOException(e);
267 } finally {
268 pm = null;
269 close();
270 }
271 return coll;
272 }
273
274 /***
275 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectById(Object, boolean)
276 */
277 public Collection getObjectsById(Collection oids, boolean validate)
278 throws JDOException, DAOException {
279 Collection coll;
280 PersistenceManager pm;
281 try {
282 pm = getPersistenceManager();
283 beginTransaction();
284 coll = pm.getObjectsById(oids, validate);
285 commit();
286 } catch (Exception e) {
287 rollback();
288 throw new DAOException(e);
289 } finally {
290 pm = null;
291 close();
292 }
293 return coll;
294 }
295
296 /***
297 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectsById(Object[])
298 */
299 public Object[] getObjectsById(Object[] oids) throws JDOException,
300 DAOException {
301 Object[] objects;
302 PersistenceManager pm;
303 try {
304 pm = getPersistenceManager();
305 beginTransaction();
306 objects = pm.getObjectsById(oids);
307 commit();
308 } catch (Exception e) {
309 rollback();
310 throw new DAOException(e);
311 } finally {
312 pm = null;
313 close();
314 }
315 return objects;
316 }
317
318 /***
319 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#getObjectsById(Object[], boolean)
320 */
321 public Object[] getObjectsById(Object[] oids, boolean validate)
322 throws JDOException, DAOException {
323 Object[] objects;
324 PersistenceManager pm;
325 try {
326 pm = getPersistenceManager();
327 beginTransaction();
328 objects = pm.getObjectsById(oids, validate);
329 commit();
330 } catch (Exception e) {
331 rollback();
332 throw new DAOException(e);
333 } finally {
334 pm = null;
335 close();
336 }
337 return objects;
338 }
339
340 /***
341 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class)
342 */
343 public Collection query(Class clazz) throws JDOException, DAOException {
344 Collection coll;
345 PersistenceManager pm;
346 Query query;
347 try {
348 pm = getPersistenceManager();
349 beginTransaction();
350 query = pm.newQuery(clazz);
351 coll = (Collection) query.execute();
352 commit();
353 } catch (Exception e) {
354 rollback();
355 throw new DAOException(e);
356 } finally {
357 pm = null;
358 close();
359 }
360 return coll;
361 }
362
363 /***
364 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, String)
365 */
366 public Collection query(Class clazz, String filter) throws JDOException,
367 DAOException {
368 return query(clazz, filter, null);
369 }
370
371 /***
372 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, String)
373 */
374 public Collection query(Class clazz, String filter, String ordering)
375 throws JDOException, DAOException {
376 Collection coll;
377 PersistenceManager pm;
378 Query query;
379 try {
380 pm = getPersistenceManager();
381 beginTransaction();
382 query = pm.newQuery(clazz, filter);
383 if (ordering != null) {
384 query.setOrdering(ordering);
385 }
386 coll = (Collection) query.execute();
387 commit();
388 } catch (Exception e) {
389 rollback();
390 throw new DAOException(e);
391 } finally {
392 pm = null;
393 close();
394 }
395 return coll;
396 }
397
398 /***
399 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, Map)
400 */
401 public Collection queryWithMap(Class clazz, String filter,
402 String parameters, Map values) throws JDOException, DAOException {
403 return queryWithMap(clazz, filter, null, parameters, values);
404 }
405
406 /***
407 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, String, Map)
408 */
409 public Collection queryWithMap(Class clazz, String filter, String ordering,
410 String parameters, Map values) throws JDOException, DAOException {
411 Collection coll;
412 PersistenceManager pm;
413 Query query;
414 try {
415 pm = getPersistenceManager();
416 beginTransaction();
417 query = pm.newQuery(clazz, filter);
418 if (ordering != null) {
419 query.setOrdering(ordering);
420 }
421 query.declareParameters(parameters);
422 coll = (Collection) query.executeWithMap(values);
423 commit();
424 } catch (Exception e) {
425 rollback();
426 throw new DAOException(e);
427 } finally {
428 pm = null;
429 close();
430 }
431 return coll;
432 }
433
434 /***
435 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, Object[])
436 */
437 public Collection queryWithMap(Class clazz, String filter,
438 String parameters, Object[] values) throws JDOException,
439 DAOException {
440 return queryWithMap(clazz, filter, null, parameters, values);
441 }
442
443 /***
444 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#queryWithMap(Class, String, String, String, Object[])
445 */
446 public Collection queryWithMap(Class clazz, String filter, String ordering,
447 String parameters, Object[] values) throws JDOException,
448 DAOException {
449 Collection coll;
450 PersistenceManager pm;
451 Query query;
452 try {
453 pm = getPersistenceManager();
454 beginTransaction();
455 query = pm.newQuery(clazz, filter);
456 if (ordering != null) {
457 query.setOrdering(ordering);
458 }
459 query.declareParameters(parameters);
460 coll = (Collection) query.executeWithArray(values);
461 commit();
462 } catch (Exception e) {
463 rollback();
464 throw new DAOException(e);
465 } finally {
466 pm = null;
467 close();
468 }
469 return coll;
470 }
471
472 /***
473 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, Collection)
474 */
475 public Collection query(Class clazz, Collection coll) throws JDOException,
476 DAOException {
477 Collection cln;
478 PersistenceManager pm;
479 Query query;
480 try {
481 pm = getPersistenceManager();
482 beginTransaction();
483 query = pm.newQuery(clazz, coll);
484 cln = (Collection) query.execute();
485 commit();
486 } catch (Exception e) {
487 rollback();
488 throw new DAOException(e);
489 } finally {
490 pm = null;
491 close();
492 }
493 return cln;
494
495 }
496
497 /***
498 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Class, Collection, String)
499 */
500 public Collection query(Class clazz, Collection coll, String filter)
501 throws JDOException, DAOException {
502 Collection cln;
503 PersistenceManager pm;
504 Query query;
505 try {
506 pm = getPersistenceManager();
507 beginTransaction();
508 query = pm.newQuery(clazz, coll, filter);
509 cln = (Collection) query.execute();
510 commit();
511 } catch (Exception e) {
512 rollback();
513 throw new DAOException(e);
514 } finally {
515 pm = null;
516 close();
517 }
518 return cln;
519 }
520
521 /***
522 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(Object)
523 */
524 public Collection query(Object object) throws JDOException, DAOException {
525 Collection cln;
526 PersistenceManager pm;
527 Query query;
528 try {
529 pm = getPersistenceManager();
530 beginTransaction();
531 query = pm.newQuery(object);
532 cln = (Collection) query.execute();
533 commit();
534 } catch (Exception e) {
535 rollback();
536 throw new DAOException(e);
537 } finally {
538 pm = null;
539 close();
540 }
541 return cln;
542 }
543
544 /***
545 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#query(String)
546 */
547 public Collection query(String queryStr) throws JDOException, DAOException {
548 Collection cln;
549 PersistenceManager pm;
550 Query query;
551 try {
552 pm = getPersistenceManager();
553 beginTransaction();
554 query = pm.newQuery(queryStr);
555 cln = (Collection) query.execute();
556 commit();
557 } catch (Exception e) {
558 rollback();
559 throw new DAOException(e);
560 } finally {
561 pm = null;
562 close();
563 }
564 return cln;
565 }
566
567 /***
568 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evict(Object)
569 */
570 public void evict(Object object) throws JDOException, DAOException {
571 PersistenceManager pm;
572 try {
573 pm = getPersistenceManager();
574 pm.evict(object);
575 } catch (Exception e) {
576 throw new DAOException(e);
577 } finally {
578 pm = null;
579 close();
580 }
581 }
582
583 /***
584 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evictAll()
585 */
586 public void evictAll() throws JDOException, DAOException {
587 PersistenceManager pm;
588 try {
589 pm = getPersistenceManager();
590 pm.evictAll();
591 } catch (Exception e) {
592 throw new DAOException(e);
593 } finally {
594 pm = null;
595 close();
596 }
597 }
598
599 /***
600 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evictAll(Collection)
601 */
602 public void evictAll(Collection coll) throws JDOException, DAOException {
603 PersistenceManager pm;
604 try {
605 pm = getPersistenceManager();
606 pm.evictAll(coll);
607 } catch (Exception e) {
608 throw new DAOException(e);
609 } finally {
610 pm = null;
611 close();
612 }
613 }
614
615 /***
616 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#evictAll(Object[])
617 */
618 public void evictAll(Object[] objects) throws JDOException, DAOException {
619 PersistenceManager pm;
620 try {
621 pm = getPersistenceManager();
622 pm.evictAll(objects);
623 } catch (Exception e) {
624 rollback();
625 throw new DAOException(e);
626 } finally {
627 pm = null;
628 close();
629 }
630 }
631
632 /***
633 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#flush()
634 */
635 public void flush() throws JDOException, DAOException {
636 PersistenceManager pm;
637 try {
638 pm = getPersistenceManager();
639 pm.flush();
640 } catch (Exception e) {
641 rollback();
642 throw new DAOException(e);
643 } finally {
644 pm = null;
645 close();
646 }
647 }
648
649 /***
650 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#makePersistent(Object)
651 */
652 public void makePersistent(Object object) throws JDOException, DAOException {
653 PersistenceManager pm;
654 try {
655 pm = getPersistenceManager();
656 pm.makePersistent(object);
657 } catch (Exception e) {
658 rollback();
659 throw new DAOException(e);
660 } finally {
661 pm = null;
662 close();
663 }
664 }
665
666 /***
667 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#makePersistentAll(Collection)
668 */
669 public void makePersistentAll(Collection coll) throws JDOException,
670 DAOException {
671 PersistenceManager pm;
672 try {
673 pm = getPersistenceManager();
674 pm.makePersistentAll(coll);
675 } catch (Exception e) {
676 rollback();
677 throw new DAOException(e);
678 } finally {
679 pm = null;
680 close();
681 }
682 }
683
684 /***
685 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#makePersistentAll(Object[])
686 */
687 public void makePersistentAll(Object[] objects) throws JDOException,
688 DAOException {
689 PersistenceManager pm;
690 try {
691 pm = getPersistenceManager();
692 pm.makePersistentAll(objects);
693 } catch (Exception e) {
694 rollback();
695 throw new DAOException(e);
696 } finally {
697 pm = null;
698 close();
699 }
700 }
701
702 /***
703 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#deletePersistent(Object)
704 */
705 public void deletePersistent(Object object) throws JDOException,
706 DAOException {
707 PersistenceManager pm;
708 try {
709 pm = getPersistenceManager();
710 pm.deletePersistent(object);
711 } catch (Exception e) {
712 rollback();
713 throw new DAOException(e);
714 } finally {
715 pm = null;
716 close();
717 }
718 }
719
720 /***
721 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#deletePersistentAll(Collection)
722 */
723 public void deletePersistentAll(Collection coll) throws JDOException,
724 DAOException {
725 PersistenceManager pm;
726 try {
727 pm = getPersistenceManager();
728 pm.deletePersistent(coll);
729 } catch (Exception e) {
730 rollback();
731 throw new DAOException(e);
732 } finally {
733 pm = null;
734 close();
735 }
736 }
737
738 /***
739 * @see org.huihoo.jfox.soaf.services.persistence.JdoService#deletePersistentAll(Object[])
740 */
741 public void deletePersistentAll(Object[] objects) {
742 PersistenceManager pm;
743 try {
744 pm = getPersistenceManager();
745 pm.deletePersistent(objects);
746 } catch (Exception e) {
747 rollback();
748 throw new DAOException(e);
749 } finally {
750 pm = null;
751 close();
752 }
753 }
754 }