001: package org.obe.event;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.obe.client.api.repository.RepositoryException;
006: import org.obe.spi.event.ApplicationEvent;
007: import org.obe.spi.service.ServiceManager;
008: import org.obe.util.CommonConfig;
009:
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015: import java.util.Properties;
016:
017: /**
018: * Abstract base class for content handlers.
019: *
020: * @author Adrian Price
021: */
022: public abstract class AbstractContentHandler implements ContentHandler {
023: private static final Log _logger = LogFactory
024: .getLog(AbstractContentHandler.class);
025: private static Map _classForMimeType;
026: protected ServiceManager _svcMgr;
027:
028: protected AbstractContentHandler() {
029: }
030:
031: protected final void init(ServiceManager svcMgr) {
032: _svcMgr = svcMgr;
033: if (_classForMimeType == null) {
034: // Load the MIME type mappings.
035: Properties props = new Properties();
036: InputStream in = CommonConfig
037: .openInputStream("MimeType2Class.properties");
038: if (in != null) {
039: try {
040: props.load(in);
041: } catch (IOException e) {
042: _logger
043: .error(
044: "Unable to load MimeType2Class mappings",
045: e);
046: return;
047: } finally {
048: try {
049: in.close();
050: } catch (IOException e) {
051: // Ignore close exceptions.
052: }
053: }
054: Map map = new HashMap();
055: for (Iterator iter = props.entrySet().iterator(); iter
056: .hasNext();) {
057:
058: Map.Entry entry = (Map.Entry) iter.next();
059: String mimeType = (String) entry.getKey();
060: String className = (String) entry.getValue();
061: try {
062: Class clazz = Class.forName(className);
063: map.put(mimeType, clazz);
064: } catch (ClassNotFoundException e) {
065: _logger.error("Unable to load class '"
066: + className + "' for MIME type '"
067: + mimeType + '\'');
068: }
069: }
070: _classForMimeType = map;
071: }
072: }
073: }
074:
075: /**
076: * Returns the schema for an object's class and content.
077: *
078: * @param data The object to classify.
079: * @param contentType The content type as determined by a prior call to a
080: * {@link #getContentType(Object)}.
081: * @return The schema name.
082: */
083: protected abstract String getSchema(Object data, String contentType);
084:
085: public ApplicationEvent[] process(Object data, Map attrs,
086: String contentType) throws RepositoryException {
087:
088: // Convert data to the required class, if required.
089: data = convert(data, contentType);
090:
091: // Wrap the data in an application event.
092: return new ApplicationEvent[] { new ApplicationEvent(data,
093: contentType, getSchema(data, contentType), attrs) };
094: }
095:
096: /**
097: * Converts raw data to the class registered for the specified content type.
098: *
099: * @param data Raw event data.
100: * @param contentType The content type as determined by a prior call to a
101: * {@link #getContentType(Object)}.
102: * @return The converted event data, or the original object if no class is
103: * registered for the content type.
104: */
105: protected Object convert(Object data, String contentType) {
106: Class targetClass = (Class) _classForMimeType.get(contentType);
107: if (targetClass == null) {
108: int n = contentType.indexOf('/');
109: if (n != -1) {
110: targetClass = (Class) _classForMimeType.get(contentType
111: .substring(0, n));
112: }
113: }
114: if (targetClass != null)
115: data = _svcMgr.getDataConverter().convertValue(data,
116: targetClass);
117: return data;
118: }
119: }
|