Commit f46177d9 authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Merge branch 'release/2.0.0'

parents 32296caa 80d3355f
Pipeline #141 skipped
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<groupId>de.cebitec.common</groupId> <groupId>de.cebitec.common</groupId>
<artifactId>dbxref</artifactId> <artifactId>dbxref</artifactId>
<version>1.3.0</version> <version>2.0.0</version>
<packaging>bundle</packaging> <packaging>bundle</packaging>
<name>DbXRef library</name> <name>DbXRef library</name>
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
<scm> <scm>
<developerConnection>scm:git:https://git.computational.bio.uni-giessen.de/gendb3/lib-dbxref.git</developerConnection> <developerConnection>scm:git:https://git.computational.bio.uni-giessen.de/gendb3/lib-dbxref.git</developerConnection>
<tag>HEAD</tag> <tag>HEAD</tag>
</scm> </scm>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...@@ -96,6 +96,11 @@ ...@@ -96,6 +96,11 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<version>2.4</version> <version>2.4</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -119,8 +124,8 @@ ...@@ -119,8 +124,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version> <version>3.1</version>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding> <encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-Xlint:unchecked</compilerArgument> <compilerArgument>-Xlint:unchecked</compilerArgument>
...@@ -147,6 +152,16 @@ ...@@ -147,6 +152,16 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>external.atlassian.jgitflow</groupId>
<artifactId>jgitflow-maven-plugin</artifactId>
<version>1.0-m5.1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<localOnly>true</localOnly>
<noDeploy>true</noDeploy>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
\ No newline at end of file
...@@ -14,22 +14,40 @@ ...@@ -14,22 +14,40 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.cebitec.common.dbxref; package de.cebitec.common.dbxref.api;
import de.cebitec.common.dbxref.api.DbXRef; import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource; import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.api.Resource.Type;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.ServiceLoader;
/** /**
* A DbXRefService resolves dbxrefs to resources. In order to obtain a dbxref use the factory methods. * A DbXRefService resolves dbxrefs to resources. In order to obtain a dbxref use the factory methods.
* *
* @see #create(CharSequence) * @see #create(CharSequence)
* @see #create(CharSequence, CharSequence) * @see #create(CharSequence, CharSequence)
* *
* @author Lukas Jelonek {@literal <Lukas.Jelonek at computational.bio.uni-giessen.de>} * @author Lukas Jelonek {@literal <Lukas.Jelonek at computational.bio.uni-giessen.de>}
*/ */
public interface DbXRefService { public interface DbXRefService {
/**
* Retrieves an instance of DbXRefService via the serviceloader.
*
* @return
*/
static DbXRefService getInstance() {
ServiceLoader<DbXRefService> loader = ServiceLoader.load(DbXRefService.class);
final Iterator<DbXRefService> it = loader.iterator();
if (it.hasNext()) {
return it.next();
} else {
throw new IllegalStateException("No DbXRefService provided");
}
}
/** /**
* Factory method for database cross references. Can instantiate any database cross reference, regardless of a valid * Factory method for database cross references. Can instantiate any database cross reference, regardless of a valid
* database or valid identifier. * database or valid identifier.
...@@ -61,11 +79,10 @@ public interface DbXRefService { ...@@ -61,11 +79,10 @@ public interface DbXRefService {
* Resolves resource links for the given reference and filtered by the given type. If multiple resolvers are * Resolves resource links for the given reference and filtered by the given type. If multiple resolvers are
* available for the resource an aggregation of the resources is returned. * available for the resource an aggregation of the resources is returned.
* *
* @param <T> The type of the requested resources.
* @param ref The dbxref. Must not be null. * @param ref The dbxref. Must not be null.
* @param targetClass The requested resource type. May be null. Then all resources are retrieved. * @param targetType The requested resource type. May be null. Then all resources are retrieved.
* @return A collection of resources for the dbxref. If no resources can be resolved the collection will be empty. * @return A collection of resources for the dbxref. If no resources can be resolved the collection will be empty.
*/ */
<T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass); Collection<? extends Resource> resolve(DbXRef ref, Type targetType);
} }
...@@ -27,12 +27,22 @@ import java.net.URI; ...@@ -27,12 +27,22 @@ import java.net.URI;
*/ */
public interface Resource { public interface Resource {
public enum Type {
Xml,
Text,
Html
}
URI getUri(); URI getUri();
/** /**
* Returns the Recourcetext. * Returns the Recourcetext.
*
* @return String that represents the Recourcetext. * @return String that represents the Recourcetext.
*/ */
String getResourceText(); String getResourceText();
DbXRef getDbXRef();
Type getType();
} }
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A resource that points to a plain text document.
*
* @author cb
*/
public class TextResource extends AbstractResource {
public TextResource(URI uri) {
super(uri);
}
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A resource that points to an xml document.
*
* @author cb
*/
public class XMLResource extends AbstractResource {
public XMLResource(URI uri) {
super(uri);
}
}
...@@ -14,11 +14,13 @@ ...@@ -14,11 +14,13 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.cebitec.common.dbxref; package de.cebitec.common.dbxref.impl;
import de.cebitec.common.dbxref.api.DbXRefService;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import de.cebitec.common.dbxref.api.DbXRef; import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource; import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef; import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver; import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.util.Collection; import java.util.Collection;
...@@ -37,21 +39,10 @@ import org.kohsuke.MetaInfServices; ...@@ -37,21 +39,10 @@ import org.kohsuke.MetaInfServices;
* *
* @author cb * @author cb
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>} * @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
* @deprecated Don't use this class directly. Use the service interface instead. The visibility of this implementation
* will be changed to package private in a future release.
*/ */
@MetaInfServices @MetaInfServices
public class DbXRefResourceRegistry implements DbXRefService { public class DbXRefResourceRegistry implements DbXRefService {
private static DbXRefResourceRegistry instance;
public static DbXRefResourceRegistry getDefault() {
if (instance == null) {
instance = new DbXRefResourceRegistry();
}
return instance;
}
Iterable<? extends DbXRefResolver> getResolvers() { Iterable<? extends DbXRefResolver> getResolvers() {
// service provider lookup von resolvern // service provider lookup von resolvern
Iterator<DbXRefResolver> it = ServiceLoader.load(DbXRefResolver.class, this.getClass().getClassLoader()).iterator(); Iterator<DbXRefResolver> it = ServiceLoader.load(DbXRefResolver.class, this.getClass().getClassLoader()).iterator();
...@@ -115,25 +106,25 @@ public class DbXRefResourceRegistry implements DbXRefService { ...@@ -115,25 +106,25 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @throws DbXrefResourceException If no handler can be found. * @throws DbXrefResourceException If no handler can be found.
*/ */
@Override @Override
public <T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass) { public Collection<? extends Resource> resolve(DbXRef ref, Type targetType) {
Set<T> result = new HashSet<>(); Set<Resource> result = new HashSet<>();
if (!hasResolverFor(ref)) { if (!hasResolverFor(ref)) {
throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases()); throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases());
} }
for (DbXRefResolver dbXResource : getResolversFor(ref)) { for (DbXRefResolver dbXResource : getResolversFor(ref)) {
result.addAll(filter(dbXResource.resolve(ref), targetClass)); result.addAll(filter(dbXResource.resolve(ref), targetType));
} }
return result; return result;
} }
public <T extends Resource> Collection<? extends T> filter(Iterable<? extends Resource> iterable, Class<T> targetClass) { public Collection<? extends Resource> filter(Iterable<? extends Resource> iterable, Type targetType) {
List<T> filtered = new LinkedList<>(); List<Resource> filtered = new LinkedList<>();
for (Resource r : iterable) { for (Resource r : iterable) {
if (targetClass.isInstance(r)) { if (r.getType() == targetType) {
filtered.add(targetClass.cast(r)); filtered.add(r);
} }
} }
return filtered; return filtered;
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.cebitec.common.dbxref; package de.cebitec.common.dbxref.impl;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef; import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.api.DbXRef; import de.cebitec.common.dbxref.api.DbXRef;
......
...@@ -14,13 +14,13 @@ ...@@ -14,13 +14,13 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.cebitec.common.dbxref; package de.cebitec.common.dbxref.impl;
/** /**
* *
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>} * @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
*/ */
public class DbXrefParserException extends RuntimeException { class DbXrefParserException extends RuntimeException {
/** /**
* Creates a new instance of * Creates a new instance of
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.cebitec.common.dbxref; package de.cebitec.common.dbxref.impl;
/** /**
* *
......
/* /*
* Copyright (C) 2012 cb * Copyright (C) 2016 Lukas Jelonek - Lukas.Jelonek at computational.bio.uni-giessen.de
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
...@@ -14,8 +14,11 @@ ...@@ -14,8 +14,11 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package de.cebitec.common.dbxref.api; package de.cebitec.common.dbxref.impl;
import com.google.common.base.Preconditions;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
...@@ -24,21 +27,37 @@ import org.apache.commons.io.IOUtils; ...@@ -24,21 +27,37 @@ import org.apache.commons.io.IOUtils;
/** /**
* *
* @author cb
*/ */
public abstract class AbstractResource implements Resource { public class GenericResource implements Resource {
private final Type type;
private final DbXRef dbxref;
protected final URI uri; protected final URI uri;
public AbstractResource(URI uri) { public GenericResource(Type type, DbXRef dbxref, URI uri) {
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(dbxref);
Preconditions.checkNotNull(uri);
this.type = type;
this.dbxref = dbxref;
this.uri = uri; this.uri = uri;
} }
@Override
public Type getType() {
return type;
}
@Override @Override
public URI getUri() { public URI getUri() {
return uri; return uri;
} }
@Override
public DbXRef getDbXRef() {
return dbxref;
}
@Override @Override
public String getResourceText() { public String getResourceText() {
try { try {
...@@ -60,4 +79,8 @@ public abstract class AbstractResource implements Resource { ...@@ -60,4 +79,8 @@ public abstract class AbstractResource implements Resource {
} }
} }
@Override
public String toString() {
return "GenericResource{" + "type=" + type + ", dbxref=" + dbxref + ", uri=" + uri + '}';
}
} }
/*
* Copyright (C) 2016 Lukas Jelonek - Lukas.Jelonek at computational.bio.uni-giessen.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.providers;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.kohsuke.MetaInfServices;
/**
*
*/
@MetaInfServices(DbXRefResolver.class)
public class CompositeProvider implements DbXRefResolver {
private final List<DbXRefResolver> providers = new ArrayList<>();
public CompositeProvider() {
Gson gson = new Gson();
InputStream is = this.getClass().getResourceAsStream("providers.json");
InputStreamReader isr = new InputStreamReader(is);
@SuppressWarnings("unchecked")
List<Map<String, List<String>>> fromJson = gson.fromJson(isr, List.class);
for (Map<String, List<String>> entry : fromJson) {
GenericDbXRefProvider.ResolverData data = new GenericDbXRefProvider.ResolverData();
for (String prefix : entry.get("prefixes")) {
data.addPrefix(prefix);
}
if (entry.containsKey("html")) {
for (String template : entry.get("html")) {
data.addResourceTemplate(Resource.Type.Html, template);
}
}
if (entry.containsKey("xml")) {
for (String template : entry.get("xml")) {
data.addResourceTemplate(Resource.Type.Xml, template);
}
}
if (entry.containsKey("text")) {
for (String template : entry.get("text")) {
data.addResourceTemplate(Resource.Type.Text, template);
}
}
GenericDbXRefProvider provider = new GenericDbXRefProvider(data);
providers.add(provider);
}
}
@Override
public Iterable<String> getDatabasePrefixes() {
List<String> prefixes = new ArrayList<>();
for (DbXRefResolver provider : providers) {
Iterables.addAll(prefixes, provider.getDatabasePrefixes());
}
return prefixes;
}
@Override
public boolean canHandle(DbXRef ref) {
for (DbXRefResolver provider : providers) {
if (provider.canHandle(ref)) {
return true;
}
}
return false;