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