Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Build validators from a list of configuration locations
Showing
8 changed files
with
260 additions
and
4 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/uk/org/iay/incommon/validator/ValidatorConfiguration.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,56 @@ | ||
/* | ||
* 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 uk.org.iay.incommon.validator; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import uk.org.iay.incommon.validator.context.ValidatorCollection; | ||
|
||
/** | ||
* Validator configuration. | ||
*/ | ||
@Configuration | ||
public class ValidatorConfiguration { | ||
|
||
/** Class logger. */ | ||
private static final Logger LOG = LoggerFactory.getLogger(ValidatorConfiguration.class); | ||
|
||
/** Property value telling us which validator configurations to load. */ | ||
@Value("${validator.configurations}") | ||
private String configurations; | ||
|
||
/** | ||
* Build the validator configuration collection. | ||
* | ||
* @param webContext the web application context | ||
* @return the {@link ValidatorCollection} bean | ||
*/ | ||
@Bean | ||
public ValidatorCollection validatorCollection(final ApplicationContext webContext) { | ||
final ValidatorCollection c = new ValidatorCollection(webContext); | ||
LOG.info("loading validator configurations: '{}'", configurations); | ||
for (final String configLocation : configurations.split("\\s+")) { | ||
c.add(configLocation); | ||
} | ||
return c; | ||
} | ||
|
||
} |
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
123 changes: 123 additions & 0 deletions
123
src/main/java/uk/org/iay/incommon/validator/context/ValidatorCollection.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,123 @@ | ||
/* | ||
* 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 uk.org.iay.incommon.validator.context; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
/** | ||
* Represents a collection of validators. | ||
*/ | ||
public class ValidatorCollection { | ||
|
||
/** | ||
* An entry representing a single available validator. | ||
*/ | ||
public static class Entry { | ||
|
||
/** Identifier for the validator. */ | ||
private final String id; | ||
|
||
/** Description for the validator. */ | ||
private final String description; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ctx {@link ApplicationContext} containing the validator. | ||
*/ | ||
public Entry(final ApplicationContext ctx) { | ||
id = ctx.getBean("id", String.class); | ||
description = ctx.getBean("description", String.class); | ||
} | ||
|
||
/** | ||
* Return the validator's identifier. | ||
* | ||
* @return the validator's identifier. | ||
*/ | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Return the validator's description. | ||
* | ||
* @return the validator's description | ||
*/ | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
} | ||
|
||
/** Class logger. */ | ||
private static final Logger LOG = LoggerFactory.getLogger(ValidatorCollection.class); | ||
|
||
/** Parent context to set on all the ones we load. */ | ||
private final ApplicationContext parentContext; | ||
|
||
/** All entries as a list. */ | ||
private final List<Entry> entries = new ArrayList<>(); | ||
|
||
/** All entries indexed by identifier. */ | ||
private final Map<String, Entry> byId = new HashMap<>(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ctx parent {@link ApplicationContext} for all validators | ||
*/ | ||
public ValidatorCollection(final ApplicationContext ctx) { | ||
parentContext = ctx; | ||
} | ||
|
||
/** | ||
* Return a {@link List} of all entries. | ||
* | ||
* @return {@link List} of all entries | ||
*/ | ||
public List<Entry> getEntries() { | ||
return entries; | ||
} | ||
|
||
/** | ||
* Build an {@link ApplicationContext} from the indicated configuration, | ||
* and add it to the collection. | ||
* | ||
* @param configLocation classpath location for the {@link ApplicationContext}'s | ||
* configuration | ||
*/ | ||
public void add(final String configLocation) { | ||
LOG.info("loading context {}", configLocation); | ||
final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(); | ||
ctx.setConfigLocation(configLocation); | ||
ctx.setParent(parentContext); | ||
ctx.setDisplayName(configLocation); | ||
ctx.refresh(); | ||
LOG.info("refreshed {} has {} beans", configLocation, ctx.getBeanDefinitionCount()); | ||
final Entry entry = new Entry(ctx); | ||
entries.add(entry); | ||
byId.put(entry.getId(), entry); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/uk/org/iay/incommon/validator/context/package-info.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,18 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Context creation and manipulation for the metadata validator service. | ||
*/ | ||
package uk.org.iay.incommon.validator.context; |
18 changes: 18 additions & 0 deletions
18
src/main/java/uk/org/iay/incommon/validator/package-info.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,18 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Metadata validator service. | ||
*/ | ||
package uk.org.iay.incommon.validator; |
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
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,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
default-lazy-init="true" | ||
xmlns:c="http://www.springframework.org/schema/c" | ||
xmlns:p="http://www.springframework.org/schema/p" | ||
xmlns:util="http://www.springframework.org/schema/util" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> | ||
|
||
<bean id="id" class="java.lang.String" c:_="default"/> | ||
<bean id="description" class="java.lang.String" c:_="Default validator."/> | ||
|
||
</beans> |
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,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
default-lazy-init="true" | ||
xmlns:c="http://www.springframework.org/schema/c" | ||
xmlns:p="http://www.springframework.org/schema/p" | ||
xmlns:util="http://www.springframework.org/schema/util" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation=" | ||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> | ||
|
||
<bean id="id" class="java.lang.String" c:_="second"/> | ||
<bean id="description" class="java.lang.String" c:_="Second validator."/> | ||
|
||
</beans> |