001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jbi.framework;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Properties;
025:
026: import javax.jbi.JBIException;
027: import javax.jbi.management.LifeCycleMBean;
028: import javax.management.JMException;
029: import javax.management.MBeanOperationInfo;
030:
031: import org.apache.servicemix.jbi.management.BaseSystemService;
032: import org.apache.servicemix.jbi.management.OperationInfoHelper;
033: import org.apache.servicemix.jbi.management.ParameterHelper;
034:
035: public class AdminCommandsService extends BaseSystemService implements
036: AdminCommandsServiceMBean {
037:
038: /**
039: * @return a description of this
040: */
041: public String getDescription() {
042: return "Admin Commands Service";
043: }
044:
045: protected Class getServiceMBean() {
046: return AdminCommandsServiceMBean.class;
047: }
048:
049: /**
050: * Install a JBI component (a Service Engine or Binding Component)
051: *
052: * @param file
053: * jbi component archive to install
054: * @param props
055: * installation properties
056: * @return
057: */
058: public String installComponent(String file, Properties props,
059: boolean deferException) throws Exception {
060: try {
061: if (deferException) {
062: container.updateExternalArchive(file);
063: } else {
064: container.getInstallationService().install(file, props,
065: false);
066: }
067: return ManagementSupport.createSuccessMessage(
068: "installComponent", file);
069: } catch (Exception e) {
070: throw ManagementSupport
071: .failure("installComponent", file, e);
072: }
073: }
074:
075: /**
076: * Uninstalls a previously install JBI Component (a Service Engine or
077: * Binding Component)
078: *
079: * @param name
080: * @return
081: */
082: public String uninstallComponent(String name) throws Exception {
083: ComponentMBeanImpl comp = container.getComponent(name);
084: if (comp == null) {
085: throw ManagementSupport.failure("uninstallComponent",
086: "Component '" + name + "' is not installed.");
087: }
088: if (!comp.isShutDown()) {
089: throw ManagementSupport.failure("uninstallComponent",
090: "Component '" + name + "' is not shut down.");
091: }
092: boolean success = container.getInstallationService()
093: .unloadInstaller(name, true);
094: if (success) {
095: return ManagementSupport.createSuccessMessage(
096: "uninstallComponent", name);
097: } else {
098: throw ManagementSupport.failure("uninstallComponent", name);
099: }
100: }
101:
102: /**
103: * Installs a Shared Library.
104: *
105: * @param file
106: * @return
107: */
108: public String installSharedLibrary(String file,
109: boolean deferException) throws Exception {
110: if (deferException) {
111: container.updateExternalArchive(file);
112: return ManagementSupport.createSuccessMessage(
113: "installSharedLibrary", file);
114: } else {
115: return container.getInstallationService()
116: .installSharedLibrary(file);
117: }
118: }
119:
120: /**
121: * Uninstalls a previously installed Shared Library.
122: *
123: * @param name
124: * @return
125: */
126: public String uninstallSharedLibrary(String name) throws Exception {
127: // Check that the library is installed
128: SharedLibrary sl = container.getRegistry().getSharedLibrary(
129: name);
130: if (sl == null) {
131: throw ManagementSupport.failure("uninstallSharedLibrary",
132: "Shared library '" + name + "' is not installed.");
133: }
134: // Check that it is not used by a running component
135: Collection components = container.getRegistry().getComponents();
136: for (Iterator iter = components.iterator(); iter.hasNext();) {
137: ComponentMBeanImpl comp = (ComponentMBeanImpl) iter.next();
138: if (!comp.isShutDown()) {
139: String[] sls = comp.getSharedLibraries();
140: if (sls != null) {
141: for (int i = 0; i < sls.length; i++) {
142: if (name.equals(sls[i])) {
143: throw ManagementSupport
144: .failure(
145: "uninstallSharedLibrary",
146: "Shared library '"
147: + name
148: + "' is used by component '"
149: + comp.getName()
150: + "'.");
151: }
152: }
153: }
154: }
155: }
156: boolean success = container.getInstallationService()
157: .uninstallSharedLibrary(name);
158: if (success) {
159: return ManagementSupport.createSuccessMessage(
160: "uninstallSharedLibrary", name);
161: } else {
162: throw ManagementSupport.failure("uninstallSharedLibrary",
163: name);
164: }
165: }
166:
167: /**
168: * Starts a particular Component (Service Engine or Binding Component).
169: *
170: * @param name
171: * @return
172: */
173: public String startComponent(String name) throws Exception {
174: try {
175: ComponentMBeanImpl lcc = container.getComponent(name);
176: if (lcc == null) {
177: throw new JBIException("Component " + name
178: + " not found");
179: }
180: lcc.start();
181: return ManagementSupport.createSuccessMessage(
182: "startComponent", name);
183: } catch (JBIException e) {
184: throw ManagementSupport.failure("startComponent", name, e);
185: }
186: }
187:
188: /**
189: * Stops a particular Component (Service Engine or Binding Component).
190: *
191: * @param name
192: * @return
193: */
194: public String stopComponent(String name) throws Exception {
195: try {
196: ComponentMBeanImpl lcc = container.getComponent(name);
197: if (lcc == null) {
198: throw new JBIException("Component " + name
199: + " not found");
200: }
201: lcc.stop();
202: return ManagementSupport.createSuccessMessage(
203: "stopComponent", name);
204: } catch (JBIException e) {
205: throw ManagementSupport.failure("stopComponent", name, e);
206: }
207: }
208:
209: /**
210: * Shuts down a particular Component.
211: *
212: * @param name
213: * @return
214: */
215: public String shutdownComponent(String name) throws Exception {
216: try {
217: ComponentMBeanImpl lcc = container.getComponent(name);
218: if (lcc == null) {
219: throw new JBIException("Component " + name
220: + " not found");
221: }
222: lcc.shutDown();
223: return ManagementSupport.createSuccessMessage(
224: "shutdownComponent", name);
225: } catch (JBIException e) {
226: throw ManagementSupport.failure("shutdownComponent", name,
227: e);
228: }
229: }
230:
231: /**
232: * Deploys a Service Assembly.
233: *
234: * @param file
235: * @return
236: */
237: public String deployServiceAssembly(String file,
238: boolean deferException) throws Exception {
239: if (deferException) {
240: container.updateExternalArchive(file);
241: return ManagementSupport.createSuccessMessage(
242: "deployServiceAssembly", file);
243: } else {
244: return container.getDeploymentService().deploy(file);
245: }
246: }
247:
248: /**
249: * Undeploys a previously deployed service assembly.
250: *
251: * @param name
252: * @return
253: */
254: public String undeployServiceAssembly(String name) throws Exception {
255: return container.getDeploymentService().undeploy(name);
256: }
257:
258: /**
259: * Starts a service assembly.
260: *
261: * @param name
262: * @return
263: */
264: public String startServiceAssembly(String name) throws Exception {
265: return container.getDeploymentService().start(name);
266: }
267:
268: /**
269: * Stops a particular service assembly.
270: *
271: * @param name
272: * @return
273: */
274: public String stopServiceAssembly(String name) throws Exception {
275: return container.getDeploymentService().stop(name);
276: }
277:
278: /**
279: * Shuts down a particular service assembly.
280: *
281: * @param name
282: * @return
283: */
284: public String shutdownServiceAssembly(String name) throws Exception {
285: return container.getDeploymentService().shutDown(name);
286: }
287:
288: /**
289: * load an archive from an external location and starts it The archive can
290: * be a Component, Service Assembly or Shared Library.
291: *
292: * @param location -
293: * can either be a url or filename (if relative - must be
294: * relative to the container)
295: * @return status
296: * @throws Exception
297: */
298: public String installArchive(String location) throws Exception {
299: try {
300: container.updateExternalArchive(location);
301: return ManagementSupport.createSuccessMessage(
302: "installComponent", location);
303: } catch (Exception e) {
304: throw ManagementSupport.failure("installComponent",
305: location, e);
306: }
307: }
308:
309: /**
310: * Prints information about all components (Service Engine or Binding
311: * Component) installed
312: *
313: * @param serviceEngines
314: * @param bindingComponents
315: * @param state
316: * @param sharedLibraryName
317: * @param serviceAssemblyName
318: * @return list of components in an XML blob
319: */
320: public String listComponents(boolean excludeSEs,
321: boolean excludeBCs, boolean excludePojos,
322: String requiredState, String sharedLibraryName,
323: String serviceAssemblyName) throws Exception {
324: // validate requiredState
325: if (requiredState != null
326: && requiredState.length() > 0
327: && !LifeCycleMBean.UNKNOWN
328: .equalsIgnoreCase(requiredState)
329: && !LifeCycleMBean.SHUTDOWN
330: .equalsIgnoreCase(requiredState)
331: && !LifeCycleMBean.STOPPED
332: .equalsIgnoreCase(requiredState)
333: && !LifeCycleMBean.STARTED
334: .equalsIgnoreCase(requiredState)) {
335: throw ManagementSupport.failure("listComponents",
336: "Required state '" + requiredState
337: + "' is not a valid state.");
338: }
339: // Get components
340: Collection connectors = container.getRegistry().getComponents();
341: List<ComponentMBeanImpl> components = new ArrayList<ComponentMBeanImpl>();
342: for (Iterator iter = connectors.iterator(); iter.hasNext();) {
343: ComponentMBeanImpl component = (ComponentMBeanImpl) iter
344: .next();
345: // Skip SEs if needed
346: if (excludeSEs && component.isService()) {
347: continue;
348: }
349: // Skip BCs if needed
350: if (excludeBCs && component.isBinding()) {
351: continue;
352: }
353: // Skip Pojos if needed
354: if (excludePojos && component.isPojo()) {
355: continue;
356: }
357: // Check status
358: if (requiredState != null
359: && requiredState.length() > 0
360: && !requiredState.equalsIgnoreCase(component
361: .getCurrentState())) {
362: continue;
363: }
364: // Check shared library
365: // TODO: check component dependency on SL
366: if (sharedLibraryName != null
367: && sharedLibraryName.length() > 0
368: && !container.getInstallationService()
369: .containsSharedLibrary(sharedLibraryName)) {
370: continue;
371: }
372: // Check deployed service assembly
373: // TODO: check SA dependency on component
374: if (serviceAssemblyName != null
375: && serviceAssemblyName.length() > 0) {
376: String[] saNames = container.getRegistry()
377: .getDeployedServiceAssembliesForComponent(
378: component.getName());
379: boolean found = false;
380: for (int i = 0; i < saNames.length; i++) {
381: if (serviceAssemblyName.equals(saNames[i])) {
382: found = true;
383: break;
384: }
385: }
386: if (!found) {
387: continue;
388: }
389: }
390: components.add(component);
391: }
392:
393: StringBuffer buffer = new StringBuffer();
394: buffer.append("<?xml version='1.0'?>\n");
395: buffer
396: .append("<component-info-list xmlns='http://java.sun.com/xml/ns/jbi/component-info-list' version='1.0'>\n");
397: for (Iterator<ComponentMBeanImpl> iter = components.iterator(); iter
398: .hasNext();) {
399: ComponentMBeanImpl component = iter.next();
400: buffer.append(" <component-info");
401: if (!component.isBinding() && component.isService()) {
402: buffer.append(" type='service-engine'");
403: } else if (component.isBinding() && !component.isService()) {
404: buffer.append(" type='binding-component'");
405: }
406: buffer.append(" name='" + component.getName() + "'");
407: buffer.append(" state='" + component.getCurrentState()
408: + "'>\n");
409: if (component.getDescription() != null) {
410: buffer.append(" <description>");
411: buffer.append(component.getDescription());
412: buffer.append("</description>\n");
413: }
414: buffer.append(" </component-info>\n");
415: }
416: buffer.append("</component-info-list>");
417: return buffer.toString();
418: }
419:
420: /**
421: * Prints information about shared libraries installed.
422: *
423: * @param componentName
424: * @param sharedLibraryName
425: * @return
426: */
427: public String listSharedLibraries(String componentName,
428: String sharedLibraryName) throws Exception {
429: Collection<SharedLibrary> libs;
430: if (sharedLibraryName != null) {
431: SharedLibrary sl = container.getRegistry()
432: .getSharedLibrary(sharedLibraryName);
433: if (sl == null) {
434: libs = Collections.EMPTY_LIST;
435: } else {
436: libs = Collections.singletonList(sl);
437: }
438: } else if (componentName != null) {
439: // TODO: handle componentName
440: libs = container.getRegistry().getSharedLibraries();
441: } else {
442: libs = container.getRegistry().getSharedLibraries();
443: }
444: StringBuffer buffer = new StringBuffer();
445: buffer.append("<?xml version='1.0'?>\n");
446: buffer
447: .append("<component-info-list xmlns='http://java.sun.com/xml/ns/jbi/component-info-list' version='1.0'>\n");
448: for (Iterator<SharedLibrary> iter = libs.iterator(); iter
449: .hasNext();) {
450: SharedLibrary sl = iter.next();
451: buffer.append(
452: " <component-info type='shared-library' name='")
453: .append(sl.getName()).append("' state='Started'>");
454: if (sl.getDescription() != null) {
455: buffer.append(" <description>");
456: buffer.append(sl.getDescription());
457: buffer.append("</description>\n");
458: }
459: buffer.append(" </component-info>\n");
460: }
461: buffer.append("</component-info-list>");
462: return buffer.toString();
463: }
464:
465: /**
466: * Prints information about service assemblies deployed.
467: *
468: * @param state
469: * @param componentName
470: * @param serviceAssemblyName
471: * @return
472: */
473: public String listServiceAssemblies(String state,
474: String componentName, String serviceAssemblyName)
475: throws Exception {
476: String[] result = null;
477: if (null != serviceAssemblyName
478: && serviceAssemblyName.length() > 0) {
479: result = new String[] { serviceAssemblyName };
480: } else if (null != componentName && componentName.length() > 0) {
481: result = container.getRegistry()
482: .getDeployedServiceAssembliesForComponent(
483: componentName);
484: } else {
485: result = container.getRegistry()
486: .getDeployedServiceAssemblies();
487: }
488:
489: List<ServiceAssemblyLifeCycle> assemblies = new ArrayList<ServiceAssemblyLifeCycle>();
490: for (int i = 0; i < result.length; i++) {
491: ServiceAssemblyLifeCycle sa = container.getRegistry()
492: .getServiceAssembly(result[i]);
493: if (sa != null) {
494: // Check status
495: if (state != null && state.length() > 0
496: && !state.equals(sa.getCurrentState())) {
497: continue;
498: }
499: assemblies.add(sa);
500: }
501: }
502:
503: StringBuffer buffer = new StringBuffer();
504: buffer.append("<?xml version='1.0'?>\n");
505: buffer
506: .append("<service-assembly-info-list xmlns='http://java.sun.com/xml/ns/jbi/service-assembly-info-list' version='1.0'>\n");
507: for (Iterator<ServiceAssemblyLifeCycle> iter = assemblies
508: .iterator(); iter.hasNext();) {
509: ServiceAssemblyLifeCycle sa = iter.next();
510: buffer.append(" <service-assembly-info");
511: buffer.append(" name='" + sa.getName() + "'");
512: buffer.append(" state='" + sa.getCurrentState() + "'>\n");
513: buffer.append(" <description>" + sa.getDescription()
514: + "</description>\n");
515:
516: ServiceUnitLifeCycle[] serviceUnitList = sa
517: .getDeployedSUs();
518: for (int i = 0; i < serviceUnitList.length; i++) {
519: buffer.append(" <service-unit-info");
520: buffer.append(" name='" + serviceUnitList[i].getName()
521: + "'");
522: buffer.append(" state='"
523: + serviceUnitList[i].getCurrentState() + "'");
524: buffer.append(" deployed-on='"
525: + serviceUnitList[i].getComponentName()
526: + "'>\n");
527: buffer.append(" <description>"
528: + serviceUnitList[i].getDescription()
529: + "</description>\n");
530: buffer.append(" </service-unit-info>\n");
531: }
532:
533: buffer.append(" </service-assembly-info>\n");
534: }
535: buffer.append("</service-assembly-info-list>");
536:
537: return buffer.toString();
538: }
539:
540: public MBeanOperationInfo[] getOperationInfos() throws JMException {
541: OperationInfoHelper helper = new OperationInfoHelper();
542: ParameterHelper ph = helper.addOperation(getObjectToManage(),
543: "installComponent", 3, "install a component");
544: ph.setDescription(0, "file",
545: "location of JBI Component to install");
546: ph.setDescription(1, "properties",
547: "component installation properties");
548: ph
549: .setDescription(1, "deferExceptions",
550: "true if exceptions due to missing dependencies should be differed");
551:
552: ph = helper.addOperation(getObjectToManage(),
553: "uninstallComponent", 1, "uninstall a component");
554: ph.setDescription(0, "name", "component name to uninstall");
555:
556: ph = helper.addOperation(getObjectToManage(),
557: "installSharedLibrary", 1, "install a shared library");
558: ph.setDescription(0, "file",
559: "location of shared library to install");
560:
561: ph = helper.addOperation(getObjectToManage(),
562: "uninstallSharedLibrary", 1,
563: "uninstall a shared library");
564: ph.setDescription(0, "name",
565: "name of shared library to uninstall");
566:
567: ph = helper.addOperation(getObjectToManage(), "installArchive",
568: 1, "install an archive (component/SA etc)");
569: ph.setDescription(0, "location",
570: "file name or url to the location");
571:
572: ph = helper.addOperation(getObjectToManage(), "startComponent",
573: 1, "start a component");
574: ph.setDescription(0, "name", "name of component to start");
575:
576: ph = helper.addOperation(getObjectToManage(), "stopComponent",
577: 1, "stop a component");
578: ph.setDescription(0, "name", "name of component to stop");
579:
580: ph = helper.addOperation(getObjectToManage(),
581: "shutdownComponent", 1, "shutdown a component");
582: ph.setDescription(0, "name", "name of component to shutdown");
583:
584: ph = helper
585: .addOperation(getObjectToManage(),
586: "deployServiceAssembly", 1,
587: "deploy a service assembly");
588: ph.setDescription(0, "file",
589: "location of service assembly to deploy");
590:
591: ph = helper.addOperation(getObjectToManage(),
592: "undeployServiceAssembly", 1,
593: "undeploy a service assembly");
594: ph.setDescription(0, "name",
595: "name of service assembly to undeploy");
596:
597: ph = helper.addOperation(getObjectToManage(),
598: "startServiceAssembly", 1, "start a service assembly");
599: ph.setDescription(0, "name",
600: "name of service assembly to start");
601:
602: ph = helper.addOperation(getObjectToManage(),
603: "stopServiceAssembly", 1, "stop a service assembly");
604: ph
605: .setDescription(0, "name",
606: "name of service assembly to stop");
607:
608: ph = helper.addOperation(getObjectToManage(),
609: "shutdownServiceAssembly",
610: "shutdown a service assembly");
611: ph.setDescription(0, "name",
612: "name of service assembly to shutdown");
613:
614: ph = helper.addOperation(getObjectToManage(), "listComponents",
615: 5, "list all components installed");
616: ph.setDescription(0, "excludeSEs",
617: "if true will exclude service engines");
618: ph.setDescription(1, "excludeBCs",
619: "if true will exclude binding components");
620: ph.setDescription(1, "excludePojos",
621: "if true will exclude pojos components");
622: ph.setDescription(2, "requiredState",
623: "component state to list, if null will list all");
624: ph.setDescription(3, "sharedLibraryName",
625: "shared library name to list");
626: ph.setDescription(4, "serviceAssemblyName",
627: "service assembly name to list");
628:
629: ph = helper.addOperation(getObjectToManage(),
630: "listSharedLibraries", 2, "list shared library");
631: ph.setDescription(0, "componentName", "component name");
632: ph
633: .setDescription(1, "sharedLibraryName",
634: "shared library name");
635:
636: ph = helper.addOperation(getObjectToManage(),
637: "listServiceAssemblies", 3, "list service assemblies");
638: ph.setDescription(0, "state", "service assembly state to list");
639: ph.setDescription(1, "componentName", "component name");
640: ph.setDescription(2, "serviceAssemblyName",
641: "service assembly name");
642:
643: return OperationInfoHelper.join(super.getOperationInfos(),
644: helper.getOperationInfos());
645: }
646:
647: }
|