View Javadoc
1 package org.jadetower.dao; 2 3 /* 4 Copyright (c) 2003, J Aaron Farr 5 All rights reserved. 6 7 This software is published under the terms of the JadeTower Software License, 8 a BSD derived license, a copy of which has been included with this 9 distribution in the LICENSE file. <http://www.jadetower.org> 10 */ 11 12 13 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.container.ContainerUtil; 16 import org.apache.avalon.framework.configuration.DefaultConfiguration; 17 import org.apache.avalon.framework.logger.Logger; 18 import org.apache.avalon.framework.configuration.ConfigurationException; 19 import org.apache.excalibur.configuration.merged.ConfigurationMerger; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import java.util.HashMap; 25 import java.util.Map; 26 import java.util.Collections; 27 import java.lang.reflect.Constructor; 28 29 import org.jadetower.resolver.Resolver; 30 import org.jadetower.resolver.impl.DefaultResolver; 31 import org.jadetower.dao.impl.CommonsLogger; 32 33 /*** 34 * a simple DAO Container. 35 */ 36 public class DaoContainer { 37 38 39 private boolean contextualized = false; 40 private Map m_context = null; 41 42 private Configuration m_configuration = null; 43 private HashMap m_managers = new HashMap(); 44 private String m_default = null; 45 private Logger m_logger = null; 46 47 /*** 48 * creates a basic DaoContainer 49 * @param configUrls a array of configuration file URLs (will merge files) 50 * @param context a default root context [can be null] 51 * @param logger an Jakarta Commons logger [can be null] 52 * @throws java.lang.Exception 53 */ 54 public DaoContainer(final String[] configUrls, Map context, Log log) throws Exception { 55 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 56 Configuration conf = builder.build(configUrls[0]); 57 for(int i=1; i < configUrls.length; i++){ 58 Configuration c = builder.build(configUrls[1]); 59 conf = ConfigurationMerger.merge(conf,c); 60 } 61 if( context != null) 62 m_context = Collections.synchronizedMap(context); 63 else 64 m_context = Collections.synchronizedMap(new HashMap()); 65 m_configuration = conf; 66 initLogger(log); 67 initialize(); 68 } 69 70 /*** 71 * get the default DaoManager 72 * @return the default DAO Manager 73 * @throws java.lang.Exception 74 */ 75 public DaoManager getManager() throws Exception { 76 return getManager(m_default); 77 } 78 79 /*** 80 * get a specific named DaoManager 81 * @param context the DaoManager ID or context name 82 * @return the named DaoManager 83 * @throws java.lang.Exception 84 */ 85 public DaoManager getManager(String context) throws Exception { 86 DaoManager manager = null; 87 if(m_managers.containsKey(context)){ 88 manager = (DaoManager) m_managers.get(context); 89 } 90 return manager; 91 } 92 93 protected void initialize() { 94 95 Configuration rez = new DefaultConfiguration("resolver"); 96 Configuration[] resolvers = m_configuration.getChildren("resolvers"); 97 for(int i=0; i < resolvers.length; i++){ 98 try { 99 Configuration r = resolvers[i]; 100 rez = ConfigurationMerger.merge(rez, r); 101 } 102 catch (ConfigurationException ex) { 103 m_logger.error("Error merging resolver configurtions",ex); 104 } 105 } 106 Resolver resolver = createResolver(rez); 107 Configuration[] daoConfs = m_configuration.getChildren("manager"); 108 109 HashMap configurations = new HashMap(); 110 111 for(int i=0; i < daoConfs.length; i++){ 112 try { 113 Configuration conf = daoConfs[i]; 114 String name = conf.getAttribute("id", "default"); 115 if(m_default == null) 116 m_default = name; 117 configurations.put(name, conf); 118 boolean isDefault = conf.getAttributeAsBoolean("default", false); 119 if(isDefault) 120 m_default = name; 121 String clazz = conf.getAttribute("impl", 122 "org.jadetower.dao.impl.DefaultDaoManager"); 123 124 String ext = conf.getAttribute("extends", null); 125 if (ext != null && configurations.containsKey(ext)) { 126 Configuration parent = (Configuration) configurations.get(ext); 127 conf = ConfigurationMerger.merge(conf, parent); 128 } 129 130 Class[] param = new Class[] { 131 Resolver.class, Logger.class, Configuration.class, Map.class}; 132 133 Object[] params = new Object[] { 134 resolver, m_logger, conf, m_context}; 135 136 Constructor c = Class.forName(clazz).getConstructor(param); 137 DaoManager manager = (DaoManager) c.newInstance(params); 138 m_managers.put(name, manager); 139 } 140 catch (Exception ex) { 141 m_logger.error("error creating dao manager",ex); 142 } 143 } 144 } 145 146 protected Resolver createResolver(Configuration config){ 147 DefaultResolver resolver = new DefaultResolver(); 148 resolver.configure(config); 149 return resolver; 150 } 151 152 protected void initLogger(Log log){ 153 if(log == null) 154 m_logger = new CommonsLogger(LogFactory.getLog("dao")); 155 else 156 m_logger = new CommonsLogger(log); 157 } 158 159 }

This page was automatically generated by Maven