@Override public void startup( Workspace workspace ) { try { if ( workspace instanceof DefaultWorkspace ) { File dir = new File( ( (DefaultWorkspace) workspace ).getLocation(), getMetadata().getWorkspacePath() ); bboxCache = new BBoxPropertiesCache( new File( dir, BBOX_CACHE_FILE ) ); } // else? } catch ( IOException e ) { LOG.error( "Unable to initialize global envelope cache: " + e.getMessage(), e ); } super.startup( workspace ); }
/** * Searches for resource managers corresponding to a workspace path. * * @param workspace * may not be <code>null</code> * @param path * may not be <code>null</code>, may contain other path elements * @return a list of matching resource managers, may be empty but never <code>null</code> */ public static List<ResourceManager<?>> getManagersForWorkspacePath( Workspace workspace, String path ) { List<ResourceManager<?>> result = new ArrayList<ResourceManager<?>>(); List<ResourceManager<?>> list = workspace.getResourceManagers(); for ( ResourceManager<?> mgr : list ) { ResourceManagerMetadata<?> md = mgr.getMetadata(); if ( path.startsWith( md.getWorkspacePath() ) ) { result.add( mgr ); } } return result; }
/** * Returns a list of possible resource identifiers for a given workspace path. * * @param workspace * @param path * @return */ public static List<ResourceIdentifier<?>> getPossibleIdentifiers( Workspace workspace, String path ) { List<ResourceManager<?>> list = getManagersForWorkspacePath( workspace, path ); List<ResourceIdentifier<?>> result = new ArrayList<ResourceIdentifier<?>>(); for ( ResourceManager<?> mgr : list ) { Class<?> cls = mgr.getMetadata().getProviderClass(); int one = mgr.getMetadata().getWorkspacePath().endsWith( "/" ) ? 0 : 1; String id = path.substring( mgr.getMetadata().getWorkspacePath().length() + one ); result.add( new DefaultResourceIdentifier( cls, id ) ); } return result; }
@Override public <T extends Resource> List<ResourceLocation<T>> findResourceLocations( ResourceManagerMetadata<T> metadata ) { List<ResourceLocation<T>> list = new ArrayList<ResourceLocation<T>>(); if ( extraResources.get( metadata.getProviderClass() ) != null ) { list.addAll( (Collection) extraResources.get( metadata.getProviderClass() ) ); } File dir = new File( directory, metadata.getWorkspacePath() ); if ( !dir.isDirectory() ) { return list; } URI base = dir.getAbsoluteFile().toURI(); for ( File f : FileUtils.listFiles( dir, new String[] { "xml", "ignore" }, true ) ) { URI uri = f.getAbsoluteFile().toURI(); uri = base.relativize( uri ); String p = uri.getPath(); ResourceState state = null; if ( p.endsWith( "xml" ) ) { p = p.substring( 0, p.length() - 4 ); } else { p = p.substring( 0, p.length() - 7 ); state = ResourceState.Deactivated; } DefaultResourceIdentifier<T> identifier = new DefaultResourceIdentifier<T>( metadata.getProviderClass(), p ); if ( state != null ) { states.setState( identifier, state ); } list.add( new DefaultResourceLocation<T>( f, identifier ) ); } return list; }
@Override public <T extends Resource> ResourceLocation<T> persist( ResourceLocation<T> location ) { ResourceManager<?> mgr = managers.get( location.getIdentifier().getProvider() ); File file = new File( directory, mgr.getMetadata().getWorkspacePath() ); file = new File( file, location.getIdentifier().getId() + ".xml" ); file.getParentFile().mkdirs(); try { // copy to avoid persisting from same file File tmp = File.createTempFile( "config", ".xml" ); FileUtils.copyInputStreamToFile( location.getAsStream(), tmp ); tmp.renameTo( file ); return new DefaultResourceLocation<T>( file, location.getIdentifier() ); } catch ( Exception e ) { throw new ResourceException( "Could not persist resource location: " + e.getLocalizedMessage(), e ); } }