Skip to content
Permalink
Browse files

Make sure BeansXmlHandler remains compatible with JBoss AS 7.1.1.Final

  • Loading branch information...
jharting committed Oct 16, 2012
1 parent bceb1a2 commit db355e7a165a38ab5c6fc0045b3616bb8b10a59f
@@ -34,6 +34,7 @@
import org.jboss.as.weld.deployment.CdiAnnotationProcessor;
import org.jboss.as.weld.deployment.processors.BeanArchiveProcessor;
import org.jboss.as.weld.deployment.processors.BeansXmlProcessor;
import org.jboss.as.weld.deployment.processors.LegacyBeansXmlProcessor;
import org.jboss.as.weld.deployment.processors.WebIntegrationProcessor;
import org.jboss.as.weld.deployment.processors.WeldBeanManagerServiceProcessor;
import org.jboss.as.weld.deployment.processors.WeldComponentIntegrationProcessor;
@@ -66,7 +67,11 @@ protected void execute(DeploymentProcessorTarget processorTarget) {
DeploymentProcessorRegistrar registrar = new DeploymentProcessorRegistrar(processorTarget);
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.DEPENDENCIES, Phase.DEPENDENCIES_WELD, new WeldDependencyProcessor());
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_CDI_ANNOTATIONS, new CdiAnnotationProcessor());
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_WELD_DEPLOYMENT, new BeansXmlProcessor());
if (ApplicationServerVersion.isEqualOrNewer(ApplicationServerVersion.AS712Final)) {
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_WELD_DEPLOYMENT, new BeansXmlProcessor());
} else {
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.PARSE, Phase.PARSE_WELD_DEPLOYMENT, new LegacyBeansXmlProcessor());
}
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_WEB_INTEGRATION, new WebIntegrationProcessor());
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_BEAN_ARCHIVE, new BeanArchiveProcessor());
registrar.addDeploymentProcessor(WeldExtension.SUBSYSTEM_NAME, Phase.POST_MODULE, Phase.POST_MODULE_WELD_PORTABLE_EXTENSIONS, new WeldPortableExtensionProcessor());
@@ -118,7 +118,6 @@ protected static String trim(String str) {
private final List<Metadata<Filter>> includes;
private final List<Metadata<Filter>> excludes;
private final URL file;
final PropertyReplacer propertyReplacer;

/*
* Parser State
@@ -128,9 +127,8 @@ protected static String trim(String str) {
private StringBuilder buffer;
private Locator locator;

public BeansXmlHandler(final URL file, final PropertyReplacer propertyReplacer) {
public BeansXmlHandler(final URL file) {
this.file = file;
this.propertyReplacer = propertyReplacer;
this.interceptors = new ArrayList<Metadata<String>>();
this.decorators = new ArrayList<Metadata<String>>();
this.alternativeClasses = new ArrayList<Metadata<String>>();
@@ -144,7 +142,7 @@ public BeansXmlHandler(final URL file, final PropertyReplacer propertyReplacer)
@Override
public void processEndChildElement(String uri, String localName, String qName, String nestedText) {
if (isInNamespace(uri) && "class".equals(localName)) {
interceptors.add(new XmlMetadata<String>(qName, propertyReplacer.replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
interceptors.add(new XmlMetadata<String>(qName, replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
}
}

@@ -159,7 +157,7 @@ public void handleMultiple() {
@Override
public void processEndChildElement(String uri, String localName, String qName, String nestedText) {
if (isInNamespace(uri) && "class".equals(localName)) {
decorators.add(new XmlMetadata<String>(qName, propertyReplacer.replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
decorators.add(new XmlMetadata<String>(qName, replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
}
}

@@ -174,9 +172,9 @@ public void handleMultiple() {
@Override
public void processEndChildElement(String uri, String localName, String qName, String nestedText) {
if (isInNamespace(uri) && "class".equals(localName)) {
alternativeClasses.add(new XmlMetadata<String>(qName, propertyReplacer.replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
alternativeClasses.add(new XmlMetadata<String>(qName, replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
} else if (isInNamespace(uri) && "stereotype".equals(localName)) {
alternativeStereotypes.add(new XmlMetadata<String>(qName, propertyReplacer.replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
alternativeStereotypes.add(new XmlMetadata<String>(qName, replaceProperties(trim(nestedText)), file, locator.getLineNumber()));
}
}

@@ -328,4 +326,8 @@ public void error(SAXParseException e) throws SAXException {
WeldLogger.DEPLOYMENT_LOGGER.beansXmlValidationError(file, e.getLineNumber(), e.getMessage());
}

protected String replaceProperties(String text) {
return text; // noop by default
}

}
@@ -58,7 +58,7 @@

private static final InputSource[] EMPTY_INPUT_SOURCE_ARRAY = new InputSource[0];

public BeansXml parse(final URL beansXml, final PropertyReplacer propertyReplacer) throws DeploymentUnitProcessingException {
public BeansXml parse(final URL beansXml) throws DeploymentUnitProcessingException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
@@ -81,7 +81,7 @@ public BeansXml parse(final URL beansXml, final PropertyReplacer propertyReplace
// The file is just acting as a marker file
return EMPTY_BEANS_XML;
}
BeansXmlHandler handler = new BeansXmlHandler(beansXml, propertyReplacer);
BeansXmlHandler handler = getHandler(beansXml);

try {
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
@@ -109,7 +109,7 @@ public BeansXml parse(final URL beansXml, final PropertyReplacer propertyReplace
}
}

public BeansXml parse(Iterable<URL> urls, final PropertyReplacer propertyReplacer) throws DeploymentUnitProcessingException {
public BeansXml parse(Iterable<URL> urls) throws DeploymentUnitProcessingException {
List<Metadata<String>> alternativeStereotypes = new ArrayList<Metadata<String>>();
List<Metadata<String>> alternativeClasses = new ArrayList<Metadata<String>>();
List<Metadata<String>> decorators = new ArrayList<Metadata<String>>();
@@ -118,7 +118,7 @@ public BeansXml parse(Iterable<URL> urls, final PropertyReplacer propertyReplace
List<Metadata<Filter>> excludes = new ArrayList<Metadata<Filter>>();
URL beansXmlUrl = null;
for (URL url : urls) {
BeansXml beansXml = parse(url, propertyReplacer);
BeansXml beansXml = parse(url);
alternativeStereotypes.addAll(beansXml.getEnabledAlternativeStereotypes());
alternativeClasses.addAll(beansXml.getEnabledAlternativeClasses());
decorators.addAll(beansXml.getEnabledDecorators());
@@ -159,5 +159,9 @@ private static InputSource loadXsd(String name, ClassLoader classLoader) {
}
}

protected BeansXmlHandler getHandler(final URL beansXml) {
return new BeansXmlHandler(beansXml);
}


}
@@ -0,0 +1,44 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.as.weld.deployment;

import java.net.URL;

import org.jboss.metadata.property.PropertyReplacer;

/**
* BeansXmlHandler that uses {@link PropertyReplacer} to replace properties.
*
* @author Jozef Hartinger
* @author Stuart Douglas
*
*/
public class ReplacingBeansXmlHandler extends BeansXmlHandler {

private final PropertyReplacer replacer;

public ReplacingBeansXmlHandler(URL file, PropertyReplacer replacer) {
super(file);
this.replacer = replacer;
}

@Override
protected String replaceProperties(String text) {
return replacer.replaceProperties(text);
}

}
@@ -21,122 +21,27 @@
*/
package org.jboss.as.weld.deployment.processors;

import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.net.URL;

import org.jboss.as.ee.structure.DeploymentType;
import org.jboss.as.ee.structure.DeploymentTypeMarker;
import org.jboss.as.ee.structure.SpecDescriptorPropertyReplacement;
import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.server.deployment.SubDeploymentMarker;
import org.jboss.as.server.deployment.module.ModuleRootMarker;
import org.jboss.as.server.deployment.module.ResourceRoot;
import org.jboss.as.weld.WeldDeploymentMarker;
import org.jboss.as.weld.WeldLogger;
import org.jboss.as.weld.WeldMessages;
import org.jboss.as.weld.deployment.BeanArchiveMetadata;
import org.jboss.as.weld.deployment.BeansXmlHandler;
import org.jboss.as.weld.deployment.BeansXmlParser;
import org.jboss.as.weld.deployment.WeldDeploymentMetadata;
import org.jboss.vfs.VirtualFile;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.as.weld.deployment.ReplacingBeansXmlHandler;

/**
* Deployment processor that finds <literal>beans.xml</literal> files and attaches the information to the deployment
*
* @author Stuart Douglas
*/
public class BeansXmlProcessor implements DeploymentUnitProcessor {

private static final String WEB_INF_BEANS_XML = "WEB-INF/beans.xml";
private static final String META_INF_BEANS_XML = "META-INF/beans.xml";
public class BeansXmlProcessor extends LegacyBeansXmlProcessor {

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();

BeansXmlParser parser = new BeansXmlParser();

Set<BeanArchiveMetadata> beanArchiveMetadata = new HashSet<BeanArchiveMetadata>();
ResourceRoot deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT);
if (deploymentRoot == null) {
return;
}

ResourceRoot classesRoot = null;
List<ResourceRoot> structure = deploymentUnit.getAttachmentList(Attachments.RESOURCE_ROOTS);
for (ResourceRoot resourceRoot : structure) {
if (ModuleRootMarker.isModuleRoot(resourceRoot) && !SubDeploymentMarker.isSubDeployment(resourceRoot)) {
if (resourceRoot.getRootName().equals("classes")) {
// hack for dealing with war modules
classesRoot = resourceRoot;
} else {
VirtualFile beansXml = resourceRoot.getRoot().getChild(META_INF_BEANS_XML);
if (beansXml.exists() && beansXml.isFile()) {
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", beansXml.toString());
beanArchiveMetadata.add(new BeanArchiveMetadata(beansXml, resourceRoot, parseBeansXml(beansXml,
parser, deploymentUnit), false));
}
}
protected BeansXmlParser getBeansXmlParser(final DeploymentUnit deploymentUnit) {
return new BeansXmlParser() {
protected BeansXmlHandler getHandler(final URL beansXml) {
return new ReplacingBeansXmlHandler(beansXml, SpecDescriptorPropertyReplacement.propertyReplacer(deploymentUnit));
}
}

if (DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
if (classesRoot != null) {
// look for WEB-INF/beans.xml
final VirtualFile rootBeansXml = deploymentRoot.getRoot().getChild(WEB_INF_BEANS_XML);
final boolean rootBeansXmlPresent = rootBeansXml.exists() && rootBeansXml.isFile();
// look for beans.xml files in the wrong location
final VirtualFile beansXml = classesRoot.getRoot().getChild(META_INF_BEANS_XML);
final boolean beansXmlPresent = beansXml.exists() && beansXml.isFile();

if (rootBeansXmlPresent) {
if (beansXmlPresent) {
// warn that it is not portable to use both locations at the same time
WeldLogger.DEPLOYMENT_LOGGER.duplicateBeansXml();
}
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", rootBeansXml);
beanArchiveMetadata.add(new BeanArchiveMetadata(rootBeansXml, classesRoot, parseBeansXml(rootBeansXml, parser, deploymentUnit), true));
} else if (beansXmlPresent) {
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", beansXml);
beanArchiveMetadata.add(new BeanArchiveMetadata(beansXml, classesRoot, parseBeansXml(beansXml, parser, deploymentUnit), true));
}
}
} else if (!DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
final VirtualFile rootBeansXml = deploymentRoot.getRoot().getChild(META_INF_BEANS_XML);
if (rootBeansXml.exists() && rootBeansXml.isFile()) {
WeldLogger.DEPLOYMENT_LOGGER.debugf("Found beans.xml: %s", rootBeansXml.toString());
beanArchiveMetadata.add(new BeanArchiveMetadata(rootBeansXml, deploymentRoot, parseBeansXml(rootBeansXml, parser, deploymentUnit), true));
}
}

if (!beanArchiveMetadata.isEmpty()) {
WeldDeploymentMetadata deploymentMetadata = new WeldDeploymentMetadata(beanArchiveMetadata);
deploymentUnit.putAttachment(WeldDeploymentMetadata.ATTACHMENT_KEY, deploymentMetadata);
// mark the deployment as requiring CDI integration
WeldDeploymentMarker.mark(deploymentUnit);
if (deploymentUnit.getParent() != null) {
WeldDeploymentMarker.mark(deploymentUnit.getParent());
}
}
}

@Override
public void undeploy(DeploymentUnit context) {

}

private BeansXml parseBeansXml(VirtualFile beansXmlFile, BeansXmlParser parser, final DeploymentUnit deploymentUnit) throws DeploymentUnitProcessingException {
try {
return parser.parse(beansXmlFile.asFileURL(), SpecDescriptorPropertyReplacement.propertyReplacer(deploymentUnit));
} catch (MalformedURLException e) {
throw WeldMessages.MESSAGES.couldNotGetBeansXmlAsURL(beansXmlFile.toString(), e);
}
};
}

}
}

0 comments on commit db355e7

Please sign in to comment.
You can’t perform that action at this time.