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.test.naming.ejb;
023:
024: import java.util.Properties;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionBean;
029: import javax.ejb.SessionContext;
030: import javax.jms.JMSException;
031: import javax.jms.Queue;
032: import javax.jms.Topic;
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036: import javax.rmi.PortableRemoteObject;
037:
038: import org.apache.log4j.Logger;
039: import org.jboss.security.SecurityAssociation;
040: import org.jboss.test.naming.interfaces.TestEjbLinkLocal;
041: import org.jboss.test.naming.interfaces.TestEjbLinkLocalHome;
042:
043: /** A bean that does nothing but access resources from the ENC
044: to test ENC usage.
045:
046: @author Scott.Stark@jboss.org
047: @version $Revision: 60317 $
048: */
049: public class TestENCBean implements SessionBean {
050: private static final long serialVersionUID = 1;
051: Logger log = Logger.getLogger(getClass());
052:
053: private SessionContext sessionContext;
054:
055: public void ejbCreate() throws CreateException {
056: }
057:
058: // --- Begin SessionBean interface methods
059: public void ejbActivate() {
060: }
061:
062: public void ejbPassivate() {
063: }
064:
065: public void ejbRemove() {
066: }
067:
068: public void setSessionContext(SessionContext sessionContext)
069: throws EJBException {
070: this .sessionContext = sessionContext;
071: }
072:
073: // --- End SessionBean interface methods
074:
075: public long stressENC(long iterations) {
076: long start = System.currentTimeMillis();
077: for (int i = 0; i < iterations; i++)
078: accessENC();
079: long end = System.currentTimeMillis();
080: return end - start;
081: }
082:
083: public void accessENC() {
084: try {
085: // Obtain the enterprise beans environment naming context.
086: Context initCtx = new InitialContext();
087: Context myEnv = (Context) initCtx.lookup("java:comp/env");
088: Boolean hasFullENC = (Boolean) myEnv.lookup("hasFullENC");
089: log.debug("ThreadContext CL = "
090: + Thread.currentThread().getContextClassLoader());
091: log.debug("hasFullENC = " + hasFullENC);
092: if (hasFullENC.equals(Boolean.TRUE)) {
093: // This bean should have the full ENC setup of the ENCBean
094: testEnvEntries(initCtx, myEnv);
095: testEjbRefs(initCtx, myEnv);
096: testJdbcDataSource(initCtx, myEnv);
097: testMail(initCtx, myEnv);
098: testJMS(initCtx, myEnv);
099: testURL(initCtx, myEnv);
100: testResourceEnvEntries(initCtx, myEnv);
101: testMessageDestinationRefs(initCtx, myEnv);
102: } else {
103: // This bean should only have the hasFullENC env entry
104: try {
105: Integer i = (Integer) myEnv.lookup("Ints/i0");
106: throw new EJBException(
107: "Was able to find java:comp/env/Ints/i0 in bean with hasFullENC = false");
108: } catch (NamingException e) {
109: // This is what we expect
110: }
111: }
112: } catch (NamingException e) {
113: log.debug("failed", e);
114: throw new EJBException(e.toString(true));
115: } catch (JMSException e) {
116: log.debug("failed", e);
117: throw new EJBException(e);
118: }
119: }
120:
121: public String testEjbLinkCallerLocalWithLogin(String jndiName,
122: String username, String password) {
123: try {
124: log.info("testEjbLinkCallerLocalWithLogin");
125: String incomingCaller = this .sessionContext
126: .getCallerPrincipal().getName();
127: log.info("incomingCaller=" + incomingCaller);
128: Properties env = new Properties();
129: env
130: .setProperty(Context.INITIAL_CONTEXT_FACTORY,
131: "org.jboss.security.jndi.JndiLoginInitialContextFactory");
132: env.setProperty(Context.SECURITY_PRINCIPAL, username);
133: env.setProperty(Context.SECURITY_CREDENTIALS, password);
134: env.setProperty("jnp.restoreLoginIdentity", "true");
135: InitialContext initial = new InitialContext(env);
136: Object object = initial.lookup(jndiName);
137: log.debug("jndiName=" + jndiName);
138:
139: // TODO: caching in the ejb context does not following changes
140: //String newCaller = this.sessionContext.getCallerPrincipal().getName();
141: String newCaller = SecurityAssociation.getCallerPrincipal()
142: .getName();
143: if (newCaller.equals(username) == false)
144: return "False-newCaller," + newCaller;
145: log.info("newCaller=" + newCaller);
146:
147: TestEjbLinkLocalHome home = (TestEjbLinkLocalHome) PortableRemoteObject
148: .narrow(object, TestEjbLinkLocalHome.class);
149: TestEjbLinkLocal bean = home.create();
150: String result = bean.testEjbLinkCalled();
151: initial.close();
152: // Validate that the incoming caller has been restored
153: String restoredCaller = this .sessionContext
154: .getCallerPrincipal().getName();
155: if (restoredCaller.equals(incomingCaller) == false)
156: return "False-restoredCalled," + restoredCaller;
157: log.info("restoredCaller=" + restoredCaller);
158:
159: return result;
160: } catch (Exception e) {
161: log.debug("failed", e);
162: return "Failed";
163: }
164: }
165:
166: private void testEnvEntries(Context initCtx, Context myEnv)
167: throws NamingException {
168: // Basic env values
169: Integer i = (Integer) myEnv.lookup("Ints/i0");
170: log.debug("Ints/i0 = " + i);
171: i = (Integer) initCtx.lookup("java:comp/env/Ints/i1");
172: log.debug("Ints/i1 = " + i);
173: Float f = (Float) myEnv.lookup("Floats/f0");
174: log.debug("Floats/f0 = " + f);
175: f = (Float) initCtx.lookup("java:comp/env/Floats/f1");
176: log.debug("Floats/f1 = " + f);
177: String s = (String) myEnv.lookup("Strings/s0");
178: log.debug("Strings/s0 = " + s);
179: s = (String) initCtx.lookup("java:comp/env/Strings/s1");
180: log.debug("Strings/s1 = " + s);
181: Short s0 = (Short) myEnv.lookup("Short/s0");
182: log.debug("Short/s0 = " + s0);
183: Long l0 = (Long) myEnv.lookup("Long/l0");
184: log.debug("Long/s0 = " + l0);
185: Double d0 = (Double) myEnv.lookup("Double/d0");
186: log.debug("Double/s0 = " + d0);
187: Byte b0 = (Byte) myEnv.lookup("Byte/b0");
188: log.debug("Byte/b0 = " + b0);
189: Character c0 = (Character) myEnv.lookup("Character/c0");
190: log.debug("Character/c0 = " + c0);
191: }
192:
193: private void testEjbRefs(Context initCtx, Context myEnv)
194: throws NamingException {
195: // EJB References
196: Object ejb = myEnv.lookup("ejb/bean0");
197: if ((ejb instanceof javax.ejb.EJBHome) == false)
198: throw new NamingException(
199: "ejb/bean0 is not a javax.ejb.EJBHome");
200: log.debug("ejb/bean0 = " + ejb);
201: ejb = initCtx.lookup("java:comp/env/ejb/bean1");
202: log.debug("ejb/bean1 = " + ejb);
203: ejb = initCtx.lookup("java:comp/env/ejb/bean2");
204: log.debug("ejb/bean2 = " + ejb);
205: //ejb = initCtx.lookup("java:comp/env/ejb/remote-bean");
206: ejb = null;
207: log.debug("ejb/remote-bean = " + ejb);
208: }
209:
210: private void testJdbcDataSource(Context initCtx, Context myEnv)
211: throws NamingException {
212: // JDBC DataSource
213: Object obj = myEnv.lookup("jdbc/DefaultDS");
214: if ((obj instanceof javax.sql.DataSource) == false)
215: throw new NamingException(
216: "jdbc/DefaultDS is not a javax.sql.DataSource");
217: log.debug("jdbc/DefaultDS = " + obj);
218: }
219:
220: private void testMail(Context initCtx, Context myEnv)
221: throws NamingException {
222: // JavaMail Session
223: Object obj = myEnv.lookup("mail/DefaultMail");
224: if ((obj instanceof javax.mail.Session) == false)
225: throw new NamingException(
226: "mail/DefaultMail is not a javax.mail.Session");
227: log.debug("mail/DefaultMail = " + obj);
228: }
229:
230: private void testJMS(Context initCtx, Context myEnv)
231: throws NamingException {
232: // JavaMail Session
233: Object obj = myEnv.lookup("jms/QueFactory");
234: if ((obj instanceof javax.jms.QueueConnectionFactory) == false)
235: throw new NamingException(
236: "mail/DefaultMail is not a javax.jms.QueueConnectionFactory");
237: log.debug("jms/QueFactory = " + obj);
238: }
239:
240: private void testURL(Context initCtx, Context myEnv)
241: throws NamingException {
242: // JavaMail Session
243: Object obj = myEnv.lookup("url/JBossHomePage");
244: if ((obj instanceof java.net.URL) == false)
245: throw new NamingException(
246: "url/JBossHomePage is not a java.net.URL");
247: log.debug("url/SourceforgeHomePage = " + obj);
248:
249: obj = myEnv.lookup("url/SourceforgeHomePage");
250: if ((obj instanceof java.net.URL) == false)
251: throw new NamingException(
252: "url/SourceforgeHomePage is not a java.net.URL");
253: log.debug("url/SourceforgeHomePage = " + obj);
254:
255: obj = myEnv.lookup("url/IndirectURL");
256: if ((obj instanceof java.net.URL) == false)
257: throw new NamingException(
258: "url/IndirectURL is not a java.net.URL");
259: log.debug("url/IndirectURL = " + obj);
260: }
261:
262: private void testResourceEnvEntries(Context initCtx, Context myEnv)
263: throws NamingException {
264: Object obj = myEnv.lookup("res/aQueue");
265: if ((obj instanceof javax.jms.Queue) == false)
266: throw new NamingException(
267: "res/aQueue is not a javax.jms.Queue");
268: log.debug("res/aQueue = " + obj);
269: }
270:
271: private void testMessageDestinationRefs(Context initCtx,
272: Context myEnv) throws NamingException, JMSException {
273: Object obj = myEnv.lookup("mdr/ConsumesLink");
274: log.debug("mdr/ConsumesLink = " + obj);
275: if ((obj instanceof Queue) == false)
276: throw new RuntimeException(
277: "mdr/ConsumesLink is not a javax.jms.Queue");
278: Queue queue = (Queue) obj;
279: if ("QUEUE.testQueue".equals(queue.getQueueName()))
280: throw new RuntimeException("Excepted QUEUE.testQueue, got "
281: + queue);
282:
283: obj = myEnv.lookup("mdr/ProducesLink");
284: log.debug("mdr/ProducesLink = " + obj);
285: if ((obj instanceof Topic) == false)
286: throw new RuntimeException(
287: "mdr/ProducesLink is not a javax.jms.Topic");
288: Topic topic = (Topic) obj;
289: if ("TOPIC.testTopic".equals(topic.getTopicName()))
290: throw new RuntimeException("Excepted TOPIC.testTopic got "
291: + topic);
292:
293: obj = myEnv.lookup("mdr/ConsumesProducesJNDIName");
294: log.debug("mdr/ConsumesProducesJNDIName = " + obj);
295: if ((obj instanceof Queue) == false)
296: throw new RuntimeException(
297: "mdr/ConsumesProducesJNDIName is not a javax.jms.Queue");
298: queue = (Queue) obj;
299: if ("QUEUE.A".equals(queue.getQueueName()))
300: throw new RuntimeException("Excepted QUEUE.A, got " + queue);
301: }
302:
303: }
|