1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.eclipse.jetty.osgi.boot.internal.serverfactory;
16
17 import java.net.URL;
18 import java.util.Dictionary;
19 import java.util.HashMap;
20 import java.util.Hashtable;
21 import java.util.Map;
22 import java.util.StringTokenizer;
23
24 import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
25 import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
26 import org.eclipse.jetty.server.Server;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.ServiceReference;
29 import org.osgi.service.cm.Configuration;
30 import org.osgi.service.cm.ConfigurationAdmin;
31 import org.osgi.service.cm.ConfigurationException;
32 import org.osgi.service.cm.ManagedServiceFactory;
33
34
35
36
37
38
39
40 public class JettyServersManagedFactory implements ManagedServiceFactory, IManagedJettyServerRegistry
41 {
42
43
44
45
46
47 public static final String JETTY_HOME = "jettyhome";
48
49 public static final String JETTY_CONFIG_XML = "jettyxml";
50
51
52
53
54
55 public static final String JETTY_FACTORY = "jettyfactory";
56
57
58
59
60 public static final String JETTY_HTTP_PORT = "jetty.http.port";
61
62
63
64
65 public static final String JETTY_HTTPS_PORT = "jetty.http.port";
66
67
68
69
70 private Map<String, ServerInstanceWrapper> _serversIndexedByPID = new HashMap<String, ServerInstanceWrapper>();
71
72
73
74 private Map<String, String> _serversNameIndexedByPID = new HashMap<String, String>();
75
76
77
78 private Map<String, String> _serversPIDIndexedByName = new HashMap<String, String>();
79
80
81
82
83
84
85 public String getName()
86 {
87 return getClass().getName();
88 }
89
90 public void updated(String pid, Dictionary properties) throws ConfigurationException
91 {
92 ServerInstanceWrapper serverInstanceWrapper = getServerByPID(pid);
93 deleted(pid);
94
95
96
97
98
99 String name = (String)properties.get(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME);
100 if (name == null)
101 {
102 throw new ConfigurationException(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME,
103 "The name of the server is mandatory");
104 }
105 serverInstanceWrapper = new ServerInstanceWrapper(name);
106 _serversIndexedByPID.put(pid, serverInstanceWrapper);
107 _serversNameIndexedByPID.put(pid, name);
108 _serversPIDIndexedByName.put(name, pid);
109 serverInstanceWrapper.start(new Server(), properties);
110 }
111
112 public synchronized void deleted(String pid)
113 {
114 ServerInstanceWrapper server = (ServerInstanceWrapper)_serversIndexedByPID.remove(pid);
115 String name = _serversNameIndexedByPID.remove(pid);
116 if (name != null)
117 {
118 _serversPIDIndexedByName.remove(name);
119 }
120 else
121 {
122
123 }
124 if (server != null)
125 {
126 try
127 {
128 server.stop();
129 }
130 catch (Exception e)
131 {
132 e.printStackTrace();
133 }
134 }
135 }
136
137 public synchronized ServerInstanceWrapper getServerByPID(String pid)
138 {
139 return _serversIndexedByPID.get(pid);
140 }
141
142
143
144
145
146 public ServerInstanceWrapper getServerInstanceWrapper(String managedServerName)
147 {
148 String pid = _serversPIDIndexedByName.get(managedServerName);
149 return pid != null ? _serversIndexedByPID.get(pid) : null;
150 }
151
152
153
154
155
156
157
158
159 public static void createNewServer(Bundle contributor, String serverName, String urlsToJettyXml) throws Exception
160 {
161 ServiceReference configurationAdminReference =
162 contributor.getBundleContext().getServiceReference( ConfigurationAdmin.class.getName() );
163
164 ConfigurationAdmin confAdmin = (ConfigurationAdmin) contributor.getBundleContext()
165 .getService( configurationAdminReference );
166
167 Configuration configuration = confAdmin.createFactoryConfiguration(
168 OSGiServerConstants.MANAGED_JETTY_SERVER_FACTORY_PID, contributor.getLocation() );
169 Dictionary properties = new Hashtable();
170 properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, serverName);
171
172 StringBuilder actualBundleUrls = new StringBuilder();
173 StringTokenizer tokenizer = new StringTokenizer(urlsToJettyXml, ",", false);
174 while (tokenizer.hasMoreTokens())
175 {
176 if (actualBundleUrls.length() != 0)
177 {
178 actualBundleUrls.append(",");
179 }
180 String token = tokenizer.nextToken();
181 if (token.indexOf(':') != -1)
182 {
183
184 actualBundleUrls.append(token);
185 }
186 else if (token.startsWith("/"))
187 {
188
189 URL url = contributor.getEntry(token);
190 if (url == null)
191 {
192 actualBundleUrls.append(token);
193 }
194 else
195 {
196 actualBundleUrls.append(url.toString());
197 }
198 }
199
200 }
201
202 properties.put(OSGiServerConstants.MANAGED_JETTY_XML_CONFIG_URLS, actualBundleUrls.toString());
203 configuration.update(properties);
204
205 }
206
207 }