Commit 32296caa authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Merge branch 'release/1.3.0'

parents d854e355 a3664cc5
### Purpose
This library resolves database cross references (dbxrefs) to URIs that contain detailed information.
...@@ -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-SNAPSHOT</version> <version>1.3.0</version>
<packaging>bundle</packaging> <packaging>bundle</packaging>
<name>DbXRef library</name> <name>DbXRef library</name>
...@@ -91,6 +91,11 @@ ...@@ -91,6 +91,11 @@
<version>1.3</version> <version>1.3</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -46,21 +46,21 @@ public class DbXRefResourceRegistry implements DbXRefService { ...@@ -46,21 +46,21 @@ public class DbXRefResourceRegistry implements DbXRefService {
private static DbXRefResourceRegistry instance; private static DbXRefResourceRegistry instance;
public static DbXRefResourceRegistry getDefault() { public static DbXRefResourceRegistry getDefault() {
if (instance == null) { if (instance == null) {
instance = new DbXRefResourceRegistry(); instance = new DbXRefResourceRegistry();
} }
return instance; 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();
List<DbXRefResolver> l = new LinkedList<>(); List<DbXRefResolver> l = new LinkedList<>();
while (it.hasNext()) { while (it.hasNext()) {
l.add(it.next()); l.add(it.next());
} }
return l; return l;
} }
/** /**
...@@ -70,14 +70,14 @@ public class DbXRefResourceRegistry implements DbXRefService { ...@@ -70,14 +70,14 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @return * @return
*/ */
List<? extends DbXRefResolver> getResolversFor(DbXRef ref) { List<? extends DbXRefResolver> getResolversFor(DbXRef ref) {
List<DbXRefResolver> result = new LinkedList<>(); List<DbXRefResolver> result = new LinkedList<>();
for (DbXRefResolver dbXRefResolver : getResolvers()) { for (DbXRefResolver dbXRefResolver : getResolvers()) {
if (dbXRefResolver.canHandle(ref)) { if (dbXRefResolver.canHandle(ref)) {
result.add(dbXRefResolver); result.add(dbXRefResolver);
} }
} }
return result; return result;
} }
/** /**
...@@ -87,7 +87,7 @@ public class DbXRefResourceRegistry implements DbXRefService { ...@@ -87,7 +87,7 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @return * @return
*/ */
public boolean hasResolverFor(DbXRef ref) { public boolean hasResolverFor(DbXRef ref) {
return !getResolversFor(ref).isEmpty(); return !getResolversFor(ref).isEmpty();
} }
/** /**
...@@ -97,62 +97,63 @@ public class DbXRefResourceRegistry implements DbXRefService { ...@@ -97,62 +97,63 @@ public class DbXRefResourceRegistry implements DbXRefService {
*/ */
@Override @Override
public Collection<? extends Resource> resolve(DbXRef ref) { public Collection<? extends Resource> resolve(DbXRef ref) {
List<Resource> result = new LinkedList<>(); List<Resource> result = new LinkedList<>();
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(dbXResource.resolve(ref)); result.addAll(dbXResource.resolve(ref));
} }
return result; return result;
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
* @throws DbXrefResourceException If no handler could be found. * @throws DbXrefResourceException If no handler can be found.
*/ */
@Override
public <T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass) { public <T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass) {
Set<T> result = new HashSet<>(); Set<T> 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), targetClass));
} }
return result; return result;
} }
public <T extends Resource> Collection<? extends T> filter(Iterable<? extends Resource> iterable, Class<T> targetClass) { public <T extends Resource> Collection<? extends T> filter(Iterable<? extends Resource> iterable, Class<T> targetClass) {
List<T> filtered = new LinkedList<>(); List<T> filtered = new LinkedList<>();
for (Resource r : iterable) { for (Resource r : iterable) {
if (targetClass.isInstance(r)) { if (targetClass.isInstance(r)) {
filtered.add(targetClass.cast(r)); filtered.add(targetClass.cast(r));
} }
} }
return filtered; return filtered;
} }
private Set<String> getSupportedDatabases() { private Set<String> getSupportedDatabases() {
Set<String> set = new LinkedHashSet<>(); Set<String> set = new LinkedHashSet<>();
for (DbXRefResolver dbXRefResolver : getResolvers()) { for (DbXRefResolver dbXRefResolver : getResolvers()) {
Iterables.addAll(set, dbXRefResolver.getDatabasePrefixes()); Iterables.addAll(set, dbXRefResolver.getDatabasePrefixes());
} }
return set; return set;
} }
@Override @Override
public DbXRef create(CharSequence database, CharSequence identifier) { public DbXRef create(CharSequence database, CharSequence identifier) {
return new SimpleDbXRef(database.toString(), identifier.toString()); return new SimpleDbXRef(database.toString(), identifier.toString());
} }
@Override @Override
public DbXRef create(CharSequence dbxref) { public DbXRef create(CharSequence dbxref) {
return new DbXrefParser().parse(dbxref.toString()); return new DbXrefParser().parse(dbxref.toString());
} }
} }
...@@ -16,19 +16,18 @@ ...@@ -16,19 +16,18 @@
*/ */
package de.cebitec.common.dbxref.api; package de.cebitec.common.dbxref.api;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.util.logging.Level; import org.apache.commons.io.IOUtils;
import java.util.logging.Logger;
/** /**
* *
* @author cb * @author cb
*/ */
public abstract class AbstractResource implements Resource { public abstract class AbstractResource implements Resource {
protected final URI uri; protected final URI uri;
public AbstractResource(URI uri) { public AbstractResource(URI uri) {
...@@ -43,36 +42,22 @@ public abstract class AbstractResource implements Resource { ...@@ -43,36 +42,22 @@ public abstract class AbstractResource implements Resource {
@Override @Override
public String getResourceText() { public String getResourceText() {
try { try {
HttpURLConnection openConnection = (HttpURLConnection) uri.toURL().openConnection(); HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
openConnection.connect(); connection.connect();
int responseCode = openConnection.getResponseCode(); int responseCode = connection.getResponseCode();
openConnection.disconnect(); connection.disconnect();
if (responseCode >= 400) if (responseCode >= 400) {
throw new UnsupportedOperationException("Invalid url"); throw new UnsupportedOperationException("Can't access " + uri + " Return code [" + responseCode + "]");
}
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(AbstractResource.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException("Error accessing " + uri, ex);
throw new RuntimeException(ex);
} }
String s = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(uri.toURL().openStream()))) {
//s = "content: "+uri.toURL().openConnection().getContent().toString();
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
s = sb.toString();
try (InputStream stream = uri.toURL().openStream()) {
} catch (Exception ex) { return IOUtils.toString(stream);
// applikations exception werfen } catch (IOException ex) {
throw new RuntimeException(ex);
} }
//return
return s;
} }
} }
/*
* Copyright (C) 2014 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;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Lukas Jelonek {@literal <Lukas.Jelonek at computational.bio.uni-giessen.de>}
*/
public class DbXRefResourceRegistryIT {
public DbXRefResourceRegistryIT() {
}
@Test
public void testSomeMethod() {
}
}
/*
* Copyright (C) 2014 Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.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 de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.util.ServiceLoader;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
*
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
*/
public class ECProviderTest {
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof ECProvider) {
present = true;
}
}
assertTrue(present);
}
@Test
public void testProvider() {
ECProvider provider = new ECProvider();
DbXRef validID = new SimpleDbXRef("EC", "1.1.1.1");
assertNotNull(provider.resolve(validID));
// mindestens eine resource
assertTrue(provider.resolve(validID).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validID);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
}
/*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import java.util.ServiceLoader;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
* @author cb
*/
public class GIProviderTest {
public GIProviderTest() {
}
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof GIProvider) {
present = true;
}
}
assertTrue(present);
}
@Test
public void testProvider() {
GIProvider provider = new GIProvider();
DbXRef validGI = new SimpleDbXRef("GI", "731497");
assertNotNull(provider.resolve(validGI));
// mindestens eine resource
assertTrue(provider.resolve(validGI).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validGI);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
}
/*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import java.util.ServiceLoader;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
* @author cb
*/
public class GOProviderTest {
public GOProviderTest() {
}
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof GOProvider) {
present = true;
}
}
assertTrue(present);
}
@Test
public void testProvider() {
GOProvider provider = new GOProvider();
DbXRef validID = new SimpleDbXRef("GO", "0097281");
assertNotNull(provider.resolve(validID));
// mindestens eine resource
assertTrue(provider.resolve(validID).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validID);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
}
/*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import java.util.ServiceLoader;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
* @author cb
*/
public class GeneIDProviderTest {