KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > naming > ejb > TestENCBean


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.naming.ejb;
23
24 import javax.ejb.CreateException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.Topic JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 import org.jboss.logging.Logger;
36
37 /** A bean that does nothing but access resources from the ENC
38  to test ENC usage.
39
40  @author Scott.Stark@jboss.org
41  @version $Revision: 58115 $
42  */

43 public class TestENCBean implements SessionBean JavaDoc
44 {
45    Logger log = Logger.getLogger(getClass());
46
47    private SessionContext JavaDoc sessionContext;
48
49    public void ejbCreate() throws CreateException JavaDoc
50    {
51    }
52
53 // --- Begin SessionBean interface methods
54
public void ejbActivate()
55    {
56    }
57
58    public void ejbPassivate()
59    {
60    }
61
62    public void ejbRemove()
63    {
64    }
65
66    public void setSessionContext(SessionContext JavaDoc sessionContext) throws EJBException JavaDoc
67    {
68       this.sessionContext = sessionContext;
69    }
70
71 // --- End SessionBean interface methods
72

73    public long stressENC(long iterations)
74    {
75       long start = System.currentTimeMillis();
76       for(int i = 0; i < iterations; i ++)
77          accessENC();
78       long end = System.currentTimeMillis();
79       return end - start;
80    }
81
82    public void accessENC()
83    {
84       try
85       {
86          // Obtain the enterprise beans environment naming context.
87
Context JavaDoc initCtx = new InitialContext JavaDoc();
88          Context JavaDoc myEnv = (Context JavaDoc) initCtx.lookup("java:comp/env");
89          Boolean JavaDoc hasFullENC = (Boolean JavaDoc) myEnv.lookup("hasFullENC");
90          log.debug("ThreadContext CL = " + Thread.currentThread().getContextClassLoader());
91          log.debug("hasFullENC = " + hasFullENC);
92          if (hasFullENC.equals(Boolean.TRUE))
93          {
94             // This bean should have the full ENC setup of the ENCBean
95
testEnvEntries(initCtx, myEnv);
96             testEjbRefs(initCtx, myEnv);
97             testJdbcDataSource(initCtx, myEnv);
98             testMail(initCtx, myEnv);
99             testJMS(initCtx, myEnv);
100             testURL(initCtx, myEnv);
101             testResourceEnvEntries(initCtx, myEnv);
102             testMessageDestinationRefs(initCtx, myEnv);
103          }
104          else
105          {
106             // This bean should only have the hasFullENC env entry
107
try
108             {
109                Integer JavaDoc i = (Integer JavaDoc) myEnv.lookup("Ints/i0");
110                throw new EJBException JavaDoc("Was able to find java:comp/env/Ints/i0 in bean with hasFullENC = false");
111             }
112             catch (NamingException JavaDoc e)
113             {
114                // This is what we expect
115
}
116          }
117       }
118       catch (NamingException JavaDoc e)
119       {
120          log.debug("failed", e);
121          throw new EJBException JavaDoc(e.toString(true));
122       }
123       catch (JMSException JavaDoc e)
124       {
125          log.debug("failed", e);
126          throw new EJBException JavaDoc(e);
127       }
128    }
129
130    private void testEnvEntries(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
131    {
132       // Basic env values
133
Integer JavaDoc i = (Integer JavaDoc) myEnv.lookup("Ints/i0");
134       log.debug("Ints/i0 = " + i);
135       i = (Integer JavaDoc) initCtx.lookup("java:comp/env/Ints/i1");
136       log.debug("Ints/i1 = " + i);
137       Float JavaDoc f = (Float JavaDoc) myEnv.lookup("Floats/f0");
138       log.debug("Floats/f0 = " + f);
139       f = (Float JavaDoc) initCtx.lookup("java:comp/env/Floats/f1");
140       log.debug("Floats/f1 = " + f);
141       String JavaDoc s = (String JavaDoc) myEnv.lookup("Strings/s0");
142       log.debug("Strings/s0 = " + s);
143       s = (String JavaDoc) initCtx.lookup("java:comp/env/Strings/s1");
144       log.debug("Strings/s1 = " + s);
145       Short JavaDoc s0 = (Short JavaDoc) myEnv.lookup("Short/s0");
146       log.debug("Short/s0 = " + s0);
147       Long JavaDoc l0 = (Long JavaDoc) myEnv.lookup("Long/l0");
148       log.debug("Long/s0 = " + l0);
149       Double JavaDoc d0 = (Double JavaDoc) myEnv.lookup("Double/d0");
150       log.debug("Double/s0 = " + d0);
151       Byte JavaDoc b0 = (Byte JavaDoc) myEnv.lookup("Byte/b0");
152       log.debug("Byte/b0 = " + b0);
153       Character JavaDoc c0 = (Character JavaDoc) myEnv.lookup("Character/c0");
154       log.debug("Character/c0 = " + c0);
155    }
156
157    private void testEjbRefs(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
158    {
159       // EJB References
160
Object JavaDoc ejb = myEnv.lookup("ejb/bean0");
161       if ((ejb instanceof javax.ejb.EJBHome JavaDoc) == false)
162          throw new NamingException JavaDoc("ejb/bean0 is not a javax.ejb.EJBHome");
163       log.debug("ejb/bean0 = " + ejb);
164       ejb = initCtx.lookup("java:comp/env/ejb/bean1");
165       log.debug("ejb/bean1 = " + ejb);
166       ejb = initCtx.lookup("java:comp/env/ejb/bean2");
167       log.debug("ejb/bean2 = " + ejb);
168       //ejb = initCtx.lookup("java:comp/env/ejb/remote-bean");
169
ejb = null;
170       log.debug("ejb/remote-bean = " + ejb);
171    }
172
173    private void testJdbcDataSource(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
174    {
175       // JDBC DataSource
176
Object JavaDoc obj = myEnv.lookup("jdbc/DefaultDS");
177       if ((obj instanceof javax.sql.DataSource JavaDoc) == false)
178          throw new NamingException JavaDoc("jdbc/DefaultDS is not a javax.sql.DataSource");
179       log.debug("jdbc/DefaultDS = " + obj);
180    }
181
182    private void testMail(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
183    {
184       // JavaMail Session
185
Object JavaDoc obj = myEnv.lookup("mail/DefaultMail");
186       if ((obj instanceof javax.mail.Session JavaDoc) == false)
187          throw new NamingException JavaDoc("mail/DefaultMail is not a javax.mail.Session");
188       log.debug("mail/DefaultMail = " + obj);
189    }
190
191    private void testJMS(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
192    {
193       // JavaMail Session
194
Object JavaDoc obj = myEnv.lookup("jms/QueFactory");
195       if ((obj instanceof javax.jms.QueueConnectionFactory JavaDoc) == false)
196          throw new NamingException JavaDoc("mail/DefaultMail is not a javax.jms.QueueConnectionFactory");
197       log.debug("jms/QueFactory = " + obj);
198    }
199
200    private void testURL(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
201    {
202       // JavaMail Session
203
Object JavaDoc obj = myEnv.lookup("url/JBossHomePage");
204       if ((obj instanceof java.net.URL JavaDoc) == false)
205          throw new NamingException JavaDoc("url/JBossHomePage is not a java.net.URL");
206       log.debug("url/SourceforgeHomePage = " + obj);
207
208       obj = myEnv.lookup("url/SourceforgeHomePage");
209       if ((obj instanceof java.net.URL JavaDoc) == false)
210          throw new NamingException JavaDoc("url/SourceforgeHomePage is not a java.net.URL");
211       log.debug("url/SourceforgeHomePage = " + obj);
212
213       obj = myEnv.lookup("url/IndirectURL");
214       if ((obj instanceof java.net.URL JavaDoc) == false)
215          throw new NamingException JavaDoc("url/IndirectURL is not a java.net.URL");
216       log.debug("url/IndirectURL = " + obj);
217    }
218
219    private void testResourceEnvEntries(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
220    {
221       Object JavaDoc obj = myEnv.lookup("res/aQueue");
222       if ((obj instanceof javax.jms.Queue JavaDoc) == false)
223          throw new NamingException JavaDoc("res/aQueue is not a javax.jms.Queue");
224       log.debug("res/aQueue = " + obj);
225    }
226
227    private void testMessageDestinationRefs(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc, JMSException JavaDoc
228    {
229       Object JavaDoc obj = myEnv.lookup("mdr/ConsumesLink");
230       log.debug("mdr/ConsumesLink = " + obj);
231       if ((obj instanceof Queue JavaDoc) == false)
232          throw new RuntimeException JavaDoc("mdr/ConsumesLink is not a javax.jms.Queue");
233       Queue JavaDoc queue = (Queue JavaDoc) obj;
234       if ("QUEUE.testQueue".equals(queue.getQueueName()))
235          throw new RuntimeException JavaDoc("Excepted QUEUE.testQueue, got " + queue);
236       
237       obj = myEnv.lookup("mdr/ProducesLink");
238       log.debug("mdr/ProducesLink = " + obj);
239       if ((obj instanceof Topic JavaDoc) == false)
240          throw new RuntimeException JavaDoc("mdr/ProducesLink is not a javax.jms.Topic");
241       Topic JavaDoc topic = (Topic JavaDoc) obj;
242       if ("TOPIC.testTopic".equals(topic.getTopicName()))
243          throw new RuntimeException JavaDoc("Excepted TOPIC.testTopic got " + topic);
244
245       obj = myEnv.lookup("mdr/ConsumesProducesJNDIName");
246       log.debug("mdr/ConsumesProducesJNDIName = " + obj);
247       if ((obj instanceof Queue JavaDoc) == false)
248          throw new RuntimeException JavaDoc("mdr/ConsumesProducesJNDIName is not a javax.jms.Queue");
249       queue = (Queue JavaDoc) obj;
250       if ("QUEUE.A".equals(queue.getQueueName()))
251          throw new RuntimeException JavaDoc("Excepted QUEUE.A, got " + queue);
252    }
253
254 }
255
Popular Tags