KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > JBossTestServices


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;
23
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import javax.management.MBeanServerConnection JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.security.auth.login.LoginContext JavaDoc;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.test.util.AppCallbackHandler;
39
40 /**
41  * This is provides services for jboss junit test cases and TestSetups. It supplies
42  * access to jboss logging, the jboss jmx server, jndi, and a method for
43  * deploying ejb packages. You may supply the JNDI name under which the
44  * RMIAdaptor interface is located via the system property jbosstest.server.name
45  * default (jmx/rmi/RMIAdaptor) and the directory for deployable packages with
46  * the system property jbosstest.deploy.dir (default output/lib).
47  *
48  * Should be subclassed to derive junit support for specific services integrated
49  * into jboss.
50  *
51  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
52  * @author <a HREF="mailto:christoph.jung@jboss.org">Christoph G. Jung</a>
53  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
54  * @version $Revision: 58481 $
55  */

56 public class JBossTestServices extends AbstractTestDelegate
57 {
58    // Constants -----------------------------------------------------
59
public final static String JavaDoc DEPLOYER_NAME = "jboss.system:service=MainDeployer";
60    public final static String JavaDoc DEFAULT_USERNAME = "jduke";
61    public final static String JavaDoc DEFAULT_PASSWORD = "theduke";
62    public final static String JavaDoc DEFAULT_LOGIN_CONFIG = "other";
63    public final static int DEFAULT_THREADCOUNT = 10;
64    public final static int DEFAULT_ITERATIONCOUNT = 1000;
65    public final static int DEFAULT_BEANCOUNT = 100;
66
67    // Attributes ----------------------------------------------------
68
protected MBeanServerConnection JavaDoc server;
69    protected InitialContext JavaDoc initialContext;
70    protected Hashtable JavaDoc jndiEnv;
71    protected LoginContext JavaDoc lc;
72
73    private static Class JavaDoc getClass(String JavaDoc className)
74    {
75       Class JavaDoc clazz;
76       try
77       {
78          clazz = Class.forName(className);
79       }
80       catch(Exception JavaDoc e)
81       {
82          throw new RuntimeException JavaDoc(e);
83       }
84       return clazz;
85    }
86    /**
87     * Constructor for the JBossTestCase object
88     *
89     * @param className Test case name
90     */

91    public JBossTestServices(String JavaDoc className)
92    {
93       super(getClass(className));
94    }
95    public JBossTestServices(Class JavaDoc clazz)
96    {
97       super(clazz);
98    }
99    
100    // Public --------------------------------------------------------
101

102    /**
103     * The JUnit setup method
104     *
105     * @throws Exception for any error
106     */

107    public void setUp() throws Exception JavaDoc
108    {
109       super.setUp();
110       log = getLog();
111       log.debug("JBossTestServices.setUp()");
112       init();
113       log.info("jbosstest.beancount: " + System.getProperty("jbosstest.beancount"));
114       log.info("jbosstest.iterationcount: " + System.getProperty("jbosstest.iterationcount"));
115       log.info("jbosstest.threadcount: " + System.getProperty("jbosstest.threadcount"));
116       log.info("jbosstest.nodeploy: " + System.getProperty("jbosstest.nodeploy"));
117       log.info("jbosstest.jndiurl: " + this.getJndiURL());
118       log.info("jbosstest.jndifactory: " + this.getJndiInitFactory());
119    }
120
121    /**
122     * The teardown method for JUnit
123     *
124     * @throws Exception for any error
125     */

126    public void tearDown() throws Exception JavaDoc
127    {
128       // server = null;
129
log.debug("JBossTestServices.tearDown()");
130    }
131
132
133    /**
134     * Gets the InitialContext attribute of the JBossTestCase object
135     *
136     * @return The InitialContext value
137     * @throws Exception for any error
138     */

139    public InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
140    {
141       return initialContext;
142    }
143
144    /**
145     * Gets the Server attribute of the JBossTestCase object
146     *
147     * @return The Server value
148     * @throws Exception for any error
149     */

150    public MBeanServerConnection JavaDoc getServer() throws Exception JavaDoc
151    {
152       if (server == null)
153       {
154          String JavaDoc adaptorName = System.getProperty("jbosstest.server.name", "jmx/invoker/RMIAdaptor");
155          server = (MBeanServerConnection JavaDoc)initialContext.lookup(adaptorName);
156       }
157       return server;
158    }
159
160    /**
161     * Gets the Main Deployer Name attribute of the JBossTestCase object
162     *
163     * @return The Main DeployerName value
164     * @exception MalformedObjectNameException Description of Exception
165     */

166    ObjectName JavaDoc getDeployerName() throws MalformedObjectNameException JavaDoc
167    {
168       return new ObjectName JavaDoc(DEPLOYER_NAME);
169    }
170
171    /**
172     * Returns the deployment directory to use. This does it's best to figure out
173     * where you are looking. If you supply a complete url, it returns it.
174     * Otherwise, it looks for jbosstest.deploy.dir or if missing output/lib. Then it
175     * tries to construct a file url or a url.
176     *
177     * @param filename name of the file/url you want
178     * @return A URL
179     * @exception MalformedURLException Description of Exception
180     */

181    protected URL JavaDoc getDeployURL(final String JavaDoc filename)
182       throws MalformedURLException JavaDoc
183    {
184       // First see if it is already a complete url.
185
try
186       {
187          return new URL JavaDoc(filename);
188       }
189       catch (MalformedURLException JavaDoc e)
190       {
191          log.debug(filename + " is not a valid URL, " + e.getMessage());
192       }
193
194       // OK, lets see if we can figure out what it might be.
195
String JavaDoc deployDir = System.getProperty("jbosstest.deploy.dir");
196       if (deployDir == null)
197       {
198          deployDir = "output/lib";
199       }
200       String JavaDoc url = deployDir + "/" + filename;
201       log.debug("Testing file: " + url);
202       // try to canonicalize the strings a bit.
203
File JavaDoc file = new File JavaDoc(url);
204       if (file.exists())
205       {
206          log.debug(file.getAbsolutePath() + " is a valid file");
207          return file.toURL();
208       }
209       else
210       {
211          log.debug("File does not exist, creating url: " + url);
212          return new URL JavaDoc(url);
213       }
214    }
215    
216    /**
217     * invoke wraps an invoke call to the mbean server in a lot of exception
218     * unwrapping.
219     *
220     * @param name ObjectName of the mbean to be called
221     * @param method mbean method to be called
222     * @param args Object[] of arguments for the mbean method.
223     * @param sig String[] of types for the mbean methods parameters.
224     * @return Object returned by mbean method invocation.
225     * @exception Exception Description of Exception
226     */

227    protected Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc method, Object JavaDoc[] args, String JavaDoc[] sig) throws Exception JavaDoc
228    {
229       return invoke(getServer(), name, method, args, sig);
230    }
231
232    protected Object JavaDoc invoke(MBeanServerConnection JavaDoc server, ObjectName JavaDoc name, String JavaDoc method, Object JavaDoc[] args, String JavaDoc[] sig)
233       throws Exception JavaDoc
234    {
235       try
236       {
237          this.getLog().debug("Invoking " + name.getCanonicalName() + " method=" + method);
238          if (args != null)
239             this.getLog().debug("args=" + Arrays.asList(args));
240          return server.invoke(name, method, args, sig);
241       }
242       catch (javax.management.MBeanException JavaDoc e)
243       {
244          log.error("MbeanException", e.getTargetException());
245          throw e.getTargetException();
246       }
247       catch (javax.management.ReflectionException JavaDoc e)
248       {
249          log.error("ReflectionException", e.getTargetException());
250          throw e.getTargetException();
251       }
252       catch (javax.management.RuntimeOperationsException JavaDoc e)
253       {
254          log.error("RuntimeOperationsException", e.getTargetException());
255          throw e.getTargetException();
256       }
257       catch (javax.management.RuntimeMBeanException JavaDoc e)
258       {
259          log.error("RuntimeMbeanException", e.getTargetException());
260          throw e.getTargetException();
261       }
262       catch (javax.management.RuntimeErrorException JavaDoc e)
263       {
264          log.error("RuntimeErrorException", e.getTargetError());
265          throw e.getTargetError();
266       }
267    }
268
269
270    /**
271     * Deploy a package with the main deployer. The supplied name is
272     * interpreted as a url, or as a filename in jbosstest.deploy.lib or output/lib.
273     *
274     * @param name filename/url of package to deploy.
275     * @exception Exception Description of Exception
276     */

277    public void deploy(String JavaDoc name) throws Exception JavaDoc
278    {
279       if (Boolean.getBoolean("jbosstest.nodeploy") == true)
280       {
281          log.debug("Skipping deployment of: " + name);
282          return;
283       }
284
285       URL JavaDoc deployURL = getDeployURL(name);
286       log.debug("Deploying " + name + ", url=" + deployURL);
287       invoke(getDeployerName(),
288          "deploy",
289          new Object JavaDoc[]{deployURL},
290          new String JavaDoc[]{"java.net.URL"});
291    }
292
293    public void redeploy(String JavaDoc name) throws Exception JavaDoc
294    {
295       if (Boolean.getBoolean("jbosstest.nodeploy") == true)
296       {
297          log.debug("Skipping redeployment of: " + name);
298          return;
299       }
300
301       URL JavaDoc deployURL = getDeployURL(name);
302       log.debug("Deploying " + name + ", url=" + deployURL);
303       invoke(getDeployerName(),
304          "redeploy",
305          new Object JavaDoc[]{deployURL},
306          new String JavaDoc[]{"java.net.URL"});
307    }
308
309    /** Do a JAAS login with the current username, password and login config.
310     * @throws Exception
311     */

312    public void login() throws Exception JavaDoc
313    {
314       flushAuthCache("other");
315       String JavaDoc username = getUsername();
316       String JavaDoc pass = getPassword();
317       String JavaDoc config = getLoginConfig();
318       char[] password = null;
319       if (pass != null)
320          password = pass.toCharArray();
321       AppCallbackHandler handler = new AppCallbackHandler(username, password);
322       getLog().debug("Creating LoginContext(" + config + ")");
323       lc = new LoginContext JavaDoc(config, handler);
324       lc.login();
325       getLog().debug("Created LoginContext, subject=" + lc.getSubject());
326    }
327
328    public void logout()
329    {
330       try
331       {
332          getLog().debug("logout, LoginContext: " + lc);
333          if (lc != null)
334             lc.logout();
335       }
336       catch (Exception JavaDoc e)
337       {
338          getLog().error("logout error: ", e);
339       }
340    }
341
342    /**
343     * Undeploy a package with the main deployer. The supplied name is
344     * interpreted as a url, or as a filename in jbosstest.deploy.lib or output/lib.
345     *
346     * @param name filename/url of package to undeploy.
347     * @exception Exception Description of Exception
348     */

349    public void undeploy(String JavaDoc name) throws Exception JavaDoc
350    {
351       if (Boolean.getBoolean("jbosstest.nodeploy") == true)
352          return;
353       URL JavaDoc deployURL = getDeployURL(name);
354       log.debug("Undeploying " + name + ", url=" + deployURL);
355       Object JavaDoc[] args = {deployURL};
356       String JavaDoc[] sig = {"java.net.URL"};
357       invoke(getDeployerName(), "undeploy", args, sig);
358    }
359
360    /** Flush all authentication credentials for the java:/jaas/other security
361     domain
362     */

363    void flushAuthCache(String JavaDoc domain) throws Exception JavaDoc
364    {
365       ObjectName JavaDoc jaasMgr = new ObjectName JavaDoc("jboss.security:service=JaasSecurityManager");
366       Object JavaDoc[] params = {domain};
367       String JavaDoc[] signature = {"java.lang.String"};
368       invoke(jaasMgr, "flushAuthenticationCache", params, signature);
369    }
370
371    void restartDBPool() throws Exception JavaDoc
372    {
373       ObjectName JavaDoc dbPool = new ObjectName JavaDoc("jboss.jca:service=ManagedConnectionPool,name=DefaultDS");
374       Object JavaDoc[] params = {};
375       String JavaDoc[] signature = {};
376       invoke(dbPool, "stop", params, signature);
377       invoke(dbPool, "start", params, signature);
378    }
379
380    boolean isSecure()
381    {
382       return Boolean.getBoolean("jbosstest.secure");
383    }
384
385    String JavaDoc getUsername()
386    {
387       return System.getProperty("jbosstest.username", DEFAULT_USERNAME);
388    }
389
390    String JavaDoc getPassword()
391    {
392       return System.getProperty("jbosstest.password", DEFAULT_PASSWORD);
393    }
394
395    String JavaDoc getLoginConfig()
396    {
397       return System.getProperty("jbosstest.loginconfig", DEFAULT_LOGIN_CONFIG);
398    }
399
400    String JavaDoc getJndiURL()
401    {
402       String JavaDoc url = (String JavaDoc)jndiEnv.get(Context.PROVIDER_URL);
403       return url;
404    }
405
406    String JavaDoc getJndiInitFactory()
407    {
408       String JavaDoc factory = (String JavaDoc)jndiEnv.get(Context.INITIAL_CONTEXT_FACTORY);
409       return factory;
410    }
411
412    int getThreadCount()
413    {
414       int result = Integer.getInteger("jbosstest.threadcount", DEFAULT_THREADCOUNT).intValue();
415       log.debug("jbosstest.threadcount=" + result);
416       return result;
417    }
418
419    int getIterationCount()
420    {
421       int result = Integer.getInteger("jbosstest.iterationcount", DEFAULT_ITERATIONCOUNT).intValue();
422       log.debug("jbosstest.iterationcount=" + result);
423       return result;
424    }
425
426    int getBeanCount()
427    {
428       int result = Integer.getInteger("jbosstest.beancount", DEFAULT_BEANCOUNT).intValue();
429       log.debug("jbosstest.beancount=" + result);
430       return result;
431    }
432
433    /**
434     * Initializes the {@link InitialContext} if not set.
435     *
436     * @throws Exception for any error
437     */

438    public void init() throws Exception JavaDoc
439    {
440       if (initialContext == null)
441       {
442          initialContext = new InitialContext JavaDoc();
443          log.debug("initialContext.getEnvironment()=" + initialContext.getEnvironment());
444          jndiEnv = initialContext.getEnvironment();
445       }
446    }
447    
448    /**
449     * Re-initializes the {@link InitialContext}.
450     *
451     * @throws Exception for any error
452     */

453    public void reinit() throws Exception JavaDoc
454    {
455       initialContext = null;
456       server = null;
457       init();
458    }
459
460    /**
461     * Returns the JBoss server host from system property "jbosstest.server.host"
462     * This defaults to "localhost"
463     *
464     * @return the host name
465     */

466    public String JavaDoc getServerHost()
467    {
468       String JavaDoc hostName = System.getProperty("jbosstest.server.host", "localhost");
469       return hostName;
470    }
471 }
472
Popular Tags