PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/webservices/server-integration/src/main/java/org/jboss/as/webservices/publish/EndpointPublisherImpl.java

#
Java | 270 lines | 212 code | 24 blank | 34 comment | 18 complexity | 15a003bbeee1917a56b62052fd3bffab 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.webservices.publish;
  23. import static org.jboss.as.webservices.WSMessages.MESSAGES;
  24. import java.io.File;
  25. import java.lang.reflect.InvocationTargetException;
  26. import java.util.List;
  27. import java.util.Map;
  28. import javax.naming.NamingException;
  29. import org.apache.catalina.Container;
  30. import org.apache.catalina.Host;
  31. import org.apache.catalina.LifecycleException;
  32. import org.apache.catalina.Loader;
  33. import org.apache.catalina.Wrapper;
  34. import org.apache.catalina.core.StandardContext;
  35. import org.apache.catalina.startup.ContextConfig;
  36. import org.apache.tomcat.InstanceManager;
  37. import org.jboss.as.web.deployment.WebCtxLoader;
  38. import org.jboss.as.webservices.deployers.deployment.DeploymentAspectsProvider;
  39. import org.jboss.as.webservices.deployers.deployment.WSDeploymentBuilder;
  40. import org.jboss.as.webservices.service.ServerConfigService;
  41. import org.jboss.as.webservices.util.WSAttachmentKeys;
  42. import org.jboss.as.webservices.util.WSServices;
  43. import org.jboss.metadata.javaee.spec.ParamValueMetaData;
  44. import org.jboss.metadata.web.jboss.JBossServletMetaData;
  45. import org.jboss.metadata.web.jboss.JBossWebMetaData;
  46. import org.jboss.metadata.web.spec.ServletMappingMetaData;
  47. import org.jboss.msc.service.ServiceTarget;
  48. import org.jboss.ws.common.deployment.DeploymentAspectManagerImpl;
  49. import org.jboss.wsf.spi.SPIProvider;
  50. import org.jboss.wsf.spi.SPIProviderResolver;
  51. import org.jboss.wsf.spi.classloading.ClassLoaderProvider;
  52. import org.jboss.wsf.spi.deployment.Deployment;
  53. import org.jboss.wsf.spi.deployment.DeploymentAspect;
  54. import org.jboss.wsf.spi.deployment.DeploymentAspectManager;
  55. import org.jboss.wsf.spi.deployment.Endpoint;
  56. import org.jboss.wsf.spi.deployment.WSFServlet;
  57. import org.jboss.wsf.spi.management.EndpointRegistry;
  58. import org.jboss.wsf.spi.management.EndpointRegistryFactory;
  59. import org.jboss.wsf.spi.metadata.webservices.WebservicesMetaData;
  60. import org.jboss.wsf.spi.publish.Context;
  61. import org.jboss.wsf.spi.publish.EndpointPublisher;
  62. /**
  63. * WS endpoint publisher, allows for publishing a WS endpoint on AS 7
  64. *
  65. * @author alessio.soldano@jboss.com
  66. * @since 12-Jul-2011
  67. */
  68. public final class EndpointPublisherImpl implements EndpointPublisher {
  69. private Host host;
  70. public EndpointPublisherImpl(Host host) {
  71. this.host = host;
  72. }
  73. @Override
  74. public Context publish(String context, ClassLoader loader, Map<String, String> urlPatternToClassNameMap) throws Exception {
  75. return publish(null, context, loader, urlPatternToClassNameMap, null);
  76. }
  77. public Context publish(String context, ClassLoader loader, Map<String, String> urlPatternToClassNameMap, WebservicesMetaData metadata) throws Exception {
  78. return publish(null, context, loader, urlPatternToClassNameMap, metadata);
  79. }
  80. public Context publish(ServiceTarget target, String context, ClassLoader loader, Map<String, String> urlPatternToClassNameMap, WebservicesMetaData metadata) throws Exception {
  81. WSEndpointDeploymentUnit unit = new WSEndpointDeploymentUnit(loader, context, urlPatternToClassNameMap, metadata);
  82. return new Context(context, publish(target, unit));
  83. }
  84. /**
  85. * Publishes the endpoints declared to the provided WSEndpointDeploymentUnit
  86. */
  87. public List<Endpoint> publish(ServiceTarget target, WSEndpointDeploymentUnit unit) throws Exception {
  88. List<DeploymentAspect> aspects = DeploymentAspectsProvider.getSortedDeploymentAspects();
  89. ClassLoader origClassLoader = SecurityActions.getContextClassLoader();
  90. Deployment dep = null;
  91. try {
  92. SecurityActions.setContextClassLoader(ClassLoaderProvider.getDefaultProvider().getServerIntegrationClassLoader());
  93. WSDeploymentBuilder.getInstance().build(unit);
  94. dep = unit.getAttachment(WSAttachmentKeys.DEPLOYMENT_KEY);
  95. dep.addAttachment(ServiceTarget.class, target);
  96. DeploymentAspectManager dam = new DeploymentAspectManagerImpl();
  97. dam.setDeploymentAspects(aspects);
  98. dam.deploy(dep);
  99. // TODO: [JBWS-3426] fix this. START workaround
  100. if (target == null) {
  101. SPIProvider spiProvider = SPIProviderResolver.getInstance().getProvider();
  102. EndpointRegistryFactory factory = spiProvider.getSPI(EndpointRegistryFactory.class);
  103. EndpointRegistry registry = factory.getEndpointRegistry();
  104. for (final Endpoint endpoint : dep.getService().getEndpoints()) {
  105. registry.register(endpoint);
  106. }
  107. }
  108. // END workaround
  109. } finally {
  110. if (dep != null) {
  111. dep.removeAttachment(ServiceTarget.class);
  112. }
  113. SecurityActions.setContextClassLoader(origClassLoader);
  114. }
  115. Deployment deployment = unit.getAttachment(WSAttachmentKeys.DEPLOYMENT_KEY);
  116. deployment.addAttachment(StandardContext.class, startWebApp(host, unit)); //TODO simplify and use findChild later in destroy()/stopWebApp()
  117. return deployment.getService().getEndpoints();
  118. }
  119. private static StandardContext startWebApp(Host host, WSEndpointDeploymentUnit unit) throws Exception {
  120. StandardContext context = new StandardContext();
  121. try {
  122. JBossWebMetaData jbwebMD = unit.getAttachment(WSAttachmentKeys.JBOSSWEB_METADATA_KEY);
  123. context.setPath(jbwebMD.getContextRoot());
  124. context.addLifecycleListener(new ContextConfig());
  125. ServerConfigService config = (ServerConfigService)unit.getServiceRegistry().getService(WSServices.CONFIG_SERVICE).getService();
  126. File docBase = new File(config.getValue().getServerTempDir(), jbwebMD.getContextRoot());
  127. if (!docBase.exists()) {
  128. docBase.mkdirs();
  129. }
  130. context.setDocBase(docBase.getPath());
  131. final Loader loader = new WebCtxLoader(unit.getAttachment(WSAttachmentKeys.CLASSLOADER_KEY));
  132. loader.setContainer(host);
  133. context.setLoader(loader);
  134. context.setInstanceManager(new LocalInstanceManager());
  135. addServlets(jbwebMD, context);
  136. host.addChild(context);
  137. context.create();
  138. } catch (Exception e) {
  139. throw MESSAGES.createContextPhaseFailed(e);
  140. }
  141. try {
  142. context.start();
  143. } catch (LifecycleException e) {
  144. throw MESSAGES.startContextPhaseFailed(e);
  145. }
  146. return context;
  147. }
  148. private static void addServlets(JBossWebMetaData jbwebMD, StandardContext context) {
  149. for (JBossServletMetaData smd : jbwebMD.getServlets()) {
  150. final String sc = smd.getServletClass();
  151. if (sc.equals(WSFServlet.class.getName())) {
  152. final String servletName = smd.getServletName();
  153. List<ParamValueMetaData> params = smd.getInitParam();
  154. List<String> urlPatterns = null;
  155. for (ServletMappingMetaData smmd : jbwebMD.getServletMappings()) {
  156. if (smmd.getServletName().equals(servletName)) {
  157. urlPatterns = smmd.getUrlPatterns();
  158. break;
  159. }
  160. }
  161. WSFServlet wsfs = new WSFServlet();
  162. Wrapper wsfsWrapper = context.createWrapper();
  163. wsfsWrapper.setName(servletName);
  164. wsfsWrapper.setServlet(wsfs);
  165. wsfsWrapper.setServletClass(WSFServlet.class.getName());
  166. for (ParamValueMetaData param : params) {
  167. wsfsWrapper.addInitParameter(param.getParamName(), param.getParamValue());
  168. }
  169. context.addChild(wsfsWrapper);
  170. for (String urlPattern : urlPatterns) {
  171. context.addServletMapping(urlPattern, servletName);
  172. }
  173. }
  174. }
  175. }
  176. @Override
  177. public void destroy(Context context) throws Exception {
  178. List<Endpoint> eps = context.getEndpoints();
  179. if (eps == null || eps.isEmpty()) {
  180. return;
  181. }
  182. Deployment deployment = eps.get(0).getService().getDeployment();
  183. List<DeploymentAspect> aspects = DeploymentAspectsProvider.getSortedDeploymentAspects();
  184. try {
  185. stopWebApp(deployment.getAttachment(StandardContext.class));
  186. } finally {
  187. ClassLoader origClassLoader = SecurityActions.getContextClassLoader();
  188. try {
  189. SecurityActions.setContextClassLoader(ClassLoaderProvider.getDefaultProvider().getServerIntegrationClassLoader());
  190. final ServiceTarget target = deployment.getAttachment(ServiceTarget.class);
  191. // TODO: [JBWS-3426] fix this. START workaround
  192. if (target == null) {
  193. SPIProvider spiProvider = SPIProviderResolver.getInstance().getProvider();
  194. EndpointRegistryFactory factory = spiProvider.getSPI(EndpointRegistryFactory.class);
  195. EndpointRegistry registry = factory.getEndpointRegistry();
  196. for (final Endpoint endpoint : deployment.getService().getEndpoints()) {
  197. registry.unregister(endpoint);
  198. }
  199. }
  200. // END workaround
  201. DeploymentAspectManager dam = new DeploymentAspectManagerImpl();
  202. dam.setDeploymentAspects(aspects);
  203. dam.undeploy(deployment);
  204. } finally {
  205. SecurityActions.setContextClassLoader(origClassLoader);
  206. }
  207. }
  208. }
  209. private static void stopWebApp(StandardContext context) throws Exception {
  210. try {
  211. Container container = context.getParent();
  212. container.removeChild(context);
  213. context.stop();
  214. } catch (LifecycleException e) {
  215. throw MESSAGES.stopContextPhaseFailed(e);
  216. }
  217. try {
  218. context.destroy();
  219. } catch (Exception e) {
  220. throw MESSAGES.destroyContextPhaseFailed(e);
  221. }
  222. }
  223. private static class LocalInstanceManager implements InstanceManager {
  224. LocalInstanceManager() {
  225. }
  226. @Override
  227. public Object newInstance(String className) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException {
  228. return Class.forName(className).newInstance();
  229. }
  230. @Override
  231. public Object newInstance(String fqcn, ClassLoader classLoader) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException, ClassNotFoundException {
  232. return Class.forName(fqcn, false, classLoader).newInstance();
  233. }
  234. @Override
  235. public Object newInstance(Class<?> c) throws IllegalAccessException, InvocationTargetException, NamingException, InstantiationException {
  236. return c.newInstance();
  237. }
  238. @Override
  239. public void newInstance(Object o) throws IllegalAccessException, InvocationTargetException, NamingException {
  240. throw new IllegalStateException();
  241. }
  242. @Override
  243. public void destroyInstance(Object o) throws IllegalAccessException, InvocationTargetException {
  244. }
  245. }
  246. }