package net.sourceforge.javautil.web.server.application.ext;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import net.sourceforge.javautil.classloader.impl.ClassContext;
import net.sourceforge.javautil.classloader.resolver.IClassPackage;
import net.sourceforge.javautil.classloader.resolver.IClassPackageDependencyReference;
import net.sourceforge.javautil.classloader.resolver.IClassPackageDescriptor;
import net.sourceforge.javautil.classloader.resolver.impl.ClassPackageDependencyReferenceImpl;
import net.sourceforge.javautil.classloader.source.CompositeClassSource;
import net.sourceforge.javautil.common.ReflectionUtil;
import net.sourceforge.javautil.common.reflection.cache.ClassCache;
import net.sourceforge.javautil.common.reflection.cache.ClassDescriptor;
import net.sourceforge.javautil.common.reflection.cache.ClassProperty;
import net.sourceforge.javautil.web.server.application.IWebApplication;
import net.sourceforge.javautil.web.server.application.WebApplicationDeploymentContext;
import net.sourceforge.javautil.web.server.descriptor.IWebXml;
import net.sourceforge.javautil.web.server.descriptor.impl.WebXml;
/**
* The base for most extension implementations.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: WebApplicationExtensionAbstract.java 2298 2010-06-16 00:20:18Z ponderator $
*/
public abstract class WebApplicationExtensionAbstract implements IWebApplicationExtension {
protected final IWebApplication application;
protected final String name;
protected IClassPackageDescriptor descriptor;
protected List<IClassPackageDependencyReference> dependencies;
public WebApplicationExtensionAbstract(String name, IWebApplication application) {
this.application = application;
this.name = name;
}
public String getName() { return this.name; }
public boolean shouldLoad(IWebXml webXml) { return true; }
public IWebApplication getWebApplication() { return this.application; }
public void setup(WebXml webXml, WebApplicationDeploymentContext ctx) {}
public void cleanup(WebApplicationDeploymentContext ctx) {}
public List<? extends IClassPackageDependencyReference> getClassPackageDependencies() { return this.dependencies; }
public boolean isArePrimaryDependencies() { return false; }
public CompositeClassSource getNonClassPackageDependencies () { return null; }
public IClassPackageDescriptor getDescriptor() { return this.descriptor; }
/**
* This will check for settings in the provided web.xml for each
* real property in this implementation class, and if it exists
* attempt to coerce the string value to the real type of the
* property.
*
* @param webXml The web xml to use for settings
*/
public void applyWebXmlSettings (IWebXml webXml) {
ClassDescriptor<?> cd = ClassCache.getFor(getClass());
Map<String, ClassProperty> properties = cd.getProperties();
List<String> prefixes = this.getSettingPrefixes();
Map<String, String> parameters = webXml.getInitParameters();
for (String name : properties.keySet()) {
for (String prefix : prefixes) {
if (parameters.containsKey(prefix + name)) {
ClassProperty property = properties.get(name);
property.setValue(this, ReflectionUtil.coerceString(property.getType(), parameters.get(prefix + name)));
break;
}
}
}
}
/**
* @return A list of prefixes that can be used in a web.xml to set/override settings along with real properties in the implementation class
* used by {@link #applyWebXmlSettings(IWebXml)}
*/
public List<String> getSettingPrefixes () {
List<String> prefixes = new ArrayList<String>();
prefixes.add(name + ".");
prefixes.add(getClass().getPackage().getName() + ".");
return prefixes;
}
/**
* This assumes no classifier.
*
* @see #addDependency(String, String, String, String)
*/
public ClassPackageDependencyReferenceImpl addDependency (String groupId, String artifactId, String version) {
return this.addDependency(groupId, artifactId, version, null);
}
/**
* Facility in order to add {@link IClassPackage} based dependencies.
*
* @param groupId The group of the package
* @param artifactId The artifact of the package
* @param version The version of the package
* @param classifier The classifier of the package
* @return A reference to the created dependency reference
*/
public ClassPackageDependencyReferenceImpl addDependency (String groupId, String artifactId, String version, String classifier) {
if (this.dependencies == null) this.dependencies = new ArrayList<IClassPackageDependencyReference>();
ClassPackageDependencyReferenceImpl dep = new ClassPackageDependencyReferenceImpl(groupId, artifactId, version, classifier);
this.dependencies.add(dep);
return dep;
}
}