KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > framework > AdminCommandsService


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.framework;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import javax.jbi.JBIException;
27 import javax.jbi.management.LifeCycleMBean;
28 import javax.management.JMException JavaDoc;
29 import javax.management.MBeanOperationInfo JavaDoc;
30
31 import org.apache.servicemix.jbi.management.BaseSystemService;
32 import org.apache.servicemix.jbi.management.OperationInfoHelper;
33 import org.apache.servicemix.jbi.management.ParameterHelper;
34
35 public class AdminCommandsService extends BaseSystemService implements
36         AdminCommandsServiceMBean {
37
38     /**
39      * @return a description of this
40      */

41     public String JavaDoc getDescription() {
42         return "Admin Commands Service";
43     }
44
45     protected Class JavaDoc getServiceMBean() {
46         return AdminCommandsServiceMBean.class;
47     }
48
49     /**
50      * Install a JBI component (a Service Engine or Binding Component)
51      *
52      * @param file
53      * @return
54      */

55     public String JavaDoc installComponent(String JavaDoc file) throws Exception JavaDoc {
56         return installComponent(file, null);
57     }
58
59     /**
60      * Install a JBI component (a Service Engine or Binding Component)
61      *
62      * @param file
63      * jbi component archive to install
64      * @param props
65      * installation properties
66      * @return
67      */

68     public String JavaDoc installComponent(String JavaDoc file, Properties JavaDoc props)
69             throws Exception JavaDoc {
70         try {
71             container.getInstallationService().install(file, props, false);
72             return ManagementSupport.createSuccessMessage("installComponent",
73                     file);
74         } catch (Exception JavaDoc e) {
75             throw ManagementSupport.failure("installComponent", file, e);
76         }
77     }
78
79     /**
80      * Uninstalls a previously install JBI Component (a Service Engine or
81      * Binding Component)
82      *
83      * @param name
84      * @return
85      */

86     public String JavaDoc uninstallComponent(String JavaDoc name) throws Exception JavaDoc {
87         ComponentMBeanImpl comp = container.getComponent(name);
88         if (comp == null) {
89             throw ManagementSupport.failure("uninstallComponent", "Component '"
90                     + name + "' is not installed.");
91         }
92         if (!comp.isShutDown()) {
93             throw ManagementSupport.failure("uninstallComponent", "Component '"
94                     + name + "' is not shut down.");
95         }
96         boolean success = container.getInstallationService().unloadInstaller(
97                 name, true);
98         if (success) {
99             return success("uninstallComponent", name);
100         } else {
101             return failure("uninstallComponent", name, null);
102         }
103     }
104
105     /**
106      * Installs a Shared Library.
107      *
108      * @param file
109      * @return
110      */

111     public String JavaDoc installSharedLibrary(String JavaDoc file) throws Exception JavaDoc {
112         return container.getInstallationService().installSharedLibrary(file);
113     }
114
115     /**
116      * Uninstalls a previously installed Shared Library.
117      *
118      * @param name
119      * @return
120      */

121     public String JavaDoc uninstallSharedLibrary(String JavaDoc name) throws Exception JavaDoc {
122         // Check that the library is installed
123
SharedLibrary sl = container.getRegistry().getSharedLibrary(name);
124         if (sl == null) {
125             throw ManagementSupport.failure("uninstallSharedLibrary",
126                     "Shared library '" + name + "' is not installed.");
127         }
128         // Check that it is not used by a running component
129
Collection JavaDoc components = container.getRegistry().getComponents();
130         for (Iterator JavaDoc iter = components.iterator(); iter.hasNext();) {
131             ComponentMBeanImpl comp = (ComponentMBeanImpl) iter.next();
132             if (!comp.isShutDown()) {
133                 String JavaDoc[] sls = comp.getSharedLibraries();
134                 if (sls != null) {
135                     for (int i = 0; i < sls.length; i++) {
136                         if (name.equals(sls[i])) {
137                             throw ManagementSupport.failure(
138                                     "uninstallSharedLibrary",
139                                     "Shared library '" + name
140                                             + "' is used by component '"
141                                             + comp.getName() + "'.");
142                         }
143                     }
144                 }
145             }
146         }
147         boolean success = container.getInstallationService()
148                 .uninstallSharedLibrary(name);
149         if (success) {
150             return success("uninstallSharedLibrary", name);
151         } else {
152             return failure("uninstallSharedLibrary", name, null);
153         }
154     }
155
156     /**
157      * Starts a particular Component (Service Engine or Binding Component).
158      *
159      * @param name
160      * @return
161      */

162     public String JavaDoc startComponent(String JavaDoc name) throws Exception JavaDoc {
163         try {
164             ComponentMBeanImpl lcc = container.getComponent(name);
165             if (lcc == null) {
166                 throw new JBIException("Component " + name + " not found");
167             }
168             lcc.start();
169             return success("startComponent", name);
170         } catch (JBIException e) {
171             throw new RuntimeException JavaDoc(failure("startComponent", name, e));
172         }
173     }
174
175     /**
176      * Stops a particular Component (Service Engine or Binding Component).
177      *
178      * @param name
179      * @return
180      */

181     public String JavaDoc stopComponent(String JavaDoc name) throws Exception JavaDoc {
182         try {
183             ComponentMBeanImpl lcc = container.getComponent(name);
184             if (lcc == null) {
185                 throw new JBIException("Component " + name + " not found");
186             }
187             lcc.stop();
188             return success("stopComponent", name);
189         } catch (JBIException e) {
190             throw new RuntimeException JavaDoc(failure("stopComponent", name, e));
191         }
192     }
193
194     /**
195      * Shuts down a particular Component.
196      *
197      * @param name
198      * @return
199      */

200     public String JavaDoc shutdownComponent(String JavaDoc name) throws Exception JavaDoc {
201         try {
202             ComponentMBeanImpl lcc = container.getComponent(name);
203             if (lcc == null) {
204                 throw new JBIException("Component " + name + " not found");
205             }
206             lcc.shutDown();
207             return success("shutdownComponent", name);
208         } catch (JBIException e) {
209             throw new RuntimeException JavaDoc(failure("shutdownComponent", name, e));
210         }
211     }
212
213     /**
214      * Deploys a Service Assembly.
215      *
216      * @param file
217      * @return
218      */

219     public String JavaDoc deployServiceAssembly(String JavaDoc file) throws Exception JavaDoc {
220         return container.getDeploymentService().deploy(file);
221     }
222
223     /**
224      * Undeploys a previously deployed service assembly.
225      *
226      * @param name
227      * @return
228      */

229     public String JavaDoc undeployServiceAssembly(String JavaDoc name) throws Exception JavaDoc {
230         return container.getDeploymentService().undeploy(name);
231     }
232
233     /**
234      * Starts a service assembly.
235      *
236      * @param name
237      * @return
238      */

239     public String JavaDoc startServiceAssembly(String JavaDoc name) throws Exception JavaDoc {
240         return container.getDeploymentService().start(name);
241     }
242
243     /**
244      * Stops a particular service assembly.
245      *
246      * @param name
247      * @return
248      */

249     public String JavaDoc stopServiceAssembly(String JavaDoc name) throws Exception JavaDoc {
250         return container.getDeploymentService().stop(name);
251     }
252
253     /**
254      * Shuts down a particular service assembly.
255      *
256      * @param name
257      * @return
258      */

259     public String JavaDoc shutdownServiceAssembly(String JavaDoc name) throws Exception JavaDoc {
260         return container.getDeploymentService().shutDown(name);
261     }
262
263     /**
264      * load an archive from an external location and starts it The archive can
265      * be a Component, Service Assembly or Shared Library.
266      *
267      * @param location -
268      * can either be a url or filename (if relative - must be
269      * relative to the container)
270      * @return status
271      * @throws Exception
272      */

273     public String JavaDoc installArchive(String JavaDoc location) throws Exception JavaDoc {
274         try {
275             container.updateExternalArchive(location);
276             return success("installArchive", location);
277
278         } catch (Exception JavaDoc e) {
279             throw new RuntimeException JavaDoc(failure("shutdownServiceAssembly",
280                     location, e));
281         }
282     }
283
284     /**
285      * Prints information about all components (Service Engine or Binding
286      * Component) installed
287      *
288      * @param serviceEngines
289      * @param bindingComponents
290      * @param state
291      * @param sharedLibraryName
292      * @param serviceAssemblyName
293      * @return list of components in an XML blob
294      */

295     public String JavaDoc listComponents(boolean excludeSEs, boolean excludeBCs,
296             boolean excludePojos, String JavaDoc requiredState,
297             String JavaDoc sharedLibraryName, String JavaDoc serviceAssemblyName)
298             throws Exception JavaDoc {
299         // validate requiredState
300
if (requiredState != null && requiredState.length() > 0) {
301             if (!LifeCycleMBean.UNKNOWN.equalsIgnoreCase(requiredState)
302                     && !LifeCycleMBean.SHUTDOWN.equalsIgnoreCase(requiredState)
303                     && !LifeCycleMBean.STOPPED.equalsIgnoreCase(requiredState)
304                     && !LifeCycleMBean.STARTED.equalsIgnoreCase(requiredState)) {
305                 throw ManagementSupport.failure("listComponents",
306                         "Required state '" + requiredState
307                                 + "' is not a valid state.");
308             }
309         }
310         // Get components
311
Collection JavaDoc connectors = container.getRegistry().getComponents();
312         List JavaDoc components = new ArrayList JavaDoc();
313         for (Iterator JavaDoc iter = connectors.iterator(); iter.hasNext();) {
314             ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next();
315             // Skip SEs if needed
316
if (excludeSEs && component.isService()) {
317                 continue;
318             }
319             // Skip BCs if needed
320
if (excludeBCs && component.isBinding()) {
321                 continue;
322             }
323             // Skip Pojos if needed
324
if (excludePojos && component.isPojo()) {
325                 continue;
326             }
327             // Check status
328
if (requiredState != null
329                     && requiredState.length() > 0
330                     && !requiredState.equalsIgnoreCase(component
331                             .getCurrentState())) {
332                 continue;
333             }
334             // Check shared library
335
// TODO: check component dependency on SL
336
if (sharedLibraryName != null
337                     && sharedLibraryName.length() > 0
338                     && !container.getInstallationService()
339                             .containsSharedLibrary(sharedLibraryName)) {
340                 continue;
341             }
342             // Check deployed service assembly
343
// TODO: check SA dependency on component
344
if (serviceAssemblyName != null && serviceAssemblyName.length() > 0) {
345                 String JavaDoc[] saNames = container.getRegistry()
346                         .getDeployedServiceAssembliesForComponent(
347                                 component.getName());
348                 boolean found = false;
349                 for (int i = 0; i < saNames.length; i++) {
350                     if (serviceAssemblyName.equals(saNames[i])) {
351                         found = true;
352                         break;
353                     }
354                 }
355                 if (!found) {
356                     continue;
357                 }
358             }
359             components.add(component);
360         }
361
362         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
363         buffer.append("<?xml version='1.0'?>\n");
364         buffer
365                 .append("<component-info-list xmlns='http://java.sun.com/xml/ns/jbi/component-info-list' version='1.0'>\n");
366         for (Iterator JavaDoc iter = components.iterator(); iter.hasNext();) {
367             ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next();
368             buffer.append(" <component-info");
369             if (!component.isBinding() && component.isService()) {
370                 buffer.append(" type='service-engine'");
371             } else if (component.isBinding() && !component.isService()) {
372                 buffer.append(" type='binding-component'");
373             }
374             buffer.append(" name='" + component.getName() + "'");
375             buffer.append(" state='" + component.getCurrentState() + "'>\n");
376             if (component.getDescription() != null) {
377                 buffer.append(" <description>");
378                 buffer.append(component.getDescription());
379                 buffer.append("</description>\n");
380             }
381             buffer.append(" </component-info>\n");
382         }
383         buffer.append("</component-info-list>");
384         return buffer.toString();
385     }
386
387     /**
388      * Prints information about shared libraries installed.
389      *
390      * @param componentName
391      * @param sharedLibraryName
392      * @return
393      */

394     public String JavaDoc listSharedLibraries(String JavaDoc componentName,
395             String JavaDoc sharedLibraryName) throws Exception JavaDoc {
396         Collection JavaDoc libs;
397         if (sharedLibraryName != null) {
398             SharedLibrary sl = container.getRegistry().getSharedLibrary(
399                     sharedLibraryName);
400             if (sl == null) {
401                 libs = Collections.EMPTY_LIST;
402             } else {
403                 libs = Collections.singletonList(sl);
404             }
405         } else if (componentName != null) {
406             // TODO: handle componentName
407
libs = container.getRegistry().getSharedLibraries();
408         } else {
409             libs = container.getRegistry().getSharedLibraries();
410         }
411         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
412         buffer.append("<?xml version='1.0'?>\n");
413         buffer
414                 .append("<component-info-list xmlns='http://java.sun.com/xml/ns/jbi/component-info-list' version='1.0'>\n");
415         for (Iterator JavaDoc iter = libs.iterator(); iter.hasNext();) {
416             SharedLibrary sl = (SharedLibrary) iter.next();
417             buffer.append(" <component-info type='shared-library' name='")
418                     .append(sl.getName()).append("' state='Started'>");
419             if (sl.getDescription() != null) {
420                 buffer.append(" <description>");
421                 buffer.append(sl.getDescription());
422                 buffer.append("</description>\n");
423             }
424             buffer.append(" </component-info>\n");
425         }
426         buffer.append("</component-info-list>");
427         return buffer.toString();
428     }
429
430     /**
431      * Prints information about service assemblies deployed.
432      *
433      * @param state
434      * @param componentName
435      * @param serviceAssemblyName
436      * @return
437      */

438     public String JavaDoc listServiceAssemblies(String JavaDoc state, String JavaDoc componentName,
439             String JavaDoc serviceAssemblyName) throws Exception JavaDoc {
440         String JavaDoc[] result = null;
441         if (null != serviceAssemblyName && serviceAssemblyName.length() > 0) {
442             result = new String JavaDoc[] { serviceAssemblyName };
443         } else if (null != componentName && componentName.length() > 0) {
444             result = container.getRegistry()
445                     .getDeployedServiceAssembliesForComponent(componentName);
446         } else {
447             result = container.getRegistry().getDeployedServiceAssemblies();
448         }
449
450         List JavaDoc assemblies = new ArrayList JavaDoc();
451         for (int i = 0; i < result.length; i++) {
452             ServiceAssemblyLifeCycle sa = container.getRegistry()
453                     .getServiceAssembly(result[i]);
454             if (sa != null) {
455                 // Check status
456
if (state != null && state.length() > 0
457                         && !state.equals(sa.getCurrentState())) {
458                     continue;
459                 }
460                 assemblies.add(sa);
461             }
462         }
463
464         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
465         buffer.append("<?xml version='1.0'?>\n");
466         buffer
467                 .append("<service-assembly-info-list xmlns='http://java.sun.com/xml/ns/jbi/service-assembly-info-list' version='1.0'>\n");
468         for (Iterator JavaDoc iter = assemblies.iterator(); iter.hasNext();) {
469             ServiceAssemblyLifeCycle sa = (ServiceAssemblyLifeCycle) iter
470                     .next();
471             buffer.append(" <service-assembly-info");
472             buffer.append(" name='" + sa.getName() + "'");
473             buffer.append(" state='" + sa.getCurrentState() + "'>\n");
474             buffer.append(" <description>" + sa.getDescription()
475                     + "</description>\n");
476
477             ServiceUnitLifeCycle[] serviceUnitList = sa.getDeployedSUs();
478             for (int i = 0; i < serviceUnitList.length; i++) {
479                 buffer.append(" <service-unit-info");
480                 buffer.append(" name='" + serviceUnitList[i].getName() + "'");
481                 buffer.append(" state='" + serviceUnitList[i].getCurrentState()
482                         + "'");
483                 buffer.append(" deployed-on='"
484                         + serviceUnitList[i].getComponentName() + "'>\n");
485                 buffer.append(" <description>"
486                         + serviceUnitList[i].getDescription()
487                         + "</description>\n");
488                 buffer.append(" </service-unit-info>\n");
489             }
490
491             buffer.append(" </service-assembly-info>\n");
492         }
493         buffer.append("</service-assembly-info-list>");
494
495         return buffer.toString();
496     }
497
498     public String JavaDoc failure(String JavaDoc task, String JavaDoc location, Exception JavaDoc e) {
499         ManagementSupport.Message msg = new ManagementSupport.Message();
500         msg.setTask(task);
501         msg.setResult("FAILED");
502         msg.setType("ERROR");
503         msg.setException(e);
504         msg.setMessage(location);
505         return ManagementSupport.createFrameworkMessage(msg, (List JavaDoc) null);
506     }
507
508     public String JavaDoc success(String JavaDoc task, String JavaDoc location) {
509         ManagementSupport.Message msg = new ManagementSupport.Message();
510         msg.setTask(task);
511         msg.setMessage(location);
512         msg.setResult("SUCCESS");
513         return ManagementSupport.createFrameworkMessage(msg, (List JavaDoc) null);
514     }
515
516     public MBeanOperationInfo JavaDoc[] getOperationInfos() throws JMException JavaDoc {
517         OperationInfoHelper helper = new OperationInfoHelper();
518         ParameterHelper ph = helper.addOperation(getObjectToManage(),
519                 "installComponent", 1, "install a component");
520         ph.setDescription(0, "file", "location of JBI Component to install");
521
522         ph = helper.addOperation(getObjectToManage(), "uninstallComponent", 1,
523                 "uninstall a component");
524         ph.setDescription(0, "name", "component name to uninstall");
525
526         ph = helper.addOperation(getObjectToManage(), "installSharedLibrary",
527                 1, "install a shared library");
528         ph.setDescription(0, "file", "location of shared library to install");
529
530         ph = helper.addOperation(getObjectToManage(), "uninstallSharedLibrary",
531                 1, "uninstall a shared library");
532         ph.setDescription(0, "name", "name of shared library to uninstall");
533
534         ph = helper.addOperation(getObjectToManage(), "installArchive", 1,
535                 "install an archive (component/SA etc)");
536         ph.setDescription(0, "location", "file name or url to the location");
537
538         ph = helper.addOperation(getObjectToManage(), "startComponent", 1,
539                 "start a component");
540         ph.setDescription(0, "name", "name of component to start");
541
542         ph = helper.addOperation(getObjectToManage(), "stopComponent", 1,
543                 "stop a component");
544         ph.setDescription(0, "name", "name of component to stop");
545
546         ph = helper.addOperation(getObjectToManage(), "shutdownComponent", 1,
547                 "shutdown a component");
548         ph.setDescription(0, "name", "name of component to shutdown");
549
550         ph = helper.addOperation(getObjectToManage(), "deployServiceAssembly",
551                 1, "deploy a service assembly");
552         ph.setDescription(0, "file", "location of service assembly to deploy");
553
554         ph = helper.addOperation(getObjectToManage(),
555                 "undeployServiceAssembly", 1, "undeploy a service assembly");
556         ph.setDescription(0, "name", "name of service assembly to undeploy");
557
558         ph = helper.addOperation(getObjectToManage(), "startServiceAssembly",
559                 1, "start a service assembly");
560         ph.setDescription(0, "name", "name of service assembly to start");
561
562         ph = helper.addOperation(getObjectToManage(), "stopServiceAssembly", 1,
563                 "stop a service assembly");
564         ph.setDescription(0, "name", "name of service assembly to stop");
565
566         ph = helper.addOperation(getObjectToManage(),
567                 "shutdownServiceAssembly", "shutdown a service assembly");
568         ph.setDescription(0, "name", "name of service assembly to shutdown");
569
570         ph = helper.addOperation(getObjectToManage(), "listComponents", 5,
571                 "list all components installed");
572         ph.setDescription(0, "excludeSEs",
573                 "if true will exclude service engines");
574         ph.setDescription(1, "excludeBCs",
575                 "if true will exclude binding components");
576         ph.setDescription(1, "excludePojos",
577                 "if true will exclude pojos components");
578         ph.setDescription(2, "requiredState",
579                 "component state to list, if null will list all");
580         ph
581                 .setDescription(3, "sharedLibraryName",
582                         "shared library name to list");
583         ph.setDescription(4, "serviceAssemblyName",
584                 "service assembly name to list");
585
586         ph = helper.addOperation(getObjectToManage(), "listSharedLibraries", 2,
587                 "list shared library");
588         ph.setDescription(0, "componentName", "component name");
589         ph.setDescription(1, "sharedLibraryName", "shared library name");
590
591         ph = helper.addOperation(getObjectToManage(), "listServiceAssemblies",
592                 3, "list service assemblies");
593         ph.setDescription(0, "state", "service assembly state to list");
594         ph.setDescription(1, "componentName", "component name");
595         ph.setDescription(2, "serviceAssemblyName", "service assembly name");
596
597         return OperationInfoHelper.join(super.getOperationInfos(), helper
598                 .getOperationInfos());
599     }
600
601 }
602
Popular Tags