KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.List JavaDoc;
26
27 import org.apache.ojb.broker.util.logging.Logger;
28 import org.apache.ojb.broker.util.logging.LoggerFactory;
29 import org.apache.ojb.ejb.SessionBeanImpl;
30
31 /**
32  * Simple example bean do nested bean calls,
33  * using {@link org.apache.ojb.ejb.odmg.PersonManagerODMGBean}
34  * and {@link org.apache.ojb.ejb.odmg.ArticleManagerODMGBean}
35  * within some test methods.
36  *
37  * @ejb:bean
38  * type="Stateless"
39  * name="PersonArticleManagerODMGBean"
40  * jndi-name="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGBean"
41  * local-jndi-name="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGBeanLocal"
42  * view-type="both"
43  * transaction-type="Container"
44  *
45  * @ejb:interface
46  * remote-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGRemote"
47  * local-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGLocal"
48  * extends="javax.ejb.EJBObject"
49  *
50  * @ejb:home
51  * remote-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGHome"
52  * local-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGLocalHome"
53  * extends="javax.ejb.EJBHome"
54  *
55  * @ejb:transaction
56  * type="Required"
57  *
58  * @ejb:ejb-ref
59  * ejb-name="PersonManagerODMGBean"
60  * view-type="local"
61  * ref-name="ejb/ojb/odmg/PersonManager"
62  *
63  * @ejb:ejb-ref
64  * ejb-name="ArticleManagerODMGBean"
65  * view-type="local"
66  * ref-name="ejb/ojb/odmg/ArticleManager"
67  *
68  *
69  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
70  * @version $Id: PersonArticleManagerODMGBean.java,v 1.4.2.2 2005/12/21 22:21:39 tomdz Exp $
71  */

72 public class PersonArticleManagerODMGBean extends SessionBeanImpl implements SessionBean JavaDoc
73 {
74     private static final String JavaDoc PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/PersonManager";
75     private static final String JavaDoc ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/ArticleManager";
76
77     private Logger log = LoggerFactory.getLogger(PersonArticleManagerODMGBean.class);
78
79     private ArticleManagerODMGLocal am;
80     private PersonManagerODMGLocal pm;
81
82     public PersonArticleManagerODMGBean()
83     {
84     }
85
86     public void ejbCreate()
87     {
88
89     }
90
91     /**
92      * @ejb:interface-method
93      */

94     public List JavaDoc storeArticles(List JavaDoc articles)
95     {
96         return new ArrayList JavaDoc(getArticleManager().storeArticles(articles));
97     }
98
99     /**
100      * @ejb:interface-method
101      */

102     public void deleteArticles(List JavaDoc articles)
103     {
104         getArticleManager().deleteArticles(articles);
105     }
106
107     /**
108      * @ejb:interface-method
109      */

110     public List JavaDoc storePersons(List JavaDoc persons)
111     {
112         return new ArrayList JavaDoc(getPersonManager().storePersons(persons));
113     }
114
115     /**
116      * @ejb:interface-method
117      */

118     public void deletePersons(List JavaDoc persons)
119     {
120         getPersonManager().deletePersons(persons);
121     }
122
123     /**
124      * @ejb:interface-method
125      */

126     public int articleCount()
127     {
128         return getArticleManager().countArticles();
129     }
130
131     /**
132      * @ejb:interface-method
133      */

134     public int personCount()
135     {
136         return getPersonManager().countPersons();
137     }
138
139     private ArticleManagerODMGLocal getArticleManager()
140     {
141         if (am == null)
142         {
143             Context JavaDoc context = null;
144             try
145             {
146                 context = new InitialContext JavaDoc();
147                 am = ((ArticleManagerODMGLocalHome) context.lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
148                 log.info("** Found bean: " + am);
149                 return am;
150             }
151             catch (NamingException JavaDoc e)
152             {
153                 log.error("Lookup using ejb-ref " + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
154                 throw new EJBException JavaDoc(e);
155             }
156             catch (CreateException JavaDoc e)
157             {
158                 log.error("Creation of ArticleManager failed", e);
159                 throw new EJBException JavaDoc(e);
160             }
161         }
162         return am;
163     }
164
165     private PersonManagerODMGLocal getPersonManager()
166     {
167         if (pm == null)
168         {
169             Context JavaDoc context = null;
170             try
171             {
172                 context = new InitialContext JavaDoc();
173                 pm = ((PersonManagerODMGLocalHome) context.lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
174                 log.info("** Found bean: " + pm);
175                 return pm;
176             }
177             catch (NamingException JavaDoc e)
178             {
179                 log.error("Lookup using ejb-ref " + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
180                 throw new EJBException JavaDoc(e);
181             }
182             catch (CreateException JavaDoc e)
183             {
184                 log.error("Creation of PersonManager failed", e);
185                 throw new EJBException JavaDoc(e);
186             }
187         }
188         return pm;
189     }
190 }
191
Popular Tags