public void addGBeans(EARContext earContext, Module module, Bundle bundle, Collection repository) throws DeploymentException {
EARContext moduleContext = module.getEarContext();
Bundle webBundle = moduleContext.getDeploymentBundle();
AbstractName moduleName = module.getModuleName();
WebModule webModule = (WebModule) module;
WebApp webApp = webModule.getSpecDD();
TomcatWebAppType tomcatWebApp = (TomcatWebAppType) webModule.getVendorDD();
GBeanData webModuleData = new GBeanData(moduleName, TomcatWebAppContext.class);
configureBasicWebModuleAttributes(webApp, tomcatWebApp, moduleContext, earContext, webModule, webModuleData);
String contextPath = webModule.getContextRoot();
if (!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
try {
module.addGBean(webModuleData);
Map<String, String> contextAttributes = new HashMap<String, String>();
webModuleData.setAttribute("contextPath", contextPath);
// unsharableResources, applicationManagedSecurityResources
GBeanResourceEnvironmentBuilder rebuilder = new GBeanResourceEnvironmentBuilder(webModuleData);
//N.B. use earContext not moduleContext
resourceEnvironmentSetter.setResourceEnvironment(rebuilder, webApp.getResourceRef(), tomcatWebApp.getResourceRefArray());
if (tomcatWebApp.isSetWebContainer()) {
AbstractNameQuery webContainerName = ENCConfigBuilder.getGBeanQuery(GBeanInfoBuilder.DEFAULT_J2EE_TYPE, tomcatWebApp.getWebContainer());
webModuleData.setReferencePattern("Container", webContainerName);
} else {
webModuleData.setReferencePattern("Container", tomcatContainerName);
}
// Process the Tomcat container-config elements
if (tomcatWebApp.isSetHost()) {
String virtualServer = tomcatWebApp.getHost().trim();
webModuleData.setAttribute("virtualServer", virtualServer);
}
if (tomcatWebApp.isSetCrossContext()) {
contextAttributes.put("crossContext", "true");
}
if (tomcatWebApp.isSetWorkDir()) {
String workDir = tomcatWebApp.getWorkDir();
contextAttributes.put("workDir", workDir);
}
if (tomcatWebApp.isSetDisableCookies()) {
contextAttributes.put("cookies", "false");
}
if (tomcatWebApp.isSetTomcatRealm()) {
String tomcatRealm = tomcatWebApp.getTomcatRealm().trim();
AbstractName realmName = earContext.getNaming().createChildName(moduleName, tomcatRealm, RealmGBean.GBEAN_INFO.getJ2eeType());
webModuleData.setReferencePattern("TomcatRealm", realmName);
}
if (tomcatWebApp.isSetValveChain()) {
String valveChain = tomcatWebApp.getValveChain().trim();
AbstractName valveName = earContext.getNaming().createChildName(moduleName, valveChain, ValveGBean.J2EE_TYPE);
webModuleData.setReferencePattern("TomcatValveChain", valveName);
}
if (tomcatWebApp.isSetListenerChain()) {
String listenerChain = tomcatWebApp.getListenerChain().trim();
AbstractName listenerName = earContext.getNaming().createChildName(moduleName, listenerChain, LifecycleListenerGBean.J2EE_TYPE);
webModuleData.setReferencePattern("LifecycleListenerChain", listenerName);
}
if (tomcatWebApp.isSetCluster()) {
String cluster = tomcatWebApp.getCluster().trim();
AbstractName clusterName = earContext.getNaming().createChildName(moduleName, cluster, CatalinaClusterGBean.J2EE_TYPE);
webModuleData.setReferencePattern("Cluster", clusterName);
}
if (tomcatWebApp.isSetManager()) {
String manager = tomcatWebApp.getManager().trim();
AbstractName managerName = earContext.getNaming().createChildName(moduleName, manager, ManagerGBean.J2EE_TYPE);
webModuleData.setReferencePattern(TomcatWebAppContext.GBEAN_REF_MANAGER_RETRIEVER, managerName);
}
Boolean distributable = !webApp.getDistributable().isEmpty();
if (TRUE == distributable) {
clusteringBuilders.build(tomcatWebApp, earContext, moduleContext);
if (null == webModuleData.getReferencePatterns(TomcatWebAppContext.GBEAN_REF_CLUSTERED_VALVE_RETRIEVER)) {
log.warn("No clustering builders configured: app will not be clustered");
}
}
WebAppInfoBuilder webAppInfoBuilder = new WebAppInfoBuilder(webApp, webAppInfoFactory);
WebAppInfo webAppInfo = webAppInfoBuilder.build();
webModuleData.setAttribute("webAppInfo", webAppInfo);
webModule.getSharedContext().put(WebModule.WEB_APP_INFO, webAppInfoBuilder);
//Add context attributes and parameters
if (tomcatWebApp.isSetContext()) {
TomcatContextType context = tomcatWebApp.getContext();
NamedNodeMap namedNodeMap = context.getDomNode().getAttributes();
for (int i = 0; i < namedNodeMap.getLength(); i++) {
Node node = namedNodeMap.item(i);
String attributeName = node.getNodeName();
if (INGORED_CONTEXT_ATTRIBUTE_NAMES.contains(attributeName.toLowerCase())) {
if (log.isWarnEnabled()) {
log.warn("Context attribute " + attributeName + " in the geronimo-web.xml is ignored, as it is not support or Geronimo has already configured it");
}
continue;
}
if (contextAttributes.containsKey(attributeName)) {
if (log.isWarnEnabled()) {
log.warn("Context attribute " + attributeName
+ " on the context element in geronimo-web.xml is ignored, as it has been explicitly configured with other elements in the geronimo-web.xml file");
}
continue;
}
contextAttributes.put(node.getNodeName(), node.getNodeValue());
}
for (TomcatParameterType parameterType : context.getParameterArray()) {
if (webAppInfo.contextParams.containsKey(parameterType.getName()) && !parameterType.getOverride()) {
if (log.isWarnEnabled()) {
log.warn("Context parameter from geronimo-web.xml is ignored, as a same name context paramter " + parameterType.getName() + " = "
+ webAppInfo.contextParams.get(parameterType.getName()) + " in web.xml, configure override with true to make the value take effect.");
}
continue;
}
webAppInfo.contextParams.put(parameterType.getName(), parameterType.getValue());
}
}
/**
* The old geronimo-web.xml also support to configure some context attributes individually,
* let's override them in the contextAttributes
*/
webModuleData.setAttribute("contextAttributes", contextAttributes);
//Handle the role permissions and webservices on the servlets.
Map<String, AbstractName> webServices = new HashMap<String, AbstractName>();
Class<?> baseServletClass;
try {
baseServletClass = webBundle.loadClass(Servlet.class.getName());
} catch (ClassNotFoundException e) {
throw new DeploymentException("Could not load javax.servlet.Servlet in bundle " + bundle, e);
}
for (org.apache.openejb.jee.Servlet servlet : webApp.getServlet()) {
String servletClassName = servlet.getServletClass();
if(servletClassName == null || servletClassName.length() == 0) {
continue;
}
String servletName = servlet.getServletName();
Class<?> servletClass;
try {
servletClass = webBundle.loadClass(servletClassName);
} catch (ClassNotFoundException e) {
throw new DeploymentException("Could not load servlet class " + servletClassName, e);
}
if (!baseServletClass.isAssignableFrom(servletClass)) {
//fake servletData
AbstractName servletAbstractName = moduleContext.getNaming().createChildName(moduleName, servletName, NameFactory.SERVLET);
GBeanData servletData = new GBeanData();
servletData.setAbstractName(servletAbstractName);
//let the web service builder deal with configuring the gbean with the web service stack
//Here we just extract the factory reference
boolean configured = false;
for (WebServiceBuilder serviceBuilder : webServiceBuilder) {
if (serviceBuilder.configurePOJO(servletData, servletName, module, servletClassName, moduleContext)) {
configured = true;
break;
}
}
if (!configured) {
throw new DeploymentException("POJO web service: " + servletName + " not configured by any web service builder");
}
ReferencePatterns patterns = servletData.getReferencePatterns("WebServiceContainerFactory");
AbstractName wsContainerFactoryName = patterns.getAbstractName();
webServices.put(servletName, wsContainerFactoryName);
//force all the factories to start before the web app that needs them.
webModuleData.addDependency(wsContainerFactoryName);
}
}
webModuleData.setAttribute("webServices", webServices);
if (tomcatWebApp.isSetSecurityRealmName()) {
if (earContext.getSecurityConfiguration() == null) {
throw new DeploymentException("You have specified a <security-realm-name> for the webapp " + moduleName + " but no <security> configuration (role mapping) is supplied in the Geronimo plan for the web application (or the Geronimo plan for the EAR if the web app is in an EAR)");
}
SecurityHolder securityHolder = new SecurityHolder();
String securityRealmName = tomcatWebApp.getSecurityRealmName().trim();
webModuleData.setReferencePattern("RunAsSource", GeronimoSecurityBuilderImpl.ROLE_MAPPER_DATA_NAME.get(earContext.getGeneralData()));
webModuleData.setReferencePattern("ConfigurationFactory", new AbstractNameQuery(null, Collections.singletonMap("name", securityRealmName), ConfigurationFactory.class.getName()));
/**
* TODO - go back to commented version when possible.
*/
String policyContextID = moduleName.toString().replaceAll("[, :]", "_");
securityHolder.setPolicyContextID(policyContextID);
/*
* For web applications, we would not calculate permissions in the deployment time, as it is allowed to update in Servlet 3.0 on the initialize step
ComponentPermissions componentPermissions = buildSpecSecurityConfig(webApp);
earContext.addSecurityContext(policyContextID, componentPermissions);
*/
//TODO WTF is this for?
securityHolder.setSecurity(true);
webModuleData.setAttribute("securityHolder", securityHolder);
//local jaspic configuration
if (tomcatWebApp.isSetAuthentication()) {
AuthenticationWrapper authType = new TomcatAuthenticationWrapper(tomcatWebApp.getAuthentication());
configureLocalJaspicProvider(authType, contextPath, module, webModuleData);
}
}
//Save Deployment Attributes
Map<String, Object> deploymentAttributes = new HashMap<String, Object>();
deploymentAttributes.put(WebApplicationConstants.META_COMPLETE, webApp.isMetadataComplete());
deploymentAttributes.put(WebApplicationConstants.SCHEMA_VERSION, INITIAL_WEB_XML_SCHEMA_VERSION.get(webModule.getEarContext().getGeneralData()));
deploymentAttributes.put(WebApplicationConstants.ORDERED_LIBS, AbstractWebModuleBuilder.ORDERED_LIBS.get(webModule.getEarContext().getGeneralData()));
deploymentAttributes.put(WebApplicationConstants.SERVLET_CONTAINER_INITIALIZERS, AbstractWebModuleBuilder.SERVLET_CONTAINER_INITIALIZERS.get(webModule.getEarContext().getGeneralData()));
webModuleData.setAttribute("deploymentAttributes", deploymentAttributes);
//listeners added directly to the StandardContext will get loaded by the tomcat classloader, not the app classloader!
//TODO this may definitely not be the best place for this!
for (ModuleBuilderExtension mbe : moduleBuilderExtensions) {