uk.ac.starlink.util
Class Loader

java.lang.Object
  |
  +--uk.ac.starlink.util.Loader

public class Loader
extends Object

Provides utilities associated with loading resources.


Field Summary
static String PROPERTIES_FILE
          Name of the file in the user's home directory from which properties are loaded.
 
Constructor Summary
Loader()
           
 
Method Summary
static Object getClassInstance(String className, Class type)
          Attempts to obtain an instance of a class with a given name which is an instance of a given type.
static List getClassInstances(String propertyName, Class type)
          Attempts to obtain instances of a class from a colon-separated list of classnames in a named system property.
static File getPropertiesFile()
          Returns the name of the file from which properties will be loaded by this class.
static void loadLibrary(String libname)
          Loads a native library given its name.
static void loadProperties()
          Ensures that the user's customised properties have been loaded; these are read once from the file returned by the getPropertiesFile() method and incorporated into the System properties.
static File starjavaDirectory()
          Returns the location of the main Starlink java directory which contains the lib, bin, etc, source directories and others.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTIES_FILE

public static final String PROPERTIES_FILE
Name of the file in the user's home directory from which properties are loaded.

See Also:
Constant Field Values
Constructor Detail

Loader

public Loader()
Method Detail

starjavaDirectory

public static File starjavaDirectory()
Returns the location of the main Starlink java directory which contains the lib, bin, etc, source directories and others. It gets this by working out what jar file this class has been loaded from - there may be circumstances under which this doesn't work? but it's a best guess.

If for some reason the directory cannot be located, null is returned.

Returns:
the top level starlink java directory, or null if it can't be found

loadLibrary

public static void loadLibrary(String libname)
                        throws SecurityException,
                               UnsatisfiedLinkError
Loads a native library given its name. If it is not found on java.library.path, the architecture-specific lib directory in the installed Starlink system is searched.

Parameters:
libname - the name of the library (not including system-specifics such as 'lib' or '.so')
Throws:
SecurityException - if a security manager exists and its checkLink method doesn't allow loading of the specified dynamic library
UnsatisfiedLinkError - if the library does not exist
See Also:
System.loadLibrary(java.lang.String)

getPropertiesFile

public static File getPropertiesFile()
Returns the name of the file from which properties will be loaded by this class.

Returns:
a file called PROPERTIES_FILE in the directory given by the System property "user.home".

loadProperties

public static void loadProperties()
Ensures that the user's customised properties have been loaded; these are read once from the file returned by the getPropertiesFile() method and incorporated into the System properties. Calling this method after the first time has no effect.

See Also:
System.getProperties()

getClassInstance

public static Object getClassInstance(String className,
                                      Class type)
Attempts to obtain an instance of a class with a given name which is an instance of a given type. If className is null or empty, null is returned directly. Otherwise, if the class className can be found using the default class loader, and if it is assignable from type, and if it has a no-arg constructor, an instance of it is constructed and returned. Otherwise, null is returned, and a message may be written through the logging system.

Parameters:
className - name of the class to instantiate
type - class which the instantiated class must be assignable from
Returns:
new className instance, or null

getClassInstances

public static List getClassInstances(String propertyName,
                                     Class type)
Attempts to obtain instances of a class from a colon-separated list of classnames in a named system property. If the named property does not exists or contains no strings, an empty list is returned. Otherwise, getClassInstance(java.lang.String, java.lang.Class) is called on each colon-separated element of the property value, and if there is a non-null return, it is added to the return list. For colon-separated elements which do not correspond to usable classes, a message may be written through the logging system.

Parameters:
propertyName - name of a system property containing colon-separated classnames
type - class which instantiated classes must be assignable from
Returns:
list of new type instances (may be empty, but not null)

Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved.