1 29 package jegg.impl; 30 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import jegg.Egg; 36 import jegg.Port; 37 import jegg.UnableToInitializeException; 38 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 43 48 final public class HenHouse 49 { 50 private static final Log LOG = LogFactory.getLog(HenHouse.class); 51 52 private final Map _info = new HashMap (); 53 private final Map _eggs = new HashMap (); 54 55 public static HenHouse getHenHouse() { return HenHouseHolder.getHenHouse(); } 56 57 HenHouse() {} 58 59 void add(final EggInfo info) 60 { 61 _info.put(info.getEggName(), info); 62 _eggs.put(info.getEggName(), new PortImpl(info)); 63 } 64 65 EggInfo hatch (final String eggName, final String handlerClass, final String dispatcherName, final boolean startEgg, boolean startDispatcher) throws HatchException 66 { 67 EggInfo info = new EggInfo(eggName, handlerClass, dispatcherName, startEgg); 68 add(info); 69 hatch(info, startEgg, startDispatcher); 70 return info; 71 72 } 73 74 void hatch(final EggInfo info, final boolean startEgg, final boolean startDispatcher) throws HatchException 75 { 76 if (LOG.isDebugEnabled()) 77 LOG.debug("hatch("+info+", "+startEgg+")"); 78 79 synchronized (info) 80 { 81 if (null == info.getEgg()) 82 { 83 String eggName = info.getEggName(); 84 85 try 86 { 87 Class claz = Class.forName(info.getHandlerClassName()); 88 if (!Egg.class.isAssignableFrom(claz)) 89 throw new HatchException(claz.getName()+" does not implement "+Egg.class.getName()); 90 Egg h = (Egg) claz.newInstance(); 91 EggShell egg = new EggShell(eggName,h); 92 egg.setProperties(info.getProperties()); 93 PortImpl eggPort = (PortImpl) _eggs.get(eggName); 94 egg.setPort(eggPort); 95 eggPort.setOwner(egg); 96 info.setEgg(egg); 97 } 98 catch (ClassNotFoundException e) 99 { 100 throw new HatchException("Not found: " + info.getHandlerClassName(), e); 101 } 102 catch (InstantiationException e) 103 { 104 throw new HatchException("No default constructor: "+info.getHandlerClassName(), e); 105 } 106 catch (IllegalAccessException e) 107 { 108 throw new HatchException("IllegalAccessException: "+e.getMessage(), e); 109 } 110 } 111 112 if (startEgg) 113 { 114 if (!info.isResolved()) 115 resolveDependencies(info, startDispatcher); 116 117 EggShell e = info.getEgg(); 118 Egg h = e.getHandler(); 119 120 try 121 { 122 h.init(); 123 } 124 catch (UnableToInitializeException e1) 125 { 126 throw new HatchException("Can't initialize egg: "+e.getId(), e1); 127 } 128 129 Dispatcher d = (null == info.getDispatcherName()) 130 ? Dispatcher.getDefaultScheduler() 131 : Dispatcher.getDispatcher(info.getDispatcherName(), startDispatcher); 132 133 e.assignTo(d); 134 } 135 } 136 } 137 138 Iterator iterator() 139 { 140 return _info.values().iterator(); 141 } 142 143 void resolveDependencies(final EggInfo info, final boolean startDispatcher) throws HatchException 144 { 145 if (LOG.isDebugEnabled()) 146 LOG.debug("resolve("+info.getEggName()+")"); 147 148 synchronized (info) 149 { 150 if (info.isResolved()) 151 return; 152 153 info.setResolved(true); 154 EggShell e = info.getEgg(); 155 String [] deps = info.getDependencies(); 156 157 for (int i=0; i<deps.length; ++i) 158 { 159 String depName = deps[i]; 160 Port depPort = (Port) _eggs.get(depName); 161 e.dispatch(new MessageImpl(depPort,null,Priority.HIGH)); 162 EggInfo depInfo = (EggInfo) _info.get(depName); 163 hatch(depInfo,true,startDispatcher); 164 } 165 } 166 } 167 } 168 | Popular Tags |