You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by va...@apache.org on 2010/10/19 13:03:37 UTC
svn commit: r1024200 - in /geronimo/server/trunk/plugins/bval:
geronimo-bval-builder/src/main/java/org/apache/geronimo/bval/deployment/BValModuleBuilderExtension.java
geronimo-bval/src/main/java/org/apache/geronimo/bval/ValidatorFactoryGBean.java
Author: vamsic007
Date: Tue Oct 19 11:03:37 2010
New Revision: 1024200
URL: http://svn.apache.org/viewvc?rev=1024200&view=rev
Log:
GERONIMO-5222 Add support for application validation descriptor support for deployed applications
o When the modules are packaged in an EAR, additional processing is required to obtain the validation descriptors
o Each module has its own ValidatorFactory. So, we need to keep track of the module names too in case of EARs.
Modified:
geronimo/server/trunk/plugins/bval/geronimo-bval-builder/src/main/java/org/apache/geronimo/bval/deployment/BValModuleBuilderExtension.java
geronimo/server/trunk/plugins/bval/geronimo-bval/src/main/java/org/apache/geronimo/bval/ValidatorFactoryGBean.java
Modified: geronimo/server/trunk/plugins/bval/geronimo-bval-builder/src/main/java/org/apache/geronimo/bval/deployment/BValModuleBuilderExtension.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/bval/geronimo-bval-builder/src/main/java/org/apache/geronimo/bval/deployment/BValModuleBuilderExtension.java?rev=1024200&r1=1024199&r2=1024200&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/bval/geronimo-bval-builder/src/main/java/org/apache/geronimo/bval/deployment/BValModuleBuilderExtension.java (original)
+++ geronimo/server/trunk/plugins/bval/geronimo-bval-builder/src/main/java/org/apache/geronimo/bval/deployment/BValModuleBuilderExtension.java Tue Oct 19 11:03:37 2010
@@ -93,6 +93,7 @@ public class BValModuleBuilderExtension
return;
}
+ String moduleName = null;
String validationConfig = null;
// the location of the validation config varies depending
// on the module type
@@ -104,7 +105,14 @@ public class BValModuleBuilderExtension
}
if(validationConfig != null) {
- if(bundle.getEntry(validationConfig) == null) {
+ URL validationConfigEntry = null;
+ if(module.isStandAlone()) {
+ validationConfigEntry = bundle.getEntry(validationConfig);
+ } else {
+ moduleName = module.getTargetPath();
+ validationConfigEntry = module.getDeployable().getResource(validationConfig);
+ }
+ if(validationConfigEntry == null) {
// No validation.xml file
validationConfig = null;
} else {
@@ -117,7 +125,7 @@ public class BValModuleBuilderExtension
JAXBContext jc = JAXBContext.newInstance(ValidationConfigType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(schema);
- inp = bundle.getEntry(validationConfig).openStream();
+ inp = validationConfigEntry.openStream();
StreamSource stream = new StreamSource(inp);
JAXBElement<ValidationConfigType> root = unmarshaller.unmarshal(stream, ValidationConfigType.class);
ValidationConfigType xmlConfig = root.getValue();
@@ -133,7 +141,11 @@ public class BValModuleBuilderExtension
InputStream inp1 = null;
try {
jc = JAXBContext.newInstance(ConstraintMappingsType.class);
- inp1 = bundle.getEntry(mappingFileName).openStream();
+ if(module.isStandAlone()) {
+ inp1 = bundle.getEntry(mappingFileName).openStream();
+ } else {
+ inp1 = module.getDeployable().getResource(mappingFileName).openStream();
+ }
stream = new StreamSource(inp1);
unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(mappingSchema);
@@ -163,6 +175,7 @@ public class BValModuleBuilderExtension
EARContext moduleContext = module.getEarContext();
AbstractName abstractName = moduleContext.getNaming().createChildName(module.getModuleName(), "ValidatorFactory", NameFactory.VALIDATOR_FACTORY);
GBeanData gbeanData = new GBeanData(abstractName, ValidatorFactoryGBean.class);
+ gbeanData.setAttribute("moduleName", moduleName);
gbeanData.setAttribute("validationConfig", validationConfig);
try {
moduleContext.addGBean(gbeanData);
Modified: geronimo/server/trunk/plugins/bval/geronimo-bval/src/main/java/org/apache/geronimo/bval/ValidatorFactoryGBean.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/bval/geronimo-bval/src/main/java/org/apache/geronimo/bval/ValidatorFactoryGBean.java?rev=1024200&r1=1024199&r2=1024200&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/bval/geronimo-bval/src/main/java/org/apache/geronimo/bval/ValidatorFactoryGBean.java (original)
+++ geronimo/server/trunk/plugins/bval/geronimo-bval/src/main/java/org/apache/geronimo/bval/ValidatorFactoryGBean.java Tue Oct 19 11:03:37 2010
@@ -16,8 +16,11 @@
*/
package org.apache.geronimo.bval;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.net.URL;
import javax.validation.ConstraintValidatorFactory;
@@ -46,6 +49,8 @@ import org.apache.geronimo.gbean.annotat
import org.apache.geronimo.gbean.annotation.ParamSpecial;
import org.apache.geronimo.gbean.annotation.SpecialAttributeType;
import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
+import org.apache.geronimo.kernel.util.FileUtils;
+import org.apache.geronimo.kernel.util.IOUtils;
import org.apache.geronimo.naming.ResourceSource;
import org.osgi.framework.Bundle;
import org.slf4j.Logger;
@@ -70,23 +75,30 @@ public class ValidatorFactoryGBean imple
private final String objectName;
private final Bundle bundle;
private final ClassLoader classLoader;
+ // module file name if the module is not standalone module. Will be null otherwise.
+ private final String moduleName;
// module validation configuration
private String validationConfig;
// The created ValidatorFactory
- private ValidatorFactory factory;
+ private ValidatorFactory factory;
+ // Temporary file to hold the extracted archive in case of nested archives
+ private File tmpArchiveFile;
/**
* Construct an instance of ValidatorFactoryGBean
* <p/>
+ * @param moduleName the name of the module in an EAR file (null implies standalone module)
* @param validationConfig the location of validation configuration xml file in the bundle
*/
public ValidatorFactoryGBean(@ParamSpecial(type = SpecialAttributeType.objectName) String objectName,
@ParamSpecial(type = SpecialAttributeType.bundle) Bundle bundle,
@ParamSpecial(type = SpecialAttributeType.classLoader) ClassLoader classLoader,
+ @ParamAttribute(name = "moduleName") String moduleName,
@ParamAttribute(name = "validationConfig") String validationConfig) {
this.objectName = objectName;
this.bundle = bundle;
this.classLoader = classLoader;
+ this.moduleName = moduleName;
this.validationConfig = validationConfig;
}
@@ -110,7 +122,6 @@ public class ValidatorFactoryGBean imple
factory = Validation.buildDefaultValidatorFactory();
} else {
// Parse the validation xml
- // TODO: Should this processing happen at build time and raise deployment exceptions?
ValidationConfigType validationConfigType = null;
URL schemaUrl = ValidationParser.class.getClassLoader().getResource("META-INF/validation-configuration-1.0.xsd");
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
@@ -119,7 +130,31 @@ public class ValidatorFactoryGBean imple
JAXBContext jc = JAXBContext.newInstance(ValidationConfigType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setSchema(schema);
- StreamSource stream = new StreamSource(bundle.getEntry(validationConfig).openStream());
+ URL validationConfigURL = null;
+ if(moduleName == null) {
+ // Standalone module
+ validationConfigURL = bundle.getEntry(validationConfig);
+ } else {
+ if(bundle.getEntry(moduleName+"/"+validationConfig) != null) {
+ // The archive is extracted and repacked as directories
+ validationConfigURL = bundle.getEntry(moduleName+"/"+validationConfig);
+ } else {
+ // Extract archive file and get the validation config entry out
+ // Copy the archive to a temp file for runtime processing
+ try {
+ tmpArchiveFile = FileUtils.createTempFile(".jar");
+ InputStream inp = bundle.getEntry(moduleName).openStream();
+ OutputStream out = new FileOutputStream(tmpArchiveFile);
+ IOUtils.copy(inp, out);
+ inp.close();
+ out.close();
+ validationConfigURL = new URL("jar:"+tmpArchiveFile.toURI().toURL()+"!/"+validationConfig);
+ } catch (IOException e) {
+ throw new ValidationException("Error processing validation configuration "+validationConfig+" in "+moduleName, e);
+ }
+ }
+ }
+ StreamSource stream = new StreamSource(validationConfigURL.openStream());
JAXBElement<ValidationConfigType> root = unmarshaller.unmarshal(stream, ValidationConfigType.class);
validationConfigType = root.getValue();
} catch(Throwable t) {
@@ -134,6 +169,9 @@ public class ValidatorFactoryGBean imple
config.ignoreXmlConfiguration();
// Create the factory instance
factory = config.buildValidatorFactory();
+ if(tmpArchiveFile != null) {
+ tmpArchiveFile.delete();
+ }
}
}
return factory;
@@ -200,12 +238,20 @@ public class ValidatorFactoryGBean imple
log.debug("Opening input stream for " + mappingFileName);
InputStream in = null;
try {
- in = bundle.getEntry(mappingFileName).openStream();
+ if(moduleName == null) {
+ // standalone module
+ in = bundle.getEntry(mappingFileName).openStream();
+ } else if(tmpArchiveFile != null) {
+ // The archive was extracted to a temporary file earlier
+ in = new URL("jar:"+tmpArchiveFile.toURI().toURL()+"!/"+mappingFileName).openStream();
+ } else {
+ in = bundle.getEntry(moduleName+"/"+mappingFileName).openStream();
+ }
if (in == null) {
- throw new ValidationException("Unable to open input stream for mapping file " + mappingFileName);
+ throw new ValidationException("Unable to open input stream for mapping file " + mappingFileName + (moduleName != null ? " in "+moduleName : ""));
}
} catch (IOException e) {
- throw new ValidationException("Unable to open input stream for mapping file " + mappingFileName, e);
+ throw new ValidationException("Unable to open input stream for mapping file " + mappingFileName + (moduleName != null ? " in "+moduleName : ""), e);
}
target.addMapping(in);
}