1 /*------------------------------------------------------------------------------
  2 Name:      Util.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Some helper methods for test clients
  6 ------------------------------------------------------------------------------*/
  7 package org.xmlBlaster.test;
  8 
  9 import org.xmlBlaster.util.Global;
 10 import org.xmlBlaster.util.def.Constants;
 11 
 12 import java.util.Vector;
 13 
 14 import org.xmlBlaster.client.I_XmlBlasterAccess;
 15 import org.xmlBlaster.client.qos.ConnectQos;
 16 import org.xmlBlaster.client.key.GetKey;
 17 import org.xmlBlaster.client.qos.GetQos;
 18 import org.xmlBlaster.util.MsgUnit;
 19 import org.xmlBlaster.util.XmlBlasterException;
 20 
 21 /**
 22  * Some helper methods for test clients
 23  */
 24 public class Util
 25 {
 26 
 27    /**
 28     * If you want to start a second xmlBlaster instances
 29     * set environment that the ports don't conflict
 30     * @return A cloned Global which is configured with different serverPort
 31     */
 32    public static Global getOtherServerPorts(Global orig, int serverPort) {
 33       return orig.getClone(getOtherServerPorts(serverPort));
 34    }
 35 
 36    /**
 37     * If you want to start a second xmlBlaster instances
 38     * set environment that the ports don't conflict
 39     */
 40    public static String[] getOtherServerPorts(int serverPort) {
 41       Vector vec = getOtherServerPortVec(serverPort);
 42       return (String[])vec.toArray(new String[0]);
 43    }
 44 
 45    /**
 46     * If you want to start a second xmlBlaster instances
 47     * set environment that the ports don't conflict
 48     * @param others Other params to add
 49     */
 50    public static String[] getOtherServerPorts(int serverPort, String[] others) {
 51       Vector vec = getOtherServerPortVec(serverPort);
 52       for (int i=0; i<others.length; i++)
 53          vec.add(others[i]);
 54       return (String[])vec.toArray(new String[0]);
 55    }
 56 
 57    /**
 58     * If you want to start a second xmlBlaster instances
 59     * set environment that the ports don't conflict
 60     */
 61    public static Vector getOtherServerPortVec(int serverPort) {
 62       // For all protocol we may use set an alternate server port
 63       Vector vec = new Vector();
 64       vec.addElement("-bootstrapPort");
 65       vec.addElement(""+serverPort);
 66       vec.addElement("-plugin/socket/port");
 67       vec.addElement(""+(serverPort-1));
 68       vec.addElement("-plugin/rmi/registryPort");
 69       vec.addElement(""+(serverPort-2));
 70       vec.addElement("-plugin/xmlrpc/port");
 71       vec.addElement(""+(serverPort-3));
 72       vec.addElement("-admin.remoteconsole.port");  // -admin.remoteconsole.port 0 : switch off telnet
 73       vec.addElement(""+0);
 74       //vec.addElement(""+(serverPort-4));
 75       return vec;
 76    }
 77 
 78    /**
 79     * Reset the server ports to default, that a client in this JVM finds the server
 80     */
 81    public static String[] getDefaultServerPorts()
 82    {
 83       /* We can't use stronger names, as a call sequence of
 84        * getOtherServerPortVec() -> getDefaultServerPorts() -> getOtherServerPortVec()
 85        * would fail.
 86       String[] argsDefault = {
 87          "-bootstrapPort",
 88          "" + Constants.XMLBLASTER_PORT,
 89          "-dispatch/connection/plugin/socket/port",
 90          "" + org.xmlBlaster.util.protocol.socket.SocketUrl.DEFAULT_SERVER_PORT,
 91          "-dispatch/connection/plugin/rmi/registryPort",
 92          "" + org.xmlBlaster.protocol.rmi.RmiDriver.DEFAULT_REGISTRY_PORT,
 93          "-dispatch/connection/plugin/xmlrpc/port",
 94          "" + org.xmlBlaster.protocol.xmlrpc.XmlRpcDriver.DEFAULT_HTTP_PORT,
 95          "-admin.remoteconsole.port",
 96          "" + org.xmlBlaster.engine.admin.extern.TelnetGateway.TELNET_PORT
 97          };
 98       */
 99       String[] argsDefault = {
100             "-bootstrapPort",
101             "" + Constants.XMLBLASTER_PORT,
102             "-plugin/socket/port",
103             "" + org.xmlBlaster.util.protocol.socket.SocketUrl.DEFAULT_SERVER_PORT,
104             "-plugin/rmi/registryPort",
105             "" + org.xmlBlaster.protocol.rmi.RmiDriver.DEFAULT_REGISTRY_PORT,
106             "-plugin/xmlrpc/port",
107             "" + org.xmlBlaster.protocol.xmlrpc.XmlRpcDriver.DEFAULT_HTTP_PORT,
108             "-admin.remoteconsole.port",
109             "" + org.xmlBlaster.engine.admin.extern.TelnetGateway.TELNET_PORT
110             };
111       return argsDefault;
112    }
113 
114    public static void resetPorts()
115    {
116       resetPorts(Global.instance());
117    }
118 
119    public static void resetPorts(Global glob)
120    {
121       glob.init(getDefaultServerPorts()); // Restes bootstrap address which this call doesn't: glob.getProperty().addArgs2Props(getDefaultServerPorts());
122       glob.shutdownHttpServer();
123    }
124 
125    /**
126     * Stop execution for some given milliseconds
127     * @param millis amount of milliseconds to wait
128     */
129    public static void delay(long millis)
130    {
131       try {
132           Thread.sleep(millis);
133       }
134       catch( InterruptedException i)
135       {}
136    }
137 
138 
139    /**
140     * Stop execution until a key is hit
141     * @param text This text is shown on command line
142     */
143    public static void ask(String text)
144    {
145       System.out.println("################### " + text + ": Hit a key to continue ###################");
146       try {
147          System.in.read();
148       } catch (java.io.IOException e) {}
149    }
150 
151 
152    /**
153     * Do some garbage collect attempts
154     */
155    public static void gc(int numGc) {
156       for (int ii=0; ii<numGc; ii++) {
157          System.gc();
158          try { Thread.sleep(100L); } catch( InterruptedException i) {}
159       }
160    }
161 
162    /**
163     * Do an administrative command to the server with a temporaty login session.
164     * @param command "__sys__UserList" or "__cmd:/node/heron/?clientList"
165     */
166    public static MsgUnit[] adminGet(Global glob, String command) throws XmlBlasterException {
167       I_XmlBlasterAccess connAdmin = null;
168       String user="ADMIN/1";
169       String passwd="secret";
170       try {
171          Global gAdmin = glob.getClone(null);
172          connAdmin = gAdmin.getXmlBlasterAccess();
173          connAdmin.connect(new ConnectQos(gAdmin, user, passwd), null);
174          GetKey gk = new GetKey(glob, command);
175          GetQos gq = new GetQos(glob);
176          MsgUnit[] msgs = connAdmin.get(gk, gq);
177          return msgs;
178       }
179       finally {
180          if (connAdmin != null) { try { connAdmin.disconnect(null); } catch (Throwable et) {} }
181       }
182    }
183 }


syntax highlighted by Code2HTML, v. 0.9.1