-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
innokenty
committed
Mar 24, 2015
0 parents
commit d18e724
Showing
21 changed files
with
983 additions
and
0 deletions.
There are no files selected for viewing
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,9 @@ | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
target | ||
.idea | ||
*.iml | ||
*.ipr | ||
*.iws |
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,41 @@ | ||
# BeanLoader | ||
|
||
[![release](http://github-release-version.herokuapp.com/github/yandex-qatools/beanloader/release.svg?style=flat)](https://github.com/yandex-qatools/beanloader/releases/latest) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/ru.yandex.qatools.beanloader/beanloader/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/ru.yandex.qatools.beanloader/beanloader) | ||
|
||
This small library provides an easy fluent API to load xml beans | ||
from different sources via JAXB, giving some of them higher priority | ||
than to the others. | ||
|
||
### Maven | ||
|
||
```xml | ||
<dependency> | ||
<groupId>ru.yandex.qatools.beanloader</groupId> | ||
<artifactId>beanloader</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
``` | ||
|
||
### Usage | ||
|
||
```java | ||
import static ru.qatools.beanloader.BeanLoaderStrategies.*; | ||
import static ru.qatools.beanloader.BeanLoader.*; | ||
|
||
BeanLoader<Bean> beanLoader = load(Bean.class) | ||
.from(resource("bean.xml")) | ||
.from(url("http://example.com?get-my-bean-dawg")) | ||
.from(file("~/beans/bean.xml")) | ||
.from(fileWithWatcher("/etc/beans/", "bean.xml")); | ||
|
||
// load bean iterating over the given strategies | ||
// until one of the returns a non-null bean | ||
Bean bean = beanLoader.getBean(); | ||
makeSomeStuff(bean); | ||
|
||
// reload the bean, if reloads are specified for any strategy | ||
// returns the same object if no reloads are specified | ||
bean = beanLoader.getBean(); | ||
makeAnotherStuff(bean); | ||
|
||
``` |
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,139 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.sonatype.oss</groupId> | ||
<artifactId>oss-parent</artifactId> | ||
<version>7</version> | ||
</parent> | ||
|
||
<groupId>ru.yandex.qatools.beanloader</groupId> | ||
<artifactId>beanloader</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>Yandex QATools BeanLoader</name> | ||
|
||
<scm> | ||
<connection>scm:git:[email protected]:yandex-qatools/beanloader.git</connection> | ||
<developerConnection>scm:git:[email protected]:yandex-qatools/beanloader.git</developerConnection> | ||
<url>https://github.com/yandex-qatools/beanloader</url> | ||
</scm> | ||
|
||
<url>https://github.com/yandex-qatools/beanloader</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.7</java.version> | ||
</properties> | ||
|
||
<organization> | ||
<name>Yandex</name> | ||
<url>http://company.yandex.com</url> | ||
</organization> | ||
|
||
<licenses> | ||
<license> | ||
<name>The Apache Software License, Version 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<issueManagement> | ||
<system>GitHub Issue Tracker</system> | ||
<url>https://github.com/yandex-qatools/beanloader/issues</url> | ||
</issueManagement> | ||
|
||
<ciManagement> | ||
<system>Jenkins</system> | ||
<url>http://jenkins.qatools.ru/</url> | ||
</ciManagement> | ||
|
||
<developers> | ||
<developer> | ||
<id>innokenty</id> | ||
<name>Innokenty Shuvalov</name> | ||
<email>[email protected]</email> | ||
<organization>Yandex</organization> | ||
</developer> | ||
</developers> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.0</version> | ||
<configuration> | ||
<source>${java.version}</source> | ||
<target>${java.version}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<version>2.2.1</version> | ||
<executions> | ||
<execution> | ||
<id>attach-sources</id> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>jaxb2-maven-plugin</artifactId> | ||
<version>1.6</version> | ||
<executions> | ||
<execution> | ||
<id>generate test bean</id> | ||
<goals> | ||
<goal>testXjc</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<packageName>ru.qatools.beanloader</packageName> | ||
<schemaDirectory>src/test/resources</schemaDirectory> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>1.7.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<version>1.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>ru.yandex.qatools.matchers</groupId> | ||
<artifactId>matcher-decorators</artifactId> | ||
<version>1.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.googlecode.lambdaj</groupId> | ||
<artifactId>lambdaj</artifactId> | ||
<version>2.3.3</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
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,40 @@ | ||
package ru.qatools.beanloader; | ||
|
||
import ru.qatools.beanloader.internal.BeanLoadStrategy; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Innokenty Shuvalov [email protected] | ||
*/ | ||
public class BeanLoader<T> { | ||
|
||
private final List<BeanLoadStrategy> strategies = new ArrayList<>(); | ||
|
||
private final Class<T> beanClass; | ||
|
||
private BeanLoader(Class<T> beanClass) { | ||
this.beanClass = beanClass; | ||
} | ||
|
||
public static <T> BeanLoader<T> load(Class<T> beanClass) { | ||
return new BeanLoader<>(beanClass); | ||
} | ||
|
||
public BeanLoader<T> from(BeanLoadStrategy strategy) { | ||
strategies.add(strategy); | ||
return this; | ||
} | ||
|
||
public T getBean() { | ||
for (BeanLoadStrategy strategy : strategies) { | ||
@SuppressWarnings("unchecked") | ||
T bean = (T) strategy.getBeanAs(beanClass); | ||
if (bean != null) { | ||
return bean; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/ru/qatools/beanloader/BeanLoaderStrategies.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,49 @@ | ||
package ru.qatools.beanloader; | ||
|
||
import ru.qatools.beanloader.internal.*; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
/** | ||
* @author Innokenty Shuvalov [email protected] | ||
*/ | ||
public abstract class BeanLoaderStrategies { | ||
|
||
public static FileLoadStrategy file(String filename) { | ||
return file(filename, false); | ||
} | ||
|
||
public static FileLoadStrategy file(String filename, boolean reloadEveryTime) { | ||
return file(new File(filename), reloadEveryTime); | ||
} | ||
|
||
public static FileLoadStrategy file(File file, boolean reloadEveryTime) { | ||
return new FileLoadStrategy(file, reloadEveryTime); | ||
} | ||
|
||
public static FileWithWatcherLoadStrategy fileWithWatcher(String directoryToWatch, String fileToWatch) { | ||
return new FileWithWatcherLoadStrategy(directoryToWatch, fileToWatch); | ||
} | ||
|
||
public static UrlLoadStrategy url(URL url) { | ||
return url(url, false); | ||
} | ||
|
||
static BeanLoadStrategy url(String url) throws MalformedURLException { | ||
return url(new URL(url)); | ||
} | ||
|
||
static BeanLoadStrategy url(String url, boolean reloadEveryTime) throws MalformedURLException { | ||
return url(new URL(url), reloadEveryTime); | ||
} | ||
|
||
public static UrlLoadStrategy url(URL url, boolean reloadEveryTime) { | ||
return new UrlLoadStrategy(url, reloadEveryTime); | ||
} | ||
|
||
public static ResourceLoadStrategy resource(String resource) { | ||
return new ResourceLoadStrategy(resource); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/ru/qatools/beanloader/internal/BeanLoadStrategy.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,61 @@ | ||
package ru.qatools.beanloader.internal; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.xml.bind.DataBindingException; | ||
|
||
/** | ||
* This class could be generified but it is not. The purpose of that is | ||
* to increase the 'fluency' of the {@link ru.qatools.beanloader.BeanLoader} API. Note that | ||
* with the proper usage through BeanLoader this code can not lead to any | ||
* ClassCastExceptions, although it is not fully generified and has class casts | ||
* in the code. This is fine because the BeanLoader itself is generified properly. | ||
* | ||
* @author Innokenty Shuvalov [email protected] | ||
*/ | ||
public abstract class BeanLoadStrategy { | ||
|
||
protected final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private Object bean; | ||
private boolean loaded; | ||
|
||
public Object getBeanAs(Class beanClass) { | ||
if (!loaded || reloadEveryTime()) { | ||
loadBean(beanClass); | ||
} | ||
return bean; | ||
} | ||
|
||
protected void loadBean(Class beanClass) { | ||
loaded = true; | ||
logger.trace("trying to load bean from " + getSourceDescription()); | ||
if (!canUnmarshal()) { | ||
logger.trace("source does not exist, aborting"); | ||
return; | ||
} | ||
|
||
try { | ||
logger.trace("source exists, trying to unmarshal"); | ||
bean = performUnmarshal(beanClass); | ||
logger.trace("successfully unmarshalled"); | ||
} catch (DataBindingException e) { | ||
logUnmarshallingException(e, getSourceDescription()); | ||
} | ||
} | ||
|
||
private void logUnmarshallingException(Exception e, String sourceDescription) { | ||
if (logger.isDebugEnabled()) { | ||
logger.error("exception caught while unmarshalling from " + sourceDescription, e); | ||
} else { | ||
logger.error(String.format("exception caught while unmarshalling from %s: %s", | ||
sourceDescription, e.getMessage())); | ||
} | ||
} | ||
|
||
protected abstract boolean canUnmarshal(); | ||
protected abstract boolean reloadEveryTime(); | ||
protected abstract Object performUnmarshal(Class beanClass); | ||
protected abstract String getSourceDescription(); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/ru/qatools/beanloader/internal/FileLoadStrategy.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,38 @@ | ||
package ru.qatools.beanloader.internal; | ||
|
||
import javax.xml.bind.JAXB; | ||
import java.io.File; | ||
|
||
/** | ||
* @author Innokenty Shuvalov [email protected] | ||
*/ | ||
public class FileLoadStrategy extends BeanLoadStrategy { | ||
|
||
private final File file; | ||
private final boolean reload; | ||
|
||
public FileLoadStrategy(File file, boolean reload) { | ||
super(); | ||
this.file = file; | ||
this.reload = reload; | ||
} | ||
|
||
@Override | ||
protected boolean canUnmarshal() { | ||
return file.exists(); | ||
} | ||
|
||
protected boolean reloadEveryTime() { | ||
return reload; | ||
} | ||
|
||
@Override | ||
protected Object performUnmarshal(Class beanClass) { | ||
return JAXB.unmarshal(file, beanClass); | ||
} | ||
|
||
@Override | ||
protected String getSourceDescription() { | ||
return "file '" + file.getPath() + "'"; | ||
} | ||
} |
Oops, something went wrong.