View Javadoc
1 /* 2 * (C) 2002 David Carr david@carr.name 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package net.sourceforge.mflow.impl; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 import java.util.logging.Logger; 24 25 import net.sourceforge.mflow.api.ComponentManager; 26 import net.sourceforge.mflow.api.MsgFlowComponent; 27 import net.sourceforge.mflow.api.MsgFolder; 28 import net.sourceforge.mflow.api.MsgSource; 29 import net.sourceforge.mflow.api.PluginManager; 30 import net.sourceforge.mflow.api.SendFolder; 31 import net.sourceforge.mflow.reflect.ParamArgPair; 32 import net.sourceforge.mflow.reflect.ReflectionException; 33 import net.sourceforge.mflow.reflect.ReflectionUtil; 34 35 /*** 36 * A class for managing instances of MsgFlowComponent 37 * 38 * @author <a href="mailto:david@carr.name">David Carr</a> 39 * @version $Revision: 1.4 $ 40 */ 41 public class ComponentManagerImpl implements ComponentManager { 42 private List instances = new ArrayList(); 43 44 private List implementations = new ArrayList(); 45 46 private static Logger log = 47 Logger.getLogger(ComponentManagerImpl.class.getName()); 48 49 private PluginManager pluginManager; 50 51 ComponentManagerImpl(PluginManager pluginManager) { 52 log.entering("ComponentManager", "ComponentManager(Config)"); 53 this.pluginManager = pluginManager; 54 log.exiting("ComponentManager", "ComponentManager(Config"); 55 } 56 57 public synchronized void addComponentType(Class ct) 58 throws ClassCastException { 59 log.entering("ComponentManager", "addComponentType(" + ct.getName() + ")"); 60 if (!Util.implementsInterface(ct, MsgFlowComponent.class)) { 61 ClassCastException ce = 62 new ClassCastException( 63 ct.getName() + " does not implement MsgFlowComponent"); 64 log.throwing("ComponentManager", "addComponentType", ce); 65 throw ce; 66 } 67 if (!this.implementations.contains(ct)) { 68 log.info("Adding component type: " + ct.getName()); 69 this.implementations.add(ct); 70 } 71 log.exiting("ComponentManager", "addComponentType(" + ct.getName() + ")"); 72 } 73 74 public synchronized MsgFlowComponent newInstance(Class c) { 75 log.entering("ComponentManager", "newInstance(" + c.getName() + ")"); 76 if (this.implementations.contains(c)) { 77 MsgFlowComponent comp = newInstanceOf(c); 78 this.instances.add(comp); 79 log.exiting("ComponentManager", "newInstance(" + c.getName() + ")", comp); 80 return comp; 81 } else { 82 log.warning("No implementation of " + c.getName()); 83 log.exiting("ComponentManager", "newInstance(" + c.getName() + ")", null); 84 return null; 85 } 86 } 87 88 public synchronized Class[] getComponentTypes() { 89 log.entering("ComponentManager", "getComponentTypes"); 90 Class[] ret = 91 (Class[]) this.implementations.toArray( 92 new Class[this.implementations.size()]); 93 log.exiting("ComponentManager", "getComponentTypes", ret); 94 return ret; 95 } 96 97 public synchronized MsgFlowComponent[] getInstances() { 98 log.entering("ComponentManager", "getInstances"); 99 MsgFlowComponent[] ret = 100 (MsgFlowComponent[]) this.instances.toArray( 101 new MsgFlowComponent[this.instances.size()]); 102 log.exiting("ComponentManager", "getInstances", ret); 103 return ret; 104 } 105 106 public synchronized void removeInstance(MsgFlowComponent comp) { 107 log.entering("ComponentManager", "removeInstace"); 108 this.instances.remove(comp); 109 log.exiting("ComponentManager", "removeInstance"); 110 } 111 112 public synchronized MsgFolder[] getRootFolders() { 113 log.entering("ComponentManager", "getRootFolders"); 114 ArrayList roots = new ArrayList(); 115 for (int i = 0; i < this.instances.size(); i++) { 116 Object o = this.instances.get(i); 117 if (Util.implementsInterface(o.getClass(), MsgFolder.class)) { 118 MsgFolder folder = (MsgFolder) o; 119 if (folder.isRootFolder()) { 120 roots.add(folder); 121 } 122 } 123 } 124 MsgFolder[] ret = (MsgFolder[]) roots.toArray(new MsgFolder[roots.size()]); 125 log.exiting("ComponentManager", "getRootFolders", ret); 126 return ret; 127 } 128 129 public SendFolder[] getSendFolders() { 130 log.entering("ComponentManager", "getSendFolders"); 131 ArrayList sendFolders = new ArrayList(); 132 for (int i = 0; i < this.instances.size(); i++) { 133 MsgFlowComponent c = (MsgFlowComponent) this.instances.get(i); 134 if (Util.implementsInterface(c.getClass(), SendFolder.class)) { 135 sendFolders.add(c); 136 } 137 } 138 SendFolder[] ret = 139 (SendFolder[]) sendFolders.toArray(new SendFolder[sendFolders.size()]); 140 log.exiting("ComponentManager", "getSendFolders", ret); 141 return ret; 142 } 143 144 public void send() { 145 log.entering("ComponentManager", "send"); 146 MsgFlowComponent[] toSend = Algorithm.sortComponents(getInstances()); 147 for (int i = 0; i < toSend.length; i++) { 148 if (toSend[i] instanceof MsgSource) { 149 MsgSource src = (MsgSource) toSend[i]; 150 src.send(); 151 } 152 } 153 log.exiting("ComponentManager", "send"); 154 } 155 156 private MsgFlowComponent newInstanceOf(Class c) { 157 List paramArgs = new ArrayList(); 158 Class[] configArgs = { PluginManager.class }; 159 Object[] configParams = { this.pluginManager }; 160 paramArgs.add(new ParamArgPair(configArgs, configParams)); 161 paramArgs.add(new ParamArgPair()); 162 try { 163 return (MsgFlowComponent) ReflectionUtil.instantiate(c, paramArgs); 164 } catch (ReflectionException re) { 165 log.throwing(getClass().getName(), "newInstanceOf", re); 166 return null; 167 } 168 } 169 }

This page was automatically generated by Maven