verinec.data.repository
Class FileRepository

java.lang.Object
  extended by verinec.data.repository.VerinecRepository
      extended by verinec.data.repository.FileRepository
All Implemented Interfaces:
IVerinecRepository

public class FileRepository
extends VerinecRepository

Implementation of the IRepository for using the file system to store nodes and network definition.

The base directory can be defined by setting the property verinec.data.repository.file_basedir If this property is not set, defaults to ./data

Creates a directory for each project. The project dir contains a nodes subdir for the nodes and a file networks.xml for the networks.
The xml is parsed using a jdom SAXBuilder and written using jdom XMLOutputter.

Version:
$Revision: 998 $
Author:
david.buchmann at unifr.ch

Field Summary
private static File fileRoot
          The directory under which the project directories are created.
private static String GLOBALS_FILE
          Filename for the globals definitions.
private  File globalsFile
          The globals data file, created from GLOBALS_FILE.
private  Logger log
           
private static String NETWORKS_FILE
          Filename for the network definitions.
private  File networksFile
          The network data file, created from NETWORKS_FILE.
private static String NODES_DIRECTORY
          Directory name for the nodes xml files.
private  File nodesDir
          The nodes directory, created from NODES_DIRECTORY.
private  File projectDir
          The project directory, created from fileRoot and projectName parameter in constructor.
private static XMLOutputter xmloutput
          The jdom XMLOutputter to write xml files.
 
Fields inherited from class verinec.data.repository.VerinecRepository
networks, nodes, projectName, validator
 
Constructor Summary
FileRepository(String projectName)
          Create a file repository for the given projectName.
 
Method Summary
 void drop()
          Drop this repository, that is delete all data associated with it.
private  File fileFromNodename(String name)
          Encapsulates how the filename is determined for some node.
 Element getGlobals()
          Get the nodes tag with only the global variables and eventual metadata from other namespaces, but no node children.
 String[] getProjectNames()
          Get the names of all existing projects that can be opened with openProject() This method just redirects to the sgetProjectNames method.
 void removeNode(String name)
          Remove an existing node from the configuration.
 void setGlobals(Element globals)
          Set the global elements without touching the nodes.
 void setNode(Element node)
          Set a network Node.
 void setPhysicalNetworks(Element physical)
          Set the networks of this project.
static String[] sgetProjectNames()
          Get the names of all existing projects.
 
Methods inherited from class verinec.data.repository.VerinecRepository
getNode, getNodeNames, getNodes, getNodesWithInterfaceIp, getPhysicalNetworks, getProjectName, init, setNodes, validateNetworks, validateNode, validatePhysicalNetworks
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NODES_DIRECTORY

private static final String NODES_DIRECTORY
Directory name for the nodes xml files.

See Also:
Constant Field Values

NETWORKS_FILE

private static final String NETWORKS_FILE
Filename for the network definitions.

See Also:
Constant Field Values

GLOBALS_FILE

private static final String GLOBALS_FILE
Filename for the globals definitions.

See Also:
Constant Field Values

fileRoot

private static final File fileRoot
The directory under which the project directories are created. Set from System.getProperty("verinec.data.repository.file_basedir")) and defaults to ./data if property is not found.


log

private Logger log

projectDir

private final File projectDir
The project directory, created from fileRoot and projectName parameter in constructor.


nodesDir

private final File nodesDir
The nodes directory, created from NODES_DIRECTORY.

See Also:
NODES_DIRECTORY

networksFile

private final File networksFile
The network data file, created from NETWORKS_FILE.

See Also:
NETWORKS_FILE

globalsFile

private final File globalsFile
The globals data file, created from GLOBALS_FILE.

See Also:
GLOBALS_FILE

xmloutput

private static final XMLOutputter xmloutput
The jdom XMLOutputter to write xml files.

Constructor Detail

FileRepository

public FileRepository(String projectName)
               throws VerinecException
Create a file repository for the given projectName. It will look for the project directory under fileRoot and create a new one, if it does not exist.

Parameters:
projectName - Name of the project, interpreted as directory name under fileRoot.
Throws:
VerinecException - If the file repository can not be instantiated.
Method Detail

getProjectNames

public String[] getProjectNames()
Get the names of all existing projects that can be opened with openProject() This method just redirects to the sgetProjectNames method. The only reason why this method is not static is that an interface may not declare any static methods.

Returns:
All existing project names. The same as sgetProjectNames().

sgetProjectNames

public static String[] sgetProjectNames()
Get the names of all existing projects. The static method is required by VerinecRepository to query the project names without having to instantiate a repository.

Returns:
An array containing the available project names.

drop

public void drop()
          throws VerinecException
Drop this repository, that is delete all data associated with it.

Specified by:
drop in interface IVerinecRepository
Overrides:
drop in class VerinecRepository
Throws:
VerinecException - If deleting the repository fails.

fileFromNodename

private File fileFromNodename(String name)
Encapsulates how the filename is determined for some node.
todo: could a node name contain characters illegal for filenames?

Parameters:
name - The name of the node to create a file for.
Returns:
A file for the xml of the node, which might not exists.

setNode

public void setNode(Element node)
             throws VerinecException
Set a network Node. If there is already a node with that name, it is overwritten. The node name is retreived from the passed XML structure.

Specified by:
setNode in interface IVerinecRepository
Overrides:
setNode in class VerinecRepository
Parameters:
node - The Jdom Element of the node.
Throws:
VerinecException - If the node is invalid or an io error occurs.

removeNode

public void removeNode(String name)
                throws VerinecException
Remove an existing node from the configuration.

Specified by:
removeNode in interface IVerinecRepository
Overrides:
removeNode in class VerinecRepository
Parameters:
name - The node name to remove. It is an error to pass a nonexisting name.
Throws:
VerinecException - if the node is not existing.

setGlobals

public void setGlobals(Element globals)
                throws VerinecException
Set the global elements without touching the nodes. Use to set global variables or metadata from other namespaces. This methods overwrites the current globals with the children of the parameter. Children named 'node' are ignored.

Specified by:
setGlobals in interface IVerinecRepository
Overrides:
setGlobals in class VerinecRepository
Parameters:
globals - A nodes tag with all elements that shoud be global.
Throws:
VerinecException - if the parameter is not a valid nodes tag or io error occur.

getGlobals

public Element getGlobals()
                   throws VerinecException
Get the nodes tag with only the global variables and eventual metadata from other namespaces, but no node children. todo: should we cache globals or is it desirable to reread the file each time? (setGlobals would have to be changed as well if we cache)

Specified by:
getGlobals in interface IVerinecRepository
Overrides:
getGlobals in class VerinecRepository
Returns:
A <nodes> element containing the global elements.
Throws:
VerinecException - if the stored file can not be read properly.

setPhysicalNetworks

public void setPhysicalNetworks(Element physical)
                         throws VerinecException
Set the networks of this project. You must ensure that all nodes with interfaces referenced in the network are saved before the network is saved, otherwise it can not be validated correctly and will be refused.

Specified by:
setPhysicalNetworks in interface IVerinecRepository
Overrides:
setPhysicalNetworks in class VerinecRepository
Parameters:
physical - Element "physical" containing the physical network definitions.
Throws:
VerinecException - if the parameter is not a valid networks tag or io error occur.

Copyright © 2005 Verinec, DIUF