1 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 ; 28 import javax.naming.InitialContext ; 29 import javax.naming.NamingException ; 30 import javax.rmi.PortableRemoteObject ; 31 import java.io.InputStream ; 32 import java.util.Properties ; 33 34 public class EJBTestClient 35 { 36 37 public String SIMPLE_CONFIG_SERVER_PROP = "services.properties"; 38 CallerSessionHome callerHome; 39 CallerSession callerBean; 40 41 public static void main(String 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 ex) 56 { 57 ex.printStackTrace(); 58 } 59 } 60 61 public String execute(String obj) throws Exception 62 { 63 String res = null; 64 String xmlString = (String ) obj; 65 try 66 { 67 init(obj); 68 res = callerBean.processRequest(xmlString); 69 callerBean.remove(); 70 } 71 catch (Exception ex) 72 { 73 res = ex.getMessage(); 74 throw new Exception (ex.getMessage()); 75 } 76 return res; 77 } 78 79 public void init(String obj) throws Exception 80 { 81 try 82 { 83 callerHome = (CallerSessionHome) 84 lookup("CallerSession", CallerSessionHome.class); 85 callerBean = callerHome.create(); 86 } 87 catch (CreateException ex) 88 { 89 throw new Exception (ex.getMessage()); 90 } 91 } 92 93 public Properties getPropAsResource(String name) throws Exception 94 { 95 InputStream is = getClass().getResourceAsStream("/META-INF/" + name); 96 if (is == null) 97 { 98 throw new Exception ("Unable to locate resource: " + SIMPLE_CONFIG_SERVER_PROP); 99 } 100 Properties confProp = new Properties (); 101 confProp.load(is); 102 return confProp; 103 } 104 105 public Object lookup(String name, Class className) throws Exception 106 { 107 Object retVal = null; 108 try 109 { 110 InitialContext jndiContext = new InitialContext (getPropAsResource(SIMPLE_CONFIG_SERVER_PROP)); 111 Object ref = jndiContext.lookup(name); 112 retVal = PortableRemoteObject.narrow(ref, className); 113 } 114 catch (NamingException ex) 115 { 116 ex.printStackTrace(); 117 throw new Exception ("Object is not Bound in the Context : " + name); 118 } 119 return retVal; 120 } 121 122 123 } 124 | Popular Tags |