PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/web/src/main/java/org/jboss/as/web/WebVirtualHostService.java

#
Java | 218 lines | 165 code | 24 blank | 29 comment | 28 complexity | 36eb75deb243332356d4954a0038b354 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2010, 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;
  23. import org.apache.catalina.Container;
  24. import org.apache.catalina.Valve;
  25. import org.apache.catalina.authenticator.SingleSignOn;
  26. import org.apache.catalina.core.StandardHost;
  27. import org.apache.catalina.valves.AccessLogValve;
  28. import org.apache.catalina.valves.ExtendedAccessLogValve;
  29. import org.jboss.as.clustering.web.sso.SSOClusterManager;
  30. import org.jboss.as.web.sso.ClusteredSingleSignOn;
  31. import org.jboss.dmr.ModelNode;
  32. import org.jboss.msc.service.Service;
  33. import org.jboss.msc.service.StartContext;
  34. import org.jboss.msc.service.StartException;
  35. import org.jboss.msc.service.StopContext;
  36. import org.jboss.msc.value.InjectedValue;
  37. import org.jboss.web.rewrite.RewriteValve;
  38. /**
  39. * Service creating and registering a virtual host.
  40. *
  41. * @author Emanuel Muckenhuber
  42. */
  43. public class WebVirtualHostService implements Service<VirtualHost> {
  44. private final String name;
  45. private final String[] aliases;
  46. private String defaultWebModule;
  47. private boolean hasWelcomeRoot;
  48. private ModelNode accessLog;
  49. private ModelNode rewrite;
  50. private ModelNode sso;
  51. private final InjectedValue<String> tempPathInjector = new InjectedValue<String>();
  52. private final InjectedValue<String> accessLogPathInjector = new InjectedValue<String>();
  53. private final InjectedValue<WebServer> webServer = new InjectedValue<WebServer>();
  54. private final InjectedValue<SSOClusterManager> ssoManager = new InjectedValue<SSOClusterManager>();
  55. private VirtualHost host;
  56. public WebVirtualHostService(String name, String[] aliases, boolean hasWelcomeRoot) {
  57. this.name = name;
  58. this.aliases = aliases;
  59. this.hasWelcomeRoot = hasWelcomeRoot;
  60. }
  61. /** {@inheritDoc} */
  62. public synchronized void start(StartContext context) throws StartException {
  63. final StandardHost host = new StandardHost();
  64. host.setAppBase(tempPathInjector.getValue());
  65. host.setName(name);
  66. for(final String alias : aliases) {
  67. host.addAlias(alias);
  68. }
  69. if(accessLog != null) {
  70. host.addValve(createAccessLogValve(host, accessLogPathInjector.getValue(), accessLog));
  71. }
  72. if(rewrite != null) {
  73. host.addValve(createRewriteValve(host, rewrite));
  74. }
  75. if(sso != null) {
  76. host.addValve(createSsoValve(host, sso));
  77. }
  78. if (defaultWebModule != null) {
  79. host.setDefaultWebapp(defaultWebModule);
  80. }
  81. try {
  82. final WebServer server = webServer.getValue();
  83. server.addHost(host);
  84. } catch(Exception e) {
  85. throw new StartException(e);
  86. }
  87. this.host = new VirtualHost(host, hasWelcomeRoot);
  88. }
  89. /** {@inheritDoc} */
  90. public synchronized void stop(StopContext context) {
  91. final VirtualHost host = this.host;
  92. this.host = null;
  93. final WebServer server = webServer.getValue();
  94. server.removeHost(host.getHost());
  95. }
  96. /** {@inheritDoc} */
  97. public synchronized VirtualHost getValue() throws IllegalStateException {
  98. final VirtualHost host = this.host;
  99. if(host == null) {
  100. throw new IllegalStateException();
  101. }
  102. return host;
  103. }
  104. void setAccessLog(final ModelNode accessLog) {
  105. this.accessLog = accessLog;
  106. }
  107. void setRewrite(ModelNode rewrite) {
  108. this.rewrite = rewrite;
  109. }
  110. void setSso(final ModelNode sso) {
  111. this.sso = sso;
  112. }
  113. protected String getDefaultWebModule() {
  114. return defaultWebModule;
  115. }
  116. protected void setDefaultWebModule(String defaultWebModule) {
  117. this.defaultWebModule = defaultWebModule;
  118. }
  119. public InjectedValue<String> getAccessLogPathInjector() {
  120. return accessLogPathInjector;
  121. }
  122. public InjectedValue<String> getTempPathInjector() {
  123. return tempPathInjector;
  124. }
  125. public InjectedValue<WebServer> getWebServer() {
  126. return webServer;
  127. }
  128. public InjectedValue<SSOClusterManager> getSSOClusterManager() {
  129. return ssoManager;
  130. }
  131. static Valve createAccessLogValve(final Container container, final String logDirectory, final ModelNode element) {
  132. boolean extended = false;
  133. if (element.hasDefined(Constants.EXTENDED)) {
  134. extended = element.get(Constants.EXTENDED).asBoolean();
  135. }
  136. final AccessLogValve log;
  137. if (extended) {
  138. log = new ExtendedAccessLogValve();
  139. } else {
  140. log = new AccessLogValve();
  141. }
  142. log.setDirectory(logDirectory);
  143. if (element.hasDefined(Constants.RESOLVE_HOSTS)) log.setResolveHosts(element.get(Constants.RESOLVE_HOSTS).asBoolean());
  144. if (element.hasDefined(Constants.ROTATE)) log.setRotatable(element.get(Constants.ROTATE).asBoolean());
  145. if (element.hasDefined(Constants.PATTERN)) {
  146. log.setPattern(element.get(Constants.PATTERN).asString());
  147. } else {
  148. log.setPattern("common");
  149. }
  150. if (element.hasDefined(Constants.PREFIX)) log.setPrefix(element.get(Constants.PREFIX).asString());
  151. return log;
  152. }
  153. static Valve createRewriteValve(final Container container, final ModelNode element) throws StartException {
  154. final RewriteValve rewriteValve = new RewriteValve();
  155. rewriteValve.setContainer(container);
  156. StringBuffer configuration = new StringBuffer();
  157. for (final ModelNode rewriteElement : element.asList()) {
  158. final ModelNode rewrite = rewriteElement.asProperty().getValue();
  159. if (rewrite.has(Constants.CONDITION)) {
  160. for (final ModelNode conditionElement : rewrite.get(Constants.CONDITION).asList()) {
  161. final ModelNode condition = conditionElement.asProperty().getValue();
  162. configuration.append("RewriteCond ")
  163. .append(condition.get(Constants.TEST).asString())
  164. .append(" ").append(condition.get(Constants.PATTERN).asString());
  165. if (condition.hasDefined(Constants.FLAGS)) {
  166. configuration.append(" [").append(condition.get(Constants.FLAGS).asString()).append("]\r\n");
  167. } else {
  168. configuration.append("\r\n");
  169. }
  170. }
  171. }
  172. configuration.append("RewriteRule ")
  173. .append(rewrite.get(Constants.PATTERN).asString())
  174. .append(" ").append(rewrite.get(Constants.SUBSTITUTION).asString());
  175. if (rewrite.hasDefined(Constants.FLAGS)) {
  176. configuration.append(" [").append(rewrite.get(Constants.FLAGS).asString()).append("]\r\n");
  177. } else {
  178. configuration.append("\r\n");
  179. }
  180. }
  181. try {
  182. rewriteValve.setConfiguration(configuration.toString());
  183. } catch(Exception e) {
  184. throw new StartException(e);
  185. }
  186. return rewriteValve;
  187. }
  188. Valve createSsoValve(final Container container, final ModelNode element) throws StartException {
  189. final SingleSignOn ssoValve = element.hasDefined(Constants.CACHE_CONTAINER) ? new ClusteredSingleSignOn(this.ssoManager.getValue()) : new SingleSignOn();
  190. if (element.hasDefined(Constants.DOMAIN)) ssoValve.setCookieDomain(element.get(Constants.DOMAIN).asString());
  191. if (element.hasDefined(Constants.REAUTHENTICATE)) ssoValve.setRequireReauthentication(element.get(Constants.REAUTHENTICATE).asBoolean());
  192. return ssoValve;
  193. }
  194. }