001/**
002 *  '$RCSfile$'
003 *  '$Author: welker $'
004 *  '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $'
005 *  '$Revision: 24234 $'
006 *
007 *  For Details:
008 *  http://www.kepler-project.org
009 *
010 *  Copyright (c) 2010 The Regents of the
011 *  University of California. All rights reserved. Permission is hereby granted,
012 *  without written agreement and without license or royalty fees, to use, copy,
013 *  modify, and distribute this software and its documentation for any purpose,
014 *  provided that the above copyright notice and the following two paragraphs
015 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
016 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
017 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
018 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
019 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
020 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
022 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 *  ENHANCEMENTS, OR MODIFICATIONS.
025 */
026
027package org.kepler.kar;
028
029import java.lang.reflect.Constructor;
030import java.util.List;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.kepler.configuration.ConfigurationManager;
035import org.kepler.configuration.ConfigurationProperty;
036import org.kepler.objectmanager.cache.CacheManager;
037
038import ptolemy.kernel.util.Attribute;
039import ptolemy.kernel.util.IllegalActionException;
040import ptolemy.kernel.util.NameDuplicationException;
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.util.MessageHandler;
043
044public class KAREntryHandlerFactory extends Attribute {
045
046        private static final Log log = LogFactory
047                        .getLog(KAREntryHandlerFactory.class.getName());
048        private static final boolean isDebugging = log.isDebugEnabled();
049
050        /**
051         * Constructor
052         * 
053         * @param container
054         * @param name
055         * @throws IllegalActionException
056         * @throws NameDuplicationException
057         */
058        public KAREntryHandlerFactory(NamedObj container, String name)
059                        throws IllegalActionException, NameDuplicationException {
060                super(container, name);
061                if (isDebugging)
062                        log.debug("Construct " + this.getClassName());
063        }
064
065        // /////////////////////////////////////////////////////////////////
066        // // public methods ////
067
068        /**
069         * Get all the KAREntryHandlers that are specified in the configuration and
070         * add them to the CacheManager.
071         */
072        public boolean registerKAREntryHandlers() {
073                if (isDebugging)
074                        log.debug("registerKAREntryHandlers()");
075                boolean success = false;
076                try {
077                        // Iterator<KAREntryHandlerFactory> factories = attributeList(
078                        // KAREntryHandlerFactory.class).iterator();
079                        CacheManager c = CacheManager.getInstance();
080
081                        ConfigurationProperty coreProp = ConfigurationManager.getInstance()
082                                        .getProperty(ConfigurationManager.getModule("core"));
083                        ConfigurationProperty kehProp = coreProp
084                                        .getProperty("karEntryHandlerFactory");
085
086                        List<ConfigurationProperty> handlers = kehProp.getProperties("karHandler");
087                        for (ConfigurationProperty handler : handlers) {
088                                String classname = handler.getProperty("class").getValue();
089                                String handlername = handler.getProperty("name").getValue();
090                                if(this.getAttribute(handlername) != null)
091                                { //this handler has already been added
092                                  return true;
093                                }
094                                Class factoryClass = Class.forName(classname);
095                                Class[] args = new Class[] { NamedObj.class, String.class };
096                                Constructor constructor = factoryClass.getConstructor(args);
097                                Object[] argsImpl = new Object[] { this, handlername };
098                                KAREntryHandlerFactory kef = (KAREntryHandlerFactory) constructor
099                                                .newInstance(argsImpl);
100
101                                // KAREntryHandlerFactory factory = factories.next();
102                                if (isDebugging)
103                                        log.debug(kef.toString());
104                                KAREntryHandler keh = kef.createKAREntryHandler();
105                                keh.initialize();
106                                c.addKAREntryHandler(keh);
107                        }
108                        success = true;
109                } 
110                catch (Exception e) 
111                {
112                        try {
113                                MessageHandler.warning("Could not create KAREntryHandler.", e);
114                        } catch (Exception ce) {
115                                // Do nothing
116                        }
117                        success = false;
118                }
119                return success;
120        }
121
122        /**
123         * Always returns null, this method should be overridden by the factory
124         * instance that extends this factory.
125         * 
126         * */
127        public KAREntryHandler createKAREntryHandler() {
128                return null;
129        }
130}