Ken Gilmer | 2c29a97 | 2011-10-13 05:01:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.http.lightweight.osgi; |
| 20 | |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | import java.util.Properties; |
| 25 | |
| 26 | import org.apache.felix.http.lightweight.server.Server; |
| 27 | import org.apache.felix.http.lightweight.servlet.HttpConstants; |
| 28 | import org.osgi.framework.BundleActivator; |
| 29 | import org.osgi.framework.BundleContext; |
| 30 | import org.osgi.framework.ServiceRegistration; |
| 31 | import org.osgi.service.http.HttpService; |
| 32 | |
| 33 | /** |
| 34 | * Activator for org.apache.felix.http.lightweight HTTP Service implementation. |
| 35 | * |
| 36 | * The activator will read in system properties that are relevant to the service |
| 37 | * and register the HttpService in the service registry. |
| 38 | * |
| 39 | **/ |
| 40 | public class Activator implements BundleActivator |
| 41 | { |
| 42 | /** |
| 43 | * Felix-specific log level setting as system property. |
| 44 | */ |
| 45 | private static final String FELIX_LOG_LEVEL = "felix.log.level"; |
| 46 | /** |
| 47 | * HTTP Service registration. |
| 48 | */ |
| 49 | private ServiceRegistration m_httpServiceReg; |
| 50 | /** |
| 51 | * Reference to socket server. |
| 52 | */ |
| 53 | private Server m_server; |
| 54 | |
| 55 | /* (non-Javadoc) |
| 56 | * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) |
| 57 | */ |
| 58 | public void start(BundleContext context) throws Exception |
| 59 | { |
| 60 | Logger logger = createLogger(context); |
| 61 | |
| 62 | //Set the internal logger's log level if specified. |
| 63 | if (context.getProperty(FELIX_LOG_LEVEL) != null) |
| 64 | { |
| 65 | logger.setLogLevel(Integer.parseInt(context.getProperty(FELIX_LOG_LEVEL))); |
| 66 | } |
| 67 | |
| 68 | //Only enable the HTTPService if the HTTP_ENABLE property is set or undefined. |
| 69 | if (isPropertyTrue(context, Server.CONFIG_PROPERTY_HTTP_ENABLE, true)) |
| 70 | { |
| 71 | Map config = createConfigMap(context); |
| 72 | m_server = new Server(config, logger); |
| 73 | m_httpServiceReg = context.registerService(HttpService.class.getName(), |
| 74 | new HttpServiceFactoryImpl(logger, m_server), |
| 75 | createHttpServiceProperties(Server.getConfiguredPort(config))); |
| 76 | } |
| 77 | |
| 78 | //Warn user that HTTPS is not supported if it is specifically enabled. |
| 79 | if (isPropertyTrue(context, Server.CONFIG_PROPERTY_HTTPS_ENABLE, false)) |
| 80 | { |
| 81 | logger.log(Logger.LOG_WARNING, Server.CONFIG_PROPERTY_HTTPS_ENABLE |
| 82 | + " is not implemented in this http service."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Create a Dictionary intended to be used with Http Service registration. |
| 88 | * |
| 89 | * @param port Port number to add to the properties. |
| 90 | * @return A dictionary of OSGi service properties associate with the HTTP service. |
| 91 | */ |
| 92 | private Dictionary createHttpServiceProperties(final int port) |
| 93 | { |
| 94 | Dictionary props = new Properties(); |
| 95 | |
| 96 | props.put(HttpConstants.SERVICE_PROPERTY_KEY_HTTP_ENABLE, Boolean.toString(true)); |
| 97 | props.put(HttpConstants.SERVICE_PROPERTY_KEY_HTTPS_ENABLE, |
| 98 | Boolean.toString(false)); |
| 99 | props.put(HttpConstants.SERVICE_PROPERTY_KEY_HTTP_PORT, Integer.toString(port)); |
| 100 | |
| 101 | return props; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Create a Map of configuration name/value pairs that the socket server requires to start. |
| 106 | * |
| 107 | * @param context BundleContext |
| 108 | * @return Map of configuration name/value pairs that the socket server requires to start. |
| 109 | */ |
| 110 | private Map createConfigMap(final BundleContext context) |
| 111 | { |
| 112 | Map config = new HashMap(); |
| 113 | |
| 114 | config.put(Server.CONFIG_PROPERTY_HTTP_PORT, |
| 115 | context.getProperty(Server.CONFIG_PROPERTY_HTTP_PORT)); |
| 116 | config.put(Server.CONFIG_PROPERTY_HTTP_ENABLE, |
| 117 | context.getProperty(Server.CONFIG_PROPERTY_HTTP_ENABLE)); |
| 118 | config.put(Server.CONFIG_PROPERTY_HTTPS_ENABLE, |
| 119 | context.getProperty(Server.CONFIG_PROPERTY_HTTPS_ENABLE)); |
| 120 | config.put(Server.CONFIG_PROPERTY_HTTP_DEBUG, |
| 121 | context.getProperty(Server.CONFIG_PROPERTY_HTTP_DEBUG)); |
| 122 | config.put(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP, |
| 123 | context.getProperty(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP)); |
| 124 | config.put(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP, |
| 125 | context.getProperty(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP)); |
| 126 | config.put(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP, |
| 127 | context.getProperty(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP)); |
| 128 | config.put(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP, |
| 129 | context.getProperty(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP)); |
| 130 | |
| 131 | return config; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param context BundleContext |
| 136 | * @return Logger instance |
| 137 | */ |
| 138 | private Logger createLogger(final BundleContext context) |
| 139 | { |
| 140 | Logger logger = new Logger(); |
| 141 | logger.setSystemBundleContext(context); |
| 142 | |
| 143 | return logger; |
| 144 | } |
| 145 | |
| 146 | /* (non-Javadoc) |
| 147 | * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) |
| 148 | */ |
| 149 | public void stop(final BundleContext context) throws Exception |
| 150 | { |
| 151 | if (m_httpServiceReg != null) |
| 152 | { |
| 153 | m_httpServiceReg.unregister(); |
| 154 | } |
| 155 | |
| 156 | if (m_server.getState() == Server.ACTIVE_STATE) |
| 157 | { |
| 158 | m_server.stop(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Convenience method that returns true if Bundle property exists and is true, false if false, and defaultValue otherwise. |
| 164 | * @param context BundleContext |
| 165 | * @param name Property name |
| 166 | * @param defaultValue default value for case that the key does not exist. |
| 167 | * @return true if Bundle property exists and is true, false if false, and defaultValue otherwise. |
| 168 | */ |
| 169 | private static boolean isPropertyTrue(final BundleContext context, final String name, |
| 170 | final boolean defaultValue) |
| 171 | { |
| 172 | String value = context.getProperty(name); |
| 173 | |
| 174 | if (value == null) |
| 175 | { |
| 176 | return defaultValue; |
| 177 | } |
| 178 | |
| 179 | return Boolean.valueOf(value).booleanValue(); |
| 180 | } |
| 181 | } |