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;
20  
21  import java.net.URL;
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.List;
25  
26  import org.eclipse.jetty.osgi.boot.internal.webapp.BundleFileLocatorHelperFactory;
27  import org.eclipse.jetty.osgi.boot.utils.internal.PackageAdminServiceTracker;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  import org.eclipse.jetty.util.resource.Resource;
31  import org.eclipse.jetty.webapp.MetaInfConfiguration;
32  import org.eclipse.jetty.webapp.WebAppContext;
33  import org.osgi.framework.Bundle;
34  
35  public class OSGiMetaInfConfiguration extends MetaInfConfiguration
36  {
37      private static final Logger LOG = Log.getLogger(OSGiMetaInfConfiguration.class);
38  
39  
40      /** 
41       * Inspect bundle fragments associated with the bundle of the webapp for web-fragment, resources, tlds.
42       *
43       * @see org.eclipse.jetty.webapp.MetaInfConfiguration#preConfigure(org.eclipse.jetty.webapp.WebAppContext)
44       */
45      @Override
46      public void preConfigure(final WebAppContext context) throws Exception
47      {
48          List<Resource> frags = (List<Resource>) context.getAttribute(METAINF_FRAGMENTS);
49          List<Resource> resfrags = (List<Resource>) context.getAttribute(METAINF_RESOURCES);
50          List<Resource> tldfrags = (List<Resource>) context.getAttribute(METAINF_TLDS);
51  
52          Bundle[] fragments = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles((Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE));
53          //TODO not convinced we need to do this, as we added any fragment jars to the MetaData.webInfJars in OSGiWebInfConfiguration, 
54          //so surely the web-fragments and resources tlds etc can be discovered normally?
55          for (Bundle frag : fragments)
56          {
57              URL webFrag = frag.getEntry("/META-INF/web-fragment.xml");
58              Enumeration<URL> resEnum = frag.findEntries("/META-INF/resources", "*", true);
59              Enumeration<URL> tldEnum = frag.findEntries("/META-INF", "*.tld", false);
60              if (webFrag != null || (resEnum != null && resEnum.hasMoreElements()) || (tldEnum != null && tldEnum.hasMoreElements()))
61              {
62                  try
63                  {
64                      if (webFrag != null)
65                      {
66                          if (frags == null)
67                          {
68                              frags = new ArrayList<Resource>();
69                              context.setAttribute(METAINF_FRAGMENTS, frags);
70                          }
71                          frags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(frag).toURI()));
72                      }
73                      if (resEnum != null && resEnum.hasMoreElements())
74                      {
75                          URL resourcesEntry = frag.getEntry("/META-INF/resources/");
76                          if (resourcesEntry == null)
77                          {
78                              // probably we found some fragments to a
79                              // bundle.
80                              // those are already contributed.
81                              // so we skip this.
82                          }
83                          else
84                          {
85                              if (resfrags == null)
86                              {
87                                  resfrags = new ArrayList<Resource>();
88                                  context.setAttribute(METAINF_RESOURCES, resfrags);
89                              }
90                              resfrags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(resourcesEntry)));
91                          }
92                      }
93                      if (tldEnum != null && tldEnum.hasMoreElements())
94                      {
95                          if (tldfrags == null)
96                          {
97                              tldfrags = new ArrayList<Resource>();
98                              context.setAttribute(METAINF_TLDS, tldfrags);
99                          }
100                         while (tldEnum.hasMoreElements())
101                         {
102                             URL tldUrl = tldEnum.nextElement();
103                             tldfrags.add(Resource.newResource(BundleFileLocatorHelperFactory.getFactory().getHelper().getLocalURL(tldUrl)));
104                         }
105                     }
106                 } 
107                 catch (Exception e)
108                 {
109                     LOG.warn("Unable to locate the bundle " + frag.getBundleId(), e);
110                 }
111             }
112         }
113         
114         super.preConfigure(context);
115     }
116 }