001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.runtime.test.runner;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.URL;
025
026import org.nuxeo.common.utils.FileUtils;
027import org.nuxeo.runtime.test.WorkingDirectoryConfigurator;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032@Deploy("org.nuxeo.runtime.jetty")
033@Features(RuntimeFeature.class)
034public class JettyFeature extends SimpleFeature implements WorkingDirectoryConfigurator {
035
036    @Override
037    public void initialize(FeaturesRunner runner) throws Exception {
038        Jetty jetty = runner.getConfig(Jetty.class);
039        if (jetty == null) {
040            jetty = Defaults.of(Jetty.class);
041        }
042        configureJetty(jetty);
043
044        runner.getFeature(RuntimeFeature.class).getHarness().addWorkingDirectoryConfigurator(this);
045    }
046
047    protected void configureJetty(Jetty jetty) {
048        int p = jetty.port();
049        try {
050            String s = System.getenv("JETTY_PORT");
051            if (s != null) {
052                p = Integer.parseInt(s);
053            }
054        } catch (Exception e) {
055            // do nothing ; the jetty.port
056        }
057        if (p > 0) {
058            System.setProperty("jetty.port", Integer.toString(p));
059        }
060
061        String host = System.getenv("JETTY_HOST");
062        if (host == null) {
063            host = jetty.host();
064        }
065        if (host.length() > 0) {
066            System.setProperty("jetty.host", host);
067        }
068
069        String config = System.getenv("JETTY_CONFIG");
070        if (config == null) {
071            config = jetty.config();
072        }
073        if (config.length() > 0) {
074            System.setProperty("org.nuxeo.jetty.config", config);
075        }
076
077        System.setProperty("org.nuxeo.jetty.propagateNaming", Boolean.toString(jetty.propagateNaming()));
078    }
079
080    @Override
081    public void configure(RuntimeHarness harness, File workingDir) throws IOException {
082        File dest = new File(workingDir, "config");
083        dest.mkdirs();
084
085        InputStream in = getResource("jetty/default-web.xml").openStream();
086        dest = new File(workingDir + "/config", "default-web.xml");
087        try {
088            FileUtils.copyToFile(in, dest);
089        } finally {
090            in.close();
091        }
092
093        in = getResource("jetty/jetty.xml").openStream();
094        dest = new File(workingDir + "/config", "jetty.xml");
095        try {
096            FileUtils.copyToFile(in, dest);
097        } finally {
098            in.close();
099        }
100    }
101
102    private static URL getResource(String resource) {
103        // return
104        // Thread.currentThread().getContextClassLoader().getResource(resource);
105        return Jetty.class.getClassLoader().getResource(resource);
106    }
107
108}