PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ee/src/main/java/org/jboss/as/ee/subsystem/EEJndiViewExtension.java

#
Java | 179 lines | 132 code | 22 blank | 25 comment | 25 complexity | b5ce6f06bfc896c116fd2e4465ed57fc 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.ee.subsystem;
  23. import org.jboss.as.controller.OperationFailedException;
  24. import org.jboss.as.controller.registry.Resource;
  25. import org.jboss.as.ee.component.ComponentDescription;
  26. import org.jboss.as.ee.component.EEModuleDescription;
  27. import org.jboss.as.ee.structure.DeploymentType;
  28. import org.jboss.as.ee.structure.DeploymentTypeMarker;
  29. import org.jboss.as.naming.NamingContext;
  30. import org.jboss.as.naming.NamingStore;
  31. import org.jboss.as.naming.deployment.ContextNames;
  32. import org.jboss.as.naming.management.JndiViewExtension;
  33. import org.jboss.as.naming.management.JndiViewExtensionContext;
  34. import org.jboss.as.naming.management.JndiViewExtensionRegistry;
  35. import org.jboss.as.server.deployment.DeploymentUnit;
  36. import org.jboss.as.server.deployment.Services;
  37. import org.jboss.as.server.deployment.SubDeploymentMarker;
  38. import org.jboss.as.server.deployment.module.ResourceRoot;
  39. import org.jboss.dmr.ModelNode;
  40. import org.jboss.msc.inject.Injector;
  41. import org.jboss.msc.service.Service;
  42. import org.jboss.msc.service.ServiceController;
  43. import org.jboss.msc.service.ServiceName;
  44. import org.jboss.msc.service.ServiceRegistry;
  45. import org.jboss.msc.service.StartContext;
  46. import org.jboss.msc.service.StartException;
  47. import org.jboss.msc.service.StopContext;
  48. import org.jboss.msc.value.InjectedValue;
  49. import javax.naming.NamingException;
  50. import java.util.Collection;
  51. import java.util.List;
  52. import java.util.Set;
  53. import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.DEPLOYMENT;
  54. import static org.jboss.as.ee.EeMessages.MESSAGES;
  55. /**
  56. * @author John Bailey
  57. */
  58. public class EEJndiViewExtension implements JndiViewExtension, Service<Void> {
  59. static final ServiceName SERVICE_NAME = ServiceName.JBOSS.append("jndi-view", "extension", "ee");
  60. private final InjectedValue<JndiViewExtensionRegistry> registry = new InjectedValue<JndiViewExtensionRegistry>();
  61. public synchronized void start(StartContext startContext) throws StartException {
  62. registry.getValue().addExtension(this);
  63. }
  64. public synchronized void stop(StopContext stopContext) {
  65. registry.getValue().removeExtension(this);
  66. }
  67. public Void getValue() throws IllegalStateException, IllegalArgumentException {
  68. return null;
  69. }
  70. public void execute(final JndiViewExtensionContext context) throws OperationFailedException {
  71. final ModelNode applicationsNode = context.getResult().get("applications");
  72. final ServiceRegistry serviceRegistry = context.getOperationContext().getServiceRegistry(false);
  73. final Set<Resource.ResourceEntry> deploymentResource = context.getOperationContext().getRootResource().getChildren(DEPLOYMENT);
  74. for (final Resource.ResourceEntry entry : deploymentResource) {
  75. final ServiceController<?> deploymentUnitServiceController = serviceRegistry.getService(ServiceName.JBOSS.append("deployment", "unit", entry.getName()));
  76. if (deploymentUnitServiceController != null) {
  77. final ModelNode deploymentNode = applicationsNode.get(entry.getName());
  78. final DeploymentUnit deploymentUnit = DeploymentUnit.class.cast(deploymentUnitServiceController.getValue());
  79. final String appName = cleanName(deploymentUnit.getName());
  80. final ServiceName appContextName = ContextNames.contextServiceNameOfApplication(appName);
  81. final ServiceController<?> appContextController = serviceRegistry.getService(appContextName);
  82. if (appContextController != null) {
  83. final NamingStore appStore = NamingStore.class.cast(appContextController.getValue());
  84. try {
  85. context.addEntries(deploymentNode.get("java:app"), new NamingContext(appStore, null));
  86. } catch (NamingException e) {
  87. throw new OperationFailedException(e, new ModelNode().set(MESSAGES.failedToRead("java:app", appName)));
  88. }
  89. }
  90. if (DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
  91. final List<ResourceRoot> roots = deploymentUnit.getAttachmentList(org.jboss.as.server.deployment.Attachments.RESOURCE_ROOTS);
  92. if(roots != null) for(ResourceRoot root : roots) {
  93. if(SubDeploymentMarker.isSubDeployment(root)) {
  94. final ResourceRoot parentRoot = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.DEPLOYMENT_ROOT);
  95. final String relativePath = root.getRoot().getPathNameRelativeTo(parentRoot.getRoot());
  96. final ServiceName subDeploymentServiceName = Services.deploymentUnitName(deploymentUnit.getName(), relativePath);
  97. final ServiceController<?> subDeploymentController = serviceRegistry.getService(subDeploymentServiceName);
  98. if(subDeploymentController != null) {
  99. final DeploymentUnit subDeploymentUnit = DeploymentUnit.class.cast(subDeploymentController.getValue());
  100. handleModule(context, subDeploymentUnit, deploymentNode.get("modules"), serviceRegistry);
  101. }
  102. }
  103. }
  104. } else {
  105. handleModule(context, deploymentUnit, deploymentNode.get("modules"), serviceRegistry);
  106. }
  107. }
  108. }
  109. }
  110. private void handleModule(final JndiViewExtensionContext context, final DeploymentUnit deploymentUnit, final ModelNode modulesNode, final ServiceRegistry serviceRegistry) throws OperationFailedException {
  111. final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
  112. // If it isn't a EE module, just return
  113. if (moduleDescription == null) {
  114. return;
  115. }
  116. final String appName = moduleDescription.getApplicationName();
  117. final String moduleName = moduleDescription.getModuleName();
  118. final ModelNode moduleNode = modulesNode.get(moduleDescription.getModuleName());
  119. final ServiceName moduleContextName = ContextNames.contextServiceNameOfModule(appName, moduleName);
  120. final ServiceController<?> moduleContextController = serviceRegistry.getService(moduleContextName);
  121. if (moduleContextController != null) {
  122. final NamingStore moduleStore = NamingStore.class.cast(moduleContextController.getValue());
  123. try {
  124. context.addEntries(moduleNode.get("java:module"), new NamingContext(moduleStore, null));
  125. } catch (NamingException e) {
  126. throw new OperationFailedException(e, new ModelNode().set(MESSAGES.failedToRead("java:module", appName, moduleName)));
  127. }
  128. final Collection<ComponentDescription> componentDescriptions = moduleDescription.getComponentDescriptions();
  129. for (ComponentDescription componentDescription : componentDescriptions) {
  130. final String componentName = componentDescription.getComponentName();
  131. final ServiceName compContextServiceName = ContextNames.contextServiceNameOfComponent(appName, moduleName, componentName);
  132. final ServiceController<?> compContextController = serviceRegistry.getService(compContextServiceName);
  133. if (compContextController != null) {
  134. final ModelNode componentNode = moduleNode.get("components").get(componentName);
  135. final NamingStore compStore = NamingStore.class.cast(compContextController.getValue());
  136. try {
  137. context.addEntries(componentNode.get("java:comp"), new NamingContext(compStore, null));
  138. } catch (NamingException e) {
  139. throw new OperationFailedException(e, new ModelNode().set(MESSAGES.failedToRead("java:comp", appName, moduleName, componentName)));
  140. }
  141. }
  142. }
  143. }
  144. }
  145. private String cleanName(final String name) {
  146. final String cleaned;
  147. if (name.endsWith(".war") || name.endsWith(".jar") || name.endsWith(".ear") || name.endsWith(".rar")) {
  148. cleaned = name.substring(0, name.length() - 4);
  149. } else {
  150. cleaned = name;
  151. }
  152. return cleaned;
  153. }
  154. public Injector<JndiViewExtensionRegistry> getRegistryInjector() {
  155. return registry;
  156. }
  157. }