git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1204 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-01-18 22:18:15 +00:00
parent 6cdd30390a
commit 59ed68cdde
1 changed files with 160 additions and 137 deletions

View File

@ -14,7 +14,6 @@
// ========================================================================
package org.eclipse.jetty.osgi.boot;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
@ -28,85 +27,93 @@ import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.resource.Resource;
/**
* AppProvider for OSGi.
* Supports the configuration of ContextHandlers and WebApps.
* Extends the AbstractAppProvider to support the scanning of context files located outside
* of the bundles.
* AppProvider for OSGi. Supports the configuration of ContextHandlers and
* WebApps. Extends the AbstractAppProvider to support the scanning of context
* files located outside of the bundles.
* <p>
* This provider must not be called outside of jetty.boot:
* it should always be called via the OSGi service listener.
* This provider must not be called outside of jetty.boot: it should always be
* called via the OSGi service listener.
* </p>
* <p>
* This provider supports the same set of parameters than the WebAppProvider
* as it supports the deployment of WebAppContexts.
* Except for the scanning of the webapps directory.
* This provider supports the same set of parameters than the WebAppProvider as
* it supports the deployment of WebAppContexts. Except for the scanning of the
* webapps directory.
* </p>
*/
public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
{
private boolean _extractWars = false;
private boolean _parentLoaderPriority = false;
private String _defaultsDescriptor;
/**
* When a context file corresponds to a deployed bundle and is changed we
* reload the corresponding bundle.
*/
/**
* When a context file corresponds to a deployed bundle and is changed we
* reload the corresponding bundle.
*/
private static class Filter implements FilenameFilter
{
OSGiAppProvider _enclosedInstance;
public boolean accept(File dir, String name)
OSGiAppProvider _enclosedInstance;
public boolean accept(File dir, String name)
{
if (!new File(dir,name).isDirectory()) {
String contextName = getDeployedAppName(name);
if (contextName != null)
{
App app = _enclosedInstance.getDeployedApps().get(contextName);
return app != null;
}
if (!new File(dir,name).isDirectory())
{
String contextName = getDeployedAppName(name);
if (contextName != null)
{
App app = _enclosedInstance.getDeployedApps().get(contextName);
return app != null;
}
}
return false;
}
}
/**
* @param contextFileName for example myContext.xml
* @return The context, for example: myContext; null if this was not a suitable contextFileName.
* @param contextFileName
* for example myContext.xml
* @return The context, for example: myContext; null if this was not a
* suitable contextFileName.
*/
private static String getDeployedAppName(String contextFileName)
{
String lowername = contextFileName.toLowerCase();
if (lowername.endsWith(".xml")) {
String contextName = contextFileName.substring(0,lowername.length()-".xml".length());
return contextName;
private static String getDeployedAppName(String contextFileName)
{
String lowername = contextFileName.toLowerCase();
if (lowername.endsWith(".xml"))
{
String contextName = contextFileName.substring(0,lowername.length() - ".xml".length());
return contextName;
}
return null;
}
/**
* Default OSGiAppProvider consutructed when none are defined in the jetty.xml
* configuration.
* @param contextsDir
*/
public OSGiAppProvider() {
super(new Filter());
((Filter)super._filenameFilter)._enclosedInstance = this;
}
}
/**
* Default OSGiAppProvider consutructed when none are defined in the
* jetty.xml configuration.
*
* @param contextsDir
*/
public OSGiAppProvider()
{
super(new Filter());
((Filter)super._filenameFilter)._enclosedInstance = this;
}
/**
* Default OSGiAppProvider consutructed when none are defined in the
* jetty.xml configuration.
*
* @param contextsDir
*/
public OSGiAppProvider(File contextsDir) throws IOException
{
this();
setMonitoredDir(Resource.newResource(contextsDir.toURI()));
}
/**
* Default OSGiAppProvider consutructed when none are defined in the jetty.xml
* configuration.
* @param contextsDir
*/
public OSGiAppProvider(File contextsDir) throws IOException {
this();
setMonitoredDir(Resource.newResource(contextsDir.toURI()));
}
/**
* Returns the ContextHandler that was created by WebappRegistractionHelper
*
* @see AppProvider
*/
public ContextHandler createContextHandler(App app) throws Exception
@ -114,14 +121,13 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
// return pre-created Context
if (app.getContextId() != null)
{
return app.getContextHandler();
return app.getContextHandler();
}
//for some reason it was not defined when the App was constructed.
//we don't support this situation at this point.
//once the WebAppRegistrationHelper is refactored, the code
//that creates the ContextHandler will actually be here.
throw new IllegalStateException("The App must be passed the " +
"instance of the ContextHandler when it is construsted");
// for some reason it was not defined when the App was constructed.
// we don't support this situation at this point.
// once the WebAppRegistrationHelper is refactored, the code
// that creates the ContextHandler will actually be here.
throw new IllegalStateException("The App must be passed the " + "instance of the ContextHandler when it is construsted");
}
/**
@ -129,8 +135,8 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
*/
public void setDeploymentManager(DeploymentManager deploymentManager)
{
//_manager=deploymentManager;
super.setDeploymentManager(deploymentManager);
// _manager=deploymentManager;
super.setDeploymentManager(deploymentManager);
}
/**
@ -140,61 +146,66 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
public void addContext(ContextHandler context) throws Exception
{
// TODO apply configuration specific to this provider
// wrap context as an App
App app = new App(getDeploymentManager(),this,context.getDisplayName(),context);
getDeployedApps().put(context.getDisplayName(),app);
getDeploymentManager().addApp(app);
}
//TODO: refactor the WebAppRegistrationHelper to add the creation of the context directly from here.
//unless the client code took care of the creation of the ContextHandler.
// public void addContext(String contextxml) throws Exception
// {
// // TODO apply configuration specific to this provider
// // TODO construct ContextHandler
// ContextHandler context=null;
// // wrap context as an App
// OSGiApp app = new OSGiApp(_manager,this,context.getDisplayName(),context);
// _apps.put(context,app);
// _manager.addApp(app);
// }
// TODO: refactor the WebAppRegistrationHelper to add the creation of the
// context directly from here.
// unless the client code took care of the creation of the ContextHandler.
// public void addContext(String contextxml) throws Exception
// {
// // TODO apply configuration specific to this provider
// // TODO construct ContextHandler
// ContextHandler context=null;
// // wrap context as an App
// OSGiApp app = new
// OSGiApp(_manager,this,context.getDisplayName(),context);
// _apps.put(context,app);
// _manager.addApp(app);
// }
/**
* Called by the scanner of the context files directory.
* If we find the corresponding deployed App we reload it by returning the App.
* Otherwise we return null and nothing happens: presumably the corresponding OSGi webapp
* is not ready yet.
* @return the corresponding already deployed App so that it will be reloaded.
* Otherwise returns null.
* Called by the scanner of the context files directory. If we find the
* corresponding deployed App we reload it by returning the App. Otherwise
* we return null and nothing happens: presumably the corresponding OSGi
* webapp is not ready yet.
*
* @return the corresponding already deployed App so that it will be
* reloaded. Otherwise returns null.
*/
@Override
protected App createApp(String filename) {
//find the corresponding bundle and ContextHandler or WebAppContext
//and reload the corresponding App.
//see the 2 pass of the refactoring of the WebAppRegistrationHelper.
String name = getDeployedAppName(filename);
if (name != null)
{
return getDeployedApps().get(name);
}
return null;
}
protected App createApp(String filename)
{
// find the corresponding bundle and ContextHandler or WebAppContext
// and reload the corresponding App.
// see the 2 pass of the refactoring of the WebAppRegistrationHelper.
String name = getDeployedAppName(filename);
if (name != null)
{
return getDeployedApps().get(name);
}
return null;
}
public void removeContext(ContextHandler context) throws Exception
public void removeContext(ContextHandler context) throws Exception
{
App app = getDeployedApps().remove(context.getDisplayName());
if (app != null)
{
getDeploymentManager().removeApp(app);
getDeploymentManager().removeApp(app);
}
}
////copied from WebAppProvider as the parameters are identical.
////only removed the parameer related to extractWars.
// //copied from WebAppProvider as the parameters are identical.
// //only removed the parameer related to extractWars.
/* ------------------------------------------------------------ */
/** Get the parentLoaderPriority.
/**
* Get the parentLoaderPriority.
*
* @return the parentLoaderPriority
*/
public boolean isParentLoaderPriority()
@ -203,8 +214,11 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
}
/* ------------------------------------------------------------ */
/** Set the parentLoaderPriority.
* @param parentLoaderPriority the parentLoaderPriority to set
/**
* Set the parentLoaderPriority.
*
* @param parentLoaderPriority
* the parentLoaderPriority to set
*/
public void setParentLoaderPriority(boolean parentLoaderPriority)
{
@ -212,7 +226,9 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
}
/* ------------------------------------------------------------ */
/** Get the defaultsDescriptor.
/**
* Get the defaultsDescriptor.
*
* @return the defaultsDescriptor
*/
public String getDefaultsDescriptor()
@ -221,8 +237,11 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
}
/* ------------------------------------------------------------ */
/** Set the defaultsDescriptor.
* @param defaultsDescriptor the defaultsDescriptor to set
/**
* Set the defaultsDescriptor.
*
* @param defaultsDescriptor
* the defaultsDescriptor to set
*/
public void setDefaultsDescriptor(String defaultsDescriptor)
{
@ -230,39 +249,44 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
}
/**
* The context xml directory.
* In fact it is the directory watched by the scanner.
* The context xml directory. In fact it is the directory watched by the
* scanner.
*/
public File getContextXmlDirAsFile()
{
try {
Resource monitoredDir = getMonitoredDir();
if (monitoredDir == null)
return null;
return monitoredDir.getFile();
} catch (IOException e) {
e.printStackTrace();
return null;
}
try
{
Resource monitoredDir = getMonitoredDir();
if (monitoredDir == null)
return null;
return monitoredDir.getFile();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
/* ------------------------------------------------------------ */
/**
* The context xml directory.
* In fact it is the directory watched by the scanner.
* The context xml directory. In fact it is the directory watched by the
* scanner.
*/
public String getContextXmlDir()
{
try {
Resource monitoredDir = getMonitoredDir();
if (monitoredDir == null)
return null;
return monitoredDir.getFile().toURI().toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
try
{
Resource monitoredDir = getMonitoredDir();
if (monitoredDir == null)
return null;
return monitoredDir.getFile().toURI().toString();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
/* ------------------------------------------------------------ */
@ -270,17 +294,16 @@ public class OSGiAppProvider extends ScanningAppProvider implements AppProvider
* Set the directory in which to look for context XML files.
* <p>
* If a webapp call "foo/" or "foo.war" is discovered in the monitored
* directory, then the ContextXmlDir is examined to see if a foo.xml
* file exists. If it does, then this deployer will not deploy the webapp
* and the ContextProvider should be used to act on the foo.xml file.
* directory, then the ContextXmlDir is examined to see if a foo.xml file
* exists. If it does, then this deployer will not deploy the webapp and the
* ContextProvider should be used to act on the foo.xml file.
*
* @see ContextProvider
* @param contextsDir
*/
public void setContextXmlDir(String contextsDir)
{
setMonitoredDir(contextsDir);
setMonitoredDir(contextsDir);
}
}