KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Component > ComponentManagerImpl


1 /* $Id: ComponentManagerImpl.java,v 1.4 2004/05/20 14:23:50 bures Exp $ */
2 package SOFA.Component;
3
4 import java.lang.reflect.Method JavaDoc;
5
6 import SOFA.Connector.Reference;
7 import SOFA.Connector.RoleBase;
8 import SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor;
9
10 /** Implementation of component manager.
11   *
12   * @author Petr Hnetynka
13   */

14 public abstract class ComponentManagerImpl implements ComponentManager, org.objectweb.fractal.api.Type {
15   
16   /** roles of connectors of provides interfaces */
17   protected NamedElementRegister provides;
18   /** roles of connectors to required interfaces */
19   protected NamedElementRegister requires;
20   /** sRoles of Requirements, which will be subsumed to subcomponents */
21   protected NamedElementRegister subsumableRequires;
22   /** cRoles of provisions, that are connect by delegation to subcomponent's provisions */
23   protected NamedElementRegister delegatedProvides;
24   /** roles of connectors to subcomponent CMs */
25   protected NamedElementRegister subcomponents;
26   /** deployment descriptor */
27   protected DeploymentDescriptor dd;
28   /** thread registry */
29   protected ThreadIDRegistry thrRegistry;
30   
31
32   /** Name of the SOFAnode */
33   protected String JavaDoc node;
34   /** Name of the home deployment dock */
35   protected String JavaDoc dockName;
36 // /** Base classloader - loading of the implementation */
37
// protected ClassLoader bcl;
38
// /** Interface classloader - loading of the interfaces and connectors */
39
// protected ClassLoader icl;
40
/** Implemenatation version */
41   protected String JavaDoc releaseVersion;
42   /** Home local dock */
43   protected SOFA.SOFAnode.Run.Deployment.DeplDock localdock;
44
45   /** Full name of the component (with version) */
46   protected String JavaDoc fullName;
47
48   /** Runtime id. Used for connector. */
49   protected String JavaDoc id;
50   
51   /** Constructor */
52   public ComponentManagerImpl(DeploymentDescriptor _dd, String JavaDoc _node, String JavaDoc _dockName, SOFA.SOFAnode.Run.Deployment.DeplDock _localdock, String JavaDoc _id) {
53     dd = _dd;
54     localdock = _localdock;
55     releaseVersion = dd.getReleaseVersion();
56     node = _node;
57     dockName = _dockName;
58     id = _id;
59
60     try {
61       thisRB = SOFA.Connector.Boot.DCUPComponentManagerConnector.createSrv(this, id);
62     } catch (SOFA.Connector.ConnectorException e) {
63       throw new ComponentLifecycleException("ConnectorException: "+e.getMessage(), e);
64     }
65   // icl = SOFA.SOFAnode.Run.SOFAInterfaceClassLoader.getInterfaceClassLoader(dd.getCDLEntities());
66
// bcl = SOFA.SOFAnode.Run.SOFABaseClassLoader.getBaseClassLoader(icl, dd.getArchitectureAbsName() , releaseVersion);
67
provides = new NamedElementRegister();
68     requires = new NamedElementRegister();
69     subsumableRequires = new NamedElementRegister();
70     delegatedProvides = new NamedElementRegister();
71     subcomponents = new NamedElementRegister();
72     thrRegistry = new ThreadIDRegistryImpl(_node);
73
74     fullName = dd.getArchitectureAbsName()+"["+dd.getReleaseVersion()+"]";
75   }
76
77   public void registerSubcomponent(String JavaDoc name, SOFA.Connector.Reference ref) throws NamingException, InstantiationException JavaDoc {
78     try {
79       RoleBase target = SOFA.Connector.Boot.DCUPComponentManagerConnector.createClt(ref);
80       subcomponents.registerElementNamed(name, target);
81     } catch (SOFA.Connector.ConnectorException e) {
82       throw new InstantiationException JavaDoc("ConnectorException: "+e.getMessage());
83     }
84   }
85   
86   public void unregisterSubcomponent(String JavaDoc name) throws NamingException {
87     subcomponents.unregisterElementNamed(name);
88   }
89
90   /** Return connector role to the provided interface.
91     * @param name name of the provision
92     * @exception NamingException no provision with given name
93     * @return connector role to the provided interface
94     */

95   public RoleBase getProvision(String JavaDoc name) throws NamingException {
96     return (RoleBase) provides.getElementNamed(name);
97   }
98   
99   public Reference getProvisionReference(String JavaDoc name) throws NamingException {
100     return ((RoleBase) provides.getElementNamed(name)).getSOFAReference();
101   }
102
103   public void setRequirement(String JavaDoc name, Reference ref) throws InstantiationException JavaDoc, NamingException {
104     RoleBase cRole = instantiateConnector(name);
105     try {
106       cRole.link(ref);
107     } catch (SOFA.Connector.LinkException e) {
108       throw new InstantiationException JavaDoc("LinkException: "+e.getMessage(), e );
109     }
110     requires.forceRegisterElementNamed(name, cRole);
111   }
112
113   /** Set implementation object to provision, object must implement provided interface.
114     * It is call from primitive components only.
115     * @param name name of the provision
116     * @param prov implementation object
117     * @exception NamingException no provision with given name
118     * @exception InstantiationException object cannot be set
119     */

120   public void setProvisionImpl(String JavaDoc name, Object JavaDoc prov) throws NamingException, InstantiationException JavaDoc {
121     try {
122     /* SOFA.Connector.DeploymentDescriptor connDD = dd.getConnectorDeploymentDescriptor(name);
123       String type = connDD.getType();
124       RoleBase ret = SOFA.Connector.ConnectorBuilder.createConnector(type, connDD, cl, node, dockName);
125       ret.setThrIDRegistry(thrRegistry);
126       ret.setRegister(this); */

127       RoleBase ret = instantiateConnector(name);
128       ret.link(prov);
129 // try {
130
// provides.unregisterElementNamed(name);
131
// } catch (NamingException e) {}
132
provides.registerElementNamed(name, ret);
133     } catch (SOFA.Connector.ConnectorException e) {
134       throw new InstantiationException JavaDoc("ConnectorException: "+e.getMessage());
135     }
136   }
137   
138   /** Set new implementation object to provision, object must implement provided interface.
139     * The method set object only, it suppose that everything is set.
140     * It is call from primitive components only.
141     * @param name name of the provision
142     * @param prov implementation object
143     * @exception NamingException no provision with given name
144     */

145   public void reSetProvisionImpl(String JavaDoc name, Object JavaDoc prov) throws NamingException {
146     try {
147       RoleBase ret = (RoleBase) provides.getElementNamed(name);
148       ret.link(prov);
149     } catch (SOFA.Connector.ConnectorException e) {
150       throw new InstantiationException JavaDoc("ConnectorException: "+e.getMessage());
151     }
152   }
153   
154   /** Delegate reference to subcomponent's provision. It is called from composed components only.
155     * @param name name of the provision
156     * @param ref reference to subcomponent's provision
157     * @exception NamingException no provision with given name
158     * @exception InstantiationException reference cannot be set
159     */

160   public void delegateProvision(String JavaDoc name, Reference ref) throws NamingException, InstantiationException JavaDoc {
161     try {
162     /* SOFA.Connector.DeploymentDescriptor connDD = dd.getConnectorDeploymentDescriptor("_delegate_"+name);
163       String type = connDD.getType();
164       RoleBase ret = SOFA.Connector.ConnectorBuilder.createConnector(type, connDD, cl, node, dockName);
165       ret.setThrIDRegistry(thrRegistry);
166       ret.setRegister(this); */

167       RoleBase ret = instantiateConnector("_delegate_"+name);
168       ret.link(ref);
169       delegatedProvides.registerElementNamed(name, ret);
170     } catch (SOFA.Connector.ConnectorException e) {
171       throw new InstantiationException JavaDoc("ConnectorException: "+e.getMessage());
172     }
173   }
174
175   /** Creates component provision.
176     * @param name name of the provision
177     * @exception NamingException no provision with given name
178     * @exception InstantiationException provision cannot be created
179     */

180   public void createProvision(String JavaDoc name) throws NamingException, InstantiationException JavaDoc {
181     RoleBase ret = instantiateConnector(name);
182     provides.registerElementNamed(name, ret);
183   }
184   
185   /** Connect component's provisions with provisions of all subcomponents. The methods createProvision
186     * and delegateProvision must be called for all component's provision before calling this method.
187     * @exception NamingException error in the names of the provisions
188     * @exception InstantiationException delegated intefaces cannot be connected
189     */

190   public void connectDelegated() throws NamingException, InstantiationException JavaDoc {
191     for (java.util.Enumeration JavaDoc e = provides.keys(); e.hasMoreElements(); ) {
192       String JavaDoc name = (String JavaDoc) e.nextElement();
193       RoleBase sRole = (RoleBase) provides.getElementNamed(name);
194       RoleBase cRole = (RoleBase) delegatedProvides.getElementNamed(name);
195       try {
196         sRole.link(cRole);
197       } catch (SOFA.Connector.LinkException ex) {
198         throw new InstantiationException JavaDoc("LinkException: "+ex.getMessage() );
199       }
200     }
201   }
202   
203   /** Creates subsumable requirment. Returned reference is passed to subcomponent by method setRequirement.
204     * @param name name of the requirment
205     * @return SOFA reference
206     * @exception NamingException no requirment with given name
207     * @exception InstantiationException requirment cannot be created
208     */

209   public Reference createSubsumableRequirement(String JavaDoc name) throws NamingException, InstantiationException JavaDoc {
210     /* SOFA.Connector.DeploymentDescriptor connDD = dd.getConnectorDeploymentDescriptor("_subsume_"+name);
211       String type = connDD.getType();
212       RoleBase ret = SOFA.Connector.ConnectorBuilder.createConnector(type, connDD, cl, node, dockName);
213       ret.setThrIDRegistry(thrRegistry);
214       ret.setRegister(this); */

215     RoleBase ret = instantiateConnector("_subsume_"+name);
216     subsumableRequires.forceRegisterElementNamed(name, ret);
217     return ret.getSOFAReference();
218   }
219
220   public void connectSubsumable() throws NamingException, InstantiationException JavaDoc {
221     for (java.util.Enumeration JavaDoc e = subsumableRequires.keys(); e.hasMoreElements(); ) {
222       String JavaDoc name = (String JavaDoc) e.nextElement();
223       RoleBase sRole = (RoleBase) subsumableRequires.getElementNamed(name);
224       RoleBase cRole = (RoleBase) requires.getElementNamed(name);
225       try {
226         sRole.link(cRole);
227       } catch (SOFA.Connector.LinkException ex) {
228         throw new InstantiationException JavaDoc("LinkException: "+ex.getMessage() );
229       }
230     }
231   }
232
233   /** Instantiated connector for provision/requirement with given name.
234     * @param name name of the provision/requirement
235     */

236   private RoleBase instantiateConnector(String JavaDoc name) throws InstantiationException JavaDoc {
237     try {
238 // SOFA.SOFAnode.Run.SOFASystemClassLoader scl = new SOFA.SOFAnode.Run.SOFASystemClassLoader();
239
SOFA.Connector.DeploymentDescriptor connDD = dd.getConnectorDeploymentDescriptor(name);
240       String JavaDoc type = connDD.getConnectorBuilder();
241       Class JavaDoc cl = Class.forName(type, true, SOFA.SOFAnode.Run.SOFACompClassLoader.getCompClassLoader());
242       Class JavaDoc[] par = new Class JavaDoc[2];
243       par[0] = Class.forName("SOFA.Connector.ECG.DeploymentDescriptor");
244       par[1] = Class.forName("java.lang.ClassLoader");
245       Method JavaDoc m = cl.getMethod("createConnector", par);
246       Object JavaDoc[] arg = new Object JavaDoc[2];
247       arg[0] = connDD;
248       arg[1] = SOFA.SOFAnode.Run.SOFACompClassLoader.getCompClassLoader();
249       RoleBase conn = (RoleBase) m.invoke(null, arg);
250       conn.setThrIDRegistry(thrRegistry);
251       conn.setComponentManager(thisRB);
252       return conn;
253       
254    /* RoleBase ret = SOFA.Connector.ConnectorBuilder.createConnector(type, connDD, cl, node, dockName);
255       ret.setThrIDRegistry(thrRegistry);
256       ret.setRegister(this);
257       return ret; */

258     } catch (ClassNotFoundException JavaDoc e) {
259       throw new InstantiationException JavaDoc("ClassNotFoundException: "+e.getMessage(), e);
260     } catch (NoSuchMethodException JavaDoc e) {
261       throw new InstantiationException JavaDoc("NoSuchMethodException: "+e.getMessage(), e);
262     } catch (IllegalAccessException JavaDoc e) {
263       throw new InstantiationException JavaDoc("IllegalAccessException: "+e.getMessage(), e);
264     } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
265       throw new InstantiationException JavaDoc("InvocationTargetException: "+e.getMessage(), e);
266     }
267   }
268
269
270   public SOFA.SOFAnode.Run.Deployment.DeplDock getLocalDeplDock() {
271     return localdock;
272   }
273
274   /** Returns requirement with given name.
275     * @param name name of the requirement
276     * @exception NamingException no requirement with the given name
277     */

278   public Object JavaDoc getRequirement(String JavaDoc name) throws NamingException {
279     return requires.getElementNamed(name);
280   }
281
282   public String JavaDoc getFullName() {
283     return fullName;
284   }
285
286   /** Loads implementation class of this component. It MUST be used for dynamic loading of classes
287     * from implementation code of component.
288     *
289     * @param name class name
290     * @return class
291     * @throws ClassNotFoundException class not found
292     */

293   public Class JavaDoc loadComponentClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
294     return SOFA.SOFAnode.Run.SOFACompClassLoader.loadComponentClass(name, releaseVersion);
295   }
296
297     /** Server side part of the connector (SRole) to this CM */
298   private SOFA.Connector.RoleBase thisRB;
299
300   /** Sets role of the connector to this CM.
301     *
302     * @param rb connector role
303     */

304   public void connToThisCM(SOFA.Connector.RoleBase rb) {
305     thisRB = rb;
306   }
307
308   /** Gets role of the connector to this CM.
309     *
310     * @return connector role
311     */

312   public SOFA.Connector.RoleBase connToThisCM() {
313     return thisRB;
314   }
315
316
317
318   //-- below - methods from fractal Component inteface
319

320   public Object JavaDoc[] getFcInterfaces() {
321     java.util.ArrayList JavaDoc ret = new java.util.ArrayList JavaDoc();
322     for ( java.util.Enumeration JavaDoc e = provides.getAllNames(); e.hasMoreElements(); ) {
323       ret.add(e.nextElement());
324     }
325     return ret.toArray();
326   }
327   
328   public Object JavaDoc getFcInterface(java.lang.String JavaDoc itfName) throws org.objectweb.fractal.api.NoSuchInterfaceException {
329     Object JavaDoc ret = null;
330     try {
331       ret = provides.getElementNamed(itfName);
332     } catch (NamingException e) {
333       throw new org.objectweb.fractal.api.NoSuchInterfaceException("No such interface", e);
334     }
335     return ret;
336   }
337   
338   public org.objectweb.fractal.api.Type getFcType() {
339     return this;
340   }
341
342   //-- below - methods from fractal Type inteface
343
public boolean isFcSubtypeOf(org.objectweb.fractal.api.Type t) {
344     // it is just temporal implementation
345
return false;
346   }
347 }
348
Popular Tags