View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2011 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.osgi.boot.jsp;
14  
15  import java.io.IOException;
16  import java.net.URL;
17  import java.util.Collection;
18  import java.util.Enumeration;
19  import java.util.LinkedHashSet;
20  
21  import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
22  import org.eclipse.jetty.osgi.boot.utils.internal.DefaultFileLocatorHelper;
23  import org.eclipse.jetty.util.log.Log;
24  import org.eclipse.jetty.util.resource.Resource;
25  import org.eclipse.jetty.webapp.TagLibConfiguration;
26  import org.eclipse.jetty.webapp.WebAppContext;
27  import org.osgi.framework.Bundle;
28  import org.osgi.framework.BundleReference;
29  import org.osgi.service.packageadmin.PackageAdmin;
30  import org.osgi.util.tracker.ServiceTracker;
31  
32  /**
33   * <p>
34   * Replacement for {@link TagLibConfiguration} for the OSGi integration.
35   * </p>
36   * <p>
37   * In the case of a WAB, tlds can be located in OSGi bundles that are dependencies
38   * of the WAB.
39   * It is expected that each WAB lists the symbolic-names of the bundles that contain
40   * tld files. The list is defined as the value of the header 'Require-TldBundle'
41   * </p>
42   * <p>
43   * Discussions about this are logged in https://bugs.eclipse.org/bugs/show_bug.cgi?id=306971
44   * </p>
45   */
46  public class TagLibOSGiConfiguration extends TagLibConfiguration
47  {
48  	private ServiceTracker packageAdminServiceTracker = null;
49  	
50  	/**
51  	 * Override the preConfigure; locates the bundles that contain
52  	 * tld files according to the value of the manifest header Require-TldBundle.
53  	 * <p>
54  	 * Set or add to the property TldProcessor.TLDResources the list of located jars
55  	 * so that the super class will scan those.
56  	 * </p>
57  	 */
58      public void preConfigure(WebAppContext context) throws Exception
59      {
60      	String requireTldBundle = (String)context.getAttribute(OSGiWebappConstants.REQUIRE_TLD_BUNDLE);
61      	if (requireTldBundle != null)
62      	{
63      		Collection<Resource> resources = getRequireTldBundleAsJettyResources(context, requireTldBundle);
64      		if (resources != null && !resources.isEmpty())
65      		{
66      			Collection<Resource> previouslySet = (Collection<Resource>)
67      				context.getAttribute(TagLibConfiguration.TLD_RESOURCES);
68      			if (previouslySet != null)
69      			{
70      				resources.addAll(previouslySet);
71      			}
72      			context.setAttribute(TagLibConfiguration.TLD_RESOURCES, resources);
73      		}
74      	}
75      	super.preConfigure(context);
76  
77      }
78  
79      /**
80       * @param requireTldBundle The comma separated list of bundles' symbolic names
81       * that contain tld for this osgi webapp.
82       * @return The collection of jars or folders that match those bundles.
83       */
84      private Collection<Resource> getRequireTldBundleAsJettyResources(
85      		WebAppContext context, String requireTldBundle)
86      {
87      	Bundle bundle = (Bundle)
88      		context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
89      	PackageAdmin packAdmin = getBundleAdmin();
90      	String[] symbNames = requireTldBundle.split(", ");
91      	Collection<Resource> tlds = new LinkedHashSet<Resource>();
92      	for (String symbName : symbNames)
93      	{
94      		Bundle[] bs = packAdmin.getBundles(symbName, null);
95      		if (bs == null || bs.length == 0)
96      		{
97      			throw new IllegalArgumentException("Unable to locate the bundle '"
98      					+ symbName + "' specified in the "
99      					+ OSGiWebappConstants.REQUIRE_TLD_BUNDLE
100     					+ " of the manifest of "
101     					+ bundle.getSymbolicName());
102     		}
103     		//take the first one as it is the most recent version?
104 			Enumeration<URL> en = bs[0].findEntries("META-INF", "*.tld", false);
105 			boolean atLeastOneTldFound = false;
106 			while (en.hasMoreElements())
107 			{
108 				atLeastOneTldFound = true;
109 				URL oriUrl = en.nextElement();
110 				URL url = DefaultFileLocatorHelper.getLocalURL(oriUrl);
111 				Resource tldResource;
112 				try
113 				{
114 					tldResource = Resource.newResource(url);
115 				}
116 				catch (IOException e)
117 				{
118 					throw new IllegalArgumentException("Unable to locate the "
119 							+ "tld resource in '"
120 	    					+ url.toString()
121 	    					+ "' in the bundle '" + bs[0].getSymbolicName()
122 	    					+ "' while registering the "
123 	    					+ OSGiWebappConstants.REQUIRE_TLD_BUNDLE
124 	    					+ " of the manifest of "
125 	    					+ bundle.getSymbolicName(), e);
126 				}
127 				tlds.add(tldResource);
128 			}
129 			if (!atLeastOneTldFound)
130 			{
131 				Log.warn("No '/META-INF/*.tld' resources were found "
132     					+ " in the bundle '" + bs[0].getSymbolicName()
133     					+ "' while registering the "
134     					+ OSGiWebappConstants.REQUIRE_TLD_BUNDLE
135     					+ " of the manifest of "
136     					+ bundle.getSymbolicName());
137 			}
138     	}
139     	return tlds;
140     }
141     
142 	private PackageAdmin getBundleAdmin()
143 	{
144 		if (packageAdminServiceTracker == null)
145 		{
146 			Bundle bootBundle = ((BundleReference)OSGiWebappConstants.class.getClassLoader()).getBundle();
147 			packageAdminServiceTracker = new ServiceTracker(bootBundle.getBundleContext(),
148 					PackageAdmin.class.getName(), null);
149 			packageAdminServiceTracker.open();
150 		}
151 		return (PackageAdmin) packageAdminServiceTracker.getService();
152 	}
153 	    
154 }