KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > util > Fractal


1 /***
2  * Fractal Util: utilities for the Fractal API
3  * Copyright (C) 2003 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.util;
25
26 import java.util.Map JavaDoc;
27
28 import org.objectweb.fractal.api.Component;
29 import org.objectweb.fractal.api.NoSuchInterfaceException;
30 import org.objectweb.fractal.api.control.AttributeController;
31 import org.objectweb.fractal.api.control.BindingController;
32 import org.objectweb.fractal.api.control.ContentController;
33 import org.objectweb.fractal.api.control.LifeCycleController;
34 import org.objectweb.fractal.api.control.NameController;
35 import org.objectweb.fractal.api.control.SuperController;
36 import org.objectweb.fractal.api.factory.Factory;
37 import org.objectweb.fractal.api.factory.GenericFactory;
38 import org.objectweb.fractal.api.factory.InstantiationException;
39 import org.objectweb.fractal.api.type.TypeFactory;
40
41 /**
42  * Provides static methods to access standard interfaces of Fractal components.
43  */

44
45 public class Fractal {
46
47   /**
48    * Private constructor (uninstantiable class).
49    */

50
51   private Fractal () {
52   }
53
54   /**
55    * Returns a bootstrap component to create other components. This method
56    * just calls the corresponding method of the
57    * <tt>org.objectweb.fractal.api.Fractal</tt> class.
58    *
59    * @return a bootstrap component to create other components.
60    * @throws InstantiationException if the bootstrap component cannot be
61    * created.
62    */

63
64   public static Component getBootstrapComponent ()
65     throws InstantiationException JavaDoc
66   {
67     return org.objectweb.fractal.api.Fractal.getBootstrapComponent();
68   }
69
70   /**
71    * Returns a bootstrap component to create other components. This method
72    * creates an instance of the class whose name is associated to the
73    * "fractal.provider" key, which much implement the {@link Factory} or
74    * {@link GenericFactory} interface, and returns the component instantiated by
75    * this factory.
76    *
77    * @param hints a map which must associate a value to the "fractal.provider"
78    * key, and which may associate a ClassLoader to the "classloader" key.
79    * This class loader will be used to load the bootstrap component.
80    * @return a bootstrap component to create other components.
81    * @throws InstantiationException if the bootstrap component cannot be
82    * created.
83    */

84   
85   public static Component getBootstrapComponent (final Map JavaDoc hints)
86     throws InstantiationException JavaDoc
87   {
88     String JavaDoc bootTmplClassName = (String JavaDoc)hints.get("fractal.provider");
89     if (bootTmplClassName == null) {
90       bootTmplClassName = System.getProperty("fractal.provider");
91     }
92     if (bootTmplClassName == null) {
93       throw new InstantiationException JavaDoc(
94         "The fractal.provider value is not defined");
95     }
96     Object JavaDoc bootTmpl;
97     try {
98       ClassLoader JavaDoc cl = (ClassLoader JavaDoc)hints.get("classloader");
99       if (cl == null) {
100         cl = new Fractal().getClass().getClassLoader();
101       }
102       Class JavaDoc bootTmplClass = cl.loadClass(bootTmplClassName);
103       bootTmpl = bootTmplClass.newInstance();
104     } catch (Exception JavaDoc e) {
105       throw new InstantiationException JavaDoc(
106         "Cannot find or instantiate the '" + bootTmplClassName +
107         "' class associated to the fractal.provider key");
108     }
109     if (bootTmpl instanceof GenericFactory) {
110       return ((GenericFactory)bootTmpl).newFcInstance(null, null, hints);
111     } else {
112       return ((Factory)bootTmpl).newFcInstance();
113     }
114   }
115   
116   /**
117    * Returns the {@link AttributeController} interface of the given component.
118    *
119    * @param component a component.
120    * @return the {@link AttributeController} interface of the given component.
121    * @throws NoSuchInterfaceException if there is no such interface.
122    */

123
124   public static AttributeController getAttributeController (
125     final Component component) throws NoSuchInterfaceException
126   {
127     return (AttributeController)component.getFcInterface("attribute-controller");
128   }
129
130   /**
131    * Returns the {@link BindingController} interface of the given component.
132    *
133    * @param component a component.
134    * @return the {@link BindingController} interface of the given component.
135    * @throws NoSuchInterfaceException if there is no such interface.
136    */

137
138   public static BindingController getBindingController (
139     final Component component) throws NoSuchInterfaceException
140   {
141     return (BindingController)component.getFcInterface("binding-controller");
142   }
143
144   /**
145    * Returns the {@link ContentController} interface of the given component.
146    *
147    * @param component a component.
148    * @return the {@link ContentController} interface of the given component.
149    * @throws NoSuchInterfaceException if there is no such interface.
150    */

151
152   public static ContentController getContentController (
153     final Component component) throws NoSuchInterfaceException
154   {
155     return (ContentController)component.getFcInterface("content-controller");
156   }
157
158   /**
159    * Returns the {@link SuperController} interface of the given component.
160    *
161    * @param component a component.
162    * @return the {@link SuperController} interface of the given component.
163    * @throws NoSuchInterfaceException if there is no such interface.
164    */

165
166   public static SuperController getSuperController (final Component component)
167     throws NoSuchInterfaceException
168   {
169     return (SuperController)component.getFcInterface("super-controller");
170   }
171
172   /**
173    * Returns the {@link NameController} interface of the given component.
174    *
175    * @param component a component.
176    * @return the {@link NameController} interface of the given component.
177    * @throws NoSuchInterfaceException if there is no such interface.
178    */

179
180   public static NameController getNameController (final Component component)
181     throws NoSuchInterfaceException
182   {
183     return (NameController)component.getFcInterface("name-controller");
184   }
185
186   /**
187    * Returns the {@link LifeCycleController} interface of the given component.
188    *
189    * @param component a component.
190    * @return the {@link LifeCycleController} interface of the given component.
191    * @throws NoSuchInterfaceException if there is no such interface.
192    */

193
194   public static LifeCycleController getLifeCycleController (
195     final Component component) throws NoSuchInterfaceException
196   {
197     return (LifeCycleController)component.getFcInterface("lifecycle-controller");
198   }
199
200   /**
201    * Returns the {@link Factory} interface of the given component.
202    *
203    * @param component a component.
204    * @return the {@link Factory} interface of the given component.
205    * @throws NoSuchInterfaceException if there is no such interface.
206    */

207
208   public static Factory getFactory (final Component component)
209     throws NoSuchInterfaceException
210   {
211     return (Factory)component.getFcInterface("factory");
212   }
213
214   /**
215    * Returns the {@link GenericFactory} interface of the given component.
216    *
217    * @param component a component.
218    * @return the {@link GenericFactory} interface of the given component.
219    * @throws NoSuchInterfaceException if there is no such interface.
220    */

221
222   public static GenericFactory getGenericFactory (final Component component)
223     throws NoSuchInterfaceException
224   {
225     return (GenericFactory)component.getFcInterface("generic-factory");
226   }
227
228   /**
229    * Returns the {@link TypeFactory} interface of the given component.
230    *
231    * @param component a component.
232    * @return the {@link TypeFactory} interface of the given component.
233    * @throws NoSuchInterfaceException if there is no such interface.
234    */

235
236   public static TypeFactory getTypeFactory (final Component component)
237     throws NoSuchInterfaceException
238   {
239     return (TypeFactory)component.getFcInterface("type-factory");
240   }
241 }
242
Popular Tags