| D:\DEV\PROJECTS\Harbor_Tests\src\harbor\test\testControllingRemoteObjects.java |
/* * testControllingRemoteObjects.java */ package harbor.test; import harbor.interfaces.I_ParamHolder; import harbor.interfaces.I_ParamHolderUser; import kewlstuff.harbor.client.Vessel; /** * * @author Johnny Kewl * TEST 3 * This test LOADS a Holder class AT the REMOTE server and adds some data to it * Then it LOADS a HolderUser class AT the REMOTE server * Then it adds (Injects) the Holder class into the HolderUser class * Then it reads back the data from the HolderUser class * */ public class testControllingRemoteObjects { private Vessel vessel = null; /** Creates a new instance of testControllingRemoteObjects */ public testControllingRemoteObjects() { String harborUrl = "http://localhost:8080/harbor/service"; //location of container harbor vessel = new Vessel(harborUrl); //get a vessel from the harbor } public void runTest(){ //Get holder class I_ParamHolder i_ParamHolder = (I_ParamHolder)vessel.loadRemoteClassInst(I_ParamHolder.class,"harbor.classContainer.ParamHolder");//load a class instance ON the harbor if(i_ParamHolder == null){ System.out.print("Class does not exist or connection problems\n"); return; } //Set a param on the holder class i_ParamHolder.setData("Message from a 100 miles away..."); //Read the message back to see if it set System.out.print("From ParamHolder: " + i_ParamHolder.getData() + "\n"); //Get holder user class I_ParamHolderUser i_ParamHolderUser = (I_ParamHolderUser)vessel.loadRemoteClassInst(I_ParamHolderUser.class,"harbor.classContainer.ParamHolderUser");//load a class instance ON the harbor if(i_ParamHolderUser == null){ System.out.print("Class does not exist or connection problems\n"); return; } //Injecting the holder class into the User class; i_ParamHolderUser.setDataParamHolder(i_ParamHolder); //Reading back from USER class System.out.print("From ParamHolderUser: " + i_ParamHolderUser.getData() + "\n"); //Clean up explictly vessel.releaseRemoteClassInst(i_ParamHolder); vessel.releaseRemoteClassInst(i_ParamHolderUser); } } /* Notes: * As of REV3... the test code running in the harbor, is conveniently packaged * in the HARBOR_TESTS_LIB * So if you want to mess around with the code... change it, compile it, and dump the resulting * JAR in the HarborRepository folder, and test again, its now very easy. * * This is what the code in the harbor for this test, looks like * public class ParamHolder implements I_ParamHolder{ private String data = null; public void setData(String data){ this.data = data; } public String getData(){ return data; } } * .... is what we loaded first and... * public class ParamHolderUser implements I_ParamHolderUser{ private I_ParamHolder paramHolder = null; public void setDataParamHolder(I_ParamHolder paramHolder){ this.paramHolder = paramHolder; } public String getData(){ if(paramHolder == null) return "Please gimme a paramHolder"; return paramHolder.getData(); } } * .... we loaded second and passed the first one into it * * This simple concept, refered to in Harbor as INJECTS, which really amounts to * nothing more than passing one class into another, goes by the creative name of * Inversion Of Control (IOC). * * When you loadRemoteClassInst, and start using it on the client, the real class is not * really on the client, it just feels like it to client software, its really all running * on the server. When you inject one object into another, or another class runs methods on * your loaded class... its all really happening at the server. * * Hope my extensive waffling gets you to see it... the code feels like, and is normal POJO, * but underneath, Harbor is working a small miracle for you... so that you dont have to change * your POJO ways. */