KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > pb > PersonArticleManagerPBBean


1 package org.apache.ojb.ejb.pb;
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.CreateException JavaDoc;
19 import javax.ejb.EJBException JavaDoc;
20 import javax.ejb.SessionBean JavaDoc;
21 import javax.naming.Context JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.ojb.broker.PersistenceBroker;
30 import org.apache.ojb.broker.core.PersistenceBrokerFactoryFactory;
31 import org.apache.ojb.broker.core.PersistenceBrokerFactoryIF;
32 import org.apache.ojb.broker.query.Query;
33 import org.apache.ojb.broker.query.QueryByCriteria;
34 import org.apache.ojb.broker.util.logging.Logger;
35 import org.apache.ojb.broker.util.logging.LoggerFactory;
36 import org.apache.ojb.ejb.ArticleVO;
37 import org.apache.ojb.ejb.PersonVO;
38 import org.apache.ojb.ejb.SessionBeanImpl;
39
40 /**
41  * Simple example bean for nested bean calls,
42  * using {@link org.apache.ojb.ejb.pb.PersonManagerPBBean}
43  * and {@link org.apache.ojb.ejb.pb.ArticleManagerPBBean}
44  * for some test methods.
45  *
46  * @ejb:bean
47  * type="Stateless"
48  * name="PersonArticleManagerPBBean"
49  * jndi-name="org.apache.ojb.ejb.pb.PersonArticleManagerPBBean"
50  * local-jndi-name="org.apache.ojb.ejb.pb.PersonArticleManagerPBBeanLocal"
51  * view-type="both"
52  * transaction-type="Container"
53  *
54  * @ejb:interface
55  * remote-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBRemote"
56  * local-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBLocal"
57  * extends="javax.ejb.EJBObject"
58  *
59  * @ejb:home
60  * remote-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBHome"
61  * local-class="org.apache.ojb.ejb.pb.PersonArticleManagerPBLocalHome"
62  * extends="javax.ejb.EJBHome"
63  *
64  * @ejb:transaction
65  * type="Required"
66  *
67  * @ejb:ejb-ref
68  * ejb-name="PersonManagerPBBean"
69  * view-type="local"
70  * ref-name="ejb/ojb/pb/PersonManager"
71  *
72  * @ejb:ejb-ref
73  * ejb-name="ArticleManagerPBBean"
74  * view-type="local"
75  * ref-name="ejb/ojb/pb/ArticleManager"
76  *
77  *
78  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
79  * @version $Id: PersonArticleManagerPBBean.java,v 1.5.2.4 2005/12/21 22:21:38 tomdz Exp $
80  */

81 public class PersonArticleManagerPBBean extends SessionBeanImpl implements SessionBean JavaDoc
82 {
83     private static final String JavaDoc PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/PersonManager";
84     private static final String JavaDoc ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/pb/ArticleManager";
85
86     private Logger log = LoggerFactory.getLogger(PersonArticleManagerPBBean.class);
87     private ArticleManagerPBLocal am;
88     private PersonManagerPBLocal pm;
89     private PersistenceBrokerFactoryIF pbf;
90
91     public PersonArticleManagerPBBean()
92     {
93     }
94
95     public void ejbCreate()
96     {
97         pbf = PersistenceBrokerFactoryFactory.instance();
98     }
99
100     /**
101      * Stores article and persons using other beans.
102      *
103      * @ejb:interface-method
104      */

105     public void storeUsingNestedPB(List JavaDoc articles, List JavaDoc persons)
106     {
107         PersistenceBroker broker = pbf.defaultPersistenceBroker();
108         try
109         {
110             // do something with broker
111
Query q = new QueryByCriteria(PersonVO.class);
112             broker.getCollectionByQuery(q);
113             // System.out.println("## broker1: con=" + broker.serviceConnectionManager().getConnection());
114
//now use nested bean call
115
// System.out.println("####### DO nested bean call");
116
ArticleManagerPBLocal am = getArticleManager();
117             am.storeArticles(articles);
118             // System.out.println("####### END nested bean call");
119
// do more with broker
120
// System.out.println("## broker1: now store objects");
121
storeObjects(broker, persons);
122             // System.out.println("## broker1: end store, con=" + broker.serviceConnectionManager().getConnection());
123
}
124 // catch(LookupException e)
125
// {
126
// throw new EJBException(e);
127
// }
128
finally
129         {
130             // System.out.println("## close broker1 now");
131
if(broker != null) broker.close();
132         }
133     }
134
135     private Collection JavaDoc storeObjects(PersistenceBroker broker, Collection JavaDoc objects)
136     {
137         for (Iterator JavaDoc it = objects.iterator(); it.hasNext();)
138         {
139             broker.store(it.next());
140         }
141         return objects;
142     }
143
144     /**
145      * Stores article and persons using other beans.
146      *
147      * @ejb:interface-method
148      */

149     public void storeUsingSubBeans(List JavaDoc articles, List JavaDoc persons)
150     {
151         //store all objects
152
ArticleManagerPBLocal am = getArticleManager();
153         PersonManagerPBLocal pm = getPersonManager();
154         am.storeArticles(articles);
155         pm.storePersons(persons);
156     }
157
158     /**
159      * @ejb:interface-method
160      */

161     public List JavaDoc storeArticles(List JavaDoc articles)
162     {
163         return new ArrayList JavaDoc(getArticleManager().storeArticles(articles));
164     }
165
166     /**
167      * @ejb:interface-method
168      */

169     public void deleteArticles(List JavaDoc articles)
170     {
171         getArticleManager().deleteArticles(articles);
172     }
173
174     /**
175      * @ejb:interface-method
176      */

177     public List JavaDoc storeArticlesIntricately(List JavaDoc articles)
178     {
179         ArrayList JavaDoc ret = new ArrayList JavaDoc();
180         for (int i = 0; i < articles.size(); i++)
181         {
182             ret.add(getArticleManager().storeArticle((ArticleVO) articles.get(i)));
183         }
184         return ret;
185     }
186
187     /**
188      * @ejb:interface-method
189      */

190     public void deleteArticlesIntricately(List JavaDoc articles)
191     {
192         for (int i = 0; i < articles.size(); i++)
193         {
194             getArticleManager().deleteArticle((ArticleVO) articles.get(i));
195         }
196     }
197
198     /**
199      * @ejb:interface-method
200      */

201     public List JavaDoc storePersons(List JavaDoc persons)
202     {
203         return new ArrayList JavaDoc(getPersonManager().storePersons(persons));
204     }
205
206     /**
207      * @ejb:interface-method
208      */

209     public void deletePersons(List JavaDoc persons)
210     {
211         getPersonManager().deletePersons(persons);
212     }
213
214     /**
215      * @ejb:interface-method
216      */

217     public int articleCount()
218     {
219         return getArticleManager().countArticles();
220     }
221
222     /**
223      * @ejb:interface-method
224      */

225     public int personCount()
226     {
227         return getPersonManager().countPersons();
228     }
229
230     private ArticleManagerPBLocal getArticleManager()
231     {
232         if (am == null)
233         {
234             Context JavaDoc context = null;
235             try
236             {
237                 context = new InitialContext JavaDoc();
238                 am = ((ArticleManagerPBLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
239                 log.info("** Found bean: " + am);
240                 return am;
241             }
242             catch (NamingException JavaDoc e)
243             {
244                 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
245                 throw new EJBException JavaDoc(e);
246             }
247             catch (CreateException JavaDoc e)
248             {
249                 log.error("Creation of ArticleManager failed", e);
250                 throw new EJBException JavaDoc(e);
251             }
252         }
253         return am;
254     }
255
256     private PersonManagerPBLocal getPersonManager()
257     {
258         if (pm == null)
259         {
260             Context JavaDoc context = null;
261             try
262             {
263                 context = new InitialContext JavaDoc();
264                 pm = ((PersonManagerPBLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
265                 log.info("** Found bean: " + pm);
266                 return pm;
267             }
268             catch (NamingException JavaDoc e)
269             {
270                 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
271                 throw new EJBException JavaDoc(e);
272             }
273             catch (CreateException JavaDoc e)
274             {
275                 log.error("Creation of PersonManager failed", e);
276                 throw new EJBException JavaDoc(e);
277             }
278         }
279         return pm;
280     }
281 }
282
Popular Tags