1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Properties ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.management.LifeCycleMBean; 28 import javax.management.JMException ; 29 import javax.management.MBeanOperationInfo ; 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 41 public String getDescription() { 42 return "Admin Commands Service"; 43 } 44 45 protected Class getServiceMBean() { 46 return AdminCommandsServiceMBean.class; 47 } 48 49 55 public String installComponent(String file) throws Exception { 56 return installComponent(file, null); 57 } 58 59 68 public String installComponent(String file, Properties props) 69 throws Exception { 70 try { 71 container.getInstallationService().install(file, props, false); 72 return ManagementSupport.createSuccessMessage("installComponent", 73 file); 74 } catch (Exception e) { 75 throw ManagementSupport.failure("installComponent", file, e); 76 } 77 } 78 79 86 public String uninstallComponent(String name) throws Exception { 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 111 public String installSharedLibrary(String file) throws Exception { 112 return container.getInstallationService().installSharedLibrary(file); 113 } 114 115 121 public String uninstallSharedLibrary(String name) throws Exception { 122 SharedLibrary sl = container.getRegistry().getSharedLibrary(name); 124 if (sl == null) { 125 throw ManagementSupport.failure("uninstallSharedLibrary", 126 "Shared library '" + name + "' is not installed."); 127 } 128 Collection components = container.getRegistry().getComponents(); 130 for (Iterator iter = components.iterator(); iter.hasNext();) { 131 ComponentMBeanImpl comp = (ComponentMBeanImpl) iter.next(); 132 if (!comp.isShutDown()) { 133 String [] 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 162 public String startComponent(String name) throws Exception { 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 (failure("startComponent", name, e)); 172 } 173 } 174 175 181 public String stopComponent(String name) throws Exception { 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 (failure("stopComponent", name, e)); 191 } 192 } 193 194 200 public String shutdownComponent(String name) throws Exception { 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 (failure("shutdownComponent", name, e)); 210 } 211 } 212 213 219 public String deployServiceAssembly(String file) throws Exception { 220 return container.getDeploymentService().deploy(file); 221 } 222 223 229 public String undeployServiceAssembly(String name) throws Exception { 230 return container.getDeploymentService().undeploy(name); 231 } 232 233 239 public String startServiceAssembly(String name) throws Exception { 240 return container.getDeploymentService().start(name); 241 } 242 243 249 public String stopServiceAssembly(String name) throws Exception { 250 return container.getDeploymentService().stop(name); 251 } 252 253 259 public String shutdownServiceAssembly(String name) throws Exception { 260 return container.getDeploymentService().shutDown(name); 261 } 262 263 273 public String installArchive(String location) throws Exception { 274 try { 275 container.updateExternalArchive(location); 276 return success("installArchive", location); 277 278 } catch (Exception e) { 279 throw new RuntimeException (failure("shutdownServiceAssembly", 280 location, e)); 281 } 282 } 283 284 295 public String listComponents(boolean excludeSEs, boolean excludeBCs, 296 boolean excludePojos, String requiredState, 297 String sharedLibraryName, String serviceAssemblyName) 298 throws Exception { 299 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 Collection connectors = container.getRegistry().getComponents(); 312 List components = new ArrayList (); 313 for (Iterator iter = connectors.iterator(); iter.hasNext();) { 314 ComponentMBeanImpl component = (ComponentMBeanImpl) iter.next(); 315 if (excludeSEs && component.isService()) { 317 continue; 318 } 319 if (excludeBCs && component.isBinding()) { 321 continue; 322 } 323 if (excludePojos && component.isPojo()) { 325 continue; 326 } 327 if (requiredState != null 329 && requiredState.length() > 0 330 && !requiredState.equalsIgnoreCase(component 331 .getCurrentState())) { 332 continue; 333 } 334 if (sharedLibraryName != null 337 && sharedLibraryName.length() > 0 338 && !container.getInstallationService() 339 .containsSharedLibrary(sharedLibraryName)) { 340 continue; 341 } 342 if (serviceAssemblyName != null && serviceAssemblyName.length() > 0) { 345 String [] 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 buffer = new StringBuffer (); 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 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 394 public String listSharedLibraries(String componentName, 395 String sharedLibraryName) throws Exception { 396 Collection 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 libs = container.getRegistry().getSharedLibraries(); 408 } else { 409 libs = container.getRegistry().getSharedLibraries(); 410 } 411 StringBuffer buffer = new StringBuffer (); 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 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 438 public String listServiceAssemblies(String state, String componentName, 439 String serviceAssemblyName) throws Exception { 440 String [] result = null; 441 if (null != serviceAssemblyName && serviceAssemblyName.length() > 0) { 442 result = new String [] { 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 assemblies = new ArrayList (); 451 for (int i = 0; i < result.length; i++) { 452 ServiceAssemblyLifeCycle sa = container.getRegistry() 453 .getServiceAssembly(result[i]); 454 if (sa != null) { 455 if (state != null && state.length() > 0 457 && !state.equals(sa.getCurrentState())) { 458 continue; 459 } 460 assemblies.add(sa); 461 } 462 } 463 464 StringBuffer buffer = new StringBuffer (); 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 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 failure(String task, String location, Exception 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 ) null); 506 } 507 508 public String success(String task, String 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 ) null); 514 } 515 516 public MBeanOperationInfo [] getOperationInfos() throws JMException { 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 |