001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.quaternary.io;
022
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * Factory to create BioUnitDataProvider instances.
028 *
029 * Unlike many other BioJava Factory classes, this class does not store
030 * singletons, but creates a new instance for every call of
031 * {@link #getBioUnitDataProvider()}.
032 */
033public class BioUnitDataProviderFactory {
034
035        private static final Logger logger = LoggerFactory.getLogger(BioUnitDataProviderFactory.class);
036
037        public static final String mmcifProviderClassName     = MmCifBiolAssemblyProvider.class.getName();
038
039        public static final String remoteProviderClassName    = RemoteBioUnitDataProvider.class.getName();
040
041        public static final String pdbProviderClassName       = PDBBioUnitDataProvider.class.getName();
042
043        public static Class<? extends BioUnitDataProvider> DEFAULT_PROVIDER_CLASS = MmCifBiolAssemblyProvider.class;
044        public static final String DEFAULT_PROVIDER_CLASSNAME =  DEFAULT_PROVIDER_CLASS.getName();
045
046        private static Class<? extends BioUnitDataProvider> providerClass = DEFAULT_PROVIDER_CLASS;
047
048        private BioUnitDataProviderFactory(){
049
050        }
051
052        /**
053         *
054         * @return A new instance of the current BioUnitDataProvider class
055         */
056        public static BioUnitDataProvider getBioUnitDataProvider() {
057
058                // use reflection to return a new instance...
059
060                try {
061                        return providerClass.newInstance();
062                } catch (IllegalAccessException e) {
063                        logger.error("Exception caught",e);
064                } catch (InstantiationException e) {
065                        logger.error("Exception caught",e);
066                }
067
068                return null;
069
070        }
071
072        /**
073         * Set the type of provider to be created
074         * @param klass A BioUnitDataProvider
075         */
076        public static void setBioUnitDataProvider(Class<? extends BioUnitDataProvider> klass) {
077                providerClass = klass;
078        }
079        /**
080         * Sets the data provider to the specified class name. Use {@link #setBioUnitDataProvider(Class)}
081         * for better type safety.
082         * @param className A class implementing BioUnitDataProvider
083         * @throws ClassNotFoundException If the class cannot be loaded
084         * @throws ClassCastException If the class does not extend BioUnitDataProvider
085         */
086        public static void setBioUnitDataProvider(String className) throws ClassNotFoundException, ClassCastException {
087                Class<?> cls = Class.forName(className);
088                Class<BioUnitDataProvider> interfaceClass = BioUnitDataProvider.class;
089                Class<? extends BioUnitDataProvider> castClass = cls.asSubclass(interfaceClass);
090
091                setBioUnitDataProvider(castClass);
092        }
093
094        /**
095         * Get the class of providers to be instantiated.
096         * @return
097         */
098        public static Class<? extends BioUnitDataProvider> getBioUnitDataProviderClass() {
099                return providerClass;
100        }
101
102
103}