KickJava   Java API By Example, From Geeks To Geeks.

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


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