View Javadoc

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