001: package org.apache.ojb.ejb.odmg;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.ejb.EJBException;
019: import javax.ejb.SessionBean;
020: import javax.ejb.SessionContext;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.ojb.broker.OJBRuntimeException;
026: import org.apache.ojb.broker.util.logging.Logger;
027: import org.apache.ojb.broker.util.logging.LoggerFactory;
028: import org.apache.ojb.ejb.ArticleVO;
029: import org.apache.ojb.ejb.PersonVO;
030: import org.odmg.Database;
031: import org.odmg.Implementation;
032: import org.odmg.OQLQuery;
033: import org.odmg.QueryException;
034: import org.odmg.Transaction;
035:
036: /**
037: * This is an session bean implementation using odmg implementation.
038: * <br/>
039: * For more structured implementations take a look at
040: * <br/>
041: * {@link org.apache.ojb.ejb.SessionBeanImpl}<br/>
042: * {@link org.apache.ojb.ejb.odmg.ODMGBaseBeanImpl}<br/>
043: * {@link org.apache.ojb.ejb.pb.PBBaseBeanImpl}
044: * <p>
045: * <b>How to use ODMG</b> <br>
046: *
047: * To keep this example as simple as possible, we lookup a static OJB ODMG implementation instance
048: * on each bean instance.
049: * But it's recommended to bind an instance of the Implementation class in JNDI
050: * (at appServer start), open the database and lookup this instances via JNDI in
051: * ejbCreate().
052: *
053: * To use the odmg-api within your bean, you can do:
054: *
055: * <ol type="a">
056: * <li>
057: * Obtain the current Database from the Implementation instance - Attend<br>
058: * that there must be already a Database opened before.<br><i>
059: * db = odmg.getDatabase(null);<br>
060: * // ... do something<br>
061: * </i></li>
062: * <li>
063: * Obtain the current odmg-Transaction from the Implementation instance<br>
064: * to lock objects - Attend that there must be already a Database opened before.<br><i>
065: * Transaction tx = odmg.currentTransaction();<br>
066: * tx.lock(aObject, mode);
067: * </i></li>
068: * </ol>
069: * </p>
070: *
071: *
072: * @ejb:bean
073: * type="Stateless"
074: * name="ODMGSessionBean"
075: * jndi-name="org.apache.ojb.ejb.odmg.ODMGSessionBean"
076: * local-jndi-name="org.apache.ojb.ejb.odmg.ODMGSessionBeanLocal"
077: * view-type="both"
078: * transaction-type="Container"
079: *
080: * @ejb:interface
081: * remote-class="org.apache.ojb.ejb.odmg.ODMGSessionRemote"
082: * local-class="org.apache.ojb.ejb.odmg.ODMGSessionLocal"
083: * extends="javax.ejb.EJBObject"
084: *
085: * @ejb:home
086: * remote-class="org.apache.ojb.ejb.odmg.ODMGSessionHome"
087: * local-class="org.apache.ojb.ejb.odmg.ODMGSessionLocalHome"
088: * extends="javax.ejb.EJBHome"
089: *
090: * @ejb:transaction
091: * type="Required"
092: *
093: *
094: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
095: * @version $Id: ODMGSessionBean.java,v 1.5.2.4 2005/12/21 22:21:39 tomdz Exp $
096: */
097: public class ODMGSessionBean implements SessionBean {
098: private Logger log = LoggerFactory.getLogger(ODMGSessionBean.class);
099: private SessionContext ctx;
100: private Implementation odmg;
101: private Database db;
102:
103: public ODMGSessionBean() {
104: }
105:
106: /**
107: * Lookup the OJB ODMG implementation.
108: * It's recommended to bind an instance of the Implementation class in JNDI
109: * (at appServer start), open the database and lookup this instance via JNDI in
110: * ejbCreate().
111: */
112: public void ejbCreate() {
113: log.info("ejbCreate was called");
114: odmg = ODMGHelper.getODMG();
115: db = odmg.getDatabase(null);
116: }
117:
118: public void ejbRemove() {
119: db = null;
120: odmg = null;
121: ctx = null;
122: }
123:
124: /**
125: * @ejb:interface-method
126: */
127: public List storeObjects(List objects) {
128: if (log.isDebugEnabled())
129: log.debug("storeObjects");
130:
131: /* One possibility of storing objects is to use the current transaction
132: associated with the container */
133: Transaction tx = odmg.currentTransaction();
134: for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
135: tx.lock(iterator.next(), Transaction.WRITE);
136: }
137: return objects;
138: }
139:
140: /**
141: * @ejb:interface-method
142: */
143: public void deleteObjects(List objects) {
144: if (log.isDebugEnabled())
145: log.debug("deleteObjects");
146: db = odmg.getDatabase(null);
147: for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
148: db.deletePersistent(iterator.next());
149: }
150: }
151:
152: protected int getObjectCount(Implementation ojb, Class target) {
153: if (log.isDebugEnabled())
154: log.debug("getObjectCount was called");
155: List list;
156: try {
157: OQLQuery query = ojb.newOQLQuery();
158: query.create("select allObjects from " + target.getName());
159: list = (List) query.execute();
160: return list.size();
161: } catch (QueryException e) {
162: throw new EJBException("Query objects failed", e);
163: }
164: }
165:
166: /**
167: * @ejb:interface-method
168: */
169: public int getArticleCount() {
170: if (log.isDebugEnabled())
171: log.debug("getArticleCount was called");
172: return getObjectCount(odmg, ArticleVO.class);
173: }
174:
175: /**
176: * @ejb:interface-method
177: */
178: public Collection getArticlesByName(String articleName) {
179: if (log.isDebugEnabled())
180: log.debug("getArticlesByName was called");
181: try {
182: OQLQuery query = odmg.newOQLQuery();
183: query
184: .create("select allArticles from "
185: + ArticleVO.class.getName()
186: + " where name like $1");
187: query.bind(articleName);
188: return (Collection) query.execute();
189: } catch (QueryException e) {
190: throw new EJBException("Query objects failed", e);
191: }
192: }
193:
194: /**
195: * @ejb:interface-method
196: */
197: public int getPersonCount() {
198: if (log.isDebugEnabled())
199: log.debug("getPersonCount was called");
200: return getObjectCount(odmg, PersonVO.class);
201: }
202:
203: /**
204: * @ejb:interface-method
205: */
206: public boolean allInOne(List articles, List persons) {
207: boolean passedWell = true;
208: if (log.isDebugEnabled())
209: log.debug("allInOne method was called");
210: StringBuffer buf = new StringBuffer();
211:
212: String sep = System.getProperty("line.separator");
213:
214: db = odmg.getDatabase(null);
215:
216: int personsBefore = getPersonCount();
217: int articlesBefore = getArticleCount();
218: buf.append(sep + "# Start with " + personsBefore + " persons");
219: buf
220: .append(sep + "# Start with " + articlesBefore
221: + " articles");
222: storeObjects(articles);
223: storeObjects(persons);
224: int personsAfterStore = getPersonCount();
225: int articlesAfterStore = getArticleCount();
226: buf.append(sep + "# After store: " + personsAfterStore
227: + " persons");
228: buf.append(sep + "# After store: " + articlesAfterStore
229: + " articles");
230: deleteObjects(articles);
231: deleteObjects(persons);
232: int personsAfterDelete = getPersonCount();
233: int articlesAfterDelete = getArticleCount();
234: buf.append(sep + "# After delete: " + personsAfterDelete
235: + " persons");
236: buf.append(sep + "# After delete: " + articlesAfterDelete
237: + " articles");
238: log.info("## allInOne-Method call: " + buf.toString());
239: // passedWell = (personsBefore + persons.size()) == personsAfterStore &&
240: // (articlesBefore + articles.size()) == articlesAfterStore &&
241: // (personsBefore) == personsAfterDelete &&
242: // (personsBefore) == personsAfterDelete;
243: // in the current odmg implementation you cannot see
244: // objects added/delete within a transaction using a OQLQuery
245: passedWell = (personsBefore) == personsAfterStore
246: && (articlesBefore) == articlesAfterStore
247: && (personsBefore) == personsAfterDelete
248: && (personsBefore) == personsAfterDelete;
249: return passedWell;
250: }
251:
252: /**
253: * @ejb:interface-method
254: */
255: public Collection getAllObjects(Class target) {
256: if (log.isDebugEnabled())
257: log.debug("getAllObjects was called");
258: OQLQuery query = odmg.newOQLQuery();
259: try {
260: query.create("select allObjects from " + target.getName());
261: return (Collection) query.execute();
262: } catch (Exception e) {
263: log.error("OQLQuery failed", e);
264: throw new OJBRuntimeException("OQLQuery failed", e);
265: }
266: }
267:
268: public void ejbActivate() {/* unused */
269: }
270:
271: public void ejbPassivate() {/* unused */
272: }
273:
274: public void setSessionContext(SessionContext ctx) {
275: this .ctx = ctx;
276: }
277:
278: public SessionContext getSessionContext() {
279: return ctx;
280: }
281: }
|