-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FORGE-1179 Implemented the Validation setup command
- Loading branch information
1 parent
6705777
commit 13298f8
Showing
9 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
javaee/api/src/main/java/org/jboss/forge/addon/javaee/descriptor/ValidationDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.descriptor; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.shrinkwrap.descriptor.api.Descriptor; | ||
|
||
/** | ||
* Bean Validation configuration descriptor | ||
* | ||
* @author Kevin Pollet | ||
*/ | ||
public interface ValidationDescriptor extends Descriptor | ||
{ | ||
ValidationDescriptor setDefaultProvider(String defaultProvider); | ||
|
||
ValidationDescriptor setMessageInterpolator(String messageInterpolator); | ||
|
||
ValidationDescriptor setTraversableResolver(String traversableResolver); | ||
|
||
ValidationDescriptor setConstraintValidatorFactory(String constraintValidatorFactory); | ||
|
||
ValidationDescriptor setConstraintMapping(String constraintMapping); | ||
|
||
String getDefaultProvider(); | ||
|
||
String getMessageInterpolator(); | ||
|
||
String getTraversableResolver(); | ||
|
||
String getConstraintValidatorFactory(); | ||
|
||
List<String> getConstraintMappings(); | ||
} |
25 changes: 25 additions & 0 deletions
25
javaee/api/src/main/java/org/jboss/forge/addon/javaee/facets/ValidationFacet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.facets; | ||
|
||
import org.jboss.forge.addon.javaee.JavaEEFacet; | ||
import org.jboss.forge.addon.javaee.descriptor.ValidationDescriptor; | ||
import org.jboss.forge.addon.resource.FileResource; | ||
|
||
/** | ||
* Facet representing JSR-303 capabilities for Bean Validation | ||
* | ||
* @author Kevin Pollet | ||
*/ | ||
public interface ValidationFacet extends JavaEEFacet | ||
{ | ||
ValidationDescriptor getConfig(); | ||
|
||
FileResource<?> getConfigFile(); | ||
|
||
void saveConfig(ValidationDescriptor descriptor); | ||
} |
28 changes: 28 additions & 0 deletions
28
...pi/src/main/java/org/jboss/forge/addon/javaee/validation/provider/ValidationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.validation.provider; | ||
|
||
import java.util.Set; | ||
|
||
import org.jboss.forge.addon.dependencies.Dependency; | ||
import org.jboss.forge.addon.javaee.descriptor.ValidationDescriptor; | ||
import org.jboss.forge.furnace.services.Exported; | ||
|
||
/** | ||
* @author Kevin Pollet | ||
*/ | ||
@Exported | ||
public interface ValidationProvider | ||
{ | ||
String getName(); | ||
|
||
ValidationDescriptor getDefaultDescriptor(); | ||
|
||
Set<Dependency> getDependencies(); | ||
|
||
Set<Dependency> getAdditionalDependencies(); | ||
} |
114 changes: 114 additions & 0 deletions
114
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/validation/ValidationFacetImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2012 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.validation; | ||
|
||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.forge.addon.javaee.AbstractJavaEEFacet; | ||
import org.jboss.forge.addon.dependencies.Dependency; | ||
import org.jboss.forge.addon.dependencies.builder.DependencyBuilder; | ||
import org.jboss.forge.addon.projects.dependencies.DependencyInstaller; | ||
import org.jboss.forge.addon.projects.facets.ResourcesFacet; | ||
import org.jboss.forge.addon.resource.FileResource; | ||
import org.jboss.forge.addon.javaee.facets.ValidationFacet; | ||
import org.jboss.forge.addon.javaee.descriptor.ValidationDescriptor; | ||
import org.jboss.forge.furnace.versions.SingleVersion; | ||
import org.jboss.forge.furnace.versions.Version; | ||
import org.jboss.shrinkwrap.descriptor.api.DescriptorImporter; | ||
import org.jboss.shrinkwrap.descriptor.api.Descriptors; | ||
|
||
/** | ||
* @author Kevin Pollet | ||
*/ | ||
public class ValidationFacetImpl extends AbstractJavaEEFacet implements ValidationFacet | ||
{ | ||
private final Dependency JAVAX_VALIDATION_API = DependencyBuilder | ||
.create("javax.validation:validation-api"); | ||
|
||
@Inject | ||
public ValidationFacetImpl(final DependencyInstaller installer) | ||
{ | ||
super(installer); | ||
} | ||
|
||
@Override | ||
public boolean install() | ||
{ | ||
if (!isInstalled()) | ||
{ | ||
FileResource<?> descriptor = getConfigFile(); | ||
if(!descriptor.exists()) | ||
{ | ||
saveConfig(Descriptors.create(ValidationDescriptor.class)); | ||
} | ||
} | ||
return super.install(); | ||
} | ||
|
||
@Override | ||
public boolean isInstalled() | ||
{ | ||
return getConfigFile().exists() && super.isInstalled(); | ||
} | ||
|
||
@Override | ||
public Version getSpecVersion() | ||
{ | ||
return new SingleVersion("1.0"); | ||
} | ||
|
||
@Override | ||
protected Map<Dependency, List<Dependency>> getRequiredDependencyOptions() | ||
{ | ||
return Collections.singletonMap(JAVAX_VALIDATION_API, Arrays.asList(JAVAX_VALIDATION_API)); | ||
} | ||
|
||
/* | ||
* Facet methods | ||
*/ | ||
|
||
@Override | ||
public ValidationDescriptor getConfig() | ||
{ | ||
DescriptorImporter<ValidationDescriptor> importer = Descriptors.importAs(ValidationDescriptor.class); | ||
final FileResource<?> configFile = getConfigFile(); | ||
if (!configFile.exists()) | ||
{ | ||
createDefaultConfig(configFile); | ||
} | ||
ValidationDescriptor descriptor = importer.fromStream(configFile.getResourceInputStream()); | ||
return descriptor; | ||
} | ||
|
||
@Override | ||
public FileResource<?> getConfigFile() | ||
{ | ||
ResourcesFacet resources = getFaceted().getFacet(ResourcesFacet.class); | ||
return resources.getResource("META-INF" + File.separator + "validation.xml"); | ||
} | ||
|
||
@Override | ||
public void saveConfig(final ValidationDescriptor descriptor) | ||
{ | ||
final FileResource<?> fileResource = getConfigFile(); | ||
fileResource.createNewFile(); | ||
fileResource.setContents(descriptor.exportAsString()); | ||
} | ||
|
||
private void createDefaultConfig(FileResource<?> descriptor) | ||
{ | ||
ValidationDescriptor descriptorContents = Descriptors.create(ValidationDescriptor.class); | ||
descriptor.setContents(descriptorContents.exportAsString()); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
javaee/impl/src/main/java/org/jboss/forge/addon/javaee/validation/ValidationOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.jboss.forge.addon.javaee.validation; | ||
|
||
import java.util.Set; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.jboss.forge.addon.dependencies.Dependency; | ||
import org.jboss.forge.addon.dependencies.builder.DependencyBuilder; | ||
import org.jboss.forge.addon.facets.FacetFactory; | ||
import org.jboss.forge.addon.javaee.descriptor.ValidationDescriptor; | ||
import org.jboss.forge.addon.javaee.facets.ValidationFacet; | ||
import org.jboss.forge.addon.javaee.validation.provider.ValidationProvider; | ||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.projects.dependencies.DependencyInstaller; | ||
import org.jboss.shrinkwrap.descriptor.api.Descriptors; | ||
|
||
public class ValidationOperations | ||
{ | ||
|
||
@Inject | ||
private DependencyInstaller installer; | ||
|
||
@Inject | ||
private FacetFactory facetFactory; | ||
|
||
public void setup(Project project, ValidationProvider provider, boolean provided, String messageInterpolator, | ||
String traversableResolver, String constraintValidatorFactory) throws Exception | ||
{ | ||
if (project != null) | ||
{ | ||
ValidationFacet facet = facetFactory.install(project, ValidationFacet.class); | ||
|
||
String scopeType = provided?"PROVIDED":"COMPILE"; | ||
installDependencies(project, provider.getDependencies(), scopeType); | ||
installDependencies(project, provider.getAdditionalDependencies(), scopeType); | ||
|
||
if (provider.getDefaultDescriptor() != null) | ||
{ | ||
final ValidationDescriptor providerDescriptor = provider.getDefaultDescriptor(); | ||
final ValidationDescriptor descriptor = Descriptors.create(ValidationDescriptor.class); | ||
String defaultProvider = providerDescriptor.getDefaultProvider(); | ||
if (defaultProvider != null && !defaultProvider.isEmpty()) | ||
{ | ||
descriptor.setDefaultProvider(defaultProvider); | ||
} | ||
if (messageInterpolator != null) | ||
{ | ||
descriptor.setMessageInterpolator(messageInterpolator); | ||
} | ||
if (traversableResolver != null) | ||
{ | ||
descriptor.setTraversableResolver(traversableResolver); | ||
} | ||
if (constraintValidatorFactory != null) | ||
{ | ||
descriptor.setConstraintValidatorFactory(constraintValidatorFactory); | ||
} | ||
|
||
project.getFacet(ValidationFacet.class).saveConfig(descriptor); | ||
} | ||
ValidationDescriptor config = facet.getConfig(); | ||
|
||
facet.saveConfig(config); | ||
} | ||
} | ||
|
||
private void installDependencies(Project project,final Set<Dependency> dependencies, String scopeType) | ||
{ | ||
for (Dependency dep : dependencies) | ||
{ | ||
if (!installer.isInstalled(project, dep)) | ||
{ | ||
dep = DependencyBuilder.create(dep).setScopeType(scopeType); | ||
installer.install(project, dep); | ||
} | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
.../java/org/jboss/forge/addon/javaee/validation/providers/ApacheBeanValidationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.javaee.validation.providers; | ||
|
||
import static java.util.Collections.unmodifiableSet; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
import org.jboss.forge.addon.dependencies.Dependency; | ||
import org.jboss.forge.addon.dependencies.builder.DependencyBuilder; | ||
import org.jboss.forge.addon.javaee.descriptor.ValidationDescriptor; | ||
import org.jboss.forge.addon.javaee.validation.provider.ValidationProvider; | ||
import org.jboss.shrinkwrap.descriptor.api.Descriptors; | ||
|
||
/** | ||
* @author Kevin Pollet | ||
*/ | ||
public class ApacheBeanValidationProvider implements ValidationProvider | ||
{ | ||
private static final String PROVIDER_NAME = "Apache Bean Validation"; | ||
private final ValidationDescriptor defaultDescriptor; | ||
private final Set<Dependency> dependencies; | ||
|
||
public ApacheBeanValidationProvider() | ||
{ | ||
// define apache bean validation default descriptor file | ||
this.defaultDescriptor = Descriptors.create(ValidationDescriptor.class) | ||
.setDefaultProvider("org.apache.bval.jsr303.ApacheValidationProvider") | ||
.setMessageInterpolator("org.apache.bval.jsr303.DefaultMessageInterpolator") | ||
.setTraversableResolver("org.apache.bval.jsr303.resolver.DefaultTraversableResolver") | ||
.setConstraintValidatorFactory("org.apache.bval.jsr303.DefaultConstraintValidatorFactory"); | ||
|
||
// add apache bean validation dependencies | ||
final DependencyBuilder apacheBeanValidation = DependencyBuilder.create() | ||
.setGroupId("org.apache.bval") | ||
.setArtifactId("org.apache.bval.bundle"); | ||
|
||
final Set<Dependency> tmpSet = new LinkedHashSet<Dependency>(); | ||
tmpSet.add(apacheBeanValidation); | ||
|
||
this.dependencies = unmodifiableSet(tmpSet); | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return PROVIDER_NAME; | ||
} | ||
|
||
@Override | ||
public ValidationDescriptor getDefaultDescriptor() | ||
{ | ||
return defaultDescriptor; | ||
} | ||
|
||
@Override | ||
public Set<Dependency> getDependencies() | ||
{ | ||
return dependencies; | ||
} | ||
|
||
@Override | ||
public Set<Dependency> getAdditionalDependencies() | ||
{ | ||
return Collections.emptySet(); | ||
} | ||
} |
Oops, something went wrong.