001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.naming;
023:
024: import javax.ejb.EJBException;
025: import javax.jms.JMSException;
026: import javax.jms.Queue;
027: import javax.jms.Topic;
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingEnumeration;
031: import javax.naming.NamingException;
032:
033: import javax.ejb.Remove;
034:
035: import org.jboss.logging.Logger;
036: import org.jboss.ejb3.Container;
037:
038: /** A bean that does nothing but access resources from the ENC
039: to test ENC usage.
040:
041: @author Scott.Stark@jboss.org
042: @version $Revision: 60233 $
043: */
044: public class TestENCBean implements TestENC {
045: Logger log = Logger.getLogger(getClass());
046:
047: @Remove
048: public void remove() {
049:
050: }
051:
052: public long stressENC(long iterations) {
053: long start = System.currentTimeMillis();
054: for (int i = 0; i < iterations; i++)
055: accessENC();
056: long end = System.currentTimeMillis();
057: return end - start;
058: }
059:
060: public void accessENC() {
061: try {
062: // Obtain the enterprise beans environment naming context.
063: Context initCtx = new InitialContext();
064: Context myEnv = (Context) initCtx
065: .lookup(Container.ENC_CTX_NAME + "/env");
066: Boolean hasFullENC = (Boolean) myEnv.lookup("hasFullENC");
067: log.debug("ThreadContext CL = "
068: + Thread.currentThread().getContextClassLoader());
069: log.debug("hasFullENC = " + hasFullENC);
070: if (hasFullENC.equals(Boolean.TRUE)) {
071: // This bean should have the full ENC setup of the ENCBean
072: testEnvEntries(initCtx, myEnv);
073: testEjbRefs(initCtx, myEnv);
074: testJdbcDataSource(initCtx, myEnv);
075: testMail(initCtx, myEnv);
076: testJMS(initCtx, myEnv);
077: // testURL(initCtx, myEnv);
078: testResourceEnvEntries(initCtx, myEnv);
079: testMessageDestinationRefs(initCtx, myEnv);
080: } else {
081: // This bean should only have the hasFullENC env entry
082: try {
083: Integer i = (Integer) myEnv.lookup("Ints/i0");
084: throw new EJBException(
085: "Was able to find java:comp/env/Ints/i0 in bean with hasFullENC = false");
086: } catch (NamingException e) {
087: // This is what we expect
088: }
089: }
090: } catch (NamingException e) {
091: e.printStackTrace();
092: log.debug("failed", e);
093: throw new EJBException(e.toString(true));
094: } catch (JMSException e) {
095: e.printStackTrace();
096: log.debug("failed", e);
097: throw new EJBException(e);
098: }
099: }
100:
101: private void testEnvEntries(Context initCtx, Context myEnv)
102: throws NamingException {
103: // Basic env values
104: Integer i = (Integer) myEnv.lookup("Ints/i0");
105: log.debug("Ints/i0 = " + i);
106: i = (Integer) initCtx.lookup(Container.ENC_CTX_NAME
107: + "/env/Ints/i1");
108: log.debug("Ints/i1 = " + i);
109: Float f = (Float) myEnv.lookup("Floats/f0");
110: log.debug("Floats/f0 = " + f);
111: f = (Float) initCtx.lookup(Container.ENC_CTX_NAME
112: + "/env/Floats/f1");
113: log.debug("Floats/f1 = " + f);
114: String s = (String) myEnv.lookup("Strings/s0");
115: log.debug("Strings/s0 = " + s);
116: s = (String) initCtx.lookup(Container.ENC_CTX_NAME
117: + "/env/Strings/s1");
118: log.debug("Strings/s1 = " + s);
119: Short s0 = (Short) myEnv.lookup("Short/s0");
120: log.debug("Short/s0 = " + s0);
121: Long l0 = (Long) myEnv.lookup("Long/l0");
122: log.debug("Long/s0 = " + l0);
123: Double d0 = (Double) myEnv.lookup("Double/d0");
124: log.debug("Double/s0 = " + d0);
125: Byte b0 = (Byte) myEnv.lookup("Byte/b0");
126: log.debug("Byte/b0 = " + b0);
127: Character c0 = (Character) myEnv.lookup("Character/c0");
128: log.debug("Character/c0 = " + c0);
129: }
130:
131: private void testEjbRefs(Context initCtx, Context myEnv)
132: throws NamingException {
133: // EJB References
134: Object ejb = myEnv.lookup("ejb/bean0");
135: if ((ejb instanceof TestENC) == false)
136: throw new NamingException("ejb/bean0 is not a TestENC");
137: log.debug("ejb/bean0 = " + ejb);
138: ejb = initCtx.lookup(Container.ENC_CTX_NAME + "/env/ejb/bean1");
139: log.debug("ejb/bean1 = " + ejb);
140: ejb = initCtx.lookup(Container.ENC_CTX_NAME + "/env/ejb/bean2");
141: log.debug("ejb/bean2 = " + ejb);
142: //ejb = initCtx.lookup("java:comp/env/ejb/remote-bean");
143: ejb = null;
144: log.debug("ejb/remote-bean = " + ejb);
145: }
146:
147: private void testJdbcDataSource(Context initCtx, Context myEnv)
148: throws NamingException {
149: // JDBC DataSource
150: Object obj = myEnv.lookup("jdbc/DefaultDS");
151: if ((obj instanceof javax.sql.DataSource) == false)
152: throw new NamingException(
153: "jdbc/DefaultDS is not a javax.sql.DataSource");
154: log.debug("jdbc/DefaultDS = " + obj);
155: }
156:
157: private void lookup(String name) {
158: log.info("lookup " + name);
159: try {
160: InitialContext jndiContext = new InitialContext();
161: NamingEnumeration names = jndiContext.list(name);
162: if (names != null) {
163: while (names.hasMore()) {
164: log.info(" " + names.next());
165: }
166: }
167: } catch (Exception e) {
168: }
169: }
170:
171: private void testMail(Context initCtx, Context myEnv)
172: throws NamingException {
173: // JavaMail Session
174: Object obj = myEnv.lookup("mail/DefaultMail");
175: if ((obj instanceof javax.mail.Session) == false)
176: throw new NamingException(
177: "DefaultMail is not a javax.mail.Session");
178: log.debug("mail/DefaultMail = " + obj);
179: }
180:
181: private void testJMS(Context initCtx, Context myEnv)
182: throws NamingException {
183: // JavaMail Session
184: Object obj = myEnv.lookup("jms/QueFactory");
185: if ((obj instanceof javax.jms.QueueConnectionFactory) == false)
186: throw new NamingException(
187: "mail/DefaultMail is not a javax.jms.QueueConnectionFactory");
188: log.debug("jms/QueFactory = " + obj);
189: }
190:
191: private void testURL(Context initCtx, Context myEnv)
192: throws NamingException {
193: // JavaMail Session
194: Object obj = myEnv.lookup("url/JBossHomePage");
195: if ((obj instanceof java.net.URL) == false)
196: throw new NamingException(
197: "url/JBossHomePage is not a java.net.URL");
198: log.debug("url/SourceforgeHomePage = " + obj);
199:
200: obj = myEnv.lookup("url/SourceforgeHomePage");
201: if ((obj instanceof java.net.URL) == false)
202: throw new NamingException(
203: "url/SourceforgeHomePage is not a java.net.URL");
204: log.debug("url/SourceforgeHomePage = " + obj);
205:
206: obj = myEnv.lookup("url/IndirectURL");
207: if ((obj instanceof java.net.URL) == false)
208: throw new NamingException(
209: "url/IndirectURL is not a java.net.URL");
210: log.debug("url/IndirectURL = " + obj);
211: }
212:
213: private void testResourceEnvEntries(Context initCtx, Context myEnv)
214: throws NamingException {
215: Object obj = myEnv.lookup("res/aQueue");
216: if ((obj instanceof javax.jms.Queue) == false)
217: throw new NamingException(
218: "res/aQueue is not a javax.jms.Queue");
219: log.debug("res/aQueue = " + obj);
220: }
221:
222: private void testMessageDestinationRefs(Context initCtx,
223: Context myEnv) throws NamingException, JMSException {
224: Object obj = myEnv.lookup("mdr/ConsumesLink");
225: log.debug("mdr/ConsumesLink = " + obj);
226: if ((obj instanceof Queue) == false)
227: throw new RuntimeException(
228: "mdr/ConsumesLink is not a javax.jms.Queue");
229: Queue queue = (Queue) obj;
230: if ("QUEUE.testQueue".equals(queue.getQueueName()))
231: throw new RuntimeException("Excepted QUEUE.testQueue, got "
232: + queue);
233:
234: obj = myEnv.lookup("mdr/ProducesLink");
235: log.debug("mdr/ProducesLink = " + obj);
236: if ((obj instanceof Topic) == false)
237: throw new RuntimeException(
238: "mdr/ProducesLink is not a javax.jms.Topic");
239: Topic topic = (Topic) obj;
240: if ("TOPIC.testTopic".equals(topic.getTopicName()))
241: throw new RuntimeException("Excepted TOPIC.testTopic got "
242: + topic);
243:
244: obj = myEnv.lookup("mdr/ConsumesProducesJNDIName");
245: log.debug("mdr/ConsumesProducesJNDIName = " + obj);
246: if ((obj instanceof Queue) == false)
247: throw new RuntimeException(
248: "mdr/ConsumesProducesJNDIName is not a javax.jms.Queue");
249: queue = (Queue) obj;
250: if ("QUEUE.A".equals(queue.getQueueName()))
251: throw new RuntimeException("Excepted QUEUE.A, got " + queue);
252: }
253:
254: }
|