Source code: org/mobicents/slee/container/management/jmx/SleeCommandInterface.java
1 /***************************************************
2 * *
3 * Mobicents: The Open Source JSLEE Platform *
4 * *
5 * Distributable under LGPL license. *
6 * See terms of license at gnu.org. *
7 * *
8 ***************************************************/
9 /*
10 * SleeCommandInterface.java
11 *
12 * This class provides a command line interface to the JAIN
13 * SLEE container. The following commands are supported:
14 * java SleeCommandInterface -startSlee
15 * java SleeCommandInterface -stopSlee
16 * java SleeCommandInterface -deploy <url>
17 * java SleeCommandInterface -undeploy <Deployment ID>
18 * java SleeCommandInterface -getDescriptor <Deployment ID>
19 * java SleeCommandInterface -getDeploymentId <file url>
20 * java SleeCommandInterface -activateService <Service ID>
21 * java SleeCommandInterface -deactivateService <Service ID>
22 * java SleeCommandInterface -getServiceState <Service ID>
23 * java SleeCommandInterface -setTraceLevel <Service ID> <level>");
24 * java SleeCommandInterface -getTraceLevel <Service ID>");
25 * java SleeCommandInterface -createRaEntity <ResourceAdaptor ID> <entity name> <props>");
26 * java SleeCommandInterface -activateRaEntity <entity name>");
27 * java SleeCommandInterface -deactivateRaEntity <entity name>");
28 * java SleeCommandInterface -removeRaEntity <entity name>");
29 * java SleeCommandInterface -createEntityLink <link name> <entity name>");
30 * java SleeCommandInterface -removeEntityLink <link name> <entity name>");
31 *
32 *
33 * @author Buddy Bright
34 *
35 */
36
37 package org.mobicents.slee.container.management.jmx;
38
39
40 import javax.naming.*;
41 import javax.management.*;
42 import java.util.*;
43
44 import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
45
46
47 import org.mobicents.slee.container.management.DeployableUnitIDImpl;
48 import org.mobicents.slee.container.management.ServiceIDImpl;
49 import org.mobicents.slee.container.management.ResourceAdaptorIDImpl;
50 import org.mobicents.slee.container.management.ComponentIDImpl;
51 import org.mobicents.slee.container.management.ComponentKey;
52
53
54
55 public class SleeCommandInterface
56 {
57
58 protected RMIAdaptor rmiserver = null;
59 protected static String commandBean=null;
60
61
62 /**
63 * Constructor
64 */
65 public SleeCommandInterface()
66 {
67 }
68
69 /**
70 * Constructor that takes a JNDI url
71 * @param jndiurl JNDI Url (jnp://localhost:1099)
72 */
73 public SleeCommandInterface( String jndiurl ) throws Exception {
74 this();
75
76 //Set Some JNDI Properties
77 Hashtable env = new Hashtable();
78 env.put( Context.PROVIDER_URL, jndiurl );
79 env.put( Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
80 env.put( Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
81
82 InitialContext ctx = new InitialContext(env);
83 rmiserver = (RMIAdaptor) ctx.lookup("jmx/rmi/RMIAdaptor");
84 if( rmiserver == null ) System.out.println( "RMIAdaptor is null");
85 }
86
87 /**
88 * Get the Metadata for the MBean
89 * @param oname ObjectName of the MBean
90 * @return MBeanInfo about the MBean
91 */
92 public MBeanInfo getMBeanInfo( ObjectName oname ) throws Exception {
93 /* Example:
94 //Get the MBeanInfo for the Tomcat MBean
95 ObjectName name = new ObjectName( "jboss.web:service=WebServer" );
96 */
97 MBeanInfo info = null;
98 info = rmiserver.getMBeanInfo( oname );
99 return info;
100 }
101
102 /**
103 *
104 * Wrapper script to invoke a SLEE command
105 */
106 public void invokeCommand(ObjectName name,
107 String commandString,
108 Object[] opArgs,
109 String[] sigs )
110 {
111 try
112 {
113
114 // Invoke the operation
115 Object result = invokeOperation(name,
116 commandString, opArgs, sigs);
117 if (result == null)
118 {
119 System.out.println("No response");
120 }
121 else
122 {
123 System.out.println(result);
124 }
125 System.exit(0);
126
127 }
128 catch (Exception ex)
129 {
130 System.err.println(ex);
131 System.out.println("Bad result: " + commandBean + "." + commandString);
132 System.exit(-1);
133 }
134 }
135
136
137 /**
138 * Invoke an Operation on the MBean
139 * @param oname ObjectName of the MBean
140 * @param methodname Name of the operation on the MBean
141 * @param pParams Arguments to the operation
142 * @param pSignature Signature for the operation.
143 * @return result from the MBean operation
144 * @throws Exception
145 */
146 public Object invokeOperation( ObjectName oname,
147 String methodname,Object[] pParams,
148 String[] pSignature )
149 throws Exception {
150 Object result = null;
151 /* Example:
152 //Stop the Tomcat Instance
153 Object result = server.invoke(name, "stop",null,null);
154 */
155
156 result = rmiserver.invoke(oname, methodname ,pParams,pSignature);
157 return result;
158 }
159
160 public static void usage()
161 {
162 System.out.println("Usage: java SleeCommandInterface -<command> <args>");
163 System.out.println("Valid commands:");
164 System.out.println("-startSlee");
165 System.out.println("-stopSlee");
166 System.out.println("-deploy <url>");
167 System.out.println("-undeploy <Deployment ID>");
168 System.out.println("-getDescriptor <Deployment ID>");
169 System.out.println("-getDeploymentId <file url>");
170 System.out.println("-activateService <Service ID>");
171 System.out.println("-deactivateService <Service ID>");
172 System.out.println("-getServiceState <Service ID>");
173 System.out.println("-setTraceLevel <Service ID> <level>");
174 System.out.println("-getTraceLevel <Service ID>");
175 System.out.println("-createRaEntity <ResourceAdaptor ID> <entity name> <props>");
176 System.out.println("-activateRaEntity <entity name>");
177 System.out.println("-deactivateRaEntity <entity name>");
178 System.out.println("-removeRaEntity <entity name>");
179 System.out.println("-createEntityLink <link name> <entity name>");
180 System.out.println("-removeEntityLink <link name> <entity name>");
181 //System.out.println("-getServices <Service State, (active, inactive)> ");
182 }
183
184
185 /**
186 * @param args the command line arguments
187 */
188 public static void main(String[] args)
189 throws Exception
190 {
191
192 ObjectName name=null;
193 String commandString=null;
194 String sig=null;
195 String sig2=null;
196 String sig3=null;
197 String data=null ;
198 String data2=null ;
199 String data3=null ;
200 String command;
201
202 Object opArg = null;
203 Object opArg2 = null;
204 Object opArg3 = null;
205
206 if( args.length < 1 )
207 {
208 usage();
209 System.exit(1);
210 }
211
212 command = args[0];
213 if( args.length >= 2 )
214 data = args[1];
215 if( args.length >= 3 )
216 data2 = args[2];
217 if( args.length >= 4 )
218 data3 = args[3];
219
220 //Start SLEE
221 if (command.equals("-startSlee") )
222 {
223 commandBean = "slee:service=SleeManagement";
224 name = new ObjectName(commandBean);
225 commandString = "start";
226
227 }
228 //Stop SLEE
229 else if (command.equals("-stopSlee") )
230 {
231 commandBean = "slee:service=SleeManagement";
232 name = new ObjectName(commandBean);
233 commandString = "stop";
234
235 }
236 //Deployment management
237 else if (command.equals("-deploy") )
238 {
239 commandBean = "slee:name=DeploymentMBean";
240 name = new ObjectName(commandBean);
241 commandString = "install";
242 sig = "java.lang.String";
243 opArg = data;
244
245 }
246 //Deployment management
247 else if (command.equals("-undeploy") )
248 {
249 commandBean = "slee:name=DeploymentMBean";
250 name = new ObjectName(commandBean);
251 commandString = "uninstall";
252 sig = "javax.slee.management.DeployableUnitID";
253
254 int id=0;
255 try {
256 StringTokenizer st = new StringTokenizer(data, "[", true);
257 String token = st.nextToken();
258 if (!token.equalsIgnoreCase("DeployableUnitID"))
259 {
260 System.out.print("Bad result: DeployableUnitID[number]\n");
261 System.exit(-1);
262 }
263 st.nextToken();
264 String idStr = st.nextToken("]");
265 id = Integer.parseInt(idStr);
266 } catch (NoSuchElementException ex) {
267 System.out.print("Bad result: DeployableUnitID[number]\n");
268 System.exit(-1);
269 } catch (NumberFormatException ex) {
270 System.out.print("Bad result: DeployableUnitID[number]\n");
271 System.exit(-1);
272 }
273
274 opArg = new DeployableUnitIDImpl(id);
275 }
276 //Deployment management
277 else if (command.equals("-getDeploymentId") )
278 {
279 commandBean = "slee:name=DeploymentMBean";
280 name = new ObjectName(commandBean);
281 commandString = "getDeployableUnit";
282 sig = "java.lang.String";
283 opArg = data;
284 }
285 //Deployment management
286 else if (command.equals("-getDescriptor") )
287 {
288 commandBean = "slee:name=DeploymentMBean";
289 name = new ObjectName(commandBean);
290 commandString = "getDescriptor";
291 sig = "javax.slee.management.DeployableUnitID";
292
293 int id=0;
294 try {
295 StringTokenizer st = new StringTokenizer(data, "[", true);
296 String token = st.nextToken();
297 if (!token.equalsIgnoreCase("DeployableUnitID"))
298 {
299 System.out.print("Bad result: DeployableUnitID[number]\n");
300 System.exit(-1);
301 }
302 st.nextToken();
303 String idStr = st.nextToken("]");
304 id = Integer.parseInt(idStr);
305 } catch (NoSuchElementException ex) {
306 System.out.print("Bad result: DeployableUnitID[number]\n");
307 System.exit(-1);
308 } catch (NumberFormatException ex) {
309 System.out.print("Bad result: DeployableUnitID[number]\n");
310 System.exit(-1);
311 }
312
313 opArg = new DeployableUnitIDImpl(id);
314 }
315 //service management
316 else if (command.equals("-activateService") )
317 {
318 commandBean = "slee:name=ServiceManagementMBean";
319 name = new ObjectName(commandBean);
320 commandString = "activate";
321 sig = "javax.slee.ServiceID";
322 try {
323 StringTokenizer stringTokenizer = new StringTokenizer(data, "[", true);
324 String componentType = stringTokenizer.nextToken();
325 stringTokenizer.nextToken();
326 String ckeyStr = stringTokenizer.nextToken("]");
327 if (componentType.equalsIgnoreCase(ComponentIDImpl.SERVICE_ID)) {
328 ComponentKey componentKey = new ComponentKey(ckeyStr);
329 opArg = new ServiceIDImpl(componentKey);
330 } else
331 {
332 System.out.println("Bad Result: ServiceID[service]\n");
333 System.exit(-1);
334 }
335 } catch (NoSuchElementException ex) {
336 System.out.println("Bad Result: ServiceID[service]\n");
337 System.exit(-1);
338 }
339
340 }
341 //service management
342 else if (command.equals("-deactivateService") )
343 {
344 commandBean = "slee:name=ServiceManagementMBean";
345 name = new ObjectName(commandBean);
346 commandString = "deactivate";
347 sig = "javax.slee.ServiceID";
348 try {
349 StringTokenizer stringTokenizer = new StringTokenizer(data, "[", true);
350 String componentType = stringTokenizer.nextToken();
351 stringTokenizer.nextToken();
352 String ckeyStr = stringTokenizer.nextToken("]");
353 if (componentType.equalsIgnoreCase(ComponentIDImpl.SERVICE_ID)) {
354 ComponentKey componentKey = new ComponentKey(ckeyStr);
355 opArg = new ServiceIDImpl(componentKey);
356 } else
357 {
358 System.out.println("Bad Result: ServiceID[service]\n");
359 System.exit(-1);
360 }
361 } catch (NoSuchElementException ex) {
362 System.out.println("Bad Result: ServiceID[service]\n");
363 System.exit(-1);
364 }
365 }
366 //service management
367 else if (command.equals("-getServiceState") )
368 {
369 commandBean = "slee:name=ServiceManagementMBean";
370 name = new ObjectName(commandBean);
371 commandString = "getState";
372 sig = "javax.slee.ServiceID";
373 try {
374 StringTokenizer stringTokenizer = new StringTokenizer(data, "[", true);
375 String componentType = stringTokenizer.nextToken();
376 stringTokenizer.nextToken();
377 String ckeyStr = stringTokenizer.nextToken("]");
378 if (componentType.equalsIgnoreCase(ComponentIDImpl.SERVICE_ID)) {
379 ComponentKey componentKey = new ComponentKey(ckeyStr);
380 opArg = new ServiceIDImpl(componentKey);
381 } else
382 {
383 System.out.println("Bad Result: ServiceID[service]\n");
384 System.exit(-1);
385 }
386 } catch (NoSuchElementException ex) {
387 System.out.println("Bad Result: ServiceID[service]\n");
388 System.exit(-1);
389 }
390 }
391 //trace management
392 else if (command.equals("-setTraceLevel") )
393 {
394 commandBean = "slee:name=TraceMBean";
395 name = new ObjectName(commandBean);
396 commandString = "setTraceLevel";
397 sig = "javax.slee.ServiceID";
398 sig2 = "javax.slee.facilities.Level";
399 opArg2 = data2;
400 try {
401 StringTokenizer stringTokenizer = new StringTokenizer(data, "[", true);
402 String componentType = stringTokenizer.nextToken();
403 stringTokenizer.nextToken();
404 String ckeyStr = stringTokenizer.nextToken("]");
405 if (componentType.equalsIgnoreCase(ComponentIDImpl.SERVICE_ID)) {
406 ComponentKey componentKey = new ComponentKey(ckeyStr);
407 opArg = new ServiceIDImpl(componentKey);
408 } else
409 {
410 System.out.println("Bad Result: Trace ServiceID[service]\n");
411 System.exit(-1);
412 }
413 } catch (NoSuchElementException ex) {
414 System.out.println("Bad Result: Trace ServiceID[service]\n");
415 System.exit(-1);
416 }
417
418 }
419 //trace management
420 else if (command.equals("-getTraceLevel") )
421 {
422 commandBean = "slee:name=TraceMBean";
423 name = new ObjectName(commandBean);
424 commandString = "setTraceLevel";
425 sig = "javax.slee.ServiceID";
426 try {
427 StringTokenizer stringTokenizer = new StringTokenizer(data, "[", true);
428 String componentType = stringTokenizer.nextToken();
429 stringTokenizer.nextToken();
430 String ckeyStr = stringTokenizer.nextToken("]");
431 if (componentType.equalsIgnoreCase(ComponentIDImpl.SERVICE_ID)) {
432 ComponentKey componentKey = new ComponentKey(ckeyStr);
433 opArg = new ServiceIDImpl(componentKey);
434 } else
435 {
436 System.out.println("Bad Result: Trace ServiceID[service]\n");
437 System.exit(-1);
438 }
439 } catch (NoSuchElementException ex) {
440 System.out.println("Bad Result: Trace ServiceID[service]\n");
441 System.exit(-1);
442 }
443
444 }
445 //Resource management
446 else if (command.equals("-createRaEntity") )
447 {
448 commandBean = "slee:name=ResourceAdaptorMBean";
449 name = new ObjectName(commandBean);
450 commandString = "createResourceAdaptorEntity";
451 sig = "javax.slee.resource.ResourceAdaptorID";
452 sig2 = "java.lang.String"; // entity name
453 opArg2 = data2;
454 sig3 = "java.util.Properties"; // RA properties
455 opArg3 = data3;
456 try {
457 StringTokenizer stringTokenizer = new StringTokenizer(data, "[", true);
458 String componentType = stringTokenizer.nextToken();
459 stringTokenizer.nextToken();
460 String ckeyStr = stringTokenizer.nextToken("]");
461 if (componentType.equalsIgnoreCase(ComponentIDImpl.RESOURCE_ADAPTOR_ID) ) {
462 ComponentKey componentKey = new ComponentKey(ckeyStr);
463 opArg = new ResourceAdaptorIDImpl(componentKey);
464 } else
465 {
466 System.out.println("Bad Result: ResourceAdaptor[resourceID]\n");
467 System.exit(-1);
468 }
469 } catch (NoSuchElementException ex) {
470 System.out.println("Bad Result: ResourceAdaptor[resourceID]\n");
471 System.exit(-1);
472 }
473
474 }
475 //Resource management
476 else if (command.equals("-activateRaEntity") )
477 {
478 commandBean = "slee:name=ResourceAdaptorMBean";
479 name = new ObjectName(commandBean);
480 commandString = "activateResourceAdaptorEntity";
481 sig = "java.lang.String"; // entity name
482 opArg = data;
483
484 }
485 //Resource management
486 else if (command.equals("-deactivateRaEntity") )
487 {
488 commandBean = "slee:name=ResourceAdaptorMBean";
489 name = new ObjectName(commandBean);
490 commandString = "deactivateResourceAdaptorEntity";
491 sig = "java.lang.String"; // entity name
492 opArg = data;
493
494 }
495 //Resource management
496 else if (command.equals("-removeRaEntity") )
497 {
498 commandBean = "slee:name=ResourceAdaptorMBean";
499 name = new ObjectName(commandBean);
500 commandString = "removeResourceAdaptorEntity";
501 sig = "java.lang.String"; // entity name
502 opArg = data;
503
504 }
505 //Resource management
506 else if (command.equals("-createEntityLink") )
507 {
508 commandBean = "slee:name=ResourceAdaptorMBean";
509 name = new ObjectName(commandBean);
510 commandString = "createEntityLink";
511 sig = "java.lang.String"; // link name
512 sig2 = "java.lang.String"; // entity name
513 opArg = data;
514 opArg2 = data2;
515
516 }
517 //Resource management
518 else if (command.equals("-removeEntityLink") )
519 {
520 commandBean = "slee:name=ResourceAdaptorMBean";
521 name = new ObjectName(commandBean);
522 commandString = "removeEntityLink";
523 sig = "java.lang.String"; // link name
524 opArg = data;
525
526 }
527 //bad command
528 else
529 {
530 usage();
531 System.exit(1);
532 }
533
534 SleeCommandInterface slee=new SleeCommandInterface("jnp://localhost:1099");
535
536
537 //passing in 3 args
538 if (sig3 != null)
539 {
540 String sigs[] = {sig,sig2,sig3};
541 Object[] opArgs = {opArg,opArg2,opArg3};
542 slee.invokeCommand(name, commandString, opArgs, sigs);
543 }
544 //passing in 2 args
545 else if (sig2 != null)
546 {
547 String sigs[] = {sig,sig2};
548 Object[] opArgs = {opArg,opArg2};
549 slee.invokeCommand(name, commandString, opArgs, sigs);
550 }
551 //passing in one argument
552 else if (sig != null)
553 {
554 String sigs[] = {sig};
555 Object[] opArgs = {opArg};
556 slee.invokeCommand(name, commandString, opArgs, sigs);
557 }
558 //no args
559 else
560 {
561 slee.invokeCommand(name, commandString, null, null);
562 }
563
564
565 //reset to null
566 sig=null;
567 sig2=null;
568 sig3=null;
569
570 }
571 }