001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.db.hibernate;
023:
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.jbpm.JbpmConfiguration;
032: import org.jbpm.JbpmException;
033: import org.jbpm.configuration.ObjectFactory;
034: import org.jbpm.context.exe.Converter;
035: import org.jbpm.util.ClassLoaderUtil;
036:
037: /**
038: * provides access to the list of converters and ensures that the converter objects are unique.
039: */
040: public abstract class Converters {
041:
042: static final int CONVERTERS_BY_CLASS_NAMES = 0;
043: static final int CONVERTERS_BY_DATABASE_ID = 1;
044: static final int CONVERTERS_IDS = 2;
045:
046: static Map converterMapsMap = new HashMap();
047:
048: // public methods
049:
050: public static Converter getConverterByClassName(String className) {
051: Converter converter = (Converter) getConvertersByClassNames()
052: .get(className);
053: if (converter == null) {
054: throw new JbpmException("converter '" + className
055: + "' is not declared in jbpm.converter.properties");
056: }
057: return converter;
058: }
059:
060: public static Converter getConverterByDatabaseId(
061: String converterDatabaseId) {
062: return (Converter) getConvertersByDatabaseId().get(
063: converterDatabaseId);
064: }
065:
066: public static String getConverterId(Converter converter) {
067: return (String) getConvertersIds().get(converter);
068: }
069:
070: // maps class names to unique converter objects
071: static Map getConvertersByClassNames() {
072: return getConverterMaps()[CONVERTERS_BY_CLASS_NAMES];
073: }
074:
075: // maps converter database-id-strings to unique converter objects
076: static Map getConvertersByDatabaseId() {
077: return getConverterMaps()[CONVERTERS_BY_DATABASE_ID];
078: }
079:
080: // maps unique converter objects to their database-id-string
081: static Map getConvertersIds() {
082: return getConverterMaps()[CONVERTERS_IDS];
083: }
084:
085: static Map[] getConverterMaps() {
086: Map[] converterMaps = null;
087: synchronized (converterMapsMap) {
088: ObjectFactory objectFactory = JbpmConfiguration.Configs
089: .getObjectFactory();
090: converterMaps = (Map[]) converterMapsMap.get(objectFactory);
091: if (converterMaps == null) {
092: converterMaps = createConverterMaps(objectFactory);
093: converterMapsMap.put(objectFactory, converterMaps);
094: }
095: }
096: return converterMaps;
097: }
098:
099: static Map[] createConverterMaps(ObjectFactory objectFactory) {
100: Map[] converterMaps = new Map[3];
101: converterMaps[CONVERTERS_BY_CLASS_NAMES] = new HashMap();
102: converterMaps[CONVERTERS_BY_DATABASE_ID] = new HashMap();
103: converterMaps[CONVERTERS_IDS] = new HashMap();
104:
105: Map convertersByClassNames = converterMaps[CONVERTERS_BY_CLASS_NAMES];
106: Map convertersByDatabaseId = converterMaps[CONVERTERS_BY_DATABASE_ID];
107: Map convertersIds = converterMaps[CONVERTERS_IDS];
108:
109: Properties converterProperties = null;
110: if (objectFactory.hasObject("resource.converter")) {
111: String resource = (String) objectFactory
112: .createObject("resource.converter");
113: converterProperties = ClassLoaderUtil
114: .getProperties(resource);
115: } else {
116: converterProperties = new Properties();
117: }
118:
119: Iterator iter = converterProperties.keySet().iterator();
120: while (iter.hasNext()) {
121: String converterDatabaseId = (String) iter.next();
122: if (converterDatabaseId.length() != 1)
123: throw new JbpmException(
124: "converter-ids must be of length 1 (to be stored in a char)");
125: if (convertersByDatabaseId.containsKey(converterDatabaseId))
126: throw new JbpmException("duplicate converter id : '"
127: + converterDatabaseId + "'");
128: String converterClassName = converterProperties
129: .getProperty(converterDatabaseId);
130: try {
131: Class converterClass = ClassLoaderUtil
132: .loadClass(converterClassName);
133: Converter converter = (Converter) converterClass
134: .newInstance();
135: log.debug("adding converter '" + converterDatabaseId
136: + "', '" + converterClassName + "'");
137: convertersByClassNames.put(converterClassName,
138: converter);
139: convertersByDatabaseId.put(converterDatabaseId,
140: converter);
141: convertersIds.put(converter, converterDatabaseId);
142: } catch (Exception e) {
143: // NOTE that Error's are not caught because that might halt the JVM and mask the original Error.
144: log.debug("couldn't instantiate converter '"
145: + converterClassName + "': " + e);
146: }
147: }
148:
149: return converterMaps;
150: }
151:
152: private static Log log = LogFactory.getLog(Converters.class);
153: }
|