/*--
Copyright (C) 2002-2005 Adrian Price.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The names "OBE" and "Open Business Engine" must not be used to
endorse or promote products derived from this software without prior
written permission. For written permission, please contact
adrianprice@sourceforge.net.
4. Products derived from this software may not be called "OBE" or
"Open Business Engine", nor may "OBE" or "Open Business Engine"
appear in their name, without prior written permission from
Adrian Price (adrianprice@users.sourceforge.net).
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
For more information on OBE, please see
<http://obe.sourceforge.net/>.
*/
package org.obe.engine.repository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.*;
import org.obe.OBERuntimeException;
import org.obe.client.api.repository.AbstractMetaData;
import org.obe.client.api.repository.ObjectAlreadyExistsException;
import org.obe.client.api.repository.ObjectNotFoundException;
import org.obe.client.api.repository.RepositoryException;
import org.obe.spi.WorkflowService;
import org.obe.spi.service.ServerConfig;
import org.obe.spi.service.ServiceManager;
import org.obe.util.CommonConfig;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.*;
/**
* An abstract base class for basic repository implementations. It is
* essentially an abstract object 'factory factory'.
*
* @author Adrian Price
*/
public abstract class AbstractRepository implements WorkflowService {
protected static final String SERVICE = "Service ";
protected static final String NOT_FOUND_MSG =
"Repository does not contain key: ";
private static Mapping _mapping;
protected final ServiceManager _svcMgr;
private final Class _metaDataClass;
private final Map _objectTypes = new HashMap();
private final Map _entries = new HashMap();
private AbstractMetaData[] _metaData;
private boolean _initialized;
private boolean _modified;
private class RepositoryUnmarshalListener implements UnmarshalListener {
private final boolean _debug;
private final Map _idToObject;
RepositoryUnmarshalListener(boolean debug, Map idToObject) {
_debug = debug;
_idToObject = idToObject;
}
public void initialized(Object object) {
if (_debug)
getLogger().debug("initialized(\"" + object + "\")");
}
public void attributesProcessed(Object object) {
if (_debug) {
getLogger().debug("attributesProcessed(\"" + object +
"\")");
}
}
public void fieldAdded(String fieldName, Object parent,
Object child) {
if (_debug) {
getLogger().debug("fieldAdded(\"" + fieldName +
"\", \"" + parent + "\", \"" + child + "\")");
}
}
public void unmarshalled(Object object) {
if (_debug)
getLogger().debug("unmarshalled(\"" + object + "\")");
// We do this to compensate for Castor's mis-handling of
// object identity. It manages to keep track of
// AbstractMetaData subclasses because it always knows
// when to expect one, but fields of type Object get
// ignored even if their runtime class declares or
// inherits an identity field.
if (object instanceof RepositoryEntry) {
RepositoryEntry entry = (RepositoryEntry)object;
if (entry.getMetaData() == null) {
Object instance = entry.getInstance();
if (instance != null)
_idToObject.put(entry.getKey(), instance);
}
}
}
}
private static synchronized Mapping getMapping() {
if (_mapping == null) {
_mapping = new Mapping(AbstractRepository.class.getClassLoader());
try {
InputSource src =
CommonConfig.openInputSource("castor-map.xml");
if (src != null) {
_mapping.loadMapping(src);
src.getByteStream().close();
}
} catch (IOException e) {
throw new OBERuntimeException(e);
} catch (MappingException e) {
throw new OBERuntimeException(e);
}
}
return _mapping;
}
protected static class Entry {
private AbstractMetaData _metadata;
private Object _instance;
Entry(AbstractMetaData metadata, Object instance) {
_metadata = metadata;
_instance = instance;
}
public AbstractMetaData getMetaData() {
return _metadata;
}
public Object getInstance(EntityResolver entityResolver)
throws RepositoryException {
return getInstance(entityResolver, null);
}
public Object getInstance(EntityResolver entityResolver, Object state)
throws RepositoryException {
Object instance;
if (_instance != null) {
// Singletons must be threadsafe by definition.
instance = _instance;
} else {
if (_metadata.isThreadsafe()) {
// If the instance class is threadsafe, create one lazily.
if (_instance == null)
_instance = _metadata.createInstance(entityResolver);
instance = _instance;
} else {
// TODO: Think about reusability, pooling, TLS.
// If not threadsafe, create a new instance each time.
instance = _metadata.createInstance(entityResolver, state);
}
}
return instance;
}
}
protected static Log getLog(Class clazz) {
return LogFactory.getLog(clazz);
}
protected AbstractRepository(ServiceManager svcMgr, Class metaDataClass) {
_svcMgr = svcMgr;
_metaDataClass = metaDataClass;
}
protected String getConfigurationFileName() {
String classname = getClass().getName();
classname = classname.substring(classname.lastIndexOf('.') + 1);
return classname + ".xml";
}
public synchronized void init() throws IOException, RepositoryException {
if (_initialized) {
throw new IllegalStateException(SERVICE + getServiceName() +
" already initialized");
}
_initialized = true;
load();
}
public synchronized void exit() {
if (!_initialized) {
throw new IllegalStateException(SERVICE + getServiceName() +
" not initialized");
}
_initialized = false;
// if (_modified)
// store();
_entries.clear();
_mapping = null;
_modified = false;
}
protected synchronized void load() throws RepositoryException, IOException {
clear();
InputSource src = null;
try {
// Attempt to locate the backing file.
src = CommonConfig.openInputSource(getConfigurationFileName());
if (src != null) {
Unmarshaller unmarshaller = new Unmarshaller(
RepositoryEntries.class, getClass().getClassLoader());
final Map idToObject = new HashMap();
unmarshaller.setUnmarshalListener(
new RepositoryUnmarshalListener(
getLogger().isDebugEnabled() &&
ServerConfig.isVerbose(), idToObject));
unmarshaller.setIDResolver(new IDResolver() {
public Object resolve(String idref) {
return idToObject.get(idref);
}
});
unmarshaller.setValidation(false);
unmarshaller.setMapping(getMapping());
RepositoryEntries wrapper = (RepositoryEntries)
unmarshaller.unmarshal(src);
AbstractMetaData[] types = wrapper.type;
RepositoryEntry[] entries = wrapper.entry;
if (getLogger().isDebugEnabled()) {
getLogger().debug("Found " +
(types == null ? 0 : types.length) + " types and " +
(entries == null ? 0 : entries.length) + " entries");
}
if (types != null) {
for (int i = 0; i < types.length; i++)
registerObjectType(types[i]);
}
if (entries != null) {
for (int i = 0; i < entries.length; i++) {
RepositoryEntry entry = entries[i];
try {
createEntry(entry.getKey(), entry.getMetaData(),
entry.getInstance());
} catch (Exception e) {
getLogger().error(
"Unable to create repository entry for " +
entry.getKey(), e);
}
}
}
}
} catch (Exception e) {
getLogger().error("Error loading " + getServiceName(), e);
} finally {
if (src != null) {
try {
src.getByteStream().close();
} catch (IOException e) {
// We don't care about stream close exceptions.
}
}
_modified = false;
}
}
public synchronized void store() {
if (!_modified)
return;
boolean allowInheritance = AbstractMetaData.allowInheritance;
Writer writer = null;
try {
// Must disable inheritance while storing repository contents
// (otherwise inherited values would get persisted for all
// inheritors).
AbstractMetaData.allowInheritance = false;
writer = new FileWriter(getConfigurationFileName());
Collection coll = _objectTypes.values();
AbstractMetaData[] types = (AbstractMetaData[])coll.toArray(
new AbstractMetaData[coll.size()]);
Set mapEntries = _entries.entrySet();
RepositoryEntry[] entries = new RepositoryEntry[mapEntries.size()];
int j = 0;
Iterator iter = mapEntries.iterator();
while (iter.hasNext()) {
Map.Entry mapEntry = (Map.Entry)iter.next();
Entry entry = (Entry)mapEntry.getValue();
entries[j] = new RepositoryEntry((String)mapEntry.getKey(),
entry._metadata,
entry._metadata == null ? entry._instance : null);
j++;
}
Marshaller marshaller = new Marshaller(writer);
marshaller.setMapping(getMapping());
marshaller.setRootElement("repository");
if (getLogger().isDebugEnabled() && ServerConfig.isVerbose()) {
marshaller.setMarshalListener(new MarshalListener() {
public boolean preMarshal(Object object) {
getLogger().debug("preMarshal(\"" + object + "\")");
return true;
}
public void postMarshal(Object object) {
getLogger().debug("postMarshal(\"" + object + "\")");
}
});
}
marshaller.marshal(new RepositoryEntries(types, entries));
writer.flush();
_modified = false;
} catch (Exception e) {
getLogger().error("Error saving repository contents", e);
} finally {
AbstractMetaData.allowInheritance = allowInheritance;
if (writer != null)
try {
writer.close();
} catch (IOException e) {
// We don't care about exceptions on close.
}
}
}
protected synchronized void clear() {
_entries.clear();
_modified = true;
}
public synchronized boolean isInitialized() {
return _initialized;
}
protected void registerObjectType(AbstractMetaData objectType)
throws ObjectAlreadyExistsException {
String key = objectType.getId();
if (_objectTypes.containsKey(key)) {
throw new ObjectAlreadyExistsException(
"Repository already contains type with key: " + key);
}
if (getLogger().isDebugEnabled())
getLogger().debug("registerObjectType(" + objectType + ')');
else
getLogger().info("Registered object type: " + key);
_objectTypes.put(key, objectType);
}
protected void createEntry(AbstractMetaData metaData)
throws RepositoryException {
createEntry(metaData.getId(), metaData, null);
}
protected synchronized Entry createEntry(String key,
AbstractMetaData metaData, Object instance) throws RepositoryException {
if (key == null) {
throw new RepositoryException("null key for createEntry(null, " +
metaData + ", " + instance + ')');
}
if (_entries.containsKey(key)) {
throw new ObjectAlreadyExistsException(
"Repository already contains key: " + key);
}
if (metaData == null && instance == null) {
throw new IllegalArgumentException(
"metaData and instance cannot both be null");
}
Entry entry = new Entry(metaData, instance);
if (getLogger().isDebugEnabled()) {
if (metaData != null) {
getLogger().debug("createEntry(" + metaData + ", " + instance +
')');
} else {
getLogger().debug("createEntry(" + key + "->" + instance +
')');
}
} else {
getLogger().info("Created entry: " + key);
}
_entries.put(key, entry);
_metaData = null;
_modified = true;
return entry;
}
protected synchronized void deleteEntry(String key)
throws RepositoryException {
if (!_entries.containsKey(key))
throw new ObjectNotFoundException(NOT_FOUND_MSG + key);
if (getLogger().isDebugEnabled()) {
getLogger().debug("deleteEntry(" + key + + ')');
}
_entries.remove(key);
_metaData = null;
_modified = true;
}
protected Entry updateEntry(String key, AbstractMetaData metaData)
throws RepositoryException {
return updateEntry(key, metaData, null);
}
protected synchronized Entry updateEntry(String key,
AbstractMetaData metaData, Object implementation)
throws RepositoryException {
if (!_entries.containsKey(key)) {
throw new ObjectNotFoundException(
NOT_FOUND_MSG + key);
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("updateEntry(" + key + ", " + metaData + ", " +
implementation + ')');
}
Entry entry = new Entry(metaData, implementation);
_entries.put(key, entry);
_metaData = null;
_modified = true;
return entry;
}
protected AbstractMetaData[] findObjectTypes() {
Collection coll = _objectTypes.values();
AbstractMetaData[] array = (AbstractMetaData[])Array.newInstance(
_metaDataClass, coll.size());
return (AbstractMetaData[])coll.toArray(array);
}
protected AbstractMetaData findObjectType(String className)
throws RepositoryException {
AbstractMetaData objType = (AbstractMetaData)_objectTypes.get(
className);
if (objType == null)
throw new ObjectNotFoundException(className);
return objType;
}
protected Entry[] findEntries() {
Collection entries = _entries.values();
return (Entry[])entries.toArray(new Entry[entries.size()]);
}
protected synchronized AbstractMetaData[] findMetaData() {
// Object[] findMetaData can return null elements if implementation
// objects were added directly (without using meta data), so we must
// build this list dynamically.
AbstractMetaData[] metaData = _metaData;
if (metaData == null) {
List list = new ArrayList(_entries.size());
for (Iterator iter = _entries.values().iterator(); iter.hasNext();)
{
Entry entry = (Entry)iter.next();
if (entry._metadata != null)
list.add(entry._metadata);
}
metaData = (AbstractMetaData[])Array.newInstance(_metaDataClass,
list.size());
_metaData = (AbstractMetaData[])list.toArray(metaData);
}
return metaData;
}
protected Entry findEntry(String key, boolean throwException)
throws RepositoryException {
Entry entry = (Entry)_entries.get(key);
if (entry == null && throwException) {
throw new ObjectNotFoundException(
NOT_FOUND_MSG + key);
}
return entry;
}
protected AbstractMetaData findMetaData(String key, boolean throwException)
throws RepositoryException {
// Object findMetaData can return null if implementation
// objects were added directly (without using meta data)
Entry entry = findEntry(key, throwException);
return entry == null ? null : entry.getMetaData();
}
protected Object findInstance(String key, boolean throwException)
throws RepositoryException {
Entry entry = findEntry(key, throwException);
return entry == null
? null : entry.getInstance(_svcMgr.getResourceRepository());
}
protected abstract Log getLogger();
public final ServiceManager getServiceManager() {
return _svcMgr;
}
}
|