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