001    package org.apache.ojb.broker.core;
002    
003    /* Copyright 2003-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.OJBRuntimeException;
019    import org.apache.ojb.broker.util.ClassHelper;
020    import org.apache.ojb.broker.util.configuration.Configuration;
021    import org.apache.ojb.broker.util.configuration.Configurator;
022    import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
023    import org.apache.ojb.broker.util.logging.Logger;
024    import org.apache.ojb.broker.util.logging.LoggerFactory;
025    
026    /**
027     *
028     * @author Thomas Mahler
029     * @version $Id: PersistenceBrokerFactoryFactory.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
030     */
031    public class PersistenceBrokerFactoryFactory
032    {
033        private static Logger log = LoggerFactory.getBootLogger();
034    
035        private static final String PBF_KEY = "PersistenceBrokerFactoryClass";
036        private static PersistenceBrokerFactoryIF singleton = init();
037    
038        /**
039         * Returns an {@link PersistenceBrokerFactoryIF} instance.
040         */
041        public static PersistenceBrokerFactoryIF instance()
042        {
043            return singleton;
044        }
045    
046        private static PersistenceBrokerFactoryIF init()
047        {
048            if (log.isDebugEnabled()) log.debug("Instantiate PersistenceBrokerFactory");
049            Class pbfClass = null;
050            try
051            {
052                Configurator configurator = OjbConfigurator.getInstance();
053                Configuration config = configurator.getConfigurationFor(null);
054                pbfClass = config.getClass(PBF_KEY, null);
055                if(pbfClass == null)
056                {
057                    log.error("Creation of PersistenceBrokerFactory (PBF) instance failed, can't get PBF class object");
058                    throw new OJBRuntimeException("Property for key '" + PBF_KEY + "' can not be found in properties file");
059                }
060                PersistenceBrokerFactoryIF result = (PersistenceBrokerFactoryIF) ClassHelper.newInstance(pbfClass);
061                configurator.configure(result);
062                log.info("PersistencebrokerFactory class instantiated: " + result);
063                return result;
064            }
065            catch (Exception e)
066            {
067                if(e instanceof OJBRuntimeException)
068                {
069                    throw (OJBRuntimeException) e;
070                }
071                else
072                {
073                    throw new OJBRuntimeException("Error while instantiation of PersistenceBrokerFactory class", e);
074                }
075            }
076        }
077    }