PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/osgi/service/src/main/java/org/jboss/as/osgi/parser/BundleRuntimeHandler.java

#
Java | 186 lines | 140 code | 22 blank | 24 comment | 32 complexity | c3441e215bd6278f2b33ef9a8c9afd13 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a 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.as.osgi.parser;
  23. import java.util.Arrays;
  24. import java.util.EnumSet;
  25. import java.util.Locale;
  26. import java.util.ResourceBundle;
  27. import org.jboss.as.controller.AbstractRuntimeOnlyHandler;
  28. import org.jboss.as.controller.OperationContext;
  29. import org.jboss.as.controller.OperationFailedException;
  30. import org.jboss.as.controller.PathAddress;
  31. import org.jboss.as.controller.descriptions.DescriptionProvider;
  32. import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
  33. import org.jboss.as.controller.descriptions.common.CommonDescriptions;
  34. import org.jboss.as.controller.registry.AttributeAccess;
  35. import org.jboss.as.controller.registry.ManagementResourceRegistration;
  36. import org.jboss.as.controller.registry.OperationEntry;
  37. import org.jboss.as.osgi.OSGiMessages;
  38. import org.jboss.dmr.ModelNode;
  39. import org.jboss.msc.service.ServiceController;
  40. import org.jboss.osgi.framework.Services;
  41. import org.osgi.framework.Bundle;
  42. import org.osgi.framework.BundleContext;
  43. import org.osgi.framework.BundleException;
  44. import org.osgi.framework.Constants;
  45. import org.osgi.framework.ServiceReference;
  46. import org.osgi.service.startlevel.StartLevel;
  47. /**
  48. * @author David Bosschaert
  49. */
  50. public class BundleRuntimeHandler extends AbstractRuntimeOnlyHandler {
  51. static final BundleRuntimeHandler INSTANCE = new BundleRuntimeHandler();
  52. static final String [] ATTRIBUTES = { ModelConstants.ID, ModelConstants.STARTLEVEL,
  53. ModelConstants.STATE, ModelConstants.SYMBOLIC_NAME, ModelConstants.TYPE, ModelConstants.VERSION };
  54. static final String START_OPERATION = "start";
  55. static final String STOP_OPERATION = "stop";
  56. static final String [] OPERATIONS = { START_OPERATION, STOP_OPERATION };
  57. private BundleRuntimeHandler() {
  58. }
  59. void register(ManagementResourceRegistration registry) {
  60. for (String attr : ATTRIBUTES) {
  61. registry.registerReadOnlyAttribute(attr, this, AttributeAccess.Storage.RUNTIME);
  62. }
  63. for (final String op : OPERATIONS) {
  64. registry.registerOperationHandler(op, this, new DescriptionProvider() {
  65. @Override
  66. public ModelNode getModelDescription(Locale locale) {
  67. ResourceBundle resourceBundle = OSGiSubsystemProviders.getResourceBundle(locale);
  68. return CommonDescriptions.getDescriptionOnlyOperation(resourceBundle, op, ModelConstants.BUNDLE);
  69. }
  70. }, EnumSet.of(OperationEntry.Flag.RESTART_NONE));
  71. }
  72. }
  73. @Override
  74. protected void executeRuntimeStep(OperationContext context, ModelNode operation) throws OperationFailedException {
  75. final String operationName = operation.require(ModelDescriptionConstants.OP).asString();
  76. if (ModelDescriptionConstants.READ_ATTRIBUTE_OPERATION.equals(operationName)) {
  77. handleReadAttribute(context, operation);
  78. } else if (Arrays.asList(OPERATIONS).contains(operationName)) {
  79. handleOperation(operationName, context, operation);
  80. }
  81. }
  82. private void handleReadAttribute(OperationContext context, ModelNode operation) {
  83. String name = operation.require(ModelDescriptionConstants.NAME).asString();
  84. Long id = Long.parseLong(PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR)).getLastElement().getValue());
  85. BundleContext bc = getBundleContext(context);
  86. Bundle bundle = bc.getBundle(id);
  87. if (ModelConstants.ID.equals(name)) {
  88. context.getResult().set(id);
  89. } else if (ModelConstants.STARTLEVEL.equals(name)) {
  90. Integer startLevel = getStartLevel(bc, bundle);
  91. if (startLevel != null) {
  92. context.getResult().set(startLevel);
  93. } else {
  94. context.getFailureDescription().set(OSGiMessages.MESSAGES.serviceNotAvailable());
  95. }
  96. } else if (ModelConstants.STATE.equals(name)) {
  97. context.getResult().set(getBundleState(bundle));
  98. } else if (ModelConstants.SYMBOLIC_NAME.equals(name)) {
  99. context.getResult().set(bundle.getSymbolicName());
  100. } else if (ModelConstants.TYPE.equals(name)) {
  101. if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null) {
  102. context.getResult().set(ModelConstants.FRAGMENT);
  103. } else {
  104. context.getResult().set(ModelConstants.BUNDLE);
  105. }
  106. } else if (ModelConstants.VERSION.equals(name)) {
  107. context.getResult().set(bundle.getVersion().toString());
  108. }
  109. context.completeStep();
  110. }
  111. private void handleOperation(String operationName, OperationContext context, ModelNode operation) {
  112. Long id = Long.parseLong(PathAddress.pathAddress(operation.require(ModelDescriptionConstants.OP_ADDR)).getLastElement().getValue());
  113. BundleContext bc = getBundleContext(context);
  114. Bundle bundle = bc.getBundle(id);
  115. try {
  116. if (START_OPERATION.equals(operationName)) {
  117. bundle.start();
  118. } else if (STOP_OPERATION.equals(operationName)) {
  119. bundle.stop();
  120. }
  121. } catch (BundleException e) {
  122. context.getFailureDescription().set(e.getLocalizedMessage());
  123. }
  124. context.completeStep();
  125. }
  126. static String getBundleState(Bundle bundle) {
  127. switch (bundle.getState()) {
  128. case Bundle.UNINSTALLED:
  129. return "UNINSTALLED";
  130. case Bundle.INSTALLED:
  131. return "INSTALLED";
  132. case Bundle.RESOLVED:
  133. return "RESOLVED";
  134. case Bundle.STARTING:
  135. return "STARTING";
  136. case Bundle.STOPPING:
  137. return "STOPPING";
  138. case Bundle.ACTIVE:
  139. return "ACTIVE";
  140. }
  141. return null;
  142. }
  143. private BundleContext getBundleContext(OperationContext context) {
  144. ServiceController<?> sbs = context.getServiceRegistry(false).getService(Services.SYSTEM_BUNDLE);
  145. if (sbs == null) {
  146. return null;
  147. }
  148. Bundle systemBundle = Bundle.class.cast(sbs.getValue());
  149. return systemBundle.getBundleContext();
  150. }
  151. private Integer getStartLevel(BundleContext bc, Bundle b) {
  152. ServiceReference sref = bc.getServiceReference(StartLevel.class.getName());
  153. if (sref == null)
  154. return null;
  155. try {
  156. Object sls = bc.getService(sref);
  157. if (sls instanceof StartLevel == false)
  158. return null;
  159. return ((StartLevel) sls).getBundleStartLevel(b);
  160. } finally {
  161. bc.ungetService(sref);
  162. }
  163. }
  164. }