work in progress for OSGi boot to use Deployment manager

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1176 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-01-05 00:23:26 +00:00
parent ac0d3ae5e6
commit 238cb5b5fc
3 changed files with 68 additions and 134 deletions

View File

@ -0,0 +1,23 @@
package org.eclipse.jetty.osgi.boot;
import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
public class OSGiAppProvider extends AbstractLifeCycle implements AppProvider
{
DeploymentManager _manager;
public ContextHandler createContextHandler(App app) throws Exception
{
return null;
}
public void setDeploymentManager(DeploymentManager deploymentManager)
{
_manager=deploymentManager;
}
}

View File

@ -35,10 +35,10 @@ import org.osgi.framework.Bundle;
* Extends the webappclassloader to insert the classloader provided by the osgi * Extends the webappclassloader to insert the classloader provided by the osgi
* bundle at the same level than any other jars palced in the webappclassloader. * bundle at the same level than any other jars palced in the webappclassloader.
*/ */
public class WebappClassLoaderForOSGi extends WebAppClassLoader public class OSGiWebappClassLoader extends WebAppClassLoader
{ {
private Logger __logger = Log.getLogger(WebappClassLoaderForOSGi.class.getName().toString()); private Logger __logger = Log.getLogger(OSGiWebappClassLoader.class.getName().toString());
/** /**
* when a logging framework is setup in the osgi classloaders, it can access * when a logging framework is setup in the osgi classloaders, it can access
@ -66,7 +66,7 @@ public class WebappClassLoaderForOSGi extends WebAppClassLoader
private boolean _lookInOsgiFirst = true; private boolean _lookInOsgiFirst = true;
private Set<String> _libsAlreadyInManifest = new HashSet<String>(); private Set<String> _libsAlreadyInManifest = new HashSet<String>();
public WebappClassLoaderForOSGi(ClassLoader parent, WebAppContext context, Bundle contributor) throws IOException public OSGiWebappClassLoader(ClassLoader parent, WebAppContext context, Bundle contributor) throws IOException
{ {
super(parent,context); super(parent,context);
_osgiBundleClassLoader = WebappRegistrationHelper.BUNDLE_CLASS_LOADER_HELPER.getBundleClassLoader(contributor); _osgiBundleClassLoader = WebappRegistrationHelper.BUNDLE_CLASS_LOADER_HELPER.getBundleClassLoader(contributor);

View File

@ -31,9 +31,8 @@ import java.util.StringTokenizer;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import org.eclipse.jetty.deploy.ConfigurationManager; import org.eclipse.jetty.deploy.AppProvider;
import org.eclipse.jetty.deploy.ContextDeployer; import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.deploy.WebAppDeployer;
import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator; import org.eclipse.jetty.osgi.boot.JettyBootstrapActivator;
import org.eclipse.jetty.osgi.boot.OSGiWebappConstants; import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
import org.eclipse.jetty.osgi.boot.internal.jsp.TldLocatableURLClassloader; import org.eclipse.jetty.osgi.boot.internal.jsp.TldLocatableURLClassloader;
@ -64,8 +63,7 @@ import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; import org.xml.sax.SAXParseException;
/** /**
* Bridges the traditional web-application deployers: {@link WebAppDeployer} and * Bridges the jetty deployers with the OSGi lifecycle where applications are
* {@link ContextDeployer} with the OSGi lifecycle where applications are
* managed inside OSGi-bundles. * managed inside OSGi-bundles.
* <p> * <p>
* This class should be called as a consequence of the activation of a new * This class should be called as a consequence of the activation of a new
@ -74,9 +72,6 @@ import org.xml.sax.SAXParseException;
* </p> * </p>
* <p> * <p>
* Helper methods to register a bundle that is a web-application or a context. * Helper methods to register a bundle that is a web-application or a context.
* It is deployed as if the server was using its WebAppDeployer or
* ContextDeployer as configured in its etc/jetty.xml file. Well as close as
* possible to that.
* </p> * </p>
* Limitations: * Limitations:
* <ul> * <ul>
@ -111,8 +106,6 @@ class WebappRegistrationHelper
public static WebappRegistrationCustomizer JSP_REGISTRATION_HELPER = null; public static WebappRegistrationCustomizer JSP_REGISTRATION_HELPER = null;
private Server _server; private Server _server;
private ContextDeployer _ctxtDeployer;
private WebAppDeployer _webappDeployer;
private ContextHandlerCollection _ctxtHandler; private ContextHandlerCollection _ctxtHandler;
/** /**
@ -120,15 +113,17 @@ class WebappRegistrationHelper
* as a migration path and for jars that are not OSGi ready. also gives * as a migration path and for jars that are not OSGi ready. also gives
* access to the jsp jars. * access to the jsp jars.
*/ */
// private URLClassLoader _libEtcClassLoader; // private URLClassLoader _libExtClassLoader;
/** /**
* This is the class loader that should be the parent classloader of any * This is the class loader that should be the parent classloader of any
* webapp classloader. It is in fact the _libEtcClassLoader with a trick to * webapp classloader. It is in fact the _libExtClassLoader with a trick to
* let the TldScanner find the jars where the tld files are. * let the TldScanner find the jars where the tld files are.
*/ */
private URLClassLoader _commonParentClassLoaderForWebapps; private URLClassLoader _commonParentClassLoaderForWebapps;
private DeploymentManager _deploymentManager;
public WebappRegistrationHelper(Server server) public WebappRegistrationHelper(Server server)
{ {
_server = server; _server = server;
@ -262,10 +257,10 @@ class WebappRegistrationHelper
File jettyHomeF = new File(jettyHome); File jettyHomeF = new File(jettyHome);
try try
{ {
URLClassLoader libEtcClassLoader = LibExtClassLoaderHelper.createLibEtcClassLoaderHelper(jettyHomeF,_server,JettyBootstrapActivator.class URLClassLoader libExtClassLoader = LibExtClassLoaderHelper.createLibEtcClassLoaderHelper(jettyHomeF,_server,JettyBootstrapActivator.class
.getClassLoader()); .getClassLoader());
URL[] jarsWithTlds = getJarsWithTlds(); URL[] jarsWithTlds = getJarsWithTlds();
_commonParentClassLoaderForWebapps = jarsWithTlds == null?libEtcClassLoader:new TldLocatableURLClassloader(libEtcClassLoader,getJarsWithTlds()); _commonParentClassLoaderForWebapps = jarsWithTlds == null?libExtClassLoader:new TldLocatableURLClassloader(libExtClassLoader,getJarsWithTlds());
} }
catch (MalformedURLException e) catch (MalformedURLException e)
{ {
@ -276,6 +271,15 @@ class WebappRegistrationHelper
String jettyetc = System.getProperty(OSGiWebappConstants.SYS_PROP_JETTY_ETC_FILES,"etc/jetty.xml"); String jettyetc = System.getProperty(OSGiWebappConstants.SYS_PROP_JETTY_ETC_FILES,"etc/jetty.xml");
StringTokenizer tokenizer = new StringTokenizer(jettyetc,";,"); StringTokenizer tokenizer = new StringTokenizer(jettyetc,";,");
Map<Object,Object> id_map = new HashMap();
id_map.put("Server","_server");
Map<Object,Object> properties = new HashMap();
properties.put("jetty.home",jettyHome);
properties.put("jetty.host",System.getProperty("jetty.host",""));
properties.put("jetty.port",System.getProperty("jetty.port","8080"));
properties.put("jetty.port.ssl",System.getProperty("jetty.port.ssl","8443"));
while (tokenizer.hasMoreTokens()) while (tokenizer.hasMoreTokens())
{ {
String etcFile = tokenizer.nextToken().trim(); String etcFile = tokenizer.nextToken().trim();
@ -286,6 +290,7 @@ class WebappRegistrationHelper
if ("etc/jetty.xml".equals(etcFile)) if ("etc/jetty.xml".equals(etcFile))
{ {
// Missing jetty.xml file, so create a minimal Jetty configuration
__logger.info("Configuring default server on 8080"); __logger.info("Configuring default server on 8080");
SelectChannelConnector connector = new SelectChannelConnector(); SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8080); connector.setPort(8080);
@ -303,12 +308,12 @@ class WebappRegistrationHelper
{ {
try try
{ {
// Execute a Jetty configuration file
XmlConfiguration config = new XmlConfiguration(new FileInputStream(conffile)); XmlConfiguration config = new XmlConfiguration(new FileInputStream(conffile));
config.getIdMap().put("Server","_server"); config.setIdMap(id_map);
config.getProperties().put("jetty.home",jettyHome); config.setProperties(properties);
config.getProperties().put("jetty.host",System.getProperty("jetty.host","")); config.configure();
config.getProperties().put("jetty.port",System.getProperty("jetty.port","8080")); id_map=config.getIdMap();
config.getProperties().put("jetty.port.ssl",System.getProperty("jetty.port.ssl","8443"));
} }
catch (SAXParseException saxparse) catch (SAXParseException saxparse)
{ {
@ -363,27 +368,19 @@ class WebappRegistrationHelper
throw new IllegalStateException("ERROR: No ContextHandlerCollection was configured" + " with the server to add applications to." throw new IllegalStateException("ERROR: No ContextHandlerCollection was configured" + " with the server to add applications to."
+ "Using a default one is not supported at" + " this point. " + " Please review the jetty.xml file used."); + "Using a default one is not supported at" + " this point. " + " Please review the jetty.xml file used.");
} }
List<ContextDeployer> ctxtDeployers = _server.getBeans(ContextDeployer.class);
if (ctxtDeployers == null || ctxtDeployers.isEmpty()) // get a deployerManager
List<DeploymentManager> deployers = _server.getBeans(DeploymentManager.class);
if (deployers != null && !deployers.isEmpty())
{ {
System.err.println("Warn: No ContextDeployer was configured" + " with the server. Using a default one is not supported at" + " this point. " _deploymentManager = deployers.get(0);
+ " Please review the jetty.xml file used.");
} for (AppProvider provider : _deploymentManager.getAppProviders())
else {
{ // if (provider instanceof OSGiAppProvider)
_ctxtDeployer = ctxtDeployers.get(0); }
}
List<WebAppDeployer> wDeployers = _server.getBeans(WebAppDeployer.class);
if (wDeployers == null || wDeployers.isEmpty())
{
System.err.println("Warn: No WebappDeployer was configured" + " with the server. Using a default one is not supported at" + " this point. "
+ " Please review the jetty.xml file used.");
}
else
{
_webappDeployer = (WebAppDeployer)wDeployers.get(0);
} }
} }
@ -453,7 +450,7 @@ class WebappRegistrationHelper
{ {
// make sure we provide access to all the jetty bundles by going // make sure we provide access to all the jetty bundles by going
// through this bundle. // through this bundle.
WebappClassLoaderForOSGi composite = createWebappClassLoader(contributor); OSGiWebappClassLoader composite = createWebappClassLoader(contributor);
// configure with access to all jetty classes and also all the // configure with access to all jetty classes and also all the
// classes // classes
// that the contributor gives access to. // that the contributor gives access to.
@ -763,7 +760,7 @@ class WebappRegistrationHelper
{ {
// make sure we provide access to all the jetty bundles by going // make sure we provide access to all the jetty bundles by going
// through this bundle. // through this bundle.
WebappClassLoaderForOSGi composite = createWebappClassLoader(contributor); OSGiWebappClassLoader composite = createWebappClassLoader(contributor);
// configure with access to all jetty classes and also all the // configure with access to all jetty classes and also all the
// classes // classes
// that the contributor gives access to. // that the contributor gives access to.
@ -845,27 +842,8 @@ class WebappRegistrationHelper
*/ */
protected void configureWebAppContext(WebAppContext wah, Bundle contributor) protected void configureWebAppContext(WebAppContext wah, Bundle contributor)
{ {
// configure it
// wah.setContextPath(context);
String[] _configurationClasses = _webappDeployer.getConfigurationClasses();
String _defaultsDescriptor = _webappDeployer.getDefaultsDescriptor();
boolean _parentLoaderPriority = _webappDeployer.isParentLoaderPriority();
AttributesMap _contextAttributes = getWebAppDeployerContextAttributes();
if (_configurationClasses != null)
wah.setConfigurationClasses(_configurationClasses);
if (_defaultsDescriptor != null)
wah.setDefaultsDescriptor(_defaultsDescriptor);
// wah.setExtractWAR(_extract);//[H]should we force to extract ?
// wah.setWar(app.toString());//[H]should we force to extract ?
wah.setParentLoaderPriority(_parentLoaderPriority);
// set up any contextAttributes
wah.setAttributes(new AttributesMap(_contextAttributes));
// rfc66 // rfc66
wah.setAttribute(OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT,contributor.getBundleContext()); wah.setAttribute(OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT,contributor.getBundleContext());
} }
/** /**
@ -894,7 +872,6 @@ class WebappRegistrationHelper
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected ContextHandler createContextHandler(Bundle bundle, InputStream contextInputStream, String extraClasspath, String overrideBundleInstallLocation) protected ContextHandler createContextHandler(Bundle bundle, InputStream contextInputStream, String extraClasspath, String overrideBundleInstallLocation)
{ {
/* /*
* Do something identical to what the ContextDeployer would have done: * Do something identical to what the ContextDeployer would have done:
* XmlConfiguration xmlConfiguration=new * XmlConfiguration xmlConfiguration=new
@ -906,17 +883,12 @@ class WebappRegistrationHelper
* context=(ContextHandler)xmlConfiguration.configure(); * context=(ContextHandler)xmlConfiguration.configure();
* context.setAttributes(new AttributesMap(_contextAttributes)); * context.setAttributes(new AttributesMap(_contextAttributes));
*/ */
ConfigurationManager _configMgr = getContextDeployerConfigurationManager();
AttributesMap _contextAttributes = getContextDeployerContextAttributes();
try try
{ {
XmlConfiguration xmlConfiguration = new XmlConfiguration(contextInputStream); XmlConfiguration xmlConfiguration = new XmlConfiguration(contextInputStream);
HashMap properties = new HashMap(); HashMap properties = new HashMap();
properties.put("Server",_server); properties.put("Server",_server);
if (_configMgr != null)
{
properties.putAll(_configMgr.getProperties());
}
// insert the bundle's location as a property. // insert the bundle's location as a property.
setThisBundleHomeProperty(bundle,properties,overrideBundleInstallLocation); setThisBundleHomeProperty(bundle,properties,overrideBundleInstallLocation);
xmlConfiguration.setProperties(properties); xmlConfiguration.setProperties(properties);
@ -926,7 +898,6 @@ class WebappRegistrationHelper
{ {
((WebAppContext)context).setExtraClasspath(extraClasspath); ((WebAppContext)context).setExtraClasspath(extraClasspath);
} }
context.setAttributes(new AttributesMap(_contextAttributes));
// rfc-66: // rfc-66:
context.setAttribute(OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT,bundle.getBundleContext()); context.setAttribute(OSGiWebappConstants.RFC66_OSGI_BUNDLE_CONTEXT,bundle.getBundleContext());
@ -995,7 +966,7 @@ class WebappRegistrationHelper
* @param classInBundle * @param classInBundle
* @throws Exception * @throws Exception
*/ */
protected void configureWebappClassLoader(Bundle contributor, ContextHandler context, WebappClassLoaderForOSGi webappClassLoader) throws Exception protected void configureWebappClassLoader(Bundle contributor, ContextHandler context, OSGiWebappClassLoader webappClassLoader) throws Exception
{ {
if (context instanceof WebAppContext) if (context instanceof WebAppContext)
{ {
@ -1012,12 +983,12 @@ class WebappRegistrationHelper
/** /**
* No matter what the type of webapp, we create a WebappClassLoader. * No matter what the type of webapp, we create a WebappClassLoader.
*/ */
protected WebappClassLoaderForOSGi createWebappClassLoader(Bundle contributor) throws Exception protected OSGiWebappClassLoader createWebappClassLoader(Bundle contributor) throws Exception
{ {
// we use a temporary WebAppContext object. // we use a temporary WebAppContext object.
// if this is a real webapp we will set it on it a bit later: once we // if this is a real webapp we will set it on it a bit later: once we
// know. // know.
WebappClassLoaderForOSGi webappClassLoader = new WebappClassLoaderForOSGi(_commonParentClassLoaderForWebapps,new WebAppContext(),contributor); OSGiWebappClassLoader webappClassLoader = new OSGiWebappClassLoader(_commonParentClassLoaderForWebapps,new WebAppContext(),contributor);
return webappClassLoader; return webappClassLoader;
} }
@ -1041,65 +1012,5 @@ class WebappRegistrationHelper
} }
} }
// some private suff in ContextDeployer that we need to
// be faithful to the ContextDeployer definition created in etc/jetty.xml
// kindly ask to have a public getter for those?
private static Field CONTEXT_DEPLOYER_CONFIGURATION_MANAGER_FIELD = null;
private static Field CONTEXT_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD = null;
private ConfigurationManager getContextDeployerConfigurationManager()
{
try
{
if (CONTEXT_DEPLOYER_CONFIGURATION_MANAGER_FIELD == null)
{
CONTEXT_DEPLOYER_CONFIGURATION_MANAGER_FIELD = ContextDeployer.class.getDeclaredField("_configMgr");
CONTEXT_DEPLOYER_CONFIGURATION_MANAGER_FIELD.setAccessible(true);
}
return (ConfigurationManager)CONTEXT_DEPLOYER_CONFIGURATION_MANAGER_FIELD.get(_ctxtDeployer);
}
catch (Throwable t)
{
t.printStackTrace();
}
return null;
}
private AttributesMap getContextDeployerContextAttributes()
{
try
{
if (CONTEXT_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD == null)
{
CONTEXT_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD = ContextDeployer.class.getDeclaredField("_contextAttributes");
CONTEXT_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD.setAccessible(true);
}
return (AttributesMap)CONTEXT_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD.get(_ctxtDeployer);
}
catch (Throwable t)
{
t.printStackTrace();
}
return null;
}
private static Field WEBAPP_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD = null;
private AttributesMap getWebAppDeployerContextAttributes()
{
try
{
if (WEBAPP_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD == null)
{
WEBAPP_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD = WebAppDeployer.class.getDeclaredField("_contextAttributes");
WEBAPP_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD.setAccessible(true);
}
return (AttributesMap)WEBAPP_DEPLOYER_CONTEXT_ATTRIBUTES_FIELD.get(_webappDeployer);
}
catch (Throwable t)
{
t.printStackTrace();
}
return null;
}
} }