1 package org.xmlBlaster.test.cluster;
  2 
  3 import java.util.logging.Logger;
  4 import org.xmlBlaster.util.Global;
  5 import org.xmlBlaster.util.EmbeddedXmlBlaster;
  6 
  7 // for client connections:
  8 import org.xmlBlaster.util.XmlBlasterException;
  9 import org.xmlBlaster.client.qos.ConnectQos;
 10 import org.xmlBlaster.client.I_Callback;
 11 import org.xmlBlaster.client.I_ConnectionStateListener;
 12 import org.xmlBlaster.client.I_XmlBlasterAccess;
 13 import org.xmlBlaster.util.dispatch.ConnectionStateEnum;
 14 
 15 
 16 import java.io.File;
 17 
 18 /**
 19  * Set up the cluster nodes. 
 20  * <p />
 21  * Don't forget to call tearDown() when you are done.
 22  *
 23  * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/cluster.html" target="others">Cluster requirement</a>
 24  */
 25 public class ServerHelper {
 26    private Global glob_;
 27    private static Logger log = Logger.getLogger(ServerHelper.class.getName());
 28    public static int heronPort = 7600;
 29    public static int avalonPort = 7601;
 30    public static int golanPort = 7602;
 31    public static int frodoPort = 7603;
 32    public static int bilboPort = 7604;
 33 
 34    private EmbeddedXmlBlaster heronThread, avalonThread, golanThread, frodoThread, bilboThread;
 35 
 36    private Global heronGlob, avalonGlob, golanGlob, frodoGlob, bilboGlob;
 37 
 38    public ServerHelper(Global glob, Logger log, String name) {
 39       this.glob_ = glob;
 40       ServerHelper.log = log;
 41       setUp();
 42    }
 43 
 44    /**
 45     * Find the property files, we look in the current directory
 46     * and in ./cluster
 47     * @exception IllegalArgumentException if you are in the wrong directory
 48     */
 49    private String findPropertyFile(String fn) {
 50       File f = new File(fn);
 51       if (f.canRead())
 52          return fn;
 53       f = new File("cluster" + File.separatorChar + fn);
 54       if (f.canRead())
 55          return f.getPath();
 56       log.severe("Can't locate property file " + fn + ". Please check your current directory or cluster directory");
 57       throw new IllegalArgumentException("Can't locate property file " + fn + ". Please check your current directory or cluster directory");
 58    }
 59 
 60    public Global getHeronGlob() {
 61       return heronGlob;
 62    }
 63 
 64    public Global getAvalonGlob() {
 65       return avalonGlob;
 66    }
 67 
 68    public Global getGolanGlob() {
 69       return golanGlob;
 70    }
 71 
 72    public Global getFrodoGlob() {
 73       return frodoGlob;
 74    }
 75 
 76    public Global getBilboGlob() {
 77       return bilboGlob;
 78    }
 79 
 80    public void initHeron() {
 81       String[] args = { "-propertyFile", findPropertyFile("heron.properties"), "-/node/heron/logging", "FINEST" };
 82       heronGlob = this.glob_.getClone(args);
 83    }
 84 
 85    public void initAvalon() {
 86       String[] args = { "-propertyFile", findPropertyFile("avalon.properties") };
 87       avalonGlob = this.glob_.getClone(args);
 88    }
 89 
 90    public void initGolan() {
 91       String[] args = { "-propertyFile", findPropertyFile("golan.properties") };
 92       golanGlob = this.glob_.getClone(args);
 93    }
 94 
 95    public void initFrodo() {
 96       String[] args = { "-propertyFile", findPropertyFile("frodo.properties") };
 97       frodoGlob = this.glob_.getClone(args);
 98    }
 99 
100    public void initBilbo() {
101       String[] args = { "-propertyFile", findPropertyFile("bilbo.properties"), "-/node/bilbo/logging", "INFO" };
102       bilboGlob = this.glob_.getClone(args);
103       if (!"bilbo".equals(bilboGlob.getId())) {
104          String tmp = "Invalid cluster node id, check biblo.properties or" +
105                    " change to the directory where the property files are!";
106          log.severe(tmp);
107          throw new IllegalArgumentException(tmp); // just to be shure
108       }
109    }
110 
111    public void startHeron() {
112       heronThread = EmbeddedXmlBlaster.startXmlBlaster(heronGlob);
113       log.info("'heron' is ready for testing on bootstrapPort " + heronPort);
114    }
115 
116    public void startAvalon() {
117       avalonThread = EmbeddedXmlBlaster.startXmlBlaster(avalonGlob);
118       log.info("'avalon' is ready for testing on bootstrapPort " + avalonPort);
119    }
120 
121    public void startGolan() {
122       golanThread = EmbeddedXmlBlaster.startXmlBlaster(golanGlob);
123       log.info("'golan' is ready for testing on bootstrapPort " + golanPort);
124    }
125 
126    public void startFrodo() {
127       frodoThread = EmbeddedXmlBlaster.startXmlBlaster(frodoGlob);
128       log.info("'frodo' is ready for testing on bootstrapPort " + frodoPort);
129    }
130 
131    public void startBilbo() {
132       bilboThread = EmbeddedXmlBlaster.startXmlBlaster(bilboGlob);
133       log.info("'bilbo' is ready for testing on bootstrapPort " + bilboPort);
134    }
135 
136    public void stopHeron() {
137       if (heronThread != null) { heronThread.stopServer(true); heronThread=null; }
138    }
139 
140    public void stopAvalon() {
141       if (avalonThread != null) { avalonThread.stopServer(true); avalonThread=null; }
142    }
143 
144    public void stopGolan() {
145       if (golanThread != null) { golanThread.stopServer(true); golanThread=null; }
146    }
147 
148    public void stopFrodo() {
149       if (frodoThread != null) { frodoThread.stopServer(true); frodoThread=null; }
150    }
151 
152    public void stopBilbo() {
153       if (bilboThread != null) { bilboThread.stopServer(true); bilboThread=null; }
154    }
155 
156    /** Connect in fail save mode to a server node (as given in glob.getId()) */
157    public I_XmlBlasterAccess connect(final Global glob, I_Callback cb) throws XmlBlasterException {
158       final String clientName = "ClientTo[" + glob.getId() + "]";
159       if (glob.getId() == null || glob.getId().length() < 1) log.severe("glob.getId() is not set");
160       I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
161 
162       con.registerConnectionListener(new I_ConnectionStateListener() {
163             public void reachedAlive(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
164                log.info("Changed from connection state " + oldState +
165                                      " to " + ConnectionStateEnum.ALIVE + " with " +
166                                      connection.getQueue().getNumOfEntries() + " queue entries pending" +
167                                      ": We were lucky, reconnected to " + connection.getGlobal().getId());
168             }
169             public void reachedPolling(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
170                log.warning("DEBUG ONLY: Changed from connection state " + oldState + " to " +
171                                     ConnectionStateEnum.POLLING + ": Lost connection to " + connection.getGlobal().getId());
172             }
173             public void reachedDead(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
174                log.severe("DEBUG ONLY: Changed from connection state " + oldState + " to " +
175                                      ConnectionStateEnum.DEAD + ": Lost connection to " + connection.getGlobal().getId());
176             }
177             public void reachedAliveSync(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
178             }
179 
180          });
181 
182       ConnectQos qos = new ConnectQos(glob, clientName, "secret");
183       con.connect(qos, cb);
184 
185       log.info("Connected to xmlBlaster.");
186       return con;
187    }
188 
189    /**
190     * Initialize the server setup ...
191     * <p />
192     * Is done automatically in constructor
193     */
194    private void setUp() {
195       log.info("Entering setUp(), test starts");
196 
197       // The init is used for server nodes but used for client connections as well
198       initHeron();
199       initAvalon();
200       initGolan();
201       initFrodo();
202       initBilbo();
203 
204       // Starts a cluster node
205       //startHeron();
206       //startAvalon();
207       //startGolan();
208       //startFrodo();
209       //startBilbo();
210       // Do it yourself
211    }
212 
213    /**
214     * Cleaning up ...
215     * <p />
216     * You have to call this when you are done.
217     */
218    public void tearDown() {
219       log.info("Entering tearDown(), test is finished");
220 
221       try { Thread.sleep(200); } catch( InterruptedException i) {} // Wait some time
222 
223       stopHeron();
224       stopAvalon();
225       stopGolan();
226       stopFrodo();
227       stopBilbo();
228    }
229 }


syntax highlighted by Code2HTML, v. 0.9.1