KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > cacheinvalidation > ejb > FacadeSessionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.cacheinvalidation.ejb;
23
24
25 import org.jboss.logging.Logger;
26
27 import javax.ejb.SessionBean JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.ejb.CreateException JavaDoc;
30 import javax.ejb.FinderException JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
36  * @version <tt>$Revision: 58391 $</tt>
37  */

38 public class FacadeSessionBean
39    implements SessionBean JavaDoc
40 {
41    private static final Logger log = Logger.getLogger(FacadeSessionBean.class);
42    SessionContext JavaDoc ctx;
43
44    private transient CLocalHome ch;
45    private transient ALocalHome ah;
46
47    // Business methods
48

49    /**
50     * @ejb.interface-method
51     * @ejb.transaction type="RequiresNew"
52     */

53    public void setup() throws Exception JavaDoc
54    {
55       CLocal c = getCLocalHome("CRWLocal").create(new Long JavaDoc(1));
56       c.setFirstName("Avoka");
57
58       ALocal a = getALocalHome("ARWLocal").create(new Long JavaDoc(2), "Ataka");
59       a.setC(c);
60    }
61
62    /**
63     * @ejb.interface-method
64     * @ejb.transaction type="RequiresNew"
65     */

66    public void tearDown() throws Exception JavaDoc
67    {
68       try
69       {
70          CLocal c = getCLocalHome("CRWLocal").findByPrimaryKey(new Long JavaDoc(1));
71          c.remove();
72       }
73       catch(FinderException JavaDoc e)
74       {
75       }
76
77       try
78       {
79          ALocal a = getALocalHome("ARWLocal").findByPrimaryKey(new Long JavaDoc(2));
80          a.remove();
81       }
82       catch(FinderException JavaDoc e)
83       {
84       }
85    }
86
87    /**
88     * @ejb.interface-method
89     * @ejb.transaction type="RequiresNew"
90     */

91    public String JavaDoc readFirstName(String JavaDoc jndiName, Long JavaDoc id) throws Exception JavaDoc
92    {
93       final CLocalHome ch = (CLocalHome) getHome(jndiName);
94       CLocal c = ch.findByPrimaryKey(id);
95
96       final String JavaDoc firstName = c.getFirstName();
97       log.debug(jndiName + ".name=" + firstName);
98       return firstName;
99    }
100
101    /**
102     * @ejb.interface-method
103     * @ejb.transaction type="RequiresNew"
104     */

105    public void writeFirstName(String JavaDoc jndiName, Long JavaDoc id, String JavaDoc name) throws Exception JavaDoc
106    {
107       final CLocalHome ch = (CLocalHome) getHome(jndiName);
108       CLocal c = ch.findByPrimaryKey(id);
109
110       c.setFirstName(name);
111       log.debug(jndiName + ".name=" + c.getFirstName());
112    }
113
114    /**
115     * @ejb.interface-method
116     * @ejb.transaction type="RequiresNew"
117     */

118    public String JavaDoc readRelatedAFirstName(String JavaDoc jndiName, Long JavaDoc id) throws Exception JavaDoc
119    {
120       final CLocalHome ch = (CLocalHome) getHome(jndiName);
121       CLocal c = ch.findByPrimaryKey(id);
122
123       final String JavaDoc firstName = c.getA() == null ? null : c.getA().getName();
124       log.debug(jndiName + ".a.name=" + firstName);
125       return firstName;
126    }
127
128    /**
129     * @ejb.interface-method
130     * @ejb.transaction type="RequiresNew"
131     */

132    public void removeA(String JavaDoc jndiName, Long JavaDoc id) throws Exception JavaDoc
133    {
134       final ALocalHome ah = (ALocalHome) getHome(jndiName);
135       ALocal a = ah.findByPrimaryKey(id);
136       a.remove();
137    }
138
139 // SessionBean implementation
140

141    /**
142     * @throws CreateException Description of Exception
143     * @ejb.create-method
144     */

145    public void ejbCreate() throws CreateException JavaDoc
146    {
147    }
148
149    public void ejbActivate()
150    {
151    }
152
153    public void ejbPassivate()
154    {
155    }
156
157    public void ejbRemove()
158    {
159    }
160
161    public void setSessionContext(SessionContext JavaDoc ctx)
162    {
163       this.ctx = ctx;
164    }
165
166    private CLocalHome getCLocalHome(String JavaDoc jndiName)
167    {
168       if(ch == null)
169       {
170          ch = (CLocalHome) getHome(jndiName);
171       }
172       return ch;
173    }
174
175    private ALocalHome getALocalHome(String JavaDoc jndiName)
176    {
177       if(ah == null)
178       {
179          ah = (ALocalHome) getHome(jndiName);
180       }
181       return ah;
182    }
183
184    private Object JavaDoc getHome(String JavaDoc jndiName)
185    {
186       try
187       {
188          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
189          return ctx.lookup(jndiName);
190       }
191       catch(NamingException JavaDoc e)
192       {
193          throw new IllegalStateException JavaDoc("Failed to look up home " + jndiName + ": " + e.getMessage());
194       }
195    }
196 }
197
Popular Tags