KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > componentassembly > ccm > deployer > base > HandlerBase


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Briclet Frédéric.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.openccm.descriptor.componentassembly.ccm.deployer.base;
27
28 // Local Interface Import Statements
29
import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import org.objectweb.apollon.framework.Bean;
33 import org.objectweb.apollon.framework.Extension;
34 import org.objectweb.openccm.descriptor.componentassembly.ccm.deployer.managers.*;
35
36 /**
37  * The HandlerBase provides all the basic feature of an handler.
38  *
39  * @author <a HREF="mailto:briclet@lifl.fr">Frederic Briclet</a>
40  *
41  * @version 0.1
42  */

43 public abstract class HandlerBase
44        extends Extension {
45     // ==================================================================
46
//
47
// Internal state.
48
//
49
// ==================================================================
50

51     //The root context of the current deployment branch
52
private RootDeployerContext rootContext;
53     //The handler context
54
private HandlerContext context;
55     
56     // ==================================================================
57
//
58
// Constructors.
59
//
60
// ==================================================================
61

62     // ==================================================================
63
//
64
// Internal methods.
65
//
66
// ==================================================================
67

68     // ==================================================================
69
//
70
// Public methods
71
//
72
// ==================================================================
73

74     /**
75      * Accessor method to connect the current root context
76      * @param rootContext the deployment root context
77      */

78     public void
79     connectRootDeployerContext(RootDeployerContext rootContext)
80     {
81         this.rootContext = rootContext;
82     }
83
84     /**
85      * Accessor method to retrieve the deployment root context.
86      * @return The deployment root context
87      */

88     protected RootDeployerContext
89     getRootDeployerContext()
90     {
91         return rootContext;
92     }
93
94     /**
95      * Util method to retrieve automatically the children deployer
96      * in charge to manage the given bean object. The deployer is
97      * retrieves using the extension mecanism provides by bean object.
98      * @param obj The bean object to use
99      * @return
100      * @throws InitializationError
101      */

102     public Deployer
103     getDeployer(Object JavaDoc obj)
104     throws InitializationError
105     {
106         Bean bean=null;
107         
108         try {
109             bean = (Bean) obj;
110             HandlerBase tmp =
111                 (HandlerBase) bean.getExtensionManager()
112                                   .getExtensionByName(resolveExtention(bean));
113             tmp.connectRootDeployerContext(getRootDeployerContext());
114             tmp.connectHandlerContext(getHandlerContext());
115             Method JavaDoc m =
116                 tmp.getClass()
117                    .getDeclaredMethod("getInstance",
118                                       new Class JavaDoc[] { obj.getClass()});
119
120             return (Deployer) m.invoke(tmp, new Object JavaDoc[] { obj });
121
122         } catch (NoSuchMethodException JavaDoc nos) {
123             throw new InitializationError
124                         (null,nos,
125                          "NoSuchMethodException was thrown when looking for a deployer child "
126                          +"The handler use to instantiate the deployer seem to be devoided "
127                          +"of the [Deployer getInstance("+obj.getClass()+")] method ");
128                 
129         } catch (IllegalAccessException JavaDoc iae) {
130             throw new InitializationError
131                         (null,iae,
132                          "IllegalAccessException was thrown when looking for a deployer child");
133         } catch (InvocationTargetException JavaDoc e) {
134             
135             if (e.getTargetException() instanceof InitializationError)
136                 throw (InitializationError) e.getTargetException();
137             else{
138
139                 throw new InitializationError
140                         (null,e,
141                         "Unespected exception was thrown when looking for a deployer child on :\n "
142                          +getStringifiedDescription(bean),1);
143                 }
144         }
145     }
146     /**
147      * Return the stringified description of the given bean
148      */

149     public String JavaDoc
150     getStringifiedDescription(Bean bean)
151     {
152         try{
153             StringWriter JavaDoc w = new StringWriter JavaDoc();
154             bean.marshal(w);
155             return w.toString();
156         }
157         catch(Exception JavaDoc e){
158             return "Description not available";
159         }
160
161     }
162     /**
163      * Apply a common configuration to the given deployer
164      *
165      * @param deploy The deployer to configure
166      */

167     public void
168     applyCommonConfig(ChildDeployerContext deploy)
169     {
170         //create the default lifecycle manager
171
LifeCycleManager dlcm = new DefaultLifeCycleManager();
172         //connected its own life cycle manager
173
deploy.connectLifeCycleManager(dlcm);
174         //connected the current deployment logger
175
dlcm.connectLogger(getRootDeployerContext().getDeploymentLogger());
176         //connected to the manager the deployer managed
177
dlcm.connectManagedDeployer(deploy);
178         //connect the default deployment scehduler
179
deploy.connectDeployerScheduler(new DefaultDeployerScheduler());
180         //connect the root context
181
deploy.connectRootDeployerContext(getRootDeployerContext());
182         //connect the main error manager
183
deploy.connectErrorManager(getRootDeployerContext().getErrorManager());
184     }
185
186     /**
187      * Accessor method to retrieve the current DeploymentLogger
188      *
189      * @return The current deployment logger
190      */

191     public DeploymentLogger
192     getLogger()
193     {
194         return getRootDeployerContext().getDeploymentLogger();
195     }
196     
197     /**
198      * Accessor method to connect the handler context
199      * @param context The handler context to connect
200      */

201     public void
202     connectHandlerContext(HandlerContext context)
203     {
204         this.context = context;
205     }
206
207     /**
208      * Accessor method to retrieve the handler context
209      *
210      * @return The handler context
211      */

212     public HandlerContext
213     getHandlerContext()
214     {
215         return context;
216     }
217     
218     /**
219      * Util method to found the deployer extension identifer relative
220      * to the given bean.
221      * @param the bean we are looking the looking the deployer for.
222      * @return The extension identifier.
223      */

224     public String JavaDoc
225     resolveExtention(Bean bean)
226     {
227         return getHandlerContext().getHandlerResolver().getExtension(bean);
228     }
229
230     /**
231      * To string method returning the handler identifier.
232      */

233     public String JavaDoc
234     toString()
235     {
236         String JavaDoc className = getClass().getName();
237         return className.substring(
238             className.lastIndexOf("ccm"),
239             className.lastIndexOf("."))
240             + ".Deployer";
241     }
242
243 }
Popular Tags