import java.lang.reflect.Proxy; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * CallInterceptionExampleServer - our "AppServer" which deploys some classes, registers them and provides * dumb naming to clients. It will run at most one client based on the server.properties file. */ public class CallInterceptionExampleServer { public static Map beans; //danger (we're not thread safe but thats fine because its just an example) CallInterceptionExampleServer() { CallInterceptionExampleServer.beans = new HashMap(); } /** * takes a keyname, bean name and interface. Creates the proxy and registers it. Unlike the JBoss * deployers, it doesn't create a seperate classloader. It can't undeploy or whatever. */ public void deploy( String name, String beanname, String nterface) throws Exception{ Object bean = Class.forName(beanname).newInstance(); Class clsInterface = Class.forName(nterface); CallInterceptionExampleInvocationHandler ih = new CallInterceptionExampleInvocationHandler(bean); CallInterceptionExampleProxyFactory factory = new CallInterceptionExampleProxyFactory(ih); Object proxy = factory.createProxy(clsInterface); beans.put(name, proxy); } /** * our naming API for clients. Call this static method with the name specified in server.properties */ public static Object lookup( String name) { return beans.get(name); } /** * reads our deployments from server.properties. Gets their "name" (like EJBName or JNDIName), bean class * and interface (like EJB's Remote or Local) and passes them to deploy(s,s,s). Finally we run whatever * is specified by the "run" property. */ public void deployments() throws Exception { InputStream propstream = Thread.currentThread().getContextClassLoader().getResourceAsStream("server.properties"); Properties props = new Properties(); props.load(propstream); propstream.close(); int num = Integer.decode((String)props.get("number")).intValue(); //deployments must say how many String bean = "bean"; String nterface = "interface"; String name = "name"; for (int n = 0; n < num; n++) { String beanprop = bean+n+"."+bean; String interfaceprop = bean+n+"."+nterface; String nameprop = bean+n+"."+name; deploy( (String)props.get(nameprop), (String)props.get(beanprop), (String)props.get(interfaceprop) ); } doRun((String)props.get("run")); } /** * runs our client (its like the servlet API kinda but not really, maybe more like JMX services) */ public void doRun(String clazz) throws Exception { Object obj = Class.forName(clazz).newInstance(); Runnable runObj = (Runnable) obj; runObj.run(); } public static void main(String args[]) { try { CallInterceptionExampleServer svr = new CallInterceptionExampleServer(); svr.deployments(); } catch (Exception e) { e.printStackTrace(); } } }