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