KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > Julia


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.fractal.api.Component;
30 import org.objectweb.fractal.api.NoSuchInterfaceException;
31 import org.objectweb.fractal.api.Type;
32 import org.objectweb.fractal.api.factory.Factory;
33 import org.objectweb.fractal.api.factory.GenericFactory;
34 import org.objectweb.fractal.api.factory.InstantiationException;
35 import org.objectweb.fractal.api.type.ComponentType;
36 import org.objectweb.fractal.api.type.InterfaceType;
37
38 import org.objectweb.fractal.julia.factory.BasicGenericFactoryMixin;
39 import org.objectweb.fractal.julia.factory.ChainedInstantiationException;
40 import org.objectweb.fractal.julia.loader.Loader;
41 import org.objectweb.fractal.julia.type.BasicTypeFactoryMixin;
42
43 /**
44  * Provides access to the Julia bootstrap component.
45  */

46
47 public class Julia implements Factory, GenericFactory {
48
49   /**
50    * The bootstrap component.
51    */

52
53   private static Component bootstrapComponent;
54
55   // -------------------------------------------------------------------------
56
// Implementation of the Factory interface
57
// -------------------------------------------------------------------------
58

59   /**
60    * @return <tt>null</tt>.
61    */

62
63   public Type getFcInstanceType () {
64     return null;
65   }
66
67   /**
68    * @return <tt>null</tt>.
69    */

70
71   public Object JavaDoc getFcControllerDesc () {
72     return null;
73   }
74
75   /**
76    * @return <tt>null</tt>.
77    */

78
79   public Object JavaDoc getFcContentDesc () {
80     return null;
81   }
82
83   /**
84    * Returns the Julia bootstrap component. If this component does not exists
85    * yet, it is created as follows:
86    * <ul>
87    * <li>a pre bootstrap component is created by assembling a {@link Loader}
88    * object, a {@link BasicTypeFactoryMixin} object, and a {@link
89    * BasicGenericFactoryMixin} object. The loader object is created by
90    * instantiating the class specified in the "julia.loader" system
91    * property.</li>
92    * <li>the pre bootstrap component is used to create the real bootstrap
93    * component, by calling the <tt>newFcInstance</tt> method of the
94    * <tt>GenericFactory</tt> interface of the pre bootstrap component, with the
95    * "bootstrap" string as controller descriptor.</li>
96    * </ul>
97    *
98    * @return the {@link Component} interface of the component instantiated from
99    * this factory.
100    * @throws InstantiationException if the component cannot be created.
101    */

102
103   public Component newFcInstance () throws InstantiationException JavaDoc {
104     return newFcInstance(new HashMap JavaDoc());
105   }
106
107   /**
108    * Returns the Julia bootstrap component. If this component does not exists
109    * yet, it is created as follows:
110    * <ul>
111    * <li>a pre bootstrap component is created by assembling a {@link Loader}
112    * object, a {@link BasicTypeFactoryMixin} object, and a {@link
113    * BasicGenericFactoryMixin} object. The loader object is created by
114    * instantiating the class specified in the "julia.loader" system
115    * property, or associated to the "julia.loader" key in the contentDesc
116    * Map.</li>
117    * <li>the pre bootstrap component is used to create the real bootstrap
118    * component, by calling the <tt>newFcInstance</tt> method of the
119    * <tt>GenericFactory</tt> interface of the pre bootstrap component, with the
120    * "bootstrap" string as controller descriptor.</li>
121    * </ul>
122    *
123    * @param type ignored.
124    * @param controllerDesc ignored.
125    * @param contentDesc an optional Map.
126    * @return the {@link Component} interface of the component instantiated from
127    * this factory.
128    * @throws InstantiationException if the component cannot be created.
129    */

130
131   public Component newFcInstance (
132     final Type type,
133     final Object JavaDoc controllerDesc,
134     final Object JavaDoc contentDesc) throws InstantiationException JavaDoc
135   {
136     Map JavaDoc context;
137     if (contentDesc instanceof Map JavaDoc) {
138       context = (Map JavaDoc)contentDesc;
139     } else {
140       context = new HashMap JavaDoc();
141     }
142     return newFcInstance(context);
143   }
144
145   private Component newFcInstance (final Map JavaDoc context)
146     throws InstantiationException JavaDoc
147   {
148     if (bootstrapComponent == null) {
149       String JavaDoc boot = (String JavaDoc)context.get("julia.loader");
150       if (boot == null) {
151         boot = System.getProperty("julia.loader");
152       }
153       if (boot == null) {
154         throw new InstantiationException JavaDoc(
155           "The julia.loader [system] property is not defined");
156       }
157
158       // creates the pre bootstrap controller components
159
Loader loader;
160       try {
161         loader = (Loader)_forName(boot).newInstance();
162         loader.init(context);
163       } catch (Exception JavaDoc e) {
164         throw new InstantiationException JavaDoc(
165           "Cannot find or instantiate the '" + boot +
166           "' class specified in the julia.loader [system] property");
167       }
168       BasicTypeFactoryMixin typeFactory = new BasicTypeFactoryMixin();
169       BasicGenericFactoryMixin genericFactory = new BasicGenericFactoryMixin();
170       genericFactory._this_weaveableL = loader;
171       genericFactory._this_weaveableTF = typeFactory;
172
173       // use the pre bootstrap component to create the real bootstrap component
174
ComponentType t = typeFactory.createFcType(new InterfaceType[0]);
175       try {
176         bootstrapComponent = genericFactory.newFcInstance(t, "bootstrap", null);
177         try {
178           ((Loader)bootstrapComponent.getFcInterface("loader")).init(context);
179         } catch (NoSuchInterfaceException ignored) {
180         }
181       } catch (Exception JavaDoc e) {
182         throw new ChainedInstantiationException(
183           e, null, "Cannot create the bootstrap component");
184       }
185     }
186     return bootstrapComponent;
187   }
188
189   /*
190    * Convenient method used for J2ME conversion
191    * (ClassLoader not available in CDLC)
192    */

193
194   private Class JavaDoc _forName (final String JavaDoc name) throws ClassNotFoundException JavaDoc {
195     return getClass().getClassLoader().loadClass(name);
196   }
197 }
198
Popular Tags