KickJava   Java API By Example, From Geeks To Geeks.

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

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