KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > impl > HenHouse


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of GOSSIP nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.impl;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
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 /**
44  *
45  *
46  * @author Bruce Lowery
47  */

48 final public class HenHouse
49 {
50     private static final Log LOG = LogFactory.getLog(HenHouse.class);
51     
52     private final Map JavaDoc _info = new HashMap JavaDoc();
53     private final Map JavaDoc _eggs = new HashMap JavaDoc();
54     
55     public static HenHouse getHenHouse() { return HenHouseHolder.getHenHouse(); }
56     
57     HenHouse() {/*EMPTY*/}
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 JavaDoc eggName, final String JavaDoc handlerClass, final String JavaDoc 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 JavaDoc eggName = info.getEggName();
84                 
85                 try
86                 {
87                     Class JavaDoc 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 JavaDoc e)
99                 {
100                     throw new HatchException("Not found: " + info.getHandlerClassName(), e);
101                 }
102                 catch (InstantiationException JavaDoc e)
103                 {
104                     throw new HatchException("No default constructor: "+info.getHandlerClassName(), e);
105                 }
106                 catch (IllegalAccessException JavaDoc 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 JavaDoc 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 JavaDoc[] deps = info.getDependencies();
156             
157             for (int i=0; i<deps.length; ++i)
158             {
159                 String JavaDoc 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