-
Notifications
You must be signed in to change notification settings - Fork 1
ResourceStore API Examples
Examples of using the ResourceStore API and utility classes.
See [Resource API Transition Plan] for examples based on updating utility classes such as GeoserverDataDirectory, GeoServerDataDirectory, SecurityManager and more…
The following examples provide a feel for how the ResourceStore API is used in practice:
LoggingStartupContextListener is an example that uses an input stream:
File f= loader.find( "logging.xml" );
XStreamPersister xp = new XStreamPersisterFactory().createXMLPersister();
BufferedInputStream in = new BufferedInputStream( new FileInputStream( f ) );
LoggingStartupContextListener after:
Resource r= loader.get( "logging.xml" );
XStreamPersister xp = new XStreamPersisterFactory().createXMLPersister();
BufferedInputStream in = new BufferedInputStream( r.in() );
StyleResource is an example that uses an output stream:
f = loader.createFile(path) ;
BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream ( f ) );
SLDFormat format = new SLDFormat(true);
format.toRepresentation(style).write(out);
StyleResource after:
BufferedOutputStream out = new BufferedOutputStream( loader.get(path).out() );
SLDFormat format = new SLDFormat(true);
format.toRepresentation(style).write(out);
GlobalSettingsPage is an example that reviews the contents of a directory:
File logsDirectory = loader.find("logs");
if(logsDirectory.exists() && logsDirectory.isDirectory()) {
String[] propFiles = logsDirectory.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith("logging.properties");
}
});
logProfiles = Arrays.asList(propFiles);
Collections.sort(logProfiles, String.CASE_INSENSITIVE_ORDER);
}
Direct migration of logic to use Resource list:
Resource logsDirectory = loader.get("logs");
if(logsDirectory.isExists() && logsDirectory.isDirectory()) {
logProfiles = new ArrayList<String>();
for( Resource child : logsDirectory.list() ){
if( child.name().toLowerCase().endsWith("logging.properties") ){
logProfiles.add( child.getName() );
}
}
Collections.sort(logProfiles, String.CASE_INSENSITIVE_ORDER);
}
Resources should only be converted to files or directory when absolutely necessary.
ApplicationSchemaXSD is an example that requires the use of a File:
File schemaFile = null;
schemaFile = catalog.getResourceLoader().find("workspaces" + "/" + prefix + "/" + store + "/" + name + "/schema.xsd");
ftSchema = Schemas.parse(schemaFile.getAbsolutePath(), locators, null);
Migrated:
File schemaFile = null;
schemaFile = catalog.getResourceLoader().get( Paths.path("workspaces",prefix,store,name,"schema.xsd") ).file();
ftSchema = Schemas.parse(schemaFile.getAbsolutePath(), locators, null);
The methods below to create directories and files are depecrated and should not be used any longer. Files and directories should only be used when absolutely necessary. Instead, just create resources.
The following examples can be used during refactoring (or accomplished by inlining the appropriate method).
The utility classes Paths and Resources are used replicate the GeoServerResourceLoader logic with a minimum of fuss and bother.
createDirectory (File parentFile, String location)
String path = Paths.convert(loader.getBaseDirectory(), parentFile, location );
Resource resource = loader.get( path );
File directory = Resources.createNewDirectory(resource);
createDirectory (File parentFile, String… location)
String path = Paths.convert(loader.getBaseDirectory(), parentFile, location );
Resource resource = loader.get( path );
File directory = Resources.createNewDirectory(resource);
createDirectory (String location)
Resource resource = loader.get( Paths.convert(location) );
File directory = Resources.createNewDirectory(resource);
createDirectory (String… location)
Resource resource = loader.get( Paths.path(location) );
File directory = Resources.createNewDirectory(resource);
createFile (File parentFile, String location):
String path = Paths.convert(loader.getBaseDirectory(), parentFile, location );
Resource resource = loader.get( path );
File file = Resources.createNewFile(resource);
createFile (File parentFile, String… location):
String path = Paths.convert(loader.getBaseDirectory(), parentFile, location );
Resource resource = loader.get( path );
File file = Resources.createNewFile(resource);
createFile (String location):
Resource resource = get( Paths.convert(location) );
File file = Resources.createNewFile( resource );
createFile (String… location):
Resource resource = get( Paths.path(location) );
File file = Resources.createNewFile( resource );
Resources.findFile returns a file if available. A DIRECTORY resource returns dir (), RESOURCE returns file (), and UNDEFINED returns null.
find ( String location):
Resource resource = loader.get( Paths.convert(location) );
File file = Resources.findFile( resource );
find ( String… location ):
Resource resource = loader.get( Paths.path(location) );
File file = Resources.findFile( resource );
find (File parentFile, String location):
String path = Paths.convert(loader.getBaseDirectory(), parentFile, location );
Resource resource = get( path );
File file = Resources.findFile( resource );
find ( File parentFile, String… location ):
String path = Paths.convert(loader.getBaseDirectory(), parentFile, location)
Resource resource = loader.get( path );
File file = Resources.findFile( resource );
Resource.dir () will create a directory if needed (i.e. the expected behaviour for findOrCreateDirectory).
findOrCreateDirectory (File, String):
String path = Paths.convert(loader.getBaseDirectory(),parentFile,location);
Resource resource = loader.get( path );
File directory = resource.dir();
findOrCreateDirectory (File, String…):
String path = Paths.convert(getBaseDirectory(), parentFile, location);
Resource resource = get(path);
File directory = resource.dir();
findOrCreateDirectory (String):
Resource resource = loader.get( Paths.convert(location) );
File directory = resource.dir();
findOrCreateDirectory (String…):
Resource resource = loader.get( Paths.path(location) );
File directory = resource.dir();
©2020 Open Source Geospatial Foundation