3535import java .net .URL ;
3636import java .net .URLConnection ;
3737import java .nio .file .Files ;
38+ import java .nio .file .Paths ;
3839import java .nio .file .StandardCopyOption ;
3940
4041/**
4445 */
4546public class OntologyLoader {
4647
47- private static Logger log = LoggerFactory .getLogger ( OntologyLoader .class );
48+ private static final Logger log = LoggerFactory .getLogger ( OntologyLoader .class );
4849 private static final int MAX_CONNECTION_TRIES = 3 ;
4950 private static final String OLD_CACHE_SUFFIX = ".old" ;
5051 private static final String TMP_CACHE_SUFFIX = ".tmp" ;
@@ -126,26 +127,21 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
126127 }
127128
128129 if ( urlc != null ) {
129- try ( InputStream in = urlc .getInputStream (); ) {
130+ try ( InputStream in = urlc .getInputStream () ) {
130131 Reader reader ;
131132 if ( cacheName != null ) {
132133 // write tmp to disk
133134 File tempFile = getTmpDiskCachePath ( cacheName );
134- if ( tempFile == null ) {
135- reader = new InputStreamReader ( in );
136- } else {
137- tempFile .getParentFile ().mkdirs ();
138- Files .copy ( in , tempFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
139- reader = new FileReader ( tempFile );
140- }
135+ tempFile .getParentFile ().mkdirs ();
136+ Files .copy ( in , tempFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
137+ reader = new FileReader ( tempFile );
141138
142139 } else {
143140 // Skip the cache
144141 reader = new InputStreamReader ( in );
145142 }
146143
147- assert reader != null ;
148- try ( BufferedReader buf = new BufferedReader ( reader ); ) {
144+ try ( BufferedReader buf = new BufferedReader ( reader ) ) {
149145 model .read ( buf , url );
150146 }
151147
@@ -164,13 +160,8 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
164160 if ( model .isEmpty () ) {
165161 // Attempt to load from disk cache
166162
167- if ( f == null ) {
168- throw new RuntimeException (
169- "Ontology cache directory required to load from disk: ontology.cache.dir" );
170- }
171-
172163 if ( f .exists () && !f .isDirectory () ) {
173- try ( BufferedReader buf = new BufferedReader ( new FileReader ( f ) ); ) {
164+ try ( BufferedReader buf = new BufferedReader ( new FileReader ( f ) ) ) {
174165 model .read ( buf , url );
175166 // We successfully loaded the cached ontology. Copy the loaded ontology to oldFile
176167 // so that we don't recreate indices during initialization based on a false change in
@@ -190,18 +181,14 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
190181 } else {
191182 // Model was successfully loaded into memory from URL with given cacheName
192183 // Save cache to disk (rename temp file)
193- log .info ( "Caching ontology to disk: " + cacheName );
194- if ( f != null ) {
195- try {
196- // Need to compare previous to current so instead of overwriting we'll move the old file
197- f .createNewFile ();
198- Files .move ( f .toPath (), oldFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
199- Files .move ( tempFile .toPath (), f .toPath (), StandardCopyOption .REPLACE_EXISTING );
200- } catch ( IOException e ) {
201- log .error ( e .getMessage (), e );
202- }
203- } else {
204- log .warn ( "Ontology cache directory required to save to disk: ontology.cache.dir" );
184+ log .info ( "Caching ontology to disk: " + cacheName + " under " + f .getAbsolutePath () );
185+ try {
186+ // Need to compare previous to current so instead of overwriting we'll move the old file
187+ f .createNewFile ();
188+ Files .move ( f .toPath (), oldFile .toPath (), StandardCopyOption .REPLACE_EXISTING );
189+ Files .move ( tempFile .toPath (), f .toPath (), StandardCopyOption .REPLACE_EXISTING );
190+ } catch ( IOException e ) {
191+ log .error ( e .getMessage (), e );
205192 }
206193 }
207194
@@ -226,8 +213,7 @@ public static boolean hasChanged( String cacheName ) {
226213 // in the worst case scenario.
227214 // In this case consider using NIO for higher-performance IO using Channels and Buffers.
228215 // Ex. Use a 4MB Memory-Mapped IO operation.
229- if ( newFile != null && oldFile != null )
230- changed = !FileUtils .contentEquals ( newFile , oldFile );
216+ changed = !FileUtils .contentEquals ( newFile , oldFile );
231217 } catch ( IOException e ) {
232218 log .error ( e .getMessage () );
233219 }
@@ -236,18 +222,12 @@ public static boolean hasChanged( String cacheName ) {
236222
237223 }
238224
239- public static boolean deleteOldCache ( String cacheName ) {
240- File f = getOldDiskCachePath ( cacheName );
241- if ( f != null )
242- return f .delete ();
243- return false ;
225+ public static void deleteOldCache ( String cacheName ) {
226+ getOldDiskCachePath ( cacheName ).delete ();
244227 }
245228
246229 /**
247230 * Get model that is entirely in memory.
248- *
249- * @param url
250- * @return
251231 */
252232 private static OntModel getMemoryModel ( String url ) {
253233 OntModelSpec spec = new OntModelSpec ( OntModelSpec .OWL_MEM_TRANS_INF );
@@ -262,42 +242,32 @@ private static OntModel getMemoryModel( String url ) {
262242 }
263243
264244 /**
265- * @param name
266- * @return
245+ * Obtain the path for the ontology cache.
267246 */
268247 public static File getDiskCachePath ( String name ) {
269248 String ontologyDir = Configuration .getString ( "ontology.cache.dir" ); // e.g., /something/gemmaData/ontologyCache
270- if ( StringUtils .isBlank ( ontologyDir ) || StringUtils .isBlank ( name ) ) {
271- return null ;
249+ if ( StringUtils .isBlank ( ontologyDir ) ) {
250+ throw new IllegalArgumentException ( "The 'ontology.cache.dir' configuration must be set to cache ontologies." );
251+ }
252+ if ( StringUtils .isBlank ( name ) ) {
253+ throw new IllegalArgumentException ( "The ontology must have a suitable name for being loaded from cache." );
272254 }
273255
274256 if ( !new File ( ontologyDir ).exists () ) {
275257 new File ( ontologyDir ).mkdirs ();
276258 }
277259
278- assert ontologyDir != null ;
279-
280- String path = ontologyDir + File .separator + "ontology" + File .separator + name ;
281-
282- File indexFile = new File ( path );
283-
284- return indexFile ;
260+ return Paths .get ( ontologyDir , "ontology" , name ).toFile ();
285261 }
286262
287- static File getOldDiskCachePath ( String name ) {
263+ private static File getOldDiskCachePath ( String name ) {
288264 File indexFile = getDiskCachePath ( name );
289- if ( indexFile == null ) {
290- return null ;
291- }
292265 return new File ( indexFile .getAbsolutePath () + OLD_CACHE_SUFFIX );
293266
294267 }
295268
296- static File getTmpDiskCachePath ( String name ) {
269+ private static File getTmpDiskCachePath ( String name ) {
297270 File indexFile = getDiskCachePath ( name );
298- if ( indexFile == null ) {
299- return null ;
300- }
301271 return new File ( indexFile .getAbsolutePath () + TMP_CACHE_SUFFIX );
302272
303273 }
0 commit comments