net.percederberg.mibble
Class MibLoader

java.lang.Object
  extended by net.percederberg.mibble.MibLoader

public class MibLoader
extends java.lang.Object

A MIB 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 other MIBs. The search path directories can either be normal file system directories or resource directories. By default the search path contains resource directories containing standard IANA and IETF MIB files (packaged in the Mibble JAR file).

The MIB loader searches for MIB files in a specific order. First, the file system directories in the search path are scanned for files with the same name as the MIB module being imported. This search ignores any file name extensions and compares the base file name in case-insensitive mode. If this search fails, the resource directories are searched for a file having the exact name of the MIB module being imported (case sensitive). The last step, if both the previous ones have failed, is to open the files in the search path one by one to check the MIB module names specified inside. Note that this may be slow for large directories with many files, and it is therefore recommended to always name the MIB files according to their module name.

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.
 java.io.File[] getDirs()
          Returns all the directories in the MIB search path.
 Mib getMib(java.io.File file)
          Returns a previously loaded MIB file.
 Mib getMib(java.lang.String name)
          Returns a previously loaded MIB file.
 java.lang.String[] getResourceDirs()
          Returns all the directories in the MIB resource path.
 ObjectIdentifierValue getRootOid()
          Returns the root object identifier value (OID).
 boolean hasDir(java.io.File dir)
          Checks if a directory is in the MIB search path.
 boolean hasResourceDir(java.lang.String dir)
          Checks if a directory is in the MIB resource path.
 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.
 void unloadAll()
          Unloads all MIBs loaded by this loaded (since the last reset).
 
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

hasDir

public boolean hasDir(java.io.File dir)
Checks if a directory is in the MIB search path. If a file is specified instead of a directory, this method checks if the parent directory is in the MIB search path.

Parameters:
dir - the directory or file to check
Returns:
true if the directory is in the MIB search path, or false otherwise
Since:
2.9

getDirs

public java.io.File[] getDirs()
Returns all the directories in the MIB search path. If a tree of directories has been added, all the individual directories will be returned by this method.

Returns:
the directories in the MIB search path
Since:
2.9

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.


hasResourceDir

public boolean hasResourceDir(java.lang.String dir)
Checks if a directory is in the MIB resource path. The resource search path is used for searching for MIB files with the ClassLoader (i.e. MIB files on the Java classpath) and is a secondary alternative to the directory search path. Note that 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 directory to check
Returns:
true if the directory is in the MIB resource path, or false otherwise
Since:
2.9

getResourceDirs

public java.lang.String[] getResourceDirs()
Returns all the directories in the MIB resource path. The resource search path is used for searching for MIB files with the ClassLoader (i.e. MIB files on the Java classpath) and is a secondary alternative to the directory search path. Note that 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.

Returns:
the directories in the MIB resource path
Since:
2.9

addResourceDir

public void addResourceDir(java.lang.String dir)
Adds a directory to the MIB resource search path. The resource search path is used for searching for MIB files with the ClassLoader (i.e. MIB files on the Java classpath) and is a secondary alternative to the directory search path. Note that 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 loader mostly 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.

Note that this is not the same operation as unloadAll, since the MIB files previously loaded will be unaffected by this this method (i.e. they remain possible to use). If the purpose is to free all memory used by the loaded MIB files, use the unloadAll() method instead.

See Also:
unloadAll()

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

getRootOid

public ObjectIdentifierValue getRootOid()
Returns the root object identifier value (OID). This OID is the "iso" symbol.

Returns:
the root object identifier value
Since:
2.7

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. Note that if the file contained several MIB modules, this method will only return the first one.

Parameters:
file - the MIB file
Returns:
the first 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 module 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. Note that if a file contains several MIB modules, this method will only return the first one (although all are loaded).

Parameters:
file - the MIB file
Returns:
the first MIB module 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. Note that if the URL data contains several MIB modules, this method will only return the first one (although all are loaded).

Parameters:
url - the URL containing the MIB
Returns:
the first MIB module 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. Note that if the input data contains several MIB modules, this method will only return the first one (although all are loaded).

Parameters:
input - the input stream containing the MIB
Returns:
the first MIB module 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 attempts to free the memory used by the MIB, as it clears both the loader and internal MIB references to the data structures (thereby allowing the garbage collector to recover the memory used if no other references exist). Other MIB:s should be unaffected by this operation.

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 attempts to free the memory used by the MIB, as it clears both the loader and internal MIB references to the data structures (thereby allowing the garbage collector to recover the memory used if no other references exist). Other MIB:s should be unaffected by this operation.

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 attempts to free the memory used by the MIB, as it clears both the loader and internal MIB references to the data structures (thereby allowing the garbage collector to recover the memory used if no other references exist). Other MIB:s should be unaffected by this operation.

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()

unloadAll

public void unloadAll()
Unloads all MIBs loaded by this loaded (since the last reset). This method attempts to free all the memory used by the MIBs, as it clears both the loader and internal MIB references to the data structures (thereby allowing the garbage collector to recover the memory used if no other references exist). Note that no previous MIBs returned by this loader should be accessed after this method has been called.

In order to just reset the MIB loader to force re-loading of MIB files, use the reset() method instead which will leave the MIBs unaffected.

Since:
2.9
See Also:
reset()