1 package org.jbpm.db.hibernate; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Properties ; 6 7 import org.jbpm.context.exe.Converter; 8 import org.jbpm.instantiation.ClassLoaderUtil; 9 10 13 public class Converters { 14 15 static HashMap convertersByClassNames = null; 17 18 static HashMap convertersByDatabaseId = null; 20 21 static HashMap convertersIds = null; 23 24 public static Converter getConverterByClassName(String className) { 25 initConvertionMaps(); 26 Converter converter = (Converter) convertersByClassNames.get(className); 27 if (converter==null) { 28 throw new RuntimeException ("converter '"+className+"' is not declared in jbpm.converter.properties"); 29 } 30 return converter; 31 } 32 33 public static Converter getConverterByDatabaseId(String converterDatabaseId) { 34 initConvertionMaps(); 35 return (Converter) convertersByDatabaseId.get(converterDatabaseId); 36 } 37 38 public static String getConverterId(Converter converter) { 39 initConvertionMaps(); 40 return (String ) convertersIds.get(converter); 41 } 42 43 static void initConvertionMaps() { 44 if (convertersByClassNames==null) { 45 Properties converterProperties = ClassLoaderUtil.getProperties("jbpm.converter.properties", "org/jbpm/db/hibernate"); 46 47 convertersByClassNames = new HashMap (); 48 convertersByDatabaseId = new HashMap (); 49 convertersIds = new HashMap (); 50 51 Iterator iter = converterProperties.keySet().iterator(); 52 while (iter.hasNext()) { 53 String converterDatabaseId = (String ) iter.next(); 54 if (converterDatabaseId.length()!=1) throw new RuntimeException ("converter-ids must be of length 1 (to be stored in a char)"); 55 if (convertersByDatabaseId.containsKey(converterDatabaseId)) throw new RuntimeException ("duplicate converter id : '"+converterDatabaseId+"'"); 56 57 String converterClassName = converterProperties.getProperty(converterDatabaseId); 58 try { 59 Class converterClass = ClassLoaderUtil.loadClass(converterClassName); 60 Converter converter = (Converter) converterClass.newInstance(); 61 convertersByClassNames.put(converterClassName, converter); 62 convertersByDatabaseId.put(converterDatabaseId, converter); 63 convertersIds.put(converter, converterDatabaseId); 64 } catch (Exception e) { 65 throw new RuntimeException ("couldn't instantiate converter '"+converterClassName+"'"); 66 } 67 } 68 } 69 } 70 } 71 | Popular Tags |