KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > harness > TestClient


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.harness;
23
24 import com.cluster.simple.sessionbeans.SimpleSession;
25 import com.cluster.simple.sessionbeans.SimpleSessionHome;
26
27 import javax.ejb.CreateException JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import javax.rmi.PortableRemoteObject JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:tom@jboss.org">Tom Elrod</a>
36  * @version $Revision: 37406 $
37  */

38 public class TestClient
39 {
40    
41    public static String JavaDoc SIMPLE_CONFIG_SERVER_PROP = "services.properties";
42    SimpleSessionHome simHome;
43    SimpleSession simBean;
44
45    public static void main(String JavaDoc args[])
46    {
47       try
48       {
49          TestClient client = new TestClient();
50          if (args.length > 0)
51          {
52             System.out.println(client.execute(args[0]));
53          }
54          else
55          {
56             System.out.println(client.execute(""));
57          }
58       }
59       catch (Exception JavaDoc ex)
60       {
61          ex.printStackTrace();
62       }
63    }
64
65    public String JavaDoc execute(String JavaDoc obj) throws Exception JavaDoc
66    {
67       String JavaDoc res = null;
68       String JavaDoc xmlString = (String JavaDoc) obj;
69       try
70       {
71          init(obj);
72          res = simBean.processRequest(xmlString);
73       }
74       catch (Exception JavaDoc ex)
75       {
76          res = ex.getMessage();
77          throw new Exception JavaDoc(ex.getMessage());
78       }
79       return res;
80    }
81
82    public void init(String JavaDoc obj) throws Exception JavaDoc
83    {
84       try
85       {
86          simHome = (SimpleSessionHome)
87                lookup(obj + "SimpleSession", SimpleSessionHome.class);
88          simBean = simHome.create();
89       }
90       catch (CreateException JavaDoc ex)
91       {
92          throw new Exception JavaDoc(ex.getMessage());
93       }
94    }
95
96    public Properties JavaDoc getPropAsResource(String JavaDoc name) throws Exception JavaDoc
97    {
98       InputStream JavaDoc is = getClass().getResourceAsStream("/META-INF/" + name);
99       if (is == null)
100       {
101          throw new Exception JavaDoc("Unable to locate resource: " + name);
102       }
103       Properties JavaDoc confProp = new Properties JavaDoc();
104       confProp.load(is);
105       return confProp;
106    }
107
108    public Object JavaDoc lookup(String JavaDoc name, Class JavaDoc className) throws Exception JavaDoc
109    {
110       Object JavaDoc retVal = null;
111       try
112       {
113          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc(getPropAsResource(SIMPLE_CONFIG_SERVER_PROP));
114          Object JavaDoc ref = jndiContext.lookup(name);
115          retVal = PortableRemoteObject.narrow(ref, className);
116       }
117       catch (NamingException JavaDoc ex)
118       {
119          ex.printStackTrace();
120          throw new Exception JavaDoc("Object is not Bound in the Context : " + name);
121       }
122       return retVal;
123    }
124
125
126 }
127
Popular Tags