KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > client > ReflectionLauncher


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.client;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.naming.client.java.javaURLContextFactory;
30
31 /**
32  * A AppClientLauncher implementation that simply looks for a static main
33  * method on the
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision:$
37  */

38 public class ReflectionLauncher
39    implements AppClientLauncher
40 {
41    private static Logger log = Logger.getLogger(ReflectionLauncher.class);
42
43    /**
44     * Launch a javaee client application.
45     *
46     * @param clientClass - the class whose main(String[]) will be invoked
47     * @param clientName - the client name that maps to the server side JNDI ENC.
48     * May be null indicating the name should be taken from the client jar
49     * descriptors/annotations.
50     * @param args - the args to pass to main method
51     * @throws Throwable
52     */

53    public void launch(String JavaDoc clientClass, String JavaDoc clientName, String JavaDoc[] args)
54       throws Throwable JavaDoc
55    {
56       try
57       {
58          System.setProperty(javaURLContextFactory.J2EE_CLIENT_NAME_PROP, clientName);
59          // invoke the client class
60
Class JavaDoc cl = Class.forName(clientClass);
61          Method JavaDoc main = cl.getDeclaredMethod("main", new Class JavaDoc[]{String JavaDoc[].class});
62          Object JavaDoc[] mainArgs = {args};
63          if( Modifier.isStatic(main.getModifiers()) )
64          {
65             main.invoke(null, mainArgs);
66          }
67          else
68          {
69             Object JavaDoc client = cl.newInstance();
70             main.invoke(client, mainArgs);
71          }
72          log.debug("Client invoker success.");
73       }
74       catch (InvocationTargetException JavaDoc e)
75       {
76          throw e.getTargetException();
77       }
78    }
79 }
80
Popular Tags