001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/query/executor/QueryExecutorDelegateFactory.java,v 1.1.1.1 2004/03/25 12:08:37 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:37 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruņa
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruņa nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040: package es.udc.mypersonalizer.kernel.model.query.executor;
041:
042: import es.udc.mypersonalizer.kernel.config.DatabaseConfig;
043: import es.udc.mypersonalizer.kernel.config.KernelConfigManager;
044: import es.udc.mypersonalizer.kernel.config.PersistenceTypeConfig;
045: import es.udc.mypersonalizer.kernel.log.Log;
046: import es.udc.mypersonalizer.kernel.log.LogManager;
047: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
048: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
049:
050: import java.util.Collection;
051: import java.util.HashMap;
052: import java.util.Iterator;
053: import java.util.Map;
054:
055: /**
056: * Factory for query executors.
057: * <p>
058: * This factory will instatiate query executors for each persistente type
059: * configured.
060: * <p>
061: * Please note that specifying a query executor delegate for a persitence
062: * method is <em>optional</em>, so it is possible to have a functional
063: * persistence type which can't be queried.
064: *
065: * @author Abel Muinho
066: * @since 1.0
067: */
068: public class QueryExecutorDelegateFactory {
069: private final static Log log = LogManager
070: .getLog(LogNamingConventions.MYPERSONALIZER);
071:
072: /** The map of <code>QueryExecutorDelegate</code>s indexed by persistence
073: * type name (<code>String</code>).
074: */
075: private static Map delegates = null;
076:
077: /**
078: * Obtains a new <code>QueryExecutorDelegate</code> for the given
079: * persistence type.
080: * @param name the name of the persistence type.
081: * @return the <code>QueryExecutorDelegate</code>.
082: * @throws InternalErrorException if the <code>QueryExecutorDelegate</code>
083: * for the given persistente type does not exist or an instance
084: * can't be created.
085: */
086: public static QueryExecutorDelegate getExecutorForPersistenceType(
087: String name) throws InternalErrorException {
088: if (delegates == null) {
089: delegates = initializeDelegates();
090: }
091:
092: Class qedClass = (Class) delegates.get(name);
093: if (qedClass == null) {
094: throw new InternalErrorException(
095: "QueryExecutorDelegate for persistence type "
096: + name + " is not propperly configured.");
097: }
098: try {
099: return (QueryExecutorDelegate) qedClass.newInstance();
100: } catch (Exception e) {
101: throw new InternalErrorException(
102: "Error creating QueryExecutorDelgate instance for class "
103: + qedClass.getName());
104: }
105: }
106:
107: /**
108: * Initializes the map of <code>QueryExecutorDelegate</code>s from the
109: * configuration.
110: * <p>
111: * This method is fault-tolerant, and when the class specified in the
112: * configuration can't be created, it will continue porcessing other
113: * entries (as if the configuration option hadn't been provided). A message
114: * will be sent to the log.
115: * @return the initialized map.
116: * @throws InternalErrorException if errors are found while accessing the
117: * configuration.
118: */
119: private static synchronized Map initializeDelegates()
120: throws InternalErrorException {
121: DatabaseConfig databaseConfig = KernelConfigManager.getConfig()
122: .getKernelModelConfig().getDatabaseConfig();
123: Collection persistenceTypes = databaseConfig
124: .getPersistenceTypes();
125: log.write("Creating map of configured QueryDelegates", null,
126: QueryExecutorDelegate.class);
127: int configurationEntries = 0;
128: HashMap result = new HashMap();
129: Iterator it = persistenceTypes.iterator();
130: while (it.hasNext()) {
131: String persistenceTypeName = (String) it.next();
132: PersistenceTypeConfig config = databaseConfig
133: .getPersistenceTypesConfig(persistenceTypeName);
134: String className = config
135: .getQueryExecutorDelegateClassName();
136: if (className != null) {
137: try {
138: configurationEntries++;
139: Class clazz = Class.forName(className);
140: result.put(config.getId(), clazz);
141: } catch (Exception e) {
142: log.write("Can't create Class: " + className, e,
143: QueryExecutorDelegateFactory.class);
144: }
145: }
146: }
147: log.write("Added " + result.size() + " of "
148: + configurationEntries
149: + " QueryExecutorDelegate Class instances.", null,
150: QueryExecutorDelegateFactory.class);
151: return result;
152: }
153:
154: /**
155: * Package visible method to allow testing of the class without
156: * having to set up any special configuration.
157: * @param delegates the map of QueryExecutorDelegate classes indexed by
158: * persistence type name.
159: */
160: static void initializeDelegates(Map delegates) {
161: QueryExecutorDelegateFactory.delegates = delegates;
162: }
163: }
|