Changed leading tabs to spaces

cusax-fix
Emil Ivov 20 years ago
parent 9866985d57
commit b8dc9a556c

@ -28,169 +28,206 @@
/**
* @author Alexander Pelov
*/
public class HistoryImpl implements History {
private static Logger log = Logger.getLogger(HistoryImpl.class);
private HistoryID id;
private HistoryRecordStructure historyRecordStructure;
private HistoryServiceImpl historyServiceImpl;
private File directory;
private HistoryReader reader;
private HistoryWriter writer;
private SortedMap historyDocuments = new TreeMap();
protected HistoryImpl(
HistoryID id,
File directory,
HistoryRecordStructure historyRecordStructure,
HistoryServiceImpl historyServiceImpl)
{
try {
log.logEntry();
// TODO: Assert: Assert.assertNonNull(historyServiceImpl, "The historyServiceImpl should be non-null.");
// TODO: Assert: Assert.assertNonNull(id, "The ID should be non-null.");
// TODO: Assert: Assert.assertNonNull(historyRecordStructure, "The structure should be non-null.");
this.id = id;
this.directory = directory;
this.historyServiceImpl = historyServiceImpl;
this.historyRecordStructure = historyRecordStructure;
this.reader = null;
this.writer = null;
this.reloadDocumentList();
} finally {
log.logExit();
}
}
public HistoryID getID() {
return this.id;
}
public HistoryRecordStructure getHistoryRecordsStructure() {
return this.historyRecordStructure;
}
public HistoryReader getReader() {
if(this.reader == null) {
this.reader = new HistoryReaderImpl(this);
}
return this.reader;
}
public HistoryWriter getWriter() {
if(this.writer == null) {
this.writer = new HistoryWriterImpl(this);
}
return this.writer;
}
protected HistoryServiceImpl getHistoryServiceImpl() {
return this.historyServiceImpl;
}
private void reloadDocumentList() {
synchronized(this.historyDocuments) {
this.historyDocuments.clear();
File[] files = this.directory.listFiles();
// TODO: Assert: Assert.assertNonNull(files, "The list of files should be non-null.");
for(int i = 0; i < files.length; i++) {
if(!files[i].isDirectory()) {
this.historyDocuments.put(files[i].getName(), files[i]);
}
}
}
}
protected Document createDocument(String filename) {
Document retVal = null;
synchronized(this.historyDocuments) {
if(this.historyDocuments.containsKey(filename)) {
retVal = getDocumentForFile(filename);
} else {
retVal = this.historyServiceImpl.getDocumentBuilder().newDocument();
retVal.appendChild(retVal.createElement("history"));
this.historyDocuments.put(filename, retVal);
}
}
return retVal;
}
protected void writeFile(String filename)
throws InvalidParameterException, IOException
{
File file = new File(this.directory, filename);
synchronized(this.historyDocuments) {
if(!this.historyDocuments.containsKey(filename)) {
throw new InvalidParameterException("The requested " +
"filename does not exist in the document list.");
}
Object obj = this.historyDocuments.get(filename);
if(obj instanceof Document) {
Document doc = (Document)obj;
synchronized(doc) {
XMLUtils.writeXML(doc, file);
}
}
}
}
protected Iterator getFileList() {
return this.historyDocuments.keySet().iterator();
}
protected Document getDocumentForFile(String filename)
throws InvalidParameterException, RuntimeException
{
Document retVal = null;
synchronized(this.historyDocuments) {
if(!this.historyDocuments.containsKey(filename)) {
throw new InvalidParameterException("The requested " +
"filename does not exist in the document list.");
}
Object obj = this.historyDocuments.get(filename);
if(obj instanceof Document) {
// Document already loaded. Use it directly
retVal = (Document)obj;
} else if(obj instanceof File) {
File file = (File)obj;
DocumentBuilder builder = this.historyServiceImpl.getDocumentBuilder();
try {
retVal = builder.parse(file);
} catch (Exception e) {
throw new RuntimeException("Error occured while " +
"parsing XML document.", e);
}
// Cache the loaded document for reuse
this.historyDocuments.put(filename, retVal);
} else {
// TODO: Assert: Assert.fail("Internal error - the data type " +
// "should be either Document or File.");
}
}
return retVal;
}
public class HistoryImpl
implements History
{
private static Logger log = Logger.getLogger(HistoryImpl.class);
private HistoryID id;
private HistoryRecordStructure historyRecordStructure;
private HistoryServiceImpl historyServiceImpl;
private File directory;
private HistoryReader reader;
private HistoryWriter writer;
private SortedMap historyDocuments = new TreeMap();
protected HistoryImpl(
HistoryID id,
File directory,
HistoryRecordStructure historyRecordStructure,
HistoryServiceImpl historyServiceImpl)
{
try
{
log.logEntry();
// TODO: Assert: Assert.assertNonNull(historyServiceImpl, "The historyServiceImpl should be non-null.");
// TODO: Assert: Assert.assertNonNull(id, "The ID should be non-null.");
// TODO: Assert: Assert.assertNonNull(historyRecordStructure, "The structure should be non-null.");
this.id = id;
this.directory = directory;
this.historyServiceImpl = historyServiceImpl;
this.historyRecordStructure = historyRecordStructure;
this.reader = null;
this.writer = null;
this.reloadDocumentList();
}
finally
{
log.logExit();
}
}
public HistoryID getID()
{
return this.id;
}
public HistoryRecordStructure getHistoryRecordsStructure()
{
return this.historyRecordStructure;
}
public HistoryReader getReader()
{
if (this.reader == null)
{
this.reader = new HistoryReaderImpl(this);
}
return this.reader;
}
public HistoryWriter getWriter()
{
if (this.writer == null)
{
this.writer = new HistoryWriterImpl(this);
}
return this.writer;
}
protected HistoryServiceImpl getHistoryServiceImpl()
{
return this.historyServiceImpl;
}
private void reloadDocumentList()
{
synchronized (this.historyDocuments)
{
this.historyDocuments.clear();
File[] files = this.directory.listFiles();
// TODO: Assert: Assert.assertNonNull(files, "The list of files should be non-null.");
for (int i = 0; i < files.length; i++)
{
if (!files[i].isDirectory())
{
this.historyDocuments.put(files[i].getName(), files[i]);
}
}
}
}
protected Document createDocument(String filename)
{
Document retVal = null;
synchronized (this.historyDocuments)
{
if (this.historyDocuments.containsKey(filename))
{
retVal = getDocumentForFile(filename);
}
else
{
retVal = this.historyServiceImpl.getDocumentBuilder().
newDocument();
retVal.appendChild(retVal.createElement("history"));
this.historyDocuments.put(filename, retVal);
}
}
return retVal;
}
protected void writeFile(String filename) throws InvalidParameterException,
IOException
{
File file = new File(this.directory, filename);
synchronized (this.historyDocuments)
{
if (!this.historyDocuments.containsKey(filename))
{
throw new InvalidParameterException("The requested " +
"filename does not exist in the document list.");
}
Object obj = this.historyDocuments.get(filename);
if (obj instanceof Document)
{
Document doc = (Document) obj;
synchronized (doc)
{
XMLUtils.writeXML(doc, file);
}
}
}
}
protected Iterator getFileList()
{
return this.historyDocuments.keySet().iterator();
}
protected Document getDocumentForFile(String filename) throws
InvalidParameterException, RuntimeException
{
Document retVal = null;
synchronized (this.historyDocuments)
{
if (!this.historyDocuments.containsKey(filename))
{
throw new InvalidParameterException("The requested " +
"filename does not exist in the document list.");
}
Object obj = this.historyDocuments.get(filename);
if (obj instanceof Document)
{
// Document already loaded. Use it directly
retVal = (Document) obj;
}
else if (obj instanceof File)
{
File file = (File) obj;
DocumentBuilder builder = this.historyServiceImpl.
getDocumentBuilder();
try
{
retVal = builder.parse(file);
}
catch (Exception e)
{
throw new RuntimeException("Error occured while " +
"parsing XML document.", e);
}
// Cache the loaded document for reuse
this.historyDocuments.put(filename, retVal);
}
else
{
// TODO: Assert: Assert.fail("Internal error - the data type " +
// "should be either Document or File.");
}
}
return retVal;
}
}

@ -29,227 +29,271 @@
/**
* @author Alexander Pelov
*/
public class HistoryServiceImpl implements HistoryService {
public static final String DATA_DIRECTORY = "history_ver1.0";
public static final String DATA_FILE = "dbstruct.dat";
/**
* The logger for this class.
*/
private static Logger log = Logger
.getLogger(HistoryServiceImpl.class);
private Map histories = Collections.synchronizedMap(new HashMap());
private Collection loadedFiles = Collections.synchronizedCollection(
new Vector());
private ConfigurationService configurationService;
private FileAccessService fileAccessService;
private Object syncRoot_FileAccess = new Object();
private Object syncRoot_Config = new Object();
private DocumentBuilder builder;
public HistoryServiceImpl()
throws Exception
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
this.builder = factory.newDocumentBuilder();
}
public Iterator getExistingIDs() {
Vector vect = new Vector();
File histDir = null;
try {
histDir = this.fileAccessService.getPrivatePersistentDirectory
(DATA_DIRECTORY);
findDatFiles(vect, histDir);
} catch (Exception e) {
log.error("Error opening directory", e);
}
DBStructSerializer structParse = new DBStructSerializer(this);
int size = vect.size();
for(int i = 0; i < size; i++) {
File f = (File)vect.get(i);
synchronized(this.loadedFiles) {
if(!this.loadedFiles.contains(f)) {
synchronized(this.histories) {
try {
History hist = structParse.loadHistory(f);
if(!this.histories.containsKey(hist.getID())) {
this.histories.put(hist.getID(), hist);
}
} catch(Exception e) {
log.error("Could not load history from file: " +
f.getAbsolutePath(), e);
}
}
}
}
}
synchronized(this.histories) {
return this.histories.keySet().iterator();
}
public class HistoryServiceImpl
implements HistoryService
{
public static final String DATA_DIRECTORY = "history_ver1.0";
public static final String DATA_FILE = "dbstruct.dat";
/**
* The logger for this class.
*/
private static Logger log = Logger
.getLogger(HistoryServiceImpl.class);
private Map histories = Collections.synchronizedMap(new HashMap());
private Collection loadedFiles = Collections.synchronizedCollection(
new Vector());
private ConfigurationService configurationService;
private FileAccessService fileAccessService;
private Object syncRoot_FileAccess = new Object();
private Object syncRoot_Config = new Object();
private DocumentBuilder builder;
public HistoryServiceImpl() throws Exception
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
this.builder = factory.newDocumentBuilder();
}
public boolean isHistoryExisting(HistoryID id) {
synchronized (this.histories) {
return this.histories.containsKey(id);
}
public Iterator getExistingIDs()
{
Vector vect = new Vector();
File histDir = null;
try
{
histDir = this.fileAccessService.getPrivatePersistentDirectory
(DATA_DIRECTORY);
findDatFiles(vect, histDir);
}
catch (Exception e)
{
log.error("Error opening directory", e);
}
DBStructSerializer structParse = new DBStructSerializer(this);
int size = vect.size();
for (int i = 0; i < size; i++)
{
File f = (File) vect.get(i);
synchronized (this.loadedFiles)
{
if (!this.loadedFiles.contains(f))
{
synchronized (this.histories)
{
try
{
History hist = structParse.loadHistory(f);
if (!this.histories.containsKey(hist.getID()))
{
this.histories.put(hist.getID(), hist);
}
}
catch (Exception e)
{
log.error("Could not load history from file: " +
f.getAbsolutePath(), e);
}
}
}
}
}
synchronized (this.histories)
{
return this.histories.keySet().iterator();
}
}
public boolean isHistoryExisting(HistoryID id)
{
synchronized (this.histories)
{
return this.histories.containsKey(id);
}
}
public History getHistory(HistoryID id) throws IllegalArgumentException {
History retVal = null;
synchronized(histories) {
if(histories.containsKey(id)) {
retVal = (History) histories.get(id);
} else {
throw new IllegalArgumentException(
"No history corresponds to the specified ID.");
}
}
return retVal;
}
public History createHistory(HistoryID id,
HistoryRecordStructure recordStructure)
throws IllegalArgumentException, IOException
{
History retVal = null;
synchronized(this.histories) {
if(this.histories.containsKey(id)) {
throw new IllegalArgumentException(
"There is already a history with the specified ID.");
} else {
File dir = this.createHistoryDirectories(id);
HistoryImpl history = new HistoryImpl(id, dir,
recordStructure, this);
File dbDatFile = new File(dir, HistoryServiceImpl.DATA_FILE);
DBStructSerializer dbss = new DBStructSerializer(this);
dbss.writeHistory(dbDatFile, history);
this.histories.put(id, history);
retVal = history;
}
}
return retVal;
}
protected FileAccessService getFileAccessService() {
return this.fileAccessService;
}
protected DocumentBuilder getDocumentBuilder() {
return builder;
}
private void findDatFiles(Vector vect, File directory) {
File[] files = directory.listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isDirectory()) {
findDatFiles(vect, files[i]);
} else if(DATA_FILE.equalsIgnoreCase(files[i].getName())) {
vect.add(files[i]);
}
}
public History getHistory(HistoryID id) throws IllegalArgumentException
{
History retVal = null;
synchronized (histories)
{
if (histories.containsKey(id))
{
retVal = (History) histories.get(id);
}
else
{
throw new IllegalArgumentException(
"No history corresponds to the specified ID.");
}
}
return retVal;
}
private File createHistoryDirectories(HistoryID id) throws IOException {
String[] idComponents = id.getID();
String[] dirs = new String[idComponents.length+1];
dirs[0] = "history";
System.arraycopy(idComponents, 0, dirs, 1, dirs.length-1);
File directory = null;
try {
directory = this.fileAccessService
.getPrivatePersistentDirectory(dirs);
} catch (Exception e) {
throw (IOException)new IOException(
"Could not create history due to file system error")
.initCause(e);
}
if(!directory.exists() && !directory.mkdirs()) {
throw new IOException(
"Could not create requested history service files:" +
directory.getAbsolutePath());
}
return directory;
}
////////////////////////////////////////////////////////////////////////////
/**
public History createHistory(HistoryID id,
HistoryRecordStructure recordStructure) throws
IllegalArgumentException, IOException
{
History retVal = null;
synchronized (this.histories)
{
if (this.histories.containsKey(id))
{
throw new IllegalArgumentException(
"There is already a history with the specified ID.");
}
else
{
File dir = this.createHistoryDirectories(id);
HistoryImpl history = new HistoryImpl(id, dir,
recordStructure, this);
File dbDatFile = new File(dir, HistoryServiceImpl.DATA_FILE);
DBStructSerializer dbss = new DBStructSerializer(this);
dbss.writeHistory(dbDatFile, history);
this.histories.put(id, history);
retVal = history;
}
}
return retVal;
}
protected FileAccessService getFileAccessService()
{
return this.fileAccessService;
}
protected DocumentBuilder getDocumentBuilder()
{
return builder;
}
private void findDatFiles(Vector vect, File directory)
{
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
findDatFiles(vect, files[i]);
}
else if (DATA_FILE.equalsIgnoreCase(files[i].getName()))
{
vect.add(files[i]);
}
}
}
private File createHistoryDirectories(HistoryID id) throws IOException
{
String[] idComponents = id.getID();
String[] dirs = new String[idComponents.length + 1];
dirs[0] = "history";
System.arraycopy(idComponents, 0, dirs, 1, dirs.length - 1);
File directory = null;
try
{
directory = this.fileAccessService
.getPrivatePersistentDirectory(dirs);
}
catch (Exception e)
{
throw (IOException)new IOException(
"Could not create history due to file system error")
.initCause(e);
}
if (!directory.exists() && !directory.mkdirs())
{
throw new IOException(
"Could not create requested history service files:" +
directory.getAbsolutePath());
}
return directory;
}
////////////////////////////////////////////////////////////////////////////
/**
* Set the configuration service.
*
*
* @param configurationService
*/
public void setConfigurationService(
ConfigurationService configurationService)
{
synchronized(this.syncRoot_Config) {
this.configurationService = configurationService;
log.debug("New configuration service registered.");
}
ConfigurationService configurationService)
{
synchronized (this.syncRoot_Config)
{
this.configurationService = configurationService;
log.debug("New configuration service registered.");
}
}
/**
* Remove a configuration service.
*
*
* @param configurationService
*/
public void unsetConfigurationService(
ConfigurationService configurationService)
ConfigurationService configurationService)
{
synchronized(this.syncRoot_Config) {
if(this.configurationService == configurationService) {
this.configurationService = null;
log.debug("Configuration service unregistered.");
}
}
synchronized (this.syncRoot_Config)
{
if (this.configurationService == configurationService)
{
this.configurationService = null;
log.debug("Configuration service unregistered.");
}
}
}
/**
* Set the file access service.
*
*
* @param fileAccessService
*/
public void setFileAccessService(FileAccessService fileAccessService) {
synchronized(this.syncRoot_FileAccess) {
this.fileAccessService = fileAccessService;
log.debug("New file access service registered.");
}
public void setFileAccessService(FileAccessService fileAccessService)
{
synchronized (this.syncRoot_FileAccess)
{
this.fileAccessService = fileAccessService;
log.debug("New file access service registered.");
}
}
/**
* Remove the file access service.
*
*
* @param fileAccessService
*/
public void unsetFileAccessService(FileAccessService fileAccessService) {
synchronized(this.syncRoot_FileAccess) {
if(this.fileAccessService == fileAccessService) {
this.fileAccessService = null;
log.debug("File access service unregistered.");
}
}
public void unsetFileAccessService(FileAccessService fileAccessService)
{
synchronized (this.syncRoot_FileAccess)
{
if (this.fileAccessService == fileAccessService)
{
this.fileAccessService = null;
log.debug("File access service unregistered.");
}
}
}
}

@ -23,111 +23,133 @@
/**
* @author Alexander Pelov
*/
public class HistoryWriterImpl implements HistoryWriter {
public static final int MAX_RECORDS_PER_FILE = 100;
private Object docCreateLock = new Object();
private Object docWriteLock = new Object();
private HistoryImpl historyImpl;
private String[] structPropertyNames;
private Document currentDoc = null;
private String currentFile = null;
private int currentDocElements = -1;
protected HistoryWriterImpl(HistoryImpl historyImpl) {
this.historyImpl = historyImpl;
HistoryRecordStructure struct =
this.historyImpl.getHistoryRecordsStructure();
this.structPropertyNames = struct.getPropertyNames();
}
public void addRecord(HistoryRecord record) throws IOException {
this.addRecord(record.getPropertyNames(), record.getPropertyValues(),
record.getTimestamp());
}
public void addRecord(String[] propertyValues) throws IOException {
this.addRecord(structPropertyNames, propertyValues, new Date());
}
private void addRecord(String[] propertyNames, String[] propertyValues, Date date)
throws InvalidParameterException, IOException
{
// Synchronized to assure that two concurent threads can insert records safely.
synchronized(docCreateLock) {
if(this.currentDoc == null || this.currentDocElements > MAX_RECORDS_PER_FILE) {
this.createNewDoc(date, this.currentDoc == null);
}
}
synchronized(this.currentDoc) {
Node root = this.currentDoc.getFirstChild();
synchronized(root) {
Element elem = this.currentDoc.createElement("record");
elem.setAttribute("timestamp", Long.toString(date.getTime()));
for(int i = 0; i < propertyNames.length; i++) {
Element propertyElement = this.currentDoc.createElement(propertyNames[i]);
Text value = this.currentDoc.createTextNode(propertyValues[i]);
propertyElement.appendChild(value);
elem.appendChild(propertyElement);
}
root.appendChild(elem);
this.currentDocElements++;
}
}
// write changes
synchronized(docWriteLock) {
this.historyImpl.writeFile(this.currentFile);
}
}
/**
* If no file is currently loaded loads the last opened file. If
* it does not exists or if the current file was set - create a
* new file.
*
* @param date
*/
private void createNewDoc(Date date, boolean loadLastFile) {
boolean loaded = false;
if(loadLastFile) {
Iterator files = historyImpl.getFileList();
String file = null;
while(files.hasNext()) {
file = (String)files.next();
}
if(file != null) {
this.currentDoc = this.historyImpl.getDocumentForFile(file);
this.currentFile = file;
loaded = true;
}
}
if(!loaded) {
this.currentFile = Long.toString(date.getTime());
while(this.currentFile.length() < 8) {
this.currentFile = "0" + this.currentFile;
}
this.currentFile += ".xml";
this.currentDoc = this.historyImpl.createDocument(this.currentFile);
}
// TODO: Assert: Assert.assertNonNull(this.currentDoc,
// "There should be a current document created.");
this.currentDocElements = this.currentDoc.getFirstChild().getChildNodes().getLength();
}
public class HistoryWriterImpl
implements HistoryWriter
{
public static final int MAX_RECORDS_PER_FILE = 100;
private Object docCreateLock = new Object();
private Object docWriteLock = new Object();
private HistoryImpl historyImpl;
private String[] structPropertyNames;
private Document currentDoc = null;
private String currentFile = null;
private int currentDocElements = -1;
protected HistoryWriterImpl(HistoryImpl historyImpl)
{
this.historyImpl = historyImpl;
HistoryRecordStructure struct =
this.historyImpl.getHistoryRecordsStructure();
this.structPropertyNames = struct.getPropertyNames();
}
public void addRecord(HistoryRecord record) throws IOException
{
this.addRecord(record.getPropertyNames(), record.getPropertyValues(),
record.getTimestamp());
}
public void addRecord(String[] propertyValues) throws IOException
{
this.addRecord(structPropertyNames, propertyValues, new Date());
}
private void addRecord(String[] propertyNames, String[] propertyValues,
Date date) throws InvalidParameterException,
IOException
{
// Synchronized to assure that two concurent threads can insert records safely.
synchronized (docCreateLock)
{
if (this.currentDoc == null ||
this.currentDocElements > MAX_RECORDS_PER_FILE)
{
this.createNewDoc(date, this.currentDoc == null);
}
}
synchronized (this.currentDoc)
{
Node root = this.currentDoc.getFirstChild();
synchronized (root)
{
Element elem = this.currentDoc.createElement("record");
elem.setAttribute("timestamp", Long.toString(date.getTime()));
for (int i = 0; i < propertyNames.length; i++)
{
Element propertyElement = this.currentDoc.createElement(
propertyNames[i]);
Text value = this.currentDoc.createTextNode(propertyValues[
i]);
propertyElement.appendChild(value);
elem.appendChild(propertyElement);
}
root.appendChild(elem);
this.currentDocElements++;
}
}
// write changes
synchronized (docWriteLock)
{
this.historyImpl.writeFile(this.currentFile);
}
}
/**
* If no file is currently loaded loads the last opened file. If
* it does not exists or if the current file was set - create a
* new file.
*
* @param date
*/
private void createNewDoc(Date date, boolean loadLastFile)
{
boolean loaded = false;
if (loadLastFile)
{
Iterator files = historyImpl.getFileList();
String file = null;
while (files.hasNext())
{
file = (String) files.next();
}
if (file != null)
{
this.currentDoc = this.historyImpl.getDocumentForFile(file);
this.currentFile = file;
loaded = true;
}
}
if (!loaded)
{
this.currentFile = Long.toString(date.getTime());
while (this.currentFile.length() < 8)
{
this.currentFile = "0" + this.currentFile;
}
this.currentFile += ".xml";
this.currentDoc = this.historyImpl.createDocument(this.currentFile);
}
// TODO: Assert: Assert.assertNonNull(this.currentDoc,
// "There should be a current document created.");
this.currentDocElements = this.currentDoc.getFirstChild().getChildNodes().
getLength();
}
}

Loading…
Cancel
Save