PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/web/src/main/java/org/jboss/as/web/deployment/component/WebComponentProcessor.java

#
Java | 223 lines | 154 code | 17 blank | 52 comment | 59 complexity | da770451c7eea04dd11758cdc1c39b49 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.web.deployment.component;
  23. import static org.jboss.as.web.WebMessages.MESSAGES;
  24. import org.jboss.as.ee.component.Attachments;
  25. import org.jboss.as.ee.component.ComponentDescription;
  26. import org.jboss.as.ee.component.EEApplicationClasses;
  27. import org.jboss.as.ee.component.EEModuleDescription;
  28. import org.jboss.as.ee.structure.DeploymentType;
  29. import org.jboss.as.ee.structure.DeploymentTypeMarker;
  30. import org.jboss.as.server.deployment.DeploymentPhaseContext;
  31. import org.jboss.as.server.deployment.DeploymentUnit;
  32. import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
  33. import org.jboss.as.server.deployment.DeploymentUnitProcessor;
  34. import org.jboss.as.server.deployment.annotation.CompositeIndex;
  35. import org.jboss.as.web.deployment.TldsMetaData;
  36. import org.jboss.as.web.deployment.WarMetaData;
  37. import org.jboss.as.web.deployment.WebAttachments;
  38. import org.jboss.jandex.ClassInfo;
  39. import org.jboss.jandex.DotName;
  40. import org.jboss.metadata.web.spec.FilterMetaData;
  41. import org.jboss.metadata.web.spec.ListenerMetaData;
  42. import org.jboss.metadata.web.spec.ServletMetaData;
  43. import org.jboss.metadata.web.spec.TagMetaData;
  44. import org.jboss.metadata.web.spec.TldMetaData;
  45. import org.jboss.metadata.web.spec.WebCommonMetaData;
  46. import org.jboss.metadata.web.spec.WebFragmentMetaData;
  47. import org.jboss.metadata.web.spec.WebMetaData;
  48. import java.util.HashMap;
  49. import java.util.HashSet;
  50. import java.util.Map;
  51. import java.util.Set;
  52. import javax.servlet.AsyncListener;
  53. /**
  54. * Processor that figures out what type of component a servlet/listener is, and registers the appropriate metadata.
  55. * The different types are:
  56. * <ul>
  57. * <li>Managed Bean - If the servlet is annotated with the <code>ManagedBean</code> annotation</li>
  58. * <li>CDI Bean - If the servlet is deployed in a bean archive</li>
  59. * <li>EE Component - If this is an EE deployment and the servlet is not one of the above</li>
  60. * <li>Normal Servlet - If the EE subsystem is disabled</li>
  61. * </ul>
  62. * <p/>
  63. * For ManagedBean Servlets no action is necessary at this stage, as the servlet is already registered as a component.
  64. * For CDI and EE components a component definition is added to the deployment.
  65. * <p/>
  66. * For now we are just using managed bean components as servlets. We may need a custom component type in future.
  67. */
  68. public class WebComponentProcessor implements DeploymentUnitProcessor {
  69. /**
  70. * Tags in these packages do not need to be computerized
  71. */
  72. private static final String[] BUILTIN_TAGLIBS = {"org.apache.taglibs.standard", "com.sun.faces.taglib.jsf_core", "com.sun.faces.ext.taglib", "com.sun.faces.taglib.html_basic",};
  73. /**
  74. * Dotname for AsyncListener, which can be injected dynamically.
  75. */
  76. private static final DotName ASYNC_LISTENER_INTERFACE = DotName.createSimple(AsyncListener.class.getName());
  77. @Override
  78. public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
  79. final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
  80. if (!DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
  81. return;
  82. }
  83. final Map<String, ComponentDescription> componentByClass = new HashMap<String, ComponentDescription>();
  84. final Map<String, ComponentInstantiator> webComponents = new HashMap<String, ComponentInstantiator>();
  85. final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
  86. final EEApplicationClasses applicationClassesDescription = deploymentUnit.getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
  87. final CompositeIndex compositeIndex = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.COMPOSITE_ANNOTATION_INDEX);
  88. final String applicationName = deploymentUnit.getParent() == null ? deploymentUnit.getName() : deploymentUnit.getParent().getName();
  89. if (moduleDescription == null) {
  90. return; //not an ee deployment
  91. }
  92. for (ComponentDescription component : moduleDescription.getComponentDescriptions()) {
  93. componentByClass.put(component.getComponentClassName(), component);
  94. }
  95. final WarMetaData warMetaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
  96. final TldsMetaData tldsMetaData = deploymentUnit.getAttachment(TldsMetaData.ATTACHMENT_KEY);
  97. final Set<String> classes = getAllComponentClasses(deploymentUnit, compositeIndex, warMetaData, tldsMetaData);
  98. for (String clazz : classes) {
  99. if (clazz == null || clazz.trim().isEmpty()) {
  100. continue;
  101. }
  102. ComponentDescription description = componentByClass.get(clazz);
  103. if (description != null) {
  104. //for now just make sure it has a single view
  105. //this will generally be a managed bean, but it could also be an EJB
  106. //TODO: make sure the component is a managed bean
  107. if (!(description.getViews().size() == 1)) {
  108. throw MESSAGES.wrongComponentType(clazz);
  109. }
  110. ManagedBeanComponentInstantiator instantiator = new ManagedBeanComponentInstantiator(deploymentUnit, description);
  111. webComponents.put(clazz, instantiator);
  112. } else {
  113. //we do not make the standard tags into components, as there is no need
  114. if (compositeIndex.getClassByName(DotName.createSimple(clazz)) == null) {
  115. boolean found = false;
  116. for (String pack : BUILTIN_TAGLIBS) {
  117. if (clazz.startsWith(pack)) {
  118. found = true;
  119. break;
  120. }
  121. }
  122. if(found) {
  123. continue;
  124. }
  125. }
  126. description = new WebComponentDescription(clazz, clazz, moduleDescription, deploymentUnit.getServiceName(), applicationClassesDescription);
  127. moduleDescription.addComponent(description);
  128. webComponents.put(clazz, new WebComponentInstantiator(deploymentUnit, description));
  129. }
  130. }
  131. deploymentUnit.putAttachment(WebAttachments.WEB_COMPONENT_INSTANTIATORS, webComponents);
  132. }
  133. @Override
  134. public void undeploy(DeploymentUnit context) {
  135. }
  136. /**
  137. * Gets all classes that are eligible for injection etc
  138. *
  139. * @param metaData
  140. * @return
  141. */
  142. private Set<String> getAllComponentClasses(DeploymentUnit deploymentUnit, CompositeIndex index, WarMetaData metaData, TldsMetaData tldsMetaData) {
  143. final Set<String> classes = new HashSet<String>();
  144. if (metaData.getAnnotationsMetaData() != null)
  145. for (Map.Entry<String, WebMetaData> webMetaData : metaData.getAnnotationsMetaData().entrySet()) {
  146. getAllComponentClasses(webMetaData.getValue(), classes);
  147. }
  148. if (metaData.getSharedWebMetaData() != null)
  149. getAllComponentClasses(metaData.getSharedWebMetaData(), classes);
  150. if (metaData.getWebFragmentsMetaData() != null)
  151. for (Map.Entry<String, WebFragmentMetaData> webMetaData : metaData.getWebFragmentsMetaData().entrySet()) {
  152. getAllComponentClasses(webMetaData.getValue(), classes);
  153. }
  154. if (metaData.getWebMetaData() != null)
  155. getAllComponentClasses(metaData.getWebMetaData(), classes);
  156. if (tldsMetaData == null)
  157. return classes;
  158. if (tldsMetaData.getSharedTlds(deploymentUnit) != null)
  159. for (TldMetaData tldMetaData : tldsMetaData.getSharedTlds(deploymentUnit)) {
  160. getAllComponentClasses(tldMetaData, classes);
  161. }
  162. if (tldsMetaData.getTlds() != null)
  163. for (Map.Entry<String, TldMetaData> tldMetaData : tldsMetaData.getTlds().entrySet()) {
  164. getAllComponentClasses(tldMetaData.getValue(), classes);
  165. }
  166. getAllAsyncListenerClasses(index, classes);
  167. return classes;
  168. }
  169. private void getAllComponentClasses(WebCommonMetaData metaData, Set<String> classes) {
  170. if (metaData.getServlets() != null)
  171. for (ServletMetaData servlet : metaData.getServlets()) {
  172. if (servlet.getServletClass() != null) {
  173. classes.add(servlet.getServletClass());
  174. }
  175. }
  176. if (metaData.getFilters() != null)
  177. for (FilterMetaData filter : metaData.getFilters()) {
  178. classes.add(filter.getFilterClass());
  179. }
  180. if (metaData.getListeners() != null)
  181. for (ListenerMetaData listener : metaData.getListeners()) {
  182. classes.add(listener.getListenerClass());
  183. }
  184. }
  185. private void getAllComponentClasses(TldMetaData metaData, Set<String> classes) {
  186. if (metaData.getValidator() != null) {
  187. classes.add(metaData.getValidator().getValidatorClass());
  188. }
  189. if (metaData.getListeners() != null)
  190. for (ListenerMetaData listener : metaData.getListeners()) {
  191. classes.add(listener.getListenerClass());
  192. }
  193. if (metaData.getTags() != null)
  194. for (TagMetaData tag : metaData.getTags()) {
  195. classes.add(tag.getTagClass());
  196. }
  197. }
  198. private void getAllAsyncListenerClasses(CompositeIndex index, Set<String> classes) {
  199. if (index != null) {
  200. Set<ClassInfo> classInfos = index.getAllKnownImplementors(ASYNC_LISTENER_INTERFACE);
  201. for (ClassInfo classInfo : classInfos) {
  202. classes.add(classInfo.name().toString());
  203. }
  204. }
  205. }
  206. }