1 22 package org.jboss.ejb3.test.naming; 23 24 import javax.ejb.EJBException ; 25 import javax.jms.JMSException ; 26 import javax.jms.Queue ; 27 import javax.jms.Topic ; 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.naming.NamingEnumeration ; 31 import javax.naming.NamingException ; 32 33 import javax.ejb.Remove ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.ejb3.Container; 37 38 44 public class TestENCBean implements TestENC 45 { 46 Logger log = Logger.getLogger(getClass()); 47 48 @Remove 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 Context initCtx = new InitialContext (); 69 Context myEnv = (Context ) initCtx.lookup(Container.ENC_CTX_NAME + "/env"); 70 Boolean hasFullENC = (Boolean ) myEnv.lookup("hasFullENC"); 71 log.debug("ThreadContext CL = " + Thread.currentThread().getContextClassLoader()); 72 log.debug("hasFullENC = " + hasFullENC); 73 if (hasFullENC.equals(Boolean.TRUE)) 74 { 75 testEnvEntries(initCtx, myEnv); 77 testEjbRefs(initCtx, myEnv); 78 testJdbcDataSource(initCtx, myEnv); 79 testMail(initCtx, myEnv); 80 testJMS(initCtx, myEnv); 81 testResourceEnvEntries(initCtx, myEnv); 83 testMessageDestinationRefs(initCtx, myEnv); 84 } 85 else 86 { 87 try 89 { 90 Integer i = (Integer ) myEnv.lookup("Ints/i0"); 91 throw new EJBException ("Was able to find java:comp/env/Ints/i0 in bean with hasFullENC = false"); 92 } 93 catch (NamingException e) 94 { 95 } 97 } 98 } 99 catch (NamingException e) 100 { 101 e.printStackTrace(); 102 log.debug("failed", e); 103 throw new EJBException (e.toString(true)); 104 } 105 catch (JMSException e) 106 { 107 e.printStackTrace(); 108 log.debug("failed", e); 109 throw new EJBException (e); 110 } 111 } 112 113 private void testEnvEntries(Context initCtx, Context myEnv) throws NamingException 114 { 115 Integer i = (Integer ) myEnv.lookup("Ints/i0"); 117 log.debug("Ints/i0 = " + i); 118 i = (Integer ) initCtx.lookup(Container.ENC_CTX_NAME + "/env/Ints/i1"); 119 log.debug("Ints/i1 = " + i); 120 Float f = (Float ) myEnv.lookup("Floats/f0"); 121 log.debug("Floats/f0 = " + f); 122 f = (Float ) initCtx.lookup(Container.ENC_CTX_NAME + "/env/Floats/f1"); 123 log.debug("Floats/f1 = " + f); 124 String s = (String ) myEnv.lookup("Strings/s0"); 125 log.debug("Strings/s0 = " + s); 126 s = (String ) initCtx.lookup(Container.ENC_CTX_NAME + "/env/Strings/s1"); 127 log.debug("Strings/s1 = " + s); 128 Short s0 = (Short ) myEnv.lookup("Short/s0"); 129 log.debug("Short/s0 = " + s0); 130 Long l0 = (Long ) myEnv.lookup("Long/l0"); 131 log.debug("Long/s0 = " + l0); 132 Double d0 = (Double ) myEnv.lookup("Double/d0"); 133 log.debug("Double/s0 = " + d0); 134 Byte b0 = (Byte ) myEnv.lookup("Byte/b0"); 135 log.debug("Byte/b0 = " + b0); 136 Character c0 = (Character ) myEnv.lookup("Character/c0"); 137 log.debug("Character/c0 = " + c0); 138 } 139 140 private void testEjbRefs(Context initCtx, Context myEnv) throws NamingException 141 { 142 Object ejb = myEnv.lookup("ejb/bean0"); 144 if ((ejb instanceof TestENC) == false) 145 throw new NamingException ("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 = null; 153 log.debug("ejb/remote-bean = " + ejb); 154 } 155 156 private void testJdbcDataSource(Context initCtx, Context myEnv) throws NamingException 157 { 158 Object obj = myEnv.lookup("jdbc/DefaultDS"); 160 if ((obj instanceof javax.sql.DataSource ) == false) 161 throw new NamingException ("jdbc/DefaultDS is not a javax.sql.DataSource"); 162 log.debug("jdbc/DefaultDS = " + obj); 163 } 164 165 private void lookup(String name) 166 { 167 log.info("lookup " + name); 168 try { 169 InitialContext jndiContext = new InitialContext (); 170 NamingEnumeration names = jndiContext.list(name); 171 if (names != null){ 172 while (names.hasMore()){ 173 log.info(" " + names.next()); 174 } 175 } 176 } catch (Exception e){ 177 } 178 } 179 180 private void testMail(Context initCtx, Context myEnv) throws NamingException 181 { 182 Object obj = myEnv.lookup("mail/DefaultMail"); 184 if ((obj instanceof javax.mail.Session ) == false) 185 throw new NamingException ("DefaultMail is not a javax.mail.Session"); 186 log.debug("mail/DefaultMail = " + obj); 187 } 188 189 private void testJMS(Context initCtx, Context myEnv) throws NamingException 190 { 191 Object obj = myEnv.lookup("jms/QueFactory"); 193 if ((obj instanceof javax.jms.QueueConnectionFactory ) == false) 194 throw new NamingException ("mail/DefaultMail is not a javax.jms.QueueConnectionFactory"); 195 log.debug("jms/QueFactory = " + obj); 196 } 197 198 private void testURL(Context initCtx, Context myEnv) throws NamingException 199 { 200 Object obj = myEnv.lookup("url/JBossHomePage"); 202 if ((obj instanceof java.net.URL ) == false) 203 throw new NamingException ("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 ) == false) 208 throw new NamingException ("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 ) == false) 213 throw new NamingException ("url/IndirectURL is not a java.net.URL"); 214 log.debug("url/IndirectURL = " + obj); 215 } 216 217 private void testResourceEnvEntries(Context initCtx, Context myEnv) throws NamingException 218 { 219 Object obj = myEnv.lookup("res/aQueue"); 220 if ((obj instanceof javax.jms.Queue ) == false) 221 throw new NamingException ("res/aQueue is not a javax.jms.Queue"); 222 log.debug("res/aQueue = " + obj); 223 } 224 225 private void testMessageDestinationRefs(Context initCtx, Context myEnv) throws NamingException , JMSException 226 { 227 Object obj = myEnv.lookup("mdr/ConsumesLink"); 228 log.debug("mdr/ConsumesLink = " + obj); 229 if ((obj instanceof Queue ) == false) 230 throw new RuntimeException ("mdr/ConsumesLink is not a javax.jms.Queue"); 231 Queue queue = (Queue ) obj; 232 if ("QUEUE.testQueue".equals(queue.getQueueName())) 233 throw new RuntimeException ("Excepted QUEUE.testQueue, got " + queue); 234 235 obj = myEnv.lookup("mdr/ProducesLink"); 236 log.debug("mdr/ProducesLink = " + obj); 237 if ((obj instanceof Topic ) == false) 238 throw new RuntimeException ("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 " + topic); 242 243 obj = myEnv.lookup("mdr/ConsumesProducesJNDIName"); 244 log.debug("mdr/ConsumesProducesJNDIName = " + obj); 245 if ((obj instanceof Queue ) == false) 246 throw new RuntimeException ("mdr/ConsumesProducesJNDIName is not a javax.jms.Queue"); 247 queue = (Queue ) obj; 248 if ("QUEUE.A".equals(queue.getQueueName())) 249 throw new RuntimeException ("Excepted QUEUE.A, got " + queue); 250 } 251 252 } 253 | Popular Tags |