KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > Deployment > DeploymentComponentFactory


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 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 Frederic
23 Contributor(s): ___________________________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.Deployment;
28
29
30
31 import org.objectweb.openccm.Containers.MetaInformation.HomeInstance;
32 import org.objectweb.openccm.Containers.MetaInformation.HomeType;
33 /**
34  * The goal of this class is to provide a
35  * temporary way to produce system component
36  * avoiding the use of deployment architecture.
37  * It will be particulary use for producing deployment
38  * component such as DCI or AssemblyFactoryManager.
39  *
40  * @author <a HREF="mailto:frederic.briclet@lifl.fr">Briclet Frederic</a>
41  * @author <a HREF="mailto:Fabien.Hameau@lifl.fr">Hameau Fabien</a>
42  * @version 0.1
43  */

44
45 public class DeploymentComponentFactory {
46
47     // ==================================================================
48
//
49
// Internal state.
50
//
51
// ==================================================================
52
private static SystemHomeManagerImpl home_manager_ =
53         new SystemHomeManagerImpl();
54     
55     private static org.objectweb.openccm.Containers.PCA rootPCA_ =
56         new org.objectweb.openccm.Containers.RootPCAImpl(home_manager_);
57
58     // Adding a reference to the home type MI managed by
59
// this container
60
protected HomeType home_type_;
61
62     /**
63      * Used by the computeFullHomeServantClassName method in order
64      * to compute full home servant class names.
65      */

66     static private org.objectweb.ccm.visitorIDL3.java.JavaTranslator
67         javaTranslator = new org.objectweb.ccm.visitorIDL3.java.JavaTranslator();
68
69     // ==================================================================
70
//
71
// Constructor.
72
//
73
// ==================================================================
74

75     /* private DeploymentComponentFactory(){
76        }*/

77     
78     // ==================================================================
79
//
80
// Internal methods.
81
//
82
// ==================================================================
83
/**
84      * Trace an error.
85      */

86     protected static void
87     trace_error(String JavaDoc method,
88                 String JavaDoc msg,
89                 Throwable JavaDoc exc)
90     {
91         System.err.println("DeploymentComponentFactory::" + method + ": " + msg);
92         if(exc != null)
93             exc.printStackTrace();
94     }
95
96
97     /**
98      * Compute the full home servant class name associated
99      * to a home executor class.
100      *
101      * @param homeExecutorClass The home executor class.
102      *
103      * @return The full home servant class name, or null if not found.
104      */

105     protected static String JavaDoc
106     computeFullHomeServantClassName(Class JavaDoc homeExecutorClass)
107     {
108         // If homeExecutorClass is null then returns null.
109
if(homeExecutorClass == null)
110             return null;
111
112         // The full class name with the package name.
113
String JavaDoc fullClassName = homeExecutorClass.getName();
114
115         // The short class name without the package name.
116
int idx = fullClassName.lastIndexOf(".") + 1;
117         String JavaDoc shortClassName = fullClassName.substring(idx);
118
119         // If the short class name is prefixed CCM_ then
120
// returns the full home servant class name.
121
if(shortClassName.startsWith("CCM_"))
122             return fullClassName.substring(0, idx)
123                  + javaTranslator.checkKeywords(shortClassName.substring(4))
124                  + "CCM";
125
126         // Find the CCM home executor interface into implemented interfaces.
127
//
128
Class JavaDoc[] itfs = homeExecutorClass.getInterfaces();
129
130     // Required to navigate in the reverse order
131
// else interface CCM_*Operations will be found
132
// before interface CCM_*
133
for(int i=itfs.length-1; i>=0; i--)
134         {
135              String JavaDoc className = computeFullHomeServantClassName(itfs[i]);
136              if(className != null)
137                  return className;
138         }
139
140     // Find the CCM home executor interface into the super class.
141
String JavaDoc className = computeFullHomeServantClassName(homeExecutorClass.getSuperclass());
142         if(className != null)
143             return className;
144
145         // Returns null as the home servant class was not found.
146
return null;
147     }
148
149
150      /**
151      * Creates a home executor.
152      *
153      * @param entrypt The entry point in the archive.
154      *
155      * @return The created home executor.
156      *
157      * @exception org.omg.Components.Deployment.ImplEntryPointNotFound
158      * Thrown if the home executor entry point is invalid.
159      */

160     protected static org.omg.Components.HomeExecutorBase
161     create_home_executor(java.lang.String JavaDoc entrypt)
162     throws org.omg.Components.Deployment.ImplEntryPointNotFound,
163            org.omg.Components.Deployment.InstallationFailure
164     {
165         try
166         {
167             // Call the static home executor 'entrypt' method.
168
java.lang.Object JavaDoc object =
169                 TheURLClassLoader.getURLClassLoader()
170                 .callStaticClassMethod(entrypt, new Object JavaDoc[0]);
171
172             // Check that the result is a CCMHome executor.
173
return (org.omg.Components.HomeExecutorBase)object;
174         }
175         catch(java.lang.ClassNotFoundException JavaDoc exc)
176         {
177             trace_error("install_home",
178                         "Class '" + exc.getMessage() + "' not found for home executor entry point '"
179                         + entrypt + "'!!!",
180                         null);
181             throw new org.omg.Components.Deployment.ImplEntryPointNotFound();
182         }
183         catch(java.lang.NoSuchMethodException JavaDoc exc)
184         {
185             trace_error("install_home",
186                         "Home executor entry point 'public static org.omg.Components.HomeExecutorBase "
187                         + entrypt + "();' not found, check the home executor entry point signature!!!",
188                          null);
189             throw new org.omg.Components.Deployment.ImplEntryPointNotFound();
190         }
191         catch(java.lang.reflect.InvocationTargetException JavaDoc exc)
192         {
193             trace_error("install_home",
194                         "Exception thrown by home executor entry point '" + entrypt + "'!!!",
195                         exc.getTargetException());
196             throw new org.omg.Components.Deployment.InstallationFailure(
197                           org.objectweb.openccm.Deployment.ExceptionThrownByHomeExecutorEntryPoint.value
198                   );
199         }
200         catch(java.lang.IllegalAccessException JavaDoc exc)
201         {
202             trace_error("install_home",
203                         "Illegal access by home executor entry point '" + entrypt + "'!!!",
204                         exc);
205             throw new org.omg.Components.Deployment.InstallationFailure(
206                           org.objectweb.openccm.Deployment.IllegalAccessByHomeExecutorEntryPoint.value
207                   );
208         }
209         catch(java.lang.ClassCastException JavaDoc exc)
210         {
211             trace_error("install_home",
212                         "Home executor entry point '" + entrypt +
213                         "' does not return org.omg.Components.HomeExecutorBase," +
214                         " check the home executor entry point signature!!!",
215                         null);
216             throw new org.omg.Components.Deployment.ImplEntryPointNotFound();
217         }
218     }
219
220
221     protected String JavaDoc retrieve_MI_name(String JavaDoc className)
222     {
223         try
224             {
225                 // isolate names
226
String JavaDoc tmpName = className.substring(0, className.lastIndexOf("Impl."));
227                 // get the IDL name of home
228
String JavaDoc IDLName = tmpName.substring(tmpName.lastIndexOf(".") + 1);
229                 // get the prefix for concerned nale
230
String JavaDoc prefix = tmpName.substring(0,tmpName.lastIndexOf("."));
231                 prefix = tmpName.substring(0,prefix.lastIndexOf(".") + 1);
232                 // compute the full name
233
if (IDLName != null)
234                     {
235                        // System.err.println("### Debug : get " + prefix + IDLName);
236
return prefix + IDLName;
237                     }
238             }
239         // problem during name computation
240
catch(Exception JavaDoc e)
241             {
242                // trace_error("retrieve_MI_name",
243
// "error when computing IDL name for MetaInformation", null);
244
}
245         return null;
246     }
247     
248     /**
249      * Install a home for a archive already load in memory
250      * that is the case of system archive.
251      *
252      * @param entryPoint the entryPoint to instantiate the home
253      */

254     public org.omg.Components.CCMHome
255     install_home(String JavaDoc entrypt)
256     {
257         try {
258             // create the home executor.
259
org.omg.Components.HomeExecutorBase home =
260                 create_home_executor(entrypt);
261             
262             String JavaDoc clazz_name = computeFullHomeServantClassName(home.getClass());
263             if (clazz_name == null)
264                 {
265                     trace_error("install_home",
266                                 "Home servant class for " + home.getClass().getName() + " not found!!!",
267                                 null);
268                     throw new org.omg.Components.Deployment.
269                         InstallationFailure(org.objectweb.openccm.Deployment.HomeServantClassNotFound.value);
270                 }
271
272             // Instantiate the executor.
273
org.objectweb.openccm.Containers.HomeExecutorBase home_exec =
274                 (org.objectweb.openccm.Containers.HomeExecutorBase)
275                 create_home_executor(clazz_name + "._create_home_executor");
276             
277             home_exec._delegate(home);
278             
279             // create the home servant and set infos.
280
org.objectweb.openccm.Containers.HomeServantImpl servant =
281                 new org.objectweb.openccm.Containers.HomeServantImpl();
282
283             //Remove but will fail when removing component
284
//servant.the_container_base(this);
285
home_exec._home_servant(servant);
286             
287             servant.the_home_executor(home_exec);
288
289             //servant.the_home_configuration(get_home_configuration());
290

291             //servant.the_component_configuration(get_component_configuration());
292
servant.the_home_finder(home_manager_);
293
294             // create and set the home reference.
295
org.omg.Components.CCMHome ref = rootPCA_.activate_home(servant);
296
297             // begin MI modif
298
/////////////////////////////////////////////
299
// create the home type MI for the given home
300
/////////////////////////////////////////////
301
// retrieving IDL Declaration name
302
String JavaDoc IDL_name = retrieve_MI_name(entrypt);
303
304             if (IDL_name != null)
305             {
306                 // setting the class and method name
307
String JavaDoc methodName = IDL_name + "_homeMI.create_type_MI";
308                 // call the computed name method (static)
309
try
310                 {
311                     home_type_ = (HomeType) TheURLClassLoader.getURLClassLoader()
312                         .callStaticClassMethod(methodName,new Object JavaDoc[0]);
313                 }
314                 catch(Exception JavaDoc e)
315                 {
316                     // no home type entypt is available
317
home_type_ = null;
318                 }
319             }
320         
321             ///////////////////////////// MODIF MetaInformation
322

323             HomeInstance _tmp_hi = null;
324             
325             if (home_type_ != null){
326                 _tmp_hi = home_type_.create_home_instance();
327                 if (_tmp_hi == null){
328                     trace_error("install_home", "home instance is null ", null);
329                 }
330                 servant.the_home_instance(_tmp_hi);
331             }
332             else {
333                 trace_error("retrieve_MI_name",
334                             "Instance MetaInformation for DCI not supported ", null);
335             }
336             servant.the_home_instance(_tmp_hi);
337             // End of MI modif
338

339             //Remove but can fail
340
//listener_.on_create(ref);
341

342
343             return ref;
344         }
345         catch(Exception JavaDoc e){
346             e.printStackTrace();
347             System.err.println(e.getMessage());
348         }
349
350         return null;
351
352     }
353     
354
355
356
357
358 }
359
Popular Tags