net.percederberg.mibble
Class MibLoader

java.lang.Object
  |
  +--net.percederberg.mibble.MibLoader

public class MibLoader
extends java.lang.Object

A MIB file loader. This class contains a search path for locating MIB files, and also holds a refererence to previously loaded MIB files to avoid loading the same file multiple times. The MIB search path consists of directories with MIB files that can be imported into another MIB. If an import isn't found in the search path, the default IANA and IETF MIB directories are searched. Note that these files are normally stored as resources together with the compiled code.

The MIB loader is not thread-safe, i.e. it cannot be used concurrently in multiple threads.

Since:
2.0

Constructor Summary
MibLoader()
          Creates a new MIB loader.
 
Method Summary
 void addAllDirs(java.io.File dir)
          Adds a directory and all subdirectories to the MIB search path.
 void addDir(java.io.File dir)
          Adds a directory to the MIB search path.
 void addDirs(java.io.File[] dirs)
          Adds directories to the MIB search path.
 void addResourceDir(java.lang.String dir)
          Adds a directory to the MIB resource search path.
 Mib[] getAllMibs()
          Returns all previously loaded MIB files.
 MibContext getDefaultContext()
          Returns the default MIB context.
 Mib getMib(java.io.File file)
          Returns a previously loaded MIB file.
 Mib getMib(java.lang.String name)
          Returns a previously loaded MIB file.
 Mib load(java.io.File file)
          Loads a MIB file.
 Mib load(java.io.Reader input)
          Loads a MIB file from the specified input reader.
 Mib load(java.lang.String name)
          Loads a MIB file with the specified base name.
 Mib load(java.net.URL url)
          Loads a MIB file from the specified URL.
 void removeAllDirs()
          Removes all directories from the MIB search path.
 void removeAllResourceDirs()
          Removes all directories from the MIB resource search path.
 void removeDir(java.io.File dir)
          Removes a directory from the MIB search path.
 void removeResourceDir(java.lang.String dir)
          Removes a directory from the MIB resource search path.
 void reset()
          Resets this loader.
 void unload(java.io.File file)
          Unloads a MIB.
 void unload(Mib mib)
          Unloads a MIB.
 void unload(java.lang.String name)
          Unloads a MIB.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MibLoader

public MibLoader()
Creates a new MIB loader.

Method Detail

addDir

public void addDir(java.io.File dir)
Adds a directory to the MIB search path. If the directory specified is null, the current working directory will be added.

Parameters:
dir - the directory to add

addDirs

public void addDirs(java.io.File[] dirs)
Adds directories to the MIB search path.

Parameters:
dirs - the directories to add

addAllDirs

public void addAllDirs(java.io.File dir)
Adds a directory and all subdirectories to the MIB search path. If the directory specified is null, the current working directory (and subdirectories) will be added.

Parameters:
dir - the directory to add

removeDir

public void removeDir(java.io.File dir)
Removes a directory from the MIB search path.

Parameters:
dir - the directory to remove

removeAllDirs

public void removeAllDirs()
Removes all directories from the MIB search path.


addResourceDir

public void addResourceDir(java.lang.String dir)
Adds a directory to the MIB resource search path. The resource search path can be used to load MIB files as resources via the ClassLoader. Note the MIB files stored as resources must have the EXACT MIB name, i.e. no file extensions can be used and name casing is important.

Parameters:
dir - the resource directory to add
Since:
2.3

removeResourceDir

public void removeResourceDir(java.lang.String dir)
Removes a directory from the MIB resource search path. The resource search path can be used to load MIB files as resources via the ClassLoader.

Parameters:
dir - the resource directory to remove
Since:
2.3

removeAllResourceDirs

public void removeAllResourceDirs()
Removes all directories from the MIB resource search path. This will also remove the default directories where the IANA and IETF MIB are present, and may thus make this MIB loaded unusable. Use this method with caution.

Since:
2.3

reset

public void reset()
Resets this loader. This means that all references to previuos MIB files will be removed, forcing a reload of any imported MIB.


getDefaultContext

public MibContext getDefaultContext()
Returns the default MIB context. This context contains the symbols that are predefined for all MIB:s (such as 'iso').

Returns:
the default MIB context

getMib

public Mib getMib(java.lang.String name)
Returns a previously loaded MIB file. If the MIB file hasn't been loaded, null will be returned. The MIB is identified by it's MIB name (i.e. the module name).

Parameters:
name - the MIB (module) name
Returns:
the MIB module if found, or null otherwise

getMib

public Mib getMib(java.io.File file)
Returns a previously loaded MIB file. If the MIB file hasn't been loaded, null will be returned. The MIB is identified by it's file name.

Parameters:
file - the MIB file
Returns:
the MIB module if found, or null otherwise
Since:
2.3

getAllMibs

public Mib[] getAllMibs()
Returns all previously loaded MIB files. If no MIB files have been loaded an empty array will be returned.

Returns:
an array with all loaded MIB files
Since:
2.2

load

public Mib load(java.lang.String name)
         throws java.io.IOException,
                MibLoaderException
Loads a MIB file with the specified base name. The file is searched for in the MIB search path. The MIB is identified by it's MIB name (i.e. the module name). This method will also load all imported MIB:s if not previously loaded by this loader. If a MIB with the same name has already been loaded, it will be returned directly instead of reloading it.

Parameters:
name - the MIB name (filename without extension)
Returns:
the MIB file loaded
Throws:
java.io.IOException - if the MIB file couldn't be found in the MIB search path
MibLoaderException - if the MIB file couldn't be loaded correctly

load

public Mib load(java.io.File file)
         throws java.io.IOException,
                MibLoaderException
Loads a MIB file. This method will also load all imported MIB:s if not previously loaded by this loader. If a MIB with the same file name has already been loaded, it will be returned directly instead of reloading it.

Parameters:
file - the MIB file
Returns:
the MIB file loaded
Throws:
java.io.IOException - if the MIB file couldn't be read
MibLoaderException - if the MIB file couldn't be loaded correctly

load

public Mib load(java.net.URL url)
         throws java.io.IOException,
                MibLoaderException
Loads a MIB file from the specified URL. This method will also load all imported MIB:s if not previously loaded by this loader.

Parameters:
url - the URL containing the MIB
Returns:
the MIB file loaded
Throws:
java.io.IOException - if the MIB URL couldn't be read
MibLoaderException - if the MIB file couldn't be loaded correctly
Since:
2.3

load

public Mib load(java.io.Reader input)
         throws java.io.IOException,
                MibLoaderException
Loads a MIB file from the specified input reader. This method will also load all imported MIB:s if not previously loaded by this loader.

Parameters:
input - the input stream containing the MIB
Returns:
the MIB file loaded
Throws:
java.io.IOException - if the input stream couldn't be read
MibLoaderException - if the MIB file couldn't be loaded correctly
Since:
2.3

unload

public void unload(java.lang.String name)
            throws MibLoaderException
Unloads a MIB. This method will remove the loader reference to a previously loaded MIB if no other MIBs are depending on it. This method does not free the memory used by the MIB, but only releases all the loader references (thereby allowing the garbage collector to recover the memory used by the MIB).

Parameters:
name - the MIB name
Throws:
MibLoaderException - if the MIB couldn't be unloaded due to dependencies from other loaded MIBs
Since:
2.3
See Also:
reset()

unload

public void unload(java.io.File file)
            throws MibLoaderException
Unloads a MIB. This method will remove the loader reference to a previously loaded MIB if no other MIBs are depending on it. This method does not free the memory used by the MIB, but only releases all the loader references (thereby allowing the garbage collector to recover the memory used by the MIB).

Parameters:
file - the MIB file
Throws:
MibLoaderException - if the MIB couldn't be unloaded due to dependencies from other loaded MIBs
Since:
2.3
See Also:
reset()

unload

public void unload(Mib mib)
            throws MibLoaderException
Unloads a MIB. This method will remove the loader reference to a previously loaded MIB if no other MIBs are depending on it. This method does not free the memory used by the MIB, but only releases all the loader references (thereby allowing the garbage collector to recover the memory used by the MIB).

Parameters:
mib - the MIB
Throws:
MibLoaderException - if the MIB couldn't be unloaded due to dependencies from other loaded MIBs
Since:
2.3
See Also:
reset()