001: package org.apache.ojb.ejb.pb;
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.CreateException;
019: import javax.ejb.EJBException;
020: import javax.ejb.SessionBean;
021: import javax.naming.Context;
022: import javax.naming.InitialContext;
023: import javax.naming.NamingException;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.ojb.broker.PersistenceBroker;
030: import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
031: import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
032: import org.apache.ojb.broker.query.Query;
033: import org.apache.ojb.broker.query.QueryByCriteria;
034: import org.apache.ojb.broker.util.logging.Logger;
035: import org.apache.ojb.broker.util.logging.LoggerFactory;
036: import org.apache.ojb.ejb.ArticleVO;
037: import org.apache.ojb.ejb.PersonVO;
038: import org.apache.ojb.ejb.SessionBeanImpl;
039:
040: /**
041: * Simple example bean for nested bean calls,
042: * using {@link org.apache.ojb.ejb.pb.PersonManagerPBBean}
043: * and {@link org.apache.ojb.ejb.pb.ArticleManagerPBBean}
044: * for some test methods.
045: *
046: * @ejb:bean
047: * type="Stateless"
048: * name="PersonArticleManagerPBBean"
049: * jndi-name="org.apache.ojb.ejb.pb.PersonArticleManagerPBBean"
050: * local-jndi-name="org.apache.ojb.ejb.pb.PersonArticleManagerPBBeanLocal"
051: * view-type="both"
052: * transaction-type="Container"
053: *
054: * @ejb:interface
055: * remote-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBRemote"
056: * local-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBLocal"
057: * extends="javax.ejb.EJBObject"
058: *
059: * @ejb:home
060: * remote-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBHome"
061: * local-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBLocalHome"
062: * extends="javax.ejb.EJBHome"
063: *
064: * @ejb:transaction
065: * type="Required"
066: *
067: * @ejb:ejb-ref
068: * ejb-name="PersonManagerPBBean"
069: * view-type="local"
070: * ref-name="ejb/ojb/pb/PersonManager"
071: *
072: * @ejb:ejb-ref
073: * ejb-name="ArticleManagerPBBean"
074: * view-type="local"
075: * ref-name="ejb/ojb/pb/ArticleManager"
076: *
077: *
078: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
079: * @version $Id: PersonArticleManagerPBBean.java,v 1.5.2.4 2005/12/21 22:21:38 tomdz Exp $
080: */
081: public class PersonArticleManagerPBBean extends SessionBeanImpl
082: implements SessionBean {
083: private static final String PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/PersonManager";
084: private static final String ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/ArticleManager";
085:
086: private Logger log = LoggerFactory
087: .getLogger(PersonArticleManagerPBBean.class);
088: private ArticleManagerPBLocal am;
089: private PersonManagerPBLocal pm;
090: private PersistenceBrokerFactoryIF pbf;
091:
092: public PersonArticleManagerPBBean() {
093: }
094:
095: public void ejbCreate() {
096: pbf = PersistenceBrokerFactoryFactory.instance();
097: }
098:
099: /**
100: * Stores article and persons using other beans.
101: *
102: * @ejb:interface-method
103: */
104: public void storeUsingNestedPB(List articles, List persons) {
105: PersistenceBroker broker = pbf.defaultPersistenceBroker();
106: try {
107: // do something with broker
108: Query q = new QueryByCriteria(PersonVO.class);
109: broker.getCollectionByQuery(q);
110: // System.out.println("## broker1: con=" + broker.serviceConnectionManager().getConnection());
111: //now use nested bean call
112: // System.out.println("####### DO nested bean call");
113: ArticleManagerPBLocal am = getArticleManager();
114: am.storeArticles(articles);
115: // System.out.println("####### END nested bean call");
116: // do more with broker
117: // System.out.println("## broker1: now store objects");
118: storeObjects(broker, persons);
119: // System.out.println("## broker1: end store, con=" + broker.serviceConnectionManager().getConnection());
120: }
121: // catch(LookupException e)
122: // {
123: // throw new EJBException(e);
124: // }
125: finally {
126: // System.out.println("## close broker1 now");
127: if (broker != null)
128: broker.close();
129: }
130: }
131:
132: private Collection storeObjects(PersistenceBroker broker,
133: Collection objects) {
134: for (Iterator it = objects.iterator(); it.hasNext();) {
135: broker.store(it.next());
136: }
137: return objects;
138: }
139:
140: /**
141: * Stores article and persons using other beans.
142: *
143: * @ejb:interface-method
144: */
145: public void storeUsingSubBeans(List articles, List persons) {
146: //store all objects
147: ArticleManagerPBLocal am = getArticleManager();
148: PersonManagerPBLocal pm = getPersonManager();
149: am.storeArticles(articles);
150: pm.storePersons(persons);
151: }
152:
153: /**
154: * @ejb:interface-method
155: */
156: public List storeArticles(List articles) {
157: return new ArrayList(getArticleManager()
158: .storeArticles(articles));
159: }
160:
161: /**
162: * @ejb:interface-method
163: */
164: public void deleteArticles(List articles) {
165: getArticleManager().deleteArticles(articles);
166: }
167:
168: /**
169: * @ejb:interface-method
170: */
171: public List storeArticlesIntricately(List articles) {
172: ArrayList ret = new ArrayList();
173: for (int i = 0; i < articles.size(); i++) {
174: ret.add(getArticleManager().storeArticle(
175: (ArticleVO) articles.get(i)));
176: }
177: return ret;
178: }
179:
180: /**
181: * @ejb:interface-method
182: */
183: public void deleteArticlesIntricately(List articles) {
184: for (int i = 0; i < articles.size(); i++) {
185: getArticleManager().deleteArticle(
186: (ArticleVO) articles.get(i));
187: }
188: }
189:
190: /**
191: * @ejb:interface-method
192: */
193: public List storePersons(List persons) {
194: return new ArrayList(getPersonManager().storePersons(persons));
195: }
196:
197: /**
198: * @ejb:interface-method
199: */
200: public void deletePersons(List persons) {
201: getPersonManager().deletePersons(persons);
202: }
203:
204: /**
205: * @ejb:interface-method
206: */
207: public int articleCount() {
208: return getArticleManager().countArticles();
209: }
210:
211: /**
212: * @ejb:interface-method
213: */
214: public int personCount() {
215: return getPersonManager().countPersons();
216: }
217:
218: private ArticleManagerPBLocal getArticleManager() {
219: if (am == null) {
220: Context context = null;
221: try {
222: context = new InitialContext();
223: am = ((ArticleManagerPBLocalHome) context
224: .lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
225: log.info("** Found bean: " + am);
226: return am;
227: } catch (NamingException e) {
228: log.error("Lookup using ejb-ref "
229: + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
230: throw new EJBException(e);
231: } catch (CreateException e) {
232: log.error("Creation of ArticleManager failed", e);
233: throw new EJBException(e);
234: }
235: }
236: return am;
237: }
238:
239: private PersonManagerPBLocal getPersonManager() {
240: if (pm == null) {
241: Context context = null;
242: try {
243: context = new InitialContext();
244: pm = ((PersonManagerPBLocalHome) context
245: .lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
246: log.info("** Found bean: " + pm);
247: return pm;
248: } catch (NamingException e) {
249: log.error("Lookup using ejb-ref "
250: + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
251: throw new EJBException(e);
252: } catch (CreateException e) {
253: log.error("Creation of PersonManager failed", e);
254: throw new EJBException(e);
255: }
256: }
257: return pm;
258: }
259: }
|