View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.osgi.boot.internal.serverfactory;
20  
21  import java.io.File;
22  import java.net.MalformedURLException;
23  import java.net.URI;
24  import java.net.URL;
25  import java.util.ArrayList;
26  import java.util.Dictionary;
27  import java.util.Enumeration;
28  import java.util.Hashtable;
29  import java.util.List;
30  import java.util.StringTokenizer;
31  
32  import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
33  import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
34  import org.eclipse.jetty.osgi.boot.utils.BundleFileLocatorHelperFactory;
35  import org.eclipse.jetty.osgi.boot.utils.OSGiClassLoader;
36  import org.eclipse.jetty.osgi.boot.utils.Util;
37  import org.eclipse.jetty.server.Server;
38  import org.eclipse.jetty.util.log.Log;
39  import org.eclipse.jetty.util.log.Logger;
40  import org.eclipse.jetty.util.resource.JarResource;
41  import org.eclipse.jetty.util.resource.Resource;
42  import org.osgi.framework.Bundle;
43  import org.osgi.framework.BundleContext;
44  
45  /**
46   * DefaultJettyAtJettyHomeHelper
47   * 
48   * 
49   * Creates a default instance of Jetty, based on the values of the
50   * System properties "jetty.home" or "jetty.home.bundle", one of which
51   * must be specified in order to create the default instance.
52   * 
53   * Called by the {@link JettyBootstrapActivator} during the starting of the
54   * bundle. 
55   */
56  public class DefaultJettyAtJettyHomeHelper
57  {
58      private static final Logger LOG = Log.getLogger(DefaultJettyAtJettyHomeHelper.class);
59  
60      /**
61       * contains a comma separated list of paths to the etc/jetty-*.xml files
62       */
63      public static final String JETTY_ETC_FILES = OSGiServerConstants.MANAGED_JETTY_XML_CONFIG_URLS;
64  
65      /**
66       * Set of config files to apply to a jetty Server instance if none are supplied by SYS_PROP_JETTY_ETC_FILES
67       */
68      public static final String DEFAULT_JETTY_ETC_FILES = "etc/jetty.xml,etc/jetty-selector.xml,etc/jetty-deployer.xml";
69      
70      /**
71       * Default location within bundle of a jetty home dir.
72       */
73      public static final String DEFAULT_JETTYHOME = "/jettyhome";
74      
75      
76      
77      /* ------------------------------------------------------------ */
78      /**
79       * Called by the JettyBootStrapActivator. If the system property jetty.home
80       * is defined and points to a folder, creates a corresponding jetty
81       * server.
82       * <p>
83       * If the system property jetty.home.bundle is defined and points to a
84       * bundle, look for the configuration of jetty inside that bundle.
85       * </p>
86       * <p>
87       * In both cases reads the system property 'jetty.etc.config.urls' to locate
88       * the configuration files for the deployed jetty. It is a comma separated
89       * list of URLs or relative paths inside the bundle or folder to the config
90       * files.
91       * </p>
92       * <p>
93       * In both cases the system properties jetty.host, jetty.port and
94       * jetty.port.ssl are passed to the configuration files that might use them
95       * as part of their properties.
96       * </p>
97       */
98      public static Server startJettyAtJettyHome(BundleContext bundleContext) throws Exception
99      {
100         String jettyHomeSysProp = System.getProperty(OSGiServerConstants.JETTY_HOME);
101         String jettyHomeBundleSysProp = System.getProperty(OSGiServerConstants.JETTY_HOME_BUNDLE);
102         File jettyHomeDir = null;
103         Bundle jettyHomeBundle = null;
104      
105         Dictionary<String,String> properties = new Hashtable<String,String>();
106         if (jettyHomeSysProp != null)
107         {
108             jettyHomeSysProp = Util.resolvePropertyValue(jettyHomeSysProp);
109             // bug 329621
110             if (jettyHomeSysProp.startsWith("\"") && jettyHomeSysProp.endsWith("\"") || (jettyHomeSysProp.startsWith("'") && jettyHomeSysProp.endsWith("'")))
111                 jettyHomeSysProp = jettyHomeSysProp.substring(1, jettyHomeSysProp.length() - 1);
112          
113             if (jettyHomeBundleSysProp != null)
114                 LOG.warn("Both jetty.home and jetty.home.bundle property defined: jetty.home.bundle ignored.");
115            
116             jettyHomeDir = new File(jettyHomeSysProp);
117             if (!jettyHomeDir.exists() || !jettyHomeDir.isDirectory())
118             {
119                 LOG.warn("Unable to locate the jetty.home folder " + jettyHomeSysProp);
120                 return null;
121             }
122             
123             //set jetty.home
124             Util.setProperty(properties, OSGiServerConstants.JETTY_HOME, jettyHomeDir.getAbsolutePath());
125         }
126         else if (jettyHomeBundleSysProp != null)
127         {
128             jettyHomeBundleSysProp = Util.resolvePropertyValue(jettyHomeBundleSysProp);
129             for (Bundle b : bundleContext.getBundles())
130             {
131                 if (b.getSymbolicName().equals(jettyHomeBundleSysProp))
132                 {
133                     jettyHomeBundle = b;
134                     break;
135                 }
136             }
137             if (jettyHomeBundle == null)
138             {
139                 LOG.warn("Unable to find the jetty.home.bundle named " + jettyHomeSysProp);
140                 return null;
141             }
142         }
143         
144         if (jettyHomeDir == null && jettyHomeBundle == null)
145         {
146             LOG.warn("No default jetty created.");
147             return null;
148         }
149         
150         
151         
152         //configure the server here rather than letting the JettyServerServiceTracker do it, because we want to be able to
153         //configure the ThreadPool, which can only be done via the constructor, ie from within the xml configuration processing
154         List<URL> configURLs = jettyHomeDir != null ? getJettyConfigurationURLs(jettyHomeDir) : getJettyConfigurationURLs(jettyHomeBundle, properties);
155 
156         LOG.info("Configuring the default jetty server with {}",configURLs);
157         LOG.info("JETTY.HOME="+properties.get(OSGiServerConstants.JETTY_HOME));
158         ClassLoader contextCl = Thread.currentThread().getContextClassLoader();
159         try
160         {
161             Thread.currentThread().setContextClassLoader(JettyBootstrapActivator.class.getClassLoader());
162             
163             // these properties usually are the ones passed to this type of
164             // configuration.
165             properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, OSGiServerConstants.MANAGED_JETTY_SERVER_DEFAULT_NAME);
166             Util.setProperty(properties, OSGiServerConstants.JETTY_HOST, System.getProperty(OSGiServerConstants.JETTY_HOST));
167             Util.setProperty(properties, OSGiServerConstants.JETTY_PORT, System.getProperty(OSGiServerConstants.JETTY_PORT));
168             Util.setProperty(properties, OSGiServerConstants.JETTY_PORT_SSL, System.getProperty(OSGiServerConstants.JETTY_PORT_SSL));
169 
170             Server server = ServerInstanceWrapper.configure(null, configURLs, properties);
171             //ensure jetty.home is set
172             server.setAttribute(OSGiServerConstants.JETTY_HOME, properties.get(OSGiServerConstants.JETTY_HOME));
173             
174             //Register the default Server instance as an OSGi service.
175             //The JettyServerServiceTracker will notice it and set it up to deploy bundles as wars etc
176             bundleContext.registerService(Server.class.getName(), server, properties);
177             LOG.info("Default jetty server configured");
178             return server;
179         }
180         finally
181         {
182             Thread.currentThread().setContextClassLoader(contextCl);
183         }
184     }
185 
186     
187    
188     
189     /* ------------------------------------------------------------ */
190     /**
191      * Minimum setup for the location of the configuration files given a
192      * jettyhome folder. Reads the system property jetty.etc.config.urls and
193      * look for the corresponding jetty configuration files that will be used to
194      * setup the jetty server.
195      * 
196      * @param jettyhome
197      * @return
198      * @throws MalformedURLException 
199      */
200     private static List<URL> getJettyConfigurationURLs(File jettyhome) 
201     throws MalformedURLException
202     {
203         List<URL> configURLs = new ArrayList<URL>();
204         String jettyetc = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
205         StringTokenizer tokenizer = new StringTokenizer(jettyetc, ";,", false);
206         while (tokenizer.hasMoreTokens())
207         {
208             String next = tokenizer.nextToken().trim();
209             //etc files can either be relative to jetty.home or absolute disk locations
210             if (!next.startsWith("/") && (next.indexOf(':') == -1))    
211                 configURLs.add(new File(jettyhome, next).toURI().toURL());
212             else 
213                 configURLs.add(new URL(next));
214         }
215         return configURLs;
216     }
217 
218     /* ------------------------------------------------------------ */
219     /**
220      * Minimum setup for the location of the configuration files given a
221      * configuration embedded inside a bundle. Reads the system property
222      * jetty.etc.config.urls and look for the corresponding jetty configuration
223      * files that will be used to setup the jetty server.
224      * 
225      * @param jettyhome
226      * @return
227      */
228     private static List<URL> getJettyConfigurationURLs(Bundle configurationBundle, Dictionary properties)
229     throws Exception
230     {
231         List<URL> configURLs = new ArrayList<URL>();
232         String files = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);       
233         StringTokenizer tokenizer = new StringTokenizer(files, ";,", false);
234 
235         while (tokenizer.hasMoreTokens())
236         {
237             String etcFile = tokenizer.nextToken().trim();
238 
239             //file path is absolute
240             if (etcFile.startsWith("/") || etcFile.indexOf(":") != -1)
241                 configURLs.add(new URL(etcFile));
242             else //relative file path
243             {
244                 Enumeration<URL> enUrls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(configurationBundle, etcFile);
245 
246                 String home = null;
247                 // default for org.eclipse.osgi.boot where we look inside
248                 // jettyhome/ for the default embedded configuration.
249                 if ((enUrls == null || !enUrls.hasMoreElements()))
250                 {
251                     home = DEFAULT_JETTYHOME;
252                     String tmp = DEFAULT_JETTYHOME+(DEFAULT_JETTYHOME.endsWith("/")?"":"/")+etcFile;
253                     enUrls = BundleFileLocatorHelperFactory.getFactory().getHelper().findEntries(configurationBundle, tmp);                    
254                     LOG.info("Configuring jetty from bundle: {} with {}", configurationBundle.getSymbolicName(),tmp);
255                 }          
256 
257                 //lazily ensure jetty.home value is set based on location of etc files
258                 if (properties.get(OSGiServerConstants.JETTY_HOME) == null)
259                 {
260                     Resource res = findDir(configurationBundle, home);
261                     if (res != null)
262                         properties.put(OSGiServerConstants.JETTY_HOME, res.toString());
263                 }
264          
265                 if (enUrls == null || !enUrls.hasMoreElements())
266                     throw new IllegalStateException ("Unable to locate a jetty configuration file for " + etcFile);
267 
268                 URL url = BundleFileLocatorHelperFactory.getFactory().getHelper().getFileURL(enUrls.nextElement());
269                 configURLs.add(url);
270            
271             }
272         }
273         return configURLs;
274     }
275     
276     /* ------------------------------------------------------------ */
277     /**
278      * Get a resource representing a directory inside a bundle. If the dir is null,
279      * return a resource representing the installation location of the bundle.
280      * @param bundle
281      * @param dir
282      * @return
283      */
284     public static Resource findDir (Bundle bundle, String dir)
285     {
286         if (bundle == null)
287             return null;
288 
289         try
290         {
291             File f = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bundle);
292             URL u = f.toURI().toURL();
293             u = BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(u);
294             Resource res = Resource.newResource(u);
295             String s = res.toString();
296    
297             //check if it is an unarchived bundle
298             if (s.endsWith(".jar") && s.startsWith("file:"))
299                 res = JarResource.newJarResource(res);
300             
301             //if looking for a directory 
302             if (dir != null)
303                 res = res.addPath(dir);
304             
305             return res;
306           
307         }
308         catch (Exception e)
309         {
310             LOG.warn("Bad bundle location" , e);
311             return null;
312         }
313     }
314 }