KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > odmg > ODMGBaseBeanImpl


1 package org.apache.ojb.ejb.odmg;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import javax.ejb.EJBException JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.ojb.broker.PersistenceBroker;
23 import org.apache.ojb.broker.query.QueryByCriteria;
24 import org.apache.ojb.broker.util.logging.Logger;
25 import org.apache.ojb.broker.util.logging.LoggerFactory;
26 import org.apache.ojb.ejb.SessionBeanImpl;
27 import org.apache.ojb.odmg.HasBroker;
28 import org.apache.ojb.odmg.TransactionExt;
29 import org.odmg.Database;
30 import org.odmg.Implementation;
31 import org.odmg.LockNotGrantedException;
32 import org.odmg.OQLQuery;
33 import org.odmg.Transaction;
34
35 /**
36  * Base class for using OJB-ODMG api within SessionBeans,
37  * subclass this class to implement your own bean
38  * implementations.
39  *
40  * To keep this example as simple as possible, we lookup a static OJB ODMG
41  * implementation instance from an helper class.
42  * But it's recommended to bind an instances of the ODMG main/access classes in JNDI
43  * (at appServer start), open the database and lookup these instances instance via JNDI in
44  * ejbCreate(), instead of lookup a static instance on each bean creation.
45  *
46  * To get the {@link org.odmg.Database} or
47  * {@link org.odmg.Implementation} instance use
48  * the {@link #getDatabase} and {@link #getImplementation}
49  * methods.
50  *
51  * Additionally there are some basic methods for
52  * storing, deleting, counting, get all objects
53  * implemented.
54  *
55  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
56  * @version $Id: ODMGBaseBeanImpl.java,v 1.5.2.4 2005/12/21 22:21:39 tomdz Exp $
57  */

58 public abstract class ODMGBaseBeanImpl extends SessionBeanImpl
59 {
60     private Logger log = LoggerFactory.getLogger(ODMGBaseBeanImpl.class);
61     private Implementation odmg;
62
63     /**
64      * Lookup the OJB ODMG implementation.
65      * It's recommended to bind an instance of the Implementation class in JNDI
66      * (at appServer start), open the database and lookup this instance via JNDI in
67      * ejbCreate().
68      */

69     public void ejbCreate()
70     {
71         if (log.isDebugEnabled()) log.debug("ejbCreate was called");
72         odmg = ODMGHelper.getODMG();
73     }
74
75     /**
76      * Here we do the OJB cleanup.
77      */

78     public void ejbRemove()
79     {
80         super.ejbRemove();
81         odmg = null;
82     }
83
84     /**
85      * Return the Database associated with
86      * this bean.
87      */

88     public Database getDatabase()
89     {
90         return odmg.getDatabase(null);
91     }
92
93     /**
94      * Return the Implementation instance associated
95      * with this bean.
96      */

97     public Implementation getImplementation()
98     {
99         return odmg;
100     }
101
102     /**
103      * Store an object.
104      */

105     public Object JavaDoc storeObject(Object JavaDoc object)
106     {
107         /* One possibility of storing objects is to use the current transaction
108          associated with the container */

109         try
110         {
111             TransactionExt tx = (TransactionExt) odmg.currentTransaction();
112             tx.lock(object, Transaction.WRITE);
113             tx.markDirty(object);
114         }
115         catch (LockNotGrantedException e)
116         {
117             log.error("Failure while storing object " + object, e);
118             throw new EJBException JavaDoc("Failure while storing object", e);
119         }
120         return object;
121     }
122
123     /**
124      * Delete an object.
125      */

126     public void deleteObject(Object JavaDoc object)
127     {
128         getDatabase().deletePersistent(object);
129     }
130
131     /**
132      * Store a collection of objects.
133      */

134     public Collection JavaDoc storeObjects(Collection JavaDoc objects)
135     {
136         try
137         {
138             /* One possibility of storing objects is to use the current transaction
139              associated with the container */

140             Transaction tx = odmg.currentTransaction();
141             for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
142             {
143                 tx.lock(iterator.next(), Transaction.WRITE);
144             }
145         }
146         catch (LockNotGrantedException e)
147         {
148             log.error("Failure while storing objects " + objects, e);
149             throw new EJBException JavaDoc("Failure while storing objects", e);
150         }
151         return objects;
152     }
153
154     /**
155      * Delete a Collection of objects.
156      */

157     public void deleteObjects(Collection JavaDoc objects)
158     {
159         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
160         {
161             getDatabase().deletePersistent(iterator.next());
162         }
163     }
164
165     /**
166      * Return the count of all objects found
167      * for given class, using the PB-api within
168      * ODMG - this may change in further versions.
169      */

170     public int getCount(Class JavaDoc target)
171     {
172         PersistenceBroker broker = ((HasBroker) odmg.currentTransaction()).getBroker();
173         int result = broker.getCount(new QueryByCriteria(target));
174         return result;
175     }
176
177     public Collection JavaDoc getAllObjects(Class JavaDoc target)
178     {
179         OQLQuery query = odmg.newOQLQuery();
180         try
181         {
182             query.create("select allObjects from " + target.getName());
183             return (Collection JavaDoc) query.execute();
184         }
185         catch (Exception JavaDoc e)
186         {
187             log.error("OQLQuery failed", e);
188             throw new EJBException JavaDoc("OQLQuery failed", e);
189         }
190     }
191 }
192
Popular Tags