|
From: <bob...@us...> - 2003-12-31 04:06:46
|
Update of /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler
In directory sc8-pr-cvs1:/tmp/cvs-serv2011/src/hk/hku/cecid/phoenix/message/handler
Modified Files:
Constants.java DirectoryManager.java
MessageServiceHandler.java
Added Files:
FilePersistenceHandler.java ObjectStorePersistenceHandler.java
PersistenceException.java PersistenceHandler.java
PersistenceManager.java RepositoryPersistenceHandler.java
Log Message:
Add PersistenceHandler interface as the customize Persistence interface
Add RepositoryPersistenceHandler and ObjectStorePersistenceHandler,
which is the default persistence Handler for Repository and Object Store
Add PersistenceManager which will probably replace the DirectoryManager,
so that the persistence will not bound to File only.
Note that we haven't changed the logic on Hermes to use the PersistenceHandler
Interface yet...
Add a src_junit to add junit test case to this directory.
add test cases on testing the default persistence handler base class
Change build.xml to run the test cases.
--- NEW FILE: FilePersistenceHandler.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/FilePersistenceHandler.java,v 1.1 2003/12/31 04:06:42 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2003-12-17]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.message.handler;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import javax.activation.DataSource;
//import org.apache.commons.codec.digest.DigestUtils;
/**
* The Persistence Handler implementation using file system
* @author pykoon
* @version $Revision: 1.1 $
*/
public class FilePersistenceHandler implements PersistenceHandler {
/**
* the content type for the data source constructed.
*/
private static final String CONTENT_TYPE = "application/binary";
/**
* the current directory the persistence handler working on.
*/
private File currentDirectory;
/**
* Construct the File Persistence Handler which store files persistence on
* the given directory
* @param dir the directory to store those files.
* @throws InitializationException thrown if there is error on initializing
* the Persistence Handler
*/
public FilePersistenceHandler(File dir) throws InitializationException {
currentDirectory = dir;
}
/**
* get the current directory for the Persistence Handler
* @return the current directory for the Persistence Handler
*/
public File getCurrentDirectory() {
return currentDirectory;
}
/**
* set the current directory for the Persistence Handler
* @param directory the directory for the Persistence Handler
*/
protected void setCurrentDirectory(File directory) {
currentDirectory = directory;
}
/**
* create the DataSource which represent a new file.
* @return a new DataSource object, which user can get the OutputStream
* to store the object.
* @throws PersistenceException thrown when there is error on creating
* a new persist object
*/
public DataSource createNewObject() throws PersistenceException {
try {
String name = createNewFileName(getCurrentDirectory());
return new FileDataSource(name);
} catch (IOException e) {
throw new PersistenceException(e);
}
}
/**
* create a new file in a input directory, and the String represent the name
* for the new DataSource. The file created shiould be used as the new
* DataSource returned by createNewObject()
* @param directory which you want to create the file
* @return name to represent the DataSource point to the new file.
* @throws PersistenceException thrown when there is error on creating
* a new file
*/
protected String createNewFileName(File directory)
throws PersistenceException {
try {
File file = createUniqueFile(directory, "object");
return file.getName();
} catch (IOException e) {
throw new PersistenceException(e);
}
}
/**
* Generates a unique file in the specified directory
*
* @param path the specified directory to search for unique file name
* @param id the id of the message/object
* @return the unique file name generated
* @throws IOException when there is error on creating a new file.
*/
static synchronized File createUniqueFile(File path, String id)
throws IOException {
File result = File.createTempFile(id, "", path);
return result;
}
/**
* get back the data stored in the persistence object using the given name
* @param name the name to access the object
* @return the DataSource which contains the object, or null if it doesn't
* exists.
* @throws PersistenceException if there are errors when getting the
* object from PersistenceHandler.
*/
public DataSource getObject(String name) throws PersistenceException {
try {
File file = getFileFromName(name);
if (!file.exists()) {
return null;
}
FileDataSource fds = new FileDataSource(name);
fds.setOutputStreamGetFail();
return fds;
} catch (IOException e) {
throw new PersistenceException(e);
}
}
/**
* remove the object from the persistence storage.
* @param name the name to access the object
* @throws PersistenceException if there are errors when removing the
* object from PersistenceHandler.
*/
public synchronized void removeObject(String name)
throws PersistenceException {
File file = getFileFromName(name);
if (file.exists() && !file.delete()) {
throw new PersistenceException("Object cannot be removed");
}
}
/**
* get the file from name
* @param name the name of the DataSource
* @return the file associate to the name
*/
protected File getFileFromName(String name) {
return new File(getCurrentDirectory(), name);
}
/**
* The self implemented FileDataSource
* The reason to re-implement it is to support the getOutputStream()
* for this DataSource, which seems failed on
* javax.activation.FileDataSource
*/
private class FileDataSource implements DataSource {
/**
* The file referenced on this FileDataSource
*/
private File file;
/**
* boolean to specify whether output stream is gotten by user
*/
private boolean outputStreamGotten;
/**
* The name of the FileDataSource
*/
private String name;
/**
* Construct the FileDataSource from file
* @param name the name for the data source
* @throws IOException throw if there is error to get the path from
* file
*/
public FileDataSource(String name) throws IOException {
this.name = name;
file = getFileFromName(name);
outputStreamGotten = false;
}
/**
* get the content type. Must return "application/binary"
* @return must return "application/binary"
*/
public String getContentType() {
return CONTENT_TYPE;
}
/**
* get the InputStream for the File
* @return the InputStream for the file.
* @throws IOException thrown when there is exception on opening
* the stream
*/
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
}
/**
* get the unique name from other FileDataSource.
* By default is the canonical file path
* @return the name for the FileDataSource
*/
public String getName() {
return name;
}
/**
* get the OutputStream for the File. Note that we only allow the
* user to get it once.
* @return the OutputStream for the file
* @throws IOException thrown when there is exception on opening the
* stream, or the OutputStream is opened once.
*/
public OutputStream getOutputStream() throws IOException {
if (outputStreamGotten) {
throw new IOException("Cannot get OutputStream");
}
outputStreamGotten = true;
return new FileOutputStream(file);
}
/**
* set the current FileDataSource to the state that the user cannot
* get the outputStream by getOutputStream()
*/
private void setOutputStreamGetFail() {
outputStreamGotten = true;
}
}
}
--- NEW FILE: ObjectStorePersistenceHandler.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/ObjectStorePersistenceHandler.java,v 1.1 2003/12/31 04:06:42 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2003-12-29]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.message.handler;
import hk.hku.cecid.phoenix.common.util.Property;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.IOException;
/**
* ObjectStorePersistenceHandler is the default Persistence Handler for the
* object store.
* It reads the property from msh.properties.xml and access the object through
* the specific directory.
* @author pykoon
* @version $Revision: 1.1 $
*/
public class ObjectStorePersistenceHandler extends FilePersistenceHandler {
/**
* logger for this class
*/
private static Logger logger = Logger.getLogger(
ObjectStorePersistenceHandler.class);
/**
* Construct the Object Store Persistence Handler
* @throws InitializationException thrown when there is error on
* initialize the ObjectStorePersistenceHandler.
*/
public ObjectStorePersistenceHandler() throws InitializationException {
super(null);
Property prop = null;
try {
prop = Property.load(Constants.MSH_SERVER_PROPERTY_FILE);
} catch (IOException e) {
throw new InitializationException("Cannot load property");
}
// Required object store attribute
String objectStore = prop.get
(Constants.PROPERTY_MESSAGE_LISTENER_OBJECT_STORE);
if (objectStore == null) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR,
Constants.PROPERTY_MESSAGE_LISTENER_OBJECT_STORE
+ " has not been set in property file");
logger.error(err);
throw new InitializationException(err);
}
File objectDir = new File(objectStore);
if (!objectDir.exists() && !objectDir.mkdirs()) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR,
"Object store \"" + objectDir + "\" does not "
+ "exist and cannot be created");
logger.error(err);
throw new InitializationException(err);
}
if (!objectDir.isDirectory() || !objectDir.canWrite()) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR,
"Object store \"" + objectDir + "\" is not "
+ "a directory or it is not writable");
logger.error(err);
throw new InitializationException(err);
}
setCurrentDirectory(objectDir);
}
}
--- NEW FILE: PersistenceException.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/PersistenceException.java,v 1.1 2003/12/31 04:06:42 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2003-12-17]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.message.handler;
/**
* Exception thrown when there is problem on getting or storing data from
* the persistence handler.
*
* @author pykoon
* @version $Revision: 1.1 $
*/
public class PersistenceException extends Exception {
/**
* Construct a persistence exception
*/
public PersistenceException() {
super();
}
/**
* Construct a persistence exception with the input message
* @param message the error message
*/
public PersistenceException(String message) {
super(message);
}
/**
* Construct a persistence exception with the input message and cause
* @param message the error message
* @param cause the case for the exception
*/
public PersistenceException(String message, Throwable cause) {
super(message, cause);
}
/**
* Construct a persistence exception with the input cause
* @param cause the case for the exception
*/
public PersistenceException(Throwable cause) {
super(cause);
}
}
--- NEW FILE: PersistenceHandler.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/PersistenceHandler.java,v 1.1 2003/12/31 04:06:42 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2003-12-17]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.message.handler;
import javax.activation.DataSource;
/**
* An interface represent a Persistence Storage, which can store objects and
* get objects
* @author pykoon
* @version $Revision: 1.1 $
*/
public interface PersistenceHandler {
/**
* create a new object to store on the Persistence.
* User can store their data by getting the OutputStream from the
* DataSource returned. The name got from the DataSource will be the key
* to access back the object
* @return the DataSource object which you can store the data.
* @throws PersistenceException if there are errors when creating the
* object from PersistenceHandler.
*/
DataSource createNewObject() throws PersistenceException;
/**
* get back the data stored in the persistence object using the given name
* @param name the name to access the object
* @return the DataSource which contains the object.
* @throws PersistenceException if there are errors when getting the
* object from PersistenceHandler.
*/
DataSource getObject(String name) throws PersistenceException;
/**
* remove the object from the persistence storage.
* @param name the name to access the object
* @throws PersistenceException if there are errors when removing the
* object from PersistenceHandler.
*/
void removeObject(String name) throws PersistenceException;
}
--- NEW FILE: PersistenceManager.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/PersistenceManager.java,v 1.1 2003/12/31 04:06:42 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2002-12-29]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.message.handler;
import hk.hku.cecid.phoenix.common.util.Property;
import org.apache.log4j.Logger;
/**
* This class manages the persistence of ebXML messages and the sending
* thread context of MSH to the file system. It is written based on
* the DirectoryManager, but allow the user to customize to way to store by
* writing custom PersistenceHandler
* @author pykoon
* @version $Revision: 1.1 $
*/
public class PersistenceManager {
/**
* logger for PersistenceManager class
*/
private static Logger logger = Logger.getLogger(
PersistenceManager.class);
/**
* The persistence handler for the repository
*/
private static PersistenceHandler repositoryPersistenceHandler;
/**
* The persistence handler for the object store
*/
private static PersistenceHandler objectStorePersistenceHandler;
/**
* The default repository persistenec handler class.
* The value is RepositoryPersistenceHandler
*/
private static final Class DEFAULT_REPOSITORY_PERSISTENCE_HANDLER
= RepositoryPersistenceHandler.class;
/**
* The default object store persistence handler class
* The value is ObjectStorePersistenceHandler
*/
private static final Class DEFAULT_OBJECT_STORE_PERSISTENCE_HANDLER
= ObjectStorePersistenceHandler.class;
/**
* Flag indicating if the class has been configured.
*/
private static boolean isConfigured = false;
/**
* Configure the class if and only if it has not been configured.
*
* @param prop <code>Property</code> object.
* @throws InitializationException thrown when there is error on
* configurating the PersistenceManager
*/
static synchronized void configure(Property prop)
throws InitializationException {
if (isConfigured) {
return;
}
String repositoryPersistHandlerClassName = prop.get(
Constants.PROPERTY_MESSAGE_REPOSITORY_PERSIST_HANDLER);
if (repositoryPersistHandlerClassName != null) {
logger.info("Use customize repository persistence handler : " +
repositoryPersistHandlerClassName);
} else {
logger.info("Use default repository persistence hander");
}
try {
repositoryPersistenceHandler = createPersistenceHandlerInstance(
DEFAULT_REPOSITORY_PERSISTENCE_HANDLER,
repositoryPersistHandlerClassName);
} catch (Exception e) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR, e,
"Cannot load repository persistence handler");
logger.error(err);
throw new InitializationException(err);
}
// Required message repository attribute
String objectStorePersistHandlerClassName = prop.get(
Constants.PROPERTY_MESSAGE_LISTENER_OBJECT_STORE_PERSIST_HANDLER
);
if (objectStorePersistHandlerClassName != null) {
logger.info("Use customize object store persistence handler : " +
objectStorePersistHandlerClassName);
} else {
logger.info("Use default object store persistence handler");
}
try {
objectStorePersistenceHandler = createPersistenceHandlerInstance(
DEFAULT_OBJECT_STORE_PERSISTENCE_HANDLER,
objectStorePersistHandlerClassName);
} catch (Exception e) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR, e,
"Cannot load repository persistence handler");
logger.error(err);
throw new InitializationException(err);
}
isConfigured = true;
}
/**
* create the Persistence Handler based on the inputted classMame.
* If the className is null, it will create the Persistence Handler using
* the defaultHandlerClass.
* @param defaultHandlerClass the default Handler class to load if the
* className is null
* @param className the class name of the Persistence Handler to load
* @return The Persistence Handler instance
* @throws Exception thrown when there is error occur on creating the
* instance.
*/
private static PersistenceHandler createPersistenceHandlerInstance(
Class defaultHandlerClass, String className) throws Exception {
Class handlerClass = defaultHandlerClass;
if (className != null) {
handlerClass = Class.forName(className);
}
Object result = handlerClass.newInstance();
if (result instanceof PersistenceHandler) {
return (PersistenceHandler) result;
} else {
throw new IllegalArgumentException(
"Specify class is not an instance of PersistenceHandler");
}
}
/**
* get the Repository Persistence Handler
* @return teh Repository Persistence Handler
*/
public static PersistenceHandler getRepositoryPersistenceHandler() {
return repositoryPersistenceHandler;
}
/**
* get the Object Store Persistence Handler
* @return teh Object Store Persistence Handler
*/
public static PersistenceHandler getObjectStorePersistenceHandler() {
return objectStorePersistenceHandler;
}
}
--- NEW FILE: RepositoryPersistenceHandler.java ---
/*
* Copyright(c) 2002 Center for E-Commerce Infrastructure Development, The
* University of Hong Kong (HKU). All Rights Reserved.
*
* This software is licensed under the Academic Free License Version 1.0
*
* Academic Free License
* Version 1.0
*
* This Academic Free License applies to any software and associated
* documentation (the "Software") whose owner (the "Licensor") has placed the
* statement "Licensed under the Academic Free License Version 1.0" immediately
* after the copyright notice that applies to the Software.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of the Software (1) to use, copy, modify, merge, publish, perform,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, and (2) under patent
* claims owned or controlled by the Licensor that are embodied in the Software
* as furnished by the Licensor, to make, use, sell and offer for sale the
* Software and derivative works thereof, subject to the following conditions:
*
* - Redistributions of the Software in source code form must retain all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers.
* - Redistributions of the Software in executable form must reproduce all
* copyright notices in the Software as furnished by the Licensor, this list
* of conditions, and the following disclaimers in the documentation and/or
* other materials provided with the distribution.
* - Neither the names of Licensor, nor the names of any contributors to the
* Software, nor any of their trademarks or service marks, may be used to
* endorse or promote products derived from this Software without express
* prior written permission of the Licensor.
*
* DISCLAIMERS: LICENSOR WARRANTS THAT THE COPYRIGHT IN AND TO THE SOFTWARE IS
* OWNED BY THE LICENSOR OR THAT THE SOFTWARE IS DISTRIBUTED BY LICENSOR UNDER
* A VALID CURRENT LICENSE. EXCEPT AS EXPRESSLY STATED IN THE IMMEDIATELY
* PRECEDING SENTENCE, THE SOFTWARE IS PROVIDED BY THE LICENSOR, CONTRIBUTORS
* AND COPYRIGHT OWNERS "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* LICENSOR, CONTRIBUTORS OR COPYRIGHT OWNERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE.
*
* This license is Copyright (C) 2002 Lawrence E. Rosen. All rights reserved.
* Permission is hereby granted to copy and distribute this license without
* modification. This license may not be modified without the express written
* permission of its copyright owner.
*/
/* =====
*
* $Header: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/RepositoryPersistenceHandler.java,v 1.1 2003/12/31 04:06:42 bobpykoon Exp $
*
* Code authored by:
*
* pykoon [2003-12-22]
*
* Code reviewed by:
*
* username [YYYY-MM-DD]
*
* Remarks:
*
* =====
*/
package hk.hku.cecid.phoenix.message.handler;
import hk.hku.cecid.phoenix.common.util.Property;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.IOException;
/**
* RepositoryPersistenceHandler is the default Persistence Handler for the
* repository object.
* It reads the property from msh.properties.xml and access the object through
* the specific directory.
* @author pykoon
* @version $Revision: 1.1 $
*/
public class RepositoryPersistenceHandler extends FilePersistenceHandler {
/**
* logger for this class
*/
private static Logger logger = Logger.getLogger(
RepositoryPersistenceHandler.class);
/**
The maximum number of files in a sub-directory
*/
private int maxNumFileInSubDir;
/**
* Current subdirectory inside the parent directory for saving ebXML
* messages
*/
private File curSubDirectory = null;
/**
* Construct the Repository Persistence Handler
* @throws InitializationException thrown when there is error on
* initialize the RepositoryPersistenceHandler.
*/
public RepositoryPersistenceHandler() throws InitializationException {
super(null);
Property prop = null;
try {
prop = Property.load(Constants.MSH_SERVER_PROPERTY_FILE);
} catch (IOException e) {
throw new InitializationException("Cannot load property");
}
String messageRepository = prop.get(
Constants.PROPERTY_MESSAGE_REPOSITORY);
if (messageRepository == null) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR,
Constants.PROPERTY_MESSAGE_REPOSITORY
+ " has not been set in property file");
logger.error(err);
throw new InitializationException(err);
}
// Optional maximum number of files in a repository subdirectory
String maxNumFile = prop.get(
Constants.PROPERTY_MAX_FILE_IN_SUBDIRECTORY);
if (maxNumFile == null) {
maxNumFileInSubDir = Constants.DEF_MAX_NUM_IN_SUBDIR;
} else {
try {
maxNumFileInSubDir = Integer.parseInt(maxNumFile);
} catch (NumberFormatException e) {
maxNumFileInSubDir = Constants.DEF_MAX_NUM_IN_SUBDIR;
}
}
// check the existence the directory
File repositoryDir = new File(messageRepository);
if (!repositoryDir.exists() && !repositoryDir.mkdirs()) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR,
"Message repository \"" + messageRepository + "\" does not "
+ "exist and cannot be created");
logger.error(err);
throw new InitializationException(err);
}
if (!repositoryDir.isDirectory() || !repositoryDir.canWrite()) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_INIT_ERROR,
"Message repository \"" + messageRepository + "\" is not "
+ "a directory or it is not writable");
logger.error(err);
throw new InitializationException(err);
}
setCurrentDirectory(repositoryDir);
}
/**
* create the DataSource which represent a new file.
* @return a new DataSource object, which user can get the OutputStream
* to store the object.
* @throws PersistenceException thrown when there is error on creating
* a new persist object
*/
public synchronized String createNewObject(File directory)
throws PersistenceException {
File repositoryDir = directory;
String directoryName = null;
if (curSubDirectory == null) {
File[] subDirs = repositoryDir.listFiles();
int maxSubDirNum = -1;
File maxSubDir = null;
for (int i = 0 ; i < subDirs.length ; i++) {
String name = subDirs[i].getName();
if (name.length() == 5 && name.startsWith("R")) {
try {
int subDirNum = Integer.parseInt(name.substring(1));
if (subDirNum > maxSubDirNum) {
maxSubDirNum = subDirNum;
maxSubDir = subDirs[i];
directoryName = name;
}
} catch (NumberFormatException e) {
continue;
}
}
}
if (maxSubDirNum != -1) {
curSubDirectory = maxSubDir;
} else {
curSubDirectory = new File(repositoryDir, "R0000");
if (!curSubDirectory.exists()) {
curSubDirectory.mkdir();
}
}
}
if (!curSubDirectory.isDirectory() || !curSubDirectory.canWrite()) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_FILE_IO_ERROR,
curSubDirectory.toString()
+ " is not a directory, or it is not writable");
logger.error(err);
throw new PersistenceException(err);
}
int numFiles = curSubDirectory.listFiles().length;
if (numFiles >= maxNumFileInSubDir) {
int subDirNum = Integer.parseInt(
curSubDirectory.getName().substring(1));
subDirNum++;
if (subDirNum > 99999) {
String err = ErrorMessages.getMessage(
ErrorMessages.ERR_HERMES_FILE_IO_ERROR,
"number of subdirectories in message repository "
+ "overflow");
throw new PersistenceException(err);
}
directoryName = "R"
+ (subDirNum > 1000 ? "" : "0")
+ (subDirNum > 100 ? "" : "0")
+ (subDirNum > 10 ? "" : "0")
+ subDirNum;
curSubDirectory = new File(repositoryDir, directoryName);
curSubDirectory.mkdir();
}
try {
File resultFile = createUniqueFile(curSubDirectory, "message");
return directoryName + File.separatorChar + resultFile.getName();
} catch (IOException e) {
throw new PersistenceException(e);
}
}
/**
* remove the object from the persistence storage.
* @param name the name to access the object
* @throws PersistenceException if there are errors when removing the
* object from PersistenceHandler.
*/
public synchronized void removeObject(String name)
throws PersistenceException {
File file = getFileFromName(name);
super.removeObject(name);
file = file.getParentFile();
if (file.listFiles().length == 0) {
file.delete();
}
}
}
Index: Constants.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/Constants.java,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** Constants.java 9 Dec 2003 08:59:04 -0000 1.34
--- Constants.java 31 Dec 2003 04:06:42 -0000 1.35
***************
*** 400,403 ****
--- 400,409 ----
public static final String PROPERTY_MESSAGE_REPOSITORY =
"MSH/Persistent/MessageRepository";
+
+ /**
+ * The PersistenceHandler to store the message in message repository
+ */
+ public static final String PROPERTY_MESSAGE_REPOSITORY_PERSIST_HANDLER
+ = "MSH/Persistent/MessageRepositoryPersistHandler";
/** Maximum number of files allowed in a directory for saving ebXML
***************
*** 503,506 ****
--- 509,518 ----
public static final String PROPERTY_MESSAGE_LISTENER_OBJECT_STORE =
"MSH/MessageListener/ObjectStore";
+ /**
+ * The Persistence Handler class to handler the persistence of objectStore
+ */
+ public static final String
+ PROPERTY_MESSAGE_LISTENER_OBJECT_STORE_PERSIST_HANDLER
+ = "MSH/MessageListener/ObjectStore/PersistHandler";
/**
Index: DirectoryManager.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/DirectoryManager.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** DirectoryManager.java 11 Dec 2003 06:41:29 -0000 1.22
--- DirectoryManager.java 31 Dec 2003 04:06:42 -0000 1.23
***************
*** 347,350 ****
--- 347,351 ----
*
* @param ebxmlMessage the message to be stored
+ * @return the file name which store the message
*/
static String store(EbxmlMessage ebxmlMessage)
Index: MessageServiceHandler.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/MessageServiceHandler.java,v
retrieving revision 1.180
retrieving revision 1.181
diff -C2 -d -r1.180 -r1.181
*** MessageServiceHandler.java 11 Dec 2003 06:41:29 -0000 1.180
--- MessageServiceHandler.java 31 Dec 2003 04:06:42 -0000 1.181
***************
*** 71,75 ****
import hk.hku.cecid.phoenix.common.util.AuthenticationManager;
import hk.hku.cecid.phoenix.common.util.Property;
- import hk.hku.cecid.phoenix.message.handler.InitializationException;
import hk.hku.cecid.phoenix.message.packaging.AckRequested;
import hk.hku.cecid.phoenix.message.packaging.Acknowledgment;
--- 71,74 ----
|