KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > JBossTestClusteredServices


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test;
23
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30
31 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
32
33 /**
34  * Derived implementation of JBossTestServices for cluster testing.
35  *
36  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 58506 $
39  * @see org.jboss.test.JBossTestServices
40  */

41 public class JBossTestClusteredServices extends JBossTestServices
42 {
43    protected ArrayList JavaDoc adaptors = null;
44    protected ArrayList JavaDoc servers = null;
45    protected ArrayList JavaDoc namingURLs = null;
46    protected ArrayList JavaDoc namingURLsHA = null;
47    protected ArrayList JavaDoc httpURLs = null;
48    
49    // Constructors --------------------------------------------------
50

51    public JBossTestClusteredServices(Class JavaDoc clazz)
52    {
53       super(clazz);
54    }
55    
56    
57
58    @Override JavaDoc
59    public void setUp() throws Exception JavaDoc
60    {
61       super.setUp();
62    }
63
64    @Override JavaDoc
65    public void tearDown() throws Exception JavaDoc
66    {
67       super.tearDown();
68    }
69
70
71
72    int getServerCount()
73    {
74       return servers.size();
75    }
76
77    RMIAdaptor[] getAdaptors() throws Exception JavaDoc
78    {
79 // init();
80
RMIAdaptor[] tmp = new RMIAdaptor[adaptors.size()];
81       adaptors.toArray(tmp);
82       return tmp;
83    }
84    RMIAdaptor getAdaptor(int index) throws Exception JavaDoc
85    {
86 // init();
87
RMIAdaptor adaptor = (RMIAdaptor) adaptors.get(index);
88       return adaptor;
89    }
90
91    String JavaDoc[] getServers() throws Exception JavaDoc
92    {
93 // init();
94
String JavaDoc[] tmp = new String JavaDoc[servers.size()];
95       servers.toArray(tmp);
96       return tmp;
97    }
98    String JavaDoc getServer(int index) throws Exception JavaDoc
99    {
100 // init();
101
String JavaDoc server = (String JavaDoc) servers.get(index);
102       return server;
103    }
104
105    /** Get the JNDI provider urls for the cluster nodes
106     * @return
107     * @throws Exception
108     */

109    String JavaDoc[] getNamingURLs() throws Exception JavaDoc
110    {
111 // init();
112
String JavaDoc[] tmp = new String JavaDoc[namingURLs.size()];
113       namingURLs.toArray(tmp);
114       return tmp;
115    }
116    String JavaDoc getNamingURL(int index) throws Exception JavaDoc
117    {
118 // init();
119
String JavaDoc server = (String JavaDoc) namingURLs.get(index);
120       return server;
121    }
122
123    /** Get the JNDI provider urls for the cluster nodes
124     * @return
125     * @throws Exception
126     */

127    String JavaDoc[] getHANamingURLs() throws Exception JavaDoc
128    {
129 // init();
130
String JavaDoc[] tmp = new String JavaDoc[namingURLsHA.size()];
131       namingURLsHA.toArray(tmp);
132       return tmp;
133    }
134    String JavaDoc getHANamingURL(int index) throws Exception JavaDoc
135    {
136 // init();
137
String JavaDoc server = (String JavaDoc) namingURLsHA.get(index);
138       return server;
139    }
140
141    /** Get the default web container urls for the cluster nodes
142     * @return
143     * @throws Exception
144     */

145    String JavaDoc[] getHttpURLs() throws Exception JavaDoc
146    {
147 // init();
148
String JavaDoc[] tmp = new String JavaDoc[httpURLs.size()];
149       httpURLs.toArray(tmp);
150       return tmp;
151    }
152    String JavaDoc getHttpURL(int index) throws Exception JavaDoc
153    {
154 // init();
155
String JavaDoc server = (String JavaDoc) httpURLs.get(index);
156       return server;
157    }
158
159    /**
160     * Deploy a package on the given server with the main deployer. The supplied
161     * name is interpreted as a url, or as a filename in jbosstest.deploy.lib or
162     * ../lib.
163     *
164     * @param server server on which the package should be deployed
165     * @param name filename/url of package to deploy.
166     * @exception Exception Description of Exception
167     */

168    public void deploy(RMIAdaptor server, String JavaDoc name) throws Exception JavaDoc
169    {
170       if (Boolean.getBoolean("jbosstest.nodeploy") == true)
171       {
172          log.debug("Skipping deployment of: " + name);
173          return;
174       }
175
176       URL JavaDoc deployURL = getDeployURL(name);
177       log.debug("Deploying " + name + ", url=" + deployURL + " to " + server);
178       invoke(server,
179             getDeployerName(),
180             "deploy",
181             new Object JavaDoc[]{deployURL},
182             new String JavaDoc[]{"java.net.URL"});
183    }
184    
185    public void redeploy(RMIAdaptor server, String JavaDoc name) throws Exception JavaDoc
186    {
187       if (Boolean.getBoolean("jbosstest.nodeploy") == true)
188       {
189          log.debug("Skipping redeployment of: " + name);
190          return;
191       }
192
193       URL JavaDoc deployURL = getDeployURL(name);
194       log.debug("Deploying " + name + ", url=" + deployURL);
195       invoke(server,
196          getDeployerName(),
197          "redeploy",
198          new Object JavaDoc[]{deployURL},
199          new String JavaDoc[]{"java.net.URL"});
200    }
201
202    /**
203     * Undeploy a package from the given server with the main deployer.
204     * The supplied name is interpreted as a url, or as a filename in
205     * jbosstest.deploy.lib or ../lib.
206     *
207     * @param server server on which the package should be deployed
208     * @param name filename/url of package to undeploy.
209     * @exception Exception Description of Exception
210     */

211    public void undeploy(RMIAdaptor server, String JavaDoc name) throws Exception JavaDoc
212    {
213       if (Boolean.getBoolean("jbosstest.nodeploy") == true)
214          return;
215       URL JavaDoc deployURL = getDeployURL(name);
216       log.debug("Undeploying " + name + ", url=" + deployURL);
217       Object JavaDoc[] args = {deployURL};
218       String JavaDoc[] sig = {"java.net.URL"};
219       invoke(server, getDeployerName(), "undeploy", args, sig);
220    }
221
222    /**
223     * Override to invoke the operation on all servers
224     *
225     * @param name
226     * @param method
227     * @param args
228     * @param sig
229     * @return
230     * @throws Exception
231     */

232    protected Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc method, Object JavaDoc[] args,
233       String JavaDoc[] sig)
234       throws Exception JavaDoc
235    {
236       RMIAdaptor[] adaptors = getAdaptors();
237
238       Object JavaDoc result = null;
239       for (int i = 0; i < adaptors.length; i++)
240       {
241          RMIAdaptor adaptor = adaptors[i];
242          log.debug("Using RMIAdaptor: "+adaptor);
243          result = invoke(adaptor, name, method, args, sig);
244       }
245
246       return result;
247
248    }
249
250    public void init() throws Exception JavaDoc
251    {
252       super.init();
253       
254       if (initialContext == null)
255       {
256          initialContext = new InitialContext JavaDoc();
257       }
258       if (adaptors == null)
259       {
260          adaptors = new ArrayList JavaDoc();
261          servers = new ArrayList JavaDoc();
262          namingURLs = new ArrayList JavaDoc();
263          namingURLsHA = new ArrayList JavaDoc();
264          httpURLs = new ArrayList JavaDoc();
265          String JavaDoc adaptorName = System.getProperty("jbosstest.server.name");
266          if (adaptorName == null)
267             adaptorName = "jmx/invoker/RMIAdaptor";
268
269          Hashtable JavaDoc env = new Hashtable JavaDoc();
270          env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
271          env.put("java.naming.factory.url.pkgs", "org.jnp.interfaces");
272
273          // Look for jbosstest.cluster.nodeN properties for the server names
274
String JavaDoc node = "jbosstest.cluster.node";
275          int count = 0;
276          while (count < 10)
277          {
278             String JavaDoc prop = node + count;
279             String JavaDoc host = System.getProperty(prop);
280             count++;
281             if (host == null)
282                break;
283             log.info(prop + " = " + host);
284             servers.add(host);
285             // See if there is a jbosstest.cluster.nodeN.jndi.url
286
String JavaDoc urlProp = prop + ".jndi.url";
287             String JavaDoc urlDefault = "jnp://" + host + ":1099";
288             String JavaDoc urlValue = System.getProperty(urlProp, urlDefault);
289             log.debug("JNDI Url for node=" + count + " is:" + urlValue);
290             namingURLs.add(urlValue);
291             env.put("java.naming.provider.url", urlValue);
292             // Lookup the adaptor
293
InitialContext JavaDoc tmpCtx = new InitialContext JavaDoc(env);
294             RMIAdaptor adaptor = (RMIAdaptor) tmpCtx.lookup(adaptorName);
295             adaptors.add(adaptor);
296
297             // See if there is a jbosstest.cluster.nodeN.hajndi.url
298
urlProp = prop + ".hajndi.url";
299             urlDefault = "jnp://" + host + ":1100";
300             urlValue = System.getProperty(urlProp, urlDefault);
301             log.debug("HA-JNDI Url for node=" + count + " is:" + urlValue);
302             namingURLsHA.add(urlValue);
303             
304             // See if there is a jbosstest.cluster.nodeN.http.url
305
urlProp = prop + ".http.url";
306             urlDefault = "http://" + host + ":8080";
307             urlValue = System.getProperty(urlProp, urlDefault);
308             log.debug("Http Url for node=" + count + " is:" + urlValue);
309             httpURLs.add(urlValue);
310          }
311
312          if (adaptors.size() == 0)
313             throw new IllegalStateException JavaDoc("No jbosstest.cluster.node values found");
314       }
315    }
316
317    /**
318     * This method gives overriding testcases to set the cluster servernames
319     */

320    public void setServerNames(String JavaDoc[] snames)
321    {
322       if (snames == null) return;
323       for (int i = 0; i < snames.length; i++)
324       {
325          servers.add(snames[i]);
326       }
327    }
328
329 }
330
Popular Tags