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.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.List;
026:
027: import org.apache.ojb.broker.util.logging.Logger;
028: import org.apache.ojb.broker.util.logging.LoggerFactory;
029: import org.apache.ojb.ejb.SessionBeanImpl;
030:
031: /**
032: * Simple example bean do nested bean calls,
033: * using {@link org.apache.ojb.ejb.odmg.PersonManagerODMGBean}
034: * and {@link org.apache.ojb.ejb.odmg.ArticleManagerODMGBean}
035: * within some test methods.
036: *
037: * @ejb:bean
038: * type="Stateless"
039: * name="PersonArticleManagerODMGBean"
040: * jndi-name="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGBean"
041: * local-jndi-name="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGBeanLocal"
042: * view-type="both"
043: * transaction-type="Container"
044: *
045: * @ejb:interface
046: * remote-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGRemote"
047: * local-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGLocal"
048: * extends="javax.ejb.EJBObject"
049: *
050: * @ejb:home
051: * remote-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGHome"
052: * local-class="org.apache.ojb.ejb.odmg.PersonArticleManagerODMGLocalHome"
053: * extends="javax.ejb.EJBHome"
054: *
055: * @ejb:transaction
056: * type="Required"
057: *
058: * @ejb:ejb-ref
059: * ejb-name="PersonManagerODMGBean"
060: * view-type="local"
061: * ref-name="ejb/ojb/odmg/PersonManager"
062: *
063: * @ejb:ejb-ref
064: * ejb-name="ArticleManagerODMGBean"
065: * view-type="local"
066: * ref-name="ejb/ojb/odmg/ArticleManager"
067: *
068: *
069: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
070: * @version $Id: PersonArticleManagerODMGBean.java,v 1.4.2.2 2005/12/21 22:21:39 tomdz Exp $
071: */
072: public class PersonArticleManagerODMGBean extends SessionBeanImpl
073: implements SessionBean {
074: private static final String PERSON_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/PersonManager";
075: private static final String ARTICLE_MANAGER_EJB_REF_NAME = "java:comp/env/ejb/ojb/odmg/ArticleManager";
076:
077: private Logger log = LoggerFactory
078: .getLogger(PersonArticleManagerODMGBean.class);
079:
080: private ArticleManagerODMGLocal am;
081: private PersonManagerODMGLocal pm;
082:
083: public PersonArticleManagerODMGBean() {
084: }
085:
086: public void ejbCreate() {
087:
088: }
089:
090: /**
091: * @ejb:interface-method
092: */
093: public List storeArticles(List articles) {
094: return new ArrayList(getArticleManager()
095: .storeArticles(articles));
096: }
097:
098: /**
099: * @ejb:interface-method
100: */
101: public void deleteArticles(List articles) {
102: getArticleManager().deleteArticles(articles);
103: }
104:
105: /**
106: * @ejb:interface-method
107: */
108: public List storePersons(List persons) {
109: return new ArrayList(getPersonManager().storePersons(persons));
110: }
111:
112: /**
113: * @ejb:interface-method
114: */
115: public void deletePersons(List persons) {
116: getPersonManager().deletePersons(persons);
117: }
118:
119: /**
120: * @ejb:interface-method
121: */
122: public int articleCount() {
123: return getArticleManager().countArticles();
124: }
125:
126: /**
127: * @ejb:interface-method
128: */
129: public int personCount() {
130: return getPersonManager().countPersons();
131: }
132:
133: private ArticleManagerODMGLocal getArticleManager() {
134: if (am == null) {
135: Context context = null;
136: try {
137: context = new InitialContext();
138: am = ((ArticleManagerODMGLocalHome) context
139: .lookup(ARTICLE_MANAGER_EJB_REF_NAME)).create();
140: log.info("** Found bean: " + am);
141: return am;
142: } catch (NamingException e) {
143: log.error("Lookup using ejb-ref "
144: + ARTICLE_MANAGER_EJB_REF_NAME + " failed", e);
145: throw new EJBException(e);
146: } catch (CreateException e) {
147: log.error("Creation of ArticleManager failed", e);
148: throw new EJBException(e);
149: }
150: }
151: return am;
152: }
153:
154: private PersonManagerODMGLocal getPersonManager() {
155: if (pm == null) {
156: Context context = null;
157: try {
158: context = new InitialContext();
159: pm = ((PersonManagerODMGLocalHome) context
160: .lookup(PERSON_MANAGER_EJB_REF_NAME)).create();
161: log.info("** Found bean: " + pm);
162: return pm;
163: } catch (NamingException e) {
164: log.error("Lookup using ejb-ref "
165: + PERSON_MANAGER_EJB_REF_NAME + " failed", e);
166: throw new EJBException(e);
167: } catch (CreateException e) {
168: log.error("Creation of PersonManager failed", e);
169: throw new EJBException(e);
170: }
171: }
172: return pm;
173: }
174: }
|