1 // ========================================================================
2 // Copyright (c) 2009 Intalio, Inc.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // Contributors:
13 // Hugues Malphettes - initial API and implementation
14 // ========================================================================
15 package org.eclipse.jetty.osgi.boot.internal.serverfactory;
16
17 import java.util.Dictionary;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.eclipse.jetty.server.Server;
22 import org.osgi.service.cm.ConfigurationException;
23 import org.osgi.service.cm.ManagedServiceFactory;
24
25 /**
26 * This is a work in progress. <br/>
27 * In particular there is a lot of work required during the update of the
28 * configuration of a server. It might not be practical to in fact support that
29 * and re-deploy the webapps in the same state than before the server was
30 * stopped.
31 * <p>
32 * jetty servers are managed as OSGi services registered here. try to find out
33 * if a configuration will fail (ports already opened etc).
34 * </p>
35 * <p>
36 * Try to enable the creation and configuration of jetty servers in all the
37 * usual standard ways. The configuration of the server is defined by the
38 * properties passed to the service:
39 * <ol>
40 * <li>First look for jettyfactory. If the value is a jetty server, use that
41 * server</li>
42 * <li>Then look for jettyhome key. The value should be a java.io.File or a
43 * String that is a path to the folder It is required that a etc/jetty.xml file
44 * will be loated from that folder.</li>
45 * <li>Then look for a jettyxml key. The value should be a java.io.File or an
46 * InputStream that contains a jetty configuration file.</li>
47 * <li>TODO: More ways to configure a jetty server? (other IOCs like spring,
48 * equinox properties...)</li>
49 * <li>Throw an exception if none of the relevant parameters are found</li>
50 * </ol>
51 * </p>
52 *
53 * @author hmalphettes
54 */
55 public class JettyServersManagedFactory implements ManagedServiceFactory
56 {
57
58 /**
59 * key to configure the server according to a jetty home folder. the value
60 * is the corresponding java.io.File
61 */
62 public static final String JETTY_HOME = "jettyhome";
63 /** key to configure the server according to a jetty.xml file */
64 public static final String JETTY_CONFIG_XML = "jettyxml";
65
66 /**
67 * invoke jetty-factory class. the value of this property is the instance of
68 * that class to call back.
69 */
70 public static final String JETTY_FACTORY = "jettyfactory";
71
72 /**
73 * default property in jetty.xml that is used as the value of the http port.
74 */
75 public static final String JETTY_HTTP_PORT = "jetty.http.port";
76 /**
77 * default property in jetty.xml that is used as the value of the https
78 * port.
79 */
80 public static final String JETTY_HTTPS_PORT = "jetty.http.port";
81
82 private Map<String, Server> _servers = new HashMap<String, Server>();
83
84 /**
85 * Return a descriptive name of this factory.
86 *
87 * @return the name for the factory, which might be localized
88 */
89 public String getName()
90 {
91 return getClass().getName();
92 }
93
94 public void updated(String pid, Dictionary properties) throws ConfigurationException
95 {
96 Server server = _servers.get(pid);
97 deleted(pid);
98 // do we need to collect the currently deployed http services and
99 // webapps
100 // to be able to re-deploy them later?
101 // probably not. simply restart and see the various service trackers
102 // do everything that is needed.
103
104 }
105
106 public synchronized void deleted(String pid)
107 {
108 Server server = (Server)_servers.remove(pid);
109 if (server != null)
110 {
111 try
112 {
113 server.stop();
114 }
115 catch (Exception e)
116 {
117 e.printStackTrace();
118 }
119 }
120 }
121
122 }