1 23 24 package examples.invoice.applications; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.api.PMapper; 28 import org.objectweb.jorm.lib.MapperJCA; 29 import org.objectweb.jorm.mapper.rdb.lib.ConnectionSpecJDBC; 30 import org.objectweb.jorm.mapper.rdb.lib.MapperJDBC; 31 import org.objectweb.jorm.util.api.Loggable; 32 import org.objectweb.perseus.connector.ra.fos.FosAttributeControler; 33 import org.objectweb.perseus.connector.ra.fos.FosManagedConnectionFactory; 34 import org.objectweb.perseus.cache.api.CacheManager; 35 import org.objectweb.util.monolog.Monolog; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 import org.objectweb.util.monolog.api.Logger; 38 import org.objectweb.util.monolog.api.LoggerFactory; 39 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.Properties ; 43 44 47 public class JormInit { 48 public static final String LOGGER_FACTORY_CLASS_NAME 50 = "loggerFactoryClassName"; 51 private static JormInit singleton = new JormInit(); 52 private static CacheManager cacheManager = new TrivialCache(); 53 protected LoggerFactory loggerFactory = null; 54 protected Logger logger = null; 55 private HashMap mapperMap = new HashMap (); 57 private HashMap urlMap = new HashMap (); 58 public static final String RESOURCE_JDBC_SUBMAPPER 60 = "jdbc.submapper"; 61 public static final String RESOURCE_FOS_MCF_CLASSNAME 62 = "fos.resource.mcf.classname"; 63 public static final String DEFAULT_FOS_URL = "fos.ConnectionURL"; 64 65 68 public static JormInit getInstance() { 69 return singleton; 70 } 71 72 75 public LoggerFactory getLoggerFactory() { 76 return loggerFactory; 77 } 78 79 82 public void initLogSystem(Properties prop) throws Exception { 83 loggerFactory = Monolog.initialize(); 84 logger = loggerFactory.getLogger("examples.invoice.applications.JormInit"); 85 logger.log(BasicLevel.INFO, "Log system initialized."); 86 } 87 88 91 public void initResourceAdapter(Properties prop) throws Exception { 92 initJDBCResourceAdapter(prop); 93 initFOSResourceAdapter(prop); 94 } 95 96 private void initJDBCResourceAdapter(Properties prop) throws Exception { 97 PMapper mapper = new MapperJDBC(); 99 mapper.setConnectionFactory( 100 new ConnectionSpecJDBC( 101 prop.getProperty("ConnectionURL"), 102 prop.getProperty("DriverClassName"), 103 prop.getProperty("UserName"), 104 prop.getProperty("Password"))); 105 mapper.setMapperName("rdb." + prop.getProperty(RESOURCE_JDBC_SUBMAPPER)); 106 ((Loggable) mapper).setLoggerFactory(loggerFactory); 107 mapper.start(); 108 mapperMap.put(mapper.getMapperName(), mapper); 109 urlMap.put(mapper.getMapperName(), prop.getProperty("ConnectionURL")); 110 logger.log(BasicLevel.INFO, "Mapper " + mapper.getMapperName() 111 + " instancied and initialised."); 112 } 113 114 private void initFOSResourceAdapter(Properties prop) throws Exception { 115 PMapper mapper; 116 String fosMcfCN = prop.getProperty(RESOURCE_FOS_MCF_CLASSNAME); 118 if (fosMcfCN == null) { 119 logger.log(BasicLevel.INFO, 120 "Cannot initialize FOS RA environment: MCF=" + fosMcfCN); 121 return; 122 } 123 FosAttributeControler ra 124 = (FosAttributeControler) Class.forName(fosMcfCN).newInstance(); 125 logger.log(BasicLevel.DEBUG, "FOS Ra=" + ra); 126 ra.setConnectionURL(prop.getProperty(DEFAULT_FOS_URL)); 128 ra.setLoggerFactory(loggerFactory); 129 ra.setInitializeAtStartUp(false); 130 ra.setLoggerBaseName("examples.invoice.ra"); 131 ((FosManagedConnectionFactory) ra).start(); 132 mapper = new MapperJCA(); 133 mapper.setConnectionFactory(((FosManagedConnectionFactory) ra).createConnectionFactory()); 134 mapper.setMapperName("fos"); 135 ((Loggable) mapper).setLoggerFactory(loggerFactory); 136 mapper.start(); 137 mapperMap.put(mapper.getMapperName(), mapper); 138 urlMap.put(mapper.getMapperName(), prop.getProperty(DEFAULT_FOS_URL)); 139 logger.log(BasicLevel.INFO, "Mapper " + mapper.getMapperName() 140 + " instancied and initialised."); 141 } 142 143 public PMapper getMapper(String name) throws PException { 144 return (PMapper) mapperMap.get(name); 145 } 146 147 public Iterator registeredMapper() { 148 return mapperMap.values().iterator(); 149 } 150 151 public String getURL(PMapper m) { 152 return (String ) urlMap.get(m.getMapperName()); 153 } 154 155 public CacheManager getCacheManager() { 156 return cacheManager; 157 } 158 } 159 | Popular Tags |