Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 03da6761ae21995457d399dc133b5f4d3f576efe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
//  ========================================================================
//  Copyright (c) 1995-2012 Sabre Holdings.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//


package org.eclipse.jetty.ant;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.ant.types.Connectors.Connector;
import org.eclipse.jetty.ant.utils.ServerProxy;
import org.eclipse.jetty.ant.utils.TaskLog;
import org.eclipse.jetty.ant.utils.WebApplicationProxy;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.SelectChannelConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.xml.sax.SAXException;

/**
 * A proxy class for interaction with Jetty server object. Used to have some
 * level of abstraction over standard Jetty classes.
 *
 * @author Jakub Pawlowicz
 */
public class ServerProxyImpl implements ServerProxy
{

    /** Proxied Jetty server object. */
    private Server server;

    /** Collection of context handlers (web application contexts). */
    private ContextHandlerCollection contexts;

    /** Location of jetty.xml file. */
    private File jettyXml;

    /** List of connectors. */
    private List connectors;

    /** Request logger. */
    private RequestLog requestLog;

    /** User realms. */
    private List loginServices;

    /** List of added web applications. */
    private Map webApplications = new HashMap();

    /**
     * Default constructor. Creates a new Jetty server with a standard connector
     * listening on a given port.
     *
     * @param connectors
     * @param loginServicesList
     * @param requestLog
     * @param jettyXml
     */
    public ServerProxyImpl(List connectors, List loginServicesList, RequestLog requestLog,
            File jettyXml)
    {
        server = new Server();
        server.setStopAtShutdown(true);

        this.connectors = connectors;
        this.loginServices = loginServicesList;
        this.requestLog = requestLog;
        this.jettyXml = jettyXml;
        configure();
    }

    /**
     * @see org.eclipse.jetty.ant.utils.ServerProxy#addWebApplication(WebApplicationProxy,
     *      int)
     */
    public void addWebApplication(WebApplicationProxy webApp, int scanIntervalSeconds)
    {
        webApp.createApplicationContext(contexts);

        if (scanIntervalSeconds > 0)
        {
            webApplications.put(webApp, new Integer(scanIntervalSeconds));
        }
    }

    /**
     * Configures Jetty server before adding any web applications to it.
     */
    private void configure()
    {
        // Applies external configuration via jetty.xml
        applyJettyXml();

        // Configures connectors for this server instance.
        Iterator<Connector> connectorIterator = connectors.iterator();
        while (connectorIterator.hasNext())
        {
            Connector jettyConnector = (Connector) connectorIterator.next();
            SelectChannelConnector jc = new SelectChannelConnector(server);
            
            jc.setPort(jettyConnector.getPort());
            jc.setIdleTimeout(jettyConnector.getMaxIdleTime());
            
            server.addConnector(jc);
        }

        // Configures login services
        Iterator servicesIterator = loginServices.iterator();
        while (servicesIterator.hasNext())
        {
            LoginService service = (LoginService) servicesIterator.next();
            server.addBean(service);
        }

        // Does not cache resources, to prevent Windows from locking files
        Resource.setDefaultUseCaches(false);

        // Set default server handlers
        configureHandlers();
    }

    private void configureHandlers()
    {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        if (requestLog != null)
        {
            requestLogHandler.setRequestLog(requestLog);
        }

        contexts = (ContextHandlerCollection) server
                .getChildHandlerByClass(ContextHandlerCollection.class);
        if (contexts == null)
        {
            contexts = new ContextHandlerCollection();
            HandlerCollection handlers = (HandlerCollection) server
                    .getChildHandlerByClass(HandlerCollection.class);
            if (handlers == null)
            {
                handlers = new HandlerCollection();
                server.setHandler(handlers);
                handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(),
                        requestLogHandler });
            }
            else
            {
                handlers.addHandler(contexts);
            }
        }
    }

    /**
     * Applies jetty.xml configuration to the Jetty server instance.
     */
    private void applyJettyXml()
    {
        if (jettyXml != null && jettyXml.exists())
        {
            TaskLog.log("Configuring jetty from xml configuration file = "
                    + jettyXml.getAbsolutePath());
            XmlConfiguration configuration;
            try
            {
                configuration = new XmlConfiguration(Resource.toURL(jettyXml));
                configuration.configure(server);
            }
            catch (MalformedURLException e)
            {
                throw new RuntimeException(e);
            }
            catch (SAXException e)
            {
                throw new RuntimeException(e);
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * @see org.eclipse.jetty.ant.utils.ServerProxy#start()
     */
    public void start()
    {
        try
        {
            server.start();
            startScanners();
            server.join();

        }
        catch (InterruptedException e)
        {
            new RuntimeException(e);
        }
        catch (Exception e)
        {
            new RuntimeException(e);
        }
    }

    /**
     * Starts web applications' scanners.
     */
    private void startScanners() throws Exception
    {
        Iterator i = webApplications.keySet().iterator();
        while (i.hasNext())
        {
            WebApplicationProxyImpl webApp = (WebApplicationProxyImpl) i.next();
            Integer scanIntervalSeconds = (Integer) webApplications.get(webApp);
            JettyRunTask.startScanner(webApp, scanIntervalSeconds.intValue());
        }
    }

    /**
     * @see org.eclipse.jetty.ant.utils.ServerProxy#getProxiedObject()
     */
    public Object getProxiedObject()
    {
        return server;
    }
}

Back to the top