Skip to content

Commit 5ca005d

Browse files
committed
Add support for processing imports
Reuse the code for establishing a URLConnection and handling redirection when resolving ontology imports.
1 parent a28e91f commit 5ca005d

2 files changed

Lines changed: 75 additions & 47 deletions

File tree

src/ubic/basecode/ontology/jena/OntologyLoader.java

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
import com.hp.hpl.jena.ontology.OntModel;
2222
import com.hp.hpl.jena.ontology.OntModelSpec;
23-
import com.hp.hpl.jena.rdf.model.Model;
24-
import com.hp.hpl.jena.rdf.model.ModelFactory;
25-
import com.hp.hpl.jena.rdf.model.ModelMaker;
23+
import com.hp.hpl.jena.rdf.model.*;
2624
import org.apache.commons.io.FileUtils;
2725
import org.apache.commons.lang3.StringUtils;
2826
import org.apache.commons.lang3.time.StopWatch;
@@ -83,49 +81,7 @@ public static OntModel loadMemoryModel( String url, @Nullable String cacheName )
8381
timer.start();
8482
OntModel model = getMemoryModel( url );
8583

86-
URLConnection urlc = null;
87-
int tries = 0;
88-
while ( tries < MAX_CONNECTION_TRIES ) {
89-
try {
90-
urlc = new URL( url ).openConnection();
91-
// help ensure mis-configured web servers aren't causing trouble.
92-
urlc.setRequestProperty( "Accept", "application/rdf+xml" );
93-
94-
try {
95-
HttpURLConnection c = ( HttpURLConnection ) urlc;
96-
c.setInstanceFollowRedirects( true );
97-
} catch ( ClassCastException e ) {
98-
// not via http, using a FileURLConnection.
99-
}
100-
101-
if ( tries > 0 ) {
102-
log.info( "Retrying connecting to " + url + " [" + tries + "/" + MAX_CONNECTION_TRIES
103-
+ " of max tries" );
104-
} else {
105-
log.info( "Connecting to " + url );
106-
}
107-
108-
urlc.connect(); // Will error here on bad URL
109-
110-
if ( urlc instanceof HttpURLConnection ) {
111-
String newUrl = urlc.getHeaderField( "Location" );
112-
113-
if ( StringUtils.isNotBlank( newUrl ) ) {
114-
log.info( "Redirect to " + newUrl );
115-
urlc = new URL( newUrl ).openConnection();
116-
// help ensure mis-configured web servers aren't causing trouble.
117-
urlc.setRequestProperty( "Accept", "application/rdf+xml" );
118-
urlc.connect();
119-
}
120-
}
121-
122-
break;
123-
} catch ( IOException e ) {
124-
// try to recover.
125-
log.error( e + " retrying?" );
126-
tries++;
127-
}
128-
}
84+
URLConnection urlc = openConnection( url );
12985

13086
if ( urlc != null ) {
13187
try ( InputStream in = urlc.getInputStream() ) {
@@ -235,13 +191,80 @@ private static OntModel getMemoryModel( String url ) {
235191
ModelMaker maker = ModelFactory.createMemModelMaker();
236192
Model base = maker.createModel( url, false );
237193
spec.setImportModelMaker( maker );
238-
spec.getDocumentManager().setProcessImports( false );
194+
spec.getDocumentManager().setProcessImports( true );
195+
spec.setImportModelGetter( new ModelGetter() {
196+
@Override
197+
public Model getModel( String URL ) {
198+
return null;
199+
}
239200

201+
@Override
202+
public Model getModel( String URL, ModelReader loadIfAbsent ) {
203+
Model model = maker.createModel( URL );
204+
URLConnection urlc = openConnection( URL );
205+
if ( urlc != null ) {
206+
try ( InputStream in = urlc.getInputStream() ) {
207+
return model.read( in, URL );
208+
} catch ( IOException e ) {
209+
log.error( String.format( "Failed to load from %s.", URL ), e );
210+
}
211+
}
212+
return loadIfAbsent.readModel( model, URL );
213+
}
214+
} );
240215
OntModel model = ModelFactory.createOntologyModel( spec, base );
241216
model.setStrictMode( false ); // fix for owl2 files
242217
return model;
243218
}
244219

220+
public static URLConnection openConnection( String url ) {
221+
URLConnection urlc = null;
222+
int tries = 0;
223+
while ( tries < MAX_CONNECTION_TRIES ) {
224+
try {
225+
urlc = new URL( url ).openConnection();
226+
// help ensure mis-configured web servers aren't causing trouble.
227+
urlc.setRequestProperty( "Accept", "application/rdf+xml" );
228+
229+
try {
230+
HttpURLConnection c = ( HttpURLConnection ) urlc;
231+
c.setInstanceFollowRedirects( true );
232+
} catch ( ClassCastException e ) {
233+
// not via http, using a FileURLConnection.
234+
}
235+
236+
if ( tries > 0 ) {
237+
log.info( "Retrying connecting to " + url + " [" + tries + "/" + MAX_CONNECTION_TRIES
238+
+ " of max tries" );
239+
} else {
240+
log.info( "Connecting to " + url );
241+
}
242+
243+
urlc.connect(); // Will error here on bad URL
244+
245+
if ( urlc instanceof HttpURLConnection ) {
246+
String newUrl = urlc.getHeaderField( "Location" );
247+
248+
if ( StringUtils.isNotBlank( newUrl ) ) {
249+
log.info( "Redirect to " + newUrl );
250+
urlc = new URL( newUrl ).openConnection();
251+
// help ensure mis-configured web servers aren't causing trouble.
252+
urlc.setRequestProperty( "Accept", "application/rdf+xml" );
253+
urlc.connect();
254+
}
255+
}
256+
257+
break;
258+
} catch ( IOException e ) {
259+
// try to recover.
260+
log.error( e + " retrying?" );
261+
tries++;
262+
}
263+
}
264+
265+
return urlc;
266+
}
267+
245268
/**
246269
* Obtain the path for the ontology cache.
247270
*/

test/ubic/basecode/ontology/OntologyLoaderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ public void testHasChanged() throws Exception {
6565
}
6666
assertTrue( OntologyLoader.hasChanged( name ) );
6767
}
68+
69+
@Test
70+
public void testLoadModelWithImports() {
71+
OntologyLoader.loadMemoryModel( "http://purl.obolibrary.org/obo/doid.owl" );
72+
}
6873
}

0 commit comments

Comments
 (0)