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