1 12 13 package org.eclipse.equinox.http.jetty.internal; 14 15 import java.io.File ; 16 import java.lang.reflect.Method ; 17 import java.util.Dictionary ; 18 import java.util.Hashtable ; 19 import org.osgi.framework.*; 20 import org.osgi.service.cm.ManagedServiceFactory; 21 import org.osgi.service.startlevel.StartLevel; 22 23 public class Activator implements BundleActivator { 24 25 private static final String JETTY_WORK_DIR = "jettywork"; private static final String DEFAULT_PID = "default"; private static final String MANAGED_SERVICE_FACTORY_PID = "org.eclipse.equinox.http.jetty.config"; 29 private static final String ORG_OSGI_SERVICE_HTTP_PORT = "org.osgi.service.http.port"; private static final String ORG_OSGI_SERVICE_HTTP_PORT_SECURE = "org.osgi.service.http.port.secure"; 33 private static final String AUTOSTART = "org.eclipse.equinox.http.jetty.autostart"; 37 private static HttpServerManager staticServerManager; 40 41 private HttpServerManager httpServerManager; 42 private ServiceRegistration registration; 43 44 public void start(BundleContext context) throws Exception { 45 File jettyWorkDir = new File (context.getDataFile(""), JETTY_WORK_DIR); jettyWorkDir.mkdir(); 47 httpServerManager = new HttpServerManager(jettyWorkDir); 48 49 String autostart = context.getProperty(AUTOSTART); 50 if ((autostart == null || Boolean.valueOf(autostart).booleanValue()) && ! isBundleActivationPolicyUsed(context)) { 51 Dictionary defaultSettings = createDefaultSettings(context); 52 httpServerManager.updated(DEFAULT_PID, defaultSettings); 53 } 54 55 Dictionary dictionary = new Hashtable (); 56 dictionary.put(Constants.SERVICE_PID, MANAGED_SERVICE_FACTORY_PID); 57 58 registration = context.registerService(ManagedServiceFactory.class.getName(), httpServerManager, dictionary); 59 setStaticServerManager(httpServerManager); 60 } 61 62 private boolean isBundleActivationPolicyUsed(BundleContext context) { 63 ServiceReference reference = context.getServiceReference(StartLevel.class.getName()); 64 StartLevel sl = ((reference != null) ? (StartLevel) context.getService(reference) : null); 65 if (sl != null) { 66 try { 67 Bundle bundle = context.getBundle(); 68 Method isBundleActivationPolicyUsed = StartLevel.class.getMethod("isBundleActivationPolicyUsed", new Class [] {Bundle.class}); Boolean result = (Boolean ) isBundleActivationPolicyUsed.invoke(sl, new Object [] {bundle}); 70 return result.booleanValue(); 71 } catch (Exception e) { 72 } finally { 75 context.ungetService(reference); 76 } 77 } 78 return false; 79 } 80 81 public void stop(BundleContext context) throws Exception { 82 setStaticServerManager(null); 83 registration.unregister(); 84 registration = null; 85 86 httpServerManager.shutdown(); 87 httpServerManager = null; 88 } 89 90 private Dictionary createDefaultSettings(BundleContext context) { 91 final String PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty."; Dictionary defaultSettings = new Hashtable (); 93 94 defaultSettings.put(Constants.SERVICE_PID, DEFAULT_PID); 96 97 String httpEnabledProperty = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTP_ENABLED); 99 Boolean httpEnabled = (httpEnabledProperty == null) ? Boolean.TRUE : new Boolean (httpEnabledProperty); 100 defaultSettings.put(HttpServerManager.HTTP_ENABLED, httpEnabled); 101 102 String httpPortProperty = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTP_PORT); 104 if (httpPortProperty == null) 105 httpPortProperty = context.getProperty(ORG_OSGI_SERVICE_HTTP_PORT); 106 107 int httpPort = 80; 108 if (httpPortProperty != null) { 109 try { 110 httpPort = Integer.parseInt(httpPortProperty); 111 } catch (NumberFormatException e) { 112 } 114 } 115 defaultSettings.put(HttpServerManager.HTTP_PORT, new Integer (httpPort)); 116 117 String httpHost = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTP_HOST); 119 if (httpHost != null) 120 defaultSettings.put(HttpServerManager.HTTP_HOST, httpHost); 121 122 Boolean httpsEnabled = new Boolean (context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTPS_ENABLED)); 124 defaultSettings.put(HttpServerManager.HTTPS_ENABLED, httpsEnabled); 125 126 if (httpsEnabled.booleanValue()) { 127 String httpsPortProperty = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTPS_PORT); 129 if (httpPortProperty == null) 130 httpPortProperty = context.getProperty(ORG_OSGI_SERVICE_HTTP_PORT_SECURE); 131 132 int httpsPort = 443; 133 if (httpsPortProperty != null) { 134 try { 135 httpsPort = Integer.parseInt(httpsPortProperty); 136 } catch (NumberFormatException e) { 137 } 139 } 140 defaultSettings.put(HttpServerManager.HTTPS_PORT, new Integer (httpsPort)); 141 142 String httpsHost = context.getProperty(PROPERTY_PREFIX + HttpServerManager.HTTPS_HOST); 144 if (httpsHost != null) 145 defaultSettings.put(HttpServerManager.HTTPS_HOST, httpsHost); 146 147 String keystore = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_KEYSTORE); 149 if (keystore != null) 150 defaultSettings.put(HttpServerManager.SSL_KEYSTORE, keystore); 151 152 String password = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_PASSWORD); 153 if (password != null) 154 defaultSettings.put(HttpServerManager.SSL_PASSWORD, password); 155 156 String keypassword = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_KEYPASSWORD); 157 if (keypassword != null) 158 defaultSettings.put(HttpServerManager.SSL_KEYPASSWORD, keypassword); 159 160 String needclientauth = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_NEEDCLIENTAUTH); 161 if (needclientauth != null) 162 defaultSettings.put(HttpServerManager.SSL_NEEDCLIENTAUTH, new Boolean (needclientauth)); 163 164 String wantclientauth = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_WANTCLIENTAUTH); 165 if (wantclientauth != null) 166 defaultSettings.put(HttpServerManager.SSL_WANTCLIENTAUTH, new Boolean (wantclientauth)); 167 168 String protocol = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_PROTOCOL); 169 if (protocol != null) 170 defaultSettings.put(HttpServerManager.SSL_PROTOCOL, protocol); 171 172 String algorithm = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_ALGORITHM); 173 if (algorithm != null) 174 defaultSettings.put(HttpServerManager.SSL_ALGORITHM, algorithm); 175 176 String keystoretype = context.getProperty(PROPERTY_PREFIX + HttpServerManager.SSL_KEYSTORETYPE); 177 if (keystoretype != null) 178 defaultSettings.put(HttpServerManager.SSL_KEYSTORETYPE, keystoretype); 179 } 180 181 String contextpath = context.getProperty(PROPERTY_PREFIX + HttpServerManager.CONTEXT_PATH); 183 if (contextpath != null) 184 defaultSettings.put(HttpServerManager.CONTEXT_PATH, contextpath); 185 186 String sessionInactiveInterval = context.getProperty(PROPERTY_PREFIX + HttpServerManager.CONTEXT_SESSIONINACTIVEINTERVAL); 188 if (sessionInactiveInterval != null) { 189 try { 190 defaultSettings.put(HttpServerManager.CONTEXT_SESSIONINACTIVEINTERVAL, new Integer (sessionInactiveInterval)); 191 } catch (NumberFormatException e) { 192 } 194 } 195 196 String otherInfo = context.getProperty(PROPERTY_PREFIX + HttpServerManager.OTHER_INFO); 198 if (otherInfo != null) 199 defaultSettings.put(HttpServerManager.OTHER_INFO, otherInfo); 200 201 return defaultSettings; 202 } 203 204 public synchronized static void startServer(String pid, Dictionary settings) throws Exception { 205 if (staticServerManager == null) 206 throw new IllegalStateException ("Inactive"); 208 staticServerManager.updated(pid, settings); 209 } 210 211 public synchronized static void stopServer(String pid) throws Exception { 212 if (staticServerManager != null) 213 staticServerManager.deleted(pid); 214 } 215 216 private synchronized static void setStaticServerManager(HttpServerManager httpServerManager) { 217 staticServerManager = httpServerManager; 218 } 219 220 } 221 | Popular Tags |