KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import javax.ejb.EJBException JavaDoc;
20 import javax.ejb.SessionBean JavaDoc;
21 import javax.ejb.SessionContext JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.ojb.broker.OJBRuntimeException;
27 import org.apache.ojb.broker.util.logging.Logger;
28 import org.apache.ojb.broker.util.logging.LoggerFactory;
29 import org.apache.ojb.ejb.ArticleVO;
30 import org.apache.ojb.ejb.PersonVO;
31 import org.odmg.Database;
32 import org.odmg.Implementation;
33 import org.odmg.OQLQuery;
34 import org.odmg.QueryException;
35 import org.odmg.Transaction;
36
37 /**
38  * This is an session bean implementation using odmg implementation.
39  * <br/>
40  * For more structured implementations take a look at
41  * <br/>
42  * {@link org.apache.ojb.ejb.SessionBeanImpl}<br/>
43  * {@link org.apache.ojb.ejb.odmg.ODMGBaseBeanImpl}<br/>
44  * {@link org.apache.ojb.ejb.pb.PBBaseBeanImpl}
45  * <p>
46  * <b>How to use ODMG</b> <br>
47  *
48  * To keep this example as simple as possible, we lookup a static OJB ODMG implementation instance
49  * on each bean instance.
50  * But it's recommended to bind an instance of the Implementation class in JNDI
51  * (at appServer start), open the database and lookup this instances via JNDI in
52  * ejbCreate().
53  *
54  * To use the odmg-api within your bean, you can do:
55  *
56  * <ol type="a">
57  * <li>
58  * Obtain the current Database from the Implementation instance - Attend<br>
59  * that there must be already a Database opened before.<br><i>
60  * db = odmg.getDatabase(null);<br>
61  * // ... do something<br>
62  * </i></li>
63  * <li>
64  * Obtain the current odmg-Transaction from the Implementation instance<br>
65  * to lock objects - Attend that there must be already a Database opened before.<br><i>
66  * Transaction tx = odmg.currentTransaction();<br>
67  * tx.lock(aObject, mode);
68  * </i></li>
69  * </ol>
70  * </p>
71  *
72  *
73  * @ejb:bean
74  * type="Stateless"
75  * name="ODMGSessionBean"
76  * jndi-name="org.apache.ojb.ejb.odmg.ODMGSessionBean"
77  * local-jndi-name="org.apache.ojb.ejb.odmg.ODMGSessionBeanLocal"
78  * view-type="both"
79  * transaction-type="Container"
80  *
81  * @ejb:interface
82  * remote-class="org.apache.ojb.ejb.odmg.ODMGSessionRemote"
83  * local-class="org.apache.ojb.ejb.odmg.ODMGSessionLocal"
84  * extends="javax.ejb.EJBObject"
85  *
86  * @ejb:home
87  * remote-class="org.apache.ojb.ejb.odmg.ODMGSessionHome"
88  * local-class="org.apache.ojb.ejb.odmg.ODMGSessionLocalHome"
89  * extends="javax.ejb.EJBHome"
90  *
91  * @ejb:transaction
92  * type="Required"
93  *
94  *
95  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
96  * @version $Id: ODMGSessionBean.java,v 1.5.2.4 2005/12/21 22:21:39 tomdz Exp $
97  */

98 public class ODMGSessionBean implements SessionBean JavaDoc
99 {
100     private Logger log = LoggerFactory.getLogger(ODMGSessionBean.class);
101     private SessionContext JavaDoc ctx;
102     private Implementation odmg;
103     private Database db;
104
105     public ODMGSessionBean()
106     {
107     }
108
109     /**
110      * Lookup the OJB ODMG implementation.
111      * It's recommended to bind an instance of the Implementation class in JNDI
112      * (at appServer start), open the database and lookup this instance via JNDI in
113      * ejbCreate().
114      */

115     public void ejbCreate()
116     {
117         log.info("ejbCreate was called");
118         odmg = ODMGHelper.getODMG();
119         db = odmg.getDatabase(null);
120     }
121
122     public void ejbRemove()
123     {
124         db = null;
125         odmg = null;
126         ctx = null;
127     }
128
129     /**
130      * @ejb:interface-method
131      */

132     public List JavaDoc storeObjects(List JavaDoc objects)
133     {
134         if(log.isDebugEnabled()) log.debug("storeObjects");
135
136         /* One possibility of storing objects is to use the current transaction
137          associated with the container */

138         Transaction tx = odmg.currentTransaction();
139         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
140         {
141             tx.lock(iterator.next(), Transaction.WRITE);
142         }
143         return objects;
144     }
145
146     /**
147      * @ejb:interface-method
148      */

149     public void deleteObjects(List JavaDoc objects)
150     {
151         if(log.isDebugEnabled()) log.debug("deleteObjects");
152         db = odmg.getDatabase(null);
153         for (Iterator JavaDoc iterator = objects.iterator(); iterator.hasNext();)
154         {
155             db.deletePersistent(iterator.next());
156         }
157     }
158
159     protected int getObjectCount(Implementation ojb, Class JavaDoc target)
160     {
161         if(log.isDebugEnabled()) log.debug("getObjectCount was called");
162         List JavaDoc list;
163         try
164         {
165             OQLQuery query = ojb.newOQLQuery();
166             query.create("select allObjects from " + target.getName());
167             list = (List JavaDoc) query.execute();
168             return list.size();
169         }
170         catch (QueryException e)
171         {
172             throw new EJBException JavaDoc("Query objects failed", e);
173         }
174     }
175
176     /**
177      * @ejb:interface-method
178      */

179     public int getArticleCount()
180     {
181         if(log.isDebugEnabled()) log.debug("getArticleCount was called");
182         return getObjectCount(odmg, ArticleVO.class);
183     }
184
185     /**
186      * @ejb:interface-method
187      */

188     public Collection JavaDoc getArticlesByName(String JavaDoc articleName)
189     {
190         if(log.isDebugEnabled()) log.debug("getArticlesByName was called");
191         try
192         {
193             OQLQuery query = odmg.newOQLQuery();
194             query.create("select allArticles from " + ArticleVO.class.getName() + " where name like $1");
195             query.bind(articleName);
196             return (Collection JavaDoc) query.execute();
197         }
198         catch (QueryException e)
199         {
200             throw new EJBException JavaDoc("Query objects failed", e);
201         }
202     }
203
204     /**
205      * @ejb:interface-method
206      */

207     public int getPersonCount()
208     {
209         if(log.isDebugEnabled()) log.debug("getPersonCount was called");
210         return getObjectCount(odmg, PersonVO.class);
211     }
212
213     /**
214      * @ejb:interface-method
215      */

216     public boolean allInOne(List JavaDoc articles, List JavaDoc persons)
217     {
218         boolean passedWell = true;
219         if(log.isDebugEnabled()) log.debug("allInOne method was called");
220         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
221
222         String JavaDoc sep = System.getProperty("line.separator");
223
224         db = odmg.getDatabase(null);
225
226         int personsBefore = getPersonCount();
227         int articlesBefore = getArticleCount();
228         buf.append(sep + "# Start with " + personsBefore + " persons");
229         buf.append(sep + "# Start with " + articlesBefore + " articles");
230         storeObjects(articles);
231         storeObjects(persons);
232         int personsAfterStore = getPersonCount();
233         int articlesAfterStore = getArticleCount();
234         buf.append(sep + "# After store: " + personsAfterStore + " persons");
235         buf.append(sep + "# After store: " + articlesAfterStore + " articles");
236         deleteObjects(articles);
237         deleteObjects(persons);
238         int personsAfterDelete = getPersonCount();
239         int articlesAfterDelete = getArticleCount();
240         buf.append(sep + "# After delete: " + personsAfterDelete + " persons");
241         buf.append(sep + "# After delete: " + articlesAfterDelete + " articles");
242         log.info("## allInOne-Method call: " + buf.toString());
243 // passedWell = (personsBefore + persons.size()) == personsAfterStore &&
244
// (articlesBefore + articles.size()) == articlesAfterStore &&
245
// (personsBefore) == personsAfterDelete &&
246
// (personsBefore) == personsAfterDelete;
247
// in the current odmg implementation you cannot see
248
// objects added/delete within a transaction using a OQLQuery
249
passedWell = (personsBefore) == personsAfterStore &&
250                 (articlesBefore) == articlesAfterStore &&
251                 (personsBefore) == personsAfterDelete &&
252                 (personsBefore) == personsAfterDelete;
253         return passedWell;
254     }
255
256     /**
257      * @ejb:interface-method
258      */

259     public Collection JavaDoc getAllObjects(Class JavaDoc target)
260     {
261         if(log.isDebugEnabled()) log.debug("getAllObjects was called");
262         OQLQuery query = odmg.newOQLQuery();
263         try
264         {
265             query.create("select allObjects from " + target.getName());
266             return (Collection JavaDoc) query.execute();
267         }
268         catch (Exception JavaDoc e)
269         {
270             log.error("OQLQuery failed", e);
271             throw new OJBRuntimeException("OQLQuery failed", e);
272         }
273     }
274
275     public void ejbActivate()
276     {/* unused */
277     }
278
279     public void ejbPassivate()
280     {/* unused */
281     }
282
283     public void setSessionContext(SessionContext JavaDoc ctx)
284     {
285         this.ctx = ctx;
286     }
287
288     public SessionContext JavaDoc getSessionContext()
289     {
290         return ctx;
291     }
292 }
293
Popular Tags