KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > explorer > graph > GraphInformations


1 /*====================================================================
2
3 Objectweb Explorer Framework
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): Alexandre Vandekerkhove.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.fractal.explorer.graph;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.objectweb.fractal.adl.ADLException;
35 import org.objectweb.fractal.adl.Factory;
36 import org.objectweb.fractal.adl.FactoryFactory;
37 import org.objectweb.fractal.api.Component;
38 import org.objectweb.fractal.api.Interface;
39 import org.objectweb.fractal.api.NoSuchInterfaceException;
40 import org.objectweb.fractal.api.control.BindingController;
41 import org.objectweb.fractal.api.control.ContentController;
42 import org.objectweb.fractal.api.control.LifeCycleController;
43 import org.objectweb.fractal.api.control.SuperController;
44 import org.objectweb.fractal.api.type.InterfaceType;
45 import org.objectweb.util.explorer.swing.graph.PortType;
46 import org.objectweb.util.explorer.swing.graph.VertexType;
47
48
49
50 /**
51  * Utilities class for graphic representation
52  * @author Alexandre Vandekerkhove
53  * @version September 2004
54  */

55
56 public class GraphInformations {
57
58     
59     /**
60      * Provides the first component (root) of an application
61      * @param nameAppli the name of the application
62      * @return the "root" component associated
63      */

64     public static Component getRootComponent (final String JavaDoc nameAppli){
65         try {
66             Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
67             Object JavaDoc o = f.newComponent(nameAppli, new HashMap JavaDoc());
68             return (Component)o;
69         } catch (ADLException e) {
70             System.out.println("Application " + nameAppli + " not found");
71             return null;
72         }
73     }
74     
75     /**
76      * @param component a component
77      * @return true if the component is a primitive component (no sub-components)
78      */

79     public static boolean isPrimitiveComponent(final Component component) {
80         ContentController cc;
81         try {
82             cc = (ContentController)component.getFcInterface("content-controller");
83             Component[] subComponents = cc.getFcSubComponents();
84             return (subComponents.length == 0);
85         } catch (NoSuchInterfaceException e) {
86             return true;
87         }
88     }
89     
90     /**
91      * Provides the type of a component (primitive/composite)
92      * @param component the component
93      * @return its type
94      */

95     public static String JavaDoc getComponentType (final Component component){
96         ContentController cc;
97         try{
98             cc = (ContentController)component.getFcInterface("content-controller");
99             Component[] subComponents = cc.getFcSubComponents();
100             if (subComponents.length==0) return VertexType.PRIMITIF_VERTEX;
101             return VertexType.COMPOSITE_VERTEX;
102         } catch (NoSuchInterfaceException e){
103             return VertexType.PRIMITIF_VERTEX;
104         }
105     }
106     
107     /**
108      * @param component a component
109      * @return true if the component is a shared component
110      */

111     public static boolean isSharedComponent (final Component component){
112         SuperController sc;
113         try{
114             sc = (SuperController)component.getFcInterface("super-controller");
115             Component[] superComponents = sc.getFcSuperComponents();
116             return (superComponents.length > 1);
117         } catch (NoSuchInterfaceException e){
118             return false;
119         }
120     }
121     
122     /**
123      * Provides an array of the sub-components of a composite component
124      * @param component the component
125      * @return the array of its sub-components
126      */

127     public static Component[] getSubComponents (final Component component){
128         try {
129             if (!isPrimitiveComponent(component)){
130                 ContentController cc = (ContentController)component.getFcInterface("content-controller");
131                 return cc.getFcSubComponents();
132             }
133         } catch (NoSuchInterfaceException e) {
134             return null;
135         }
136         return null;
137     }
138     
139     /**
140      * Provides the execution state of a component
141      * @param component the component
142      * @return its execution state ("STARTED" / "STOPPED")
143      */

144     public static String JavaDoc getComponentState (final Component component){
145         try {
146             LifeCycleController lc = (LifeCycleController)component.getFcInterface("lifecycle-controller");
147             return lc.getFcState();
148         } catch (NoSuchInterfaceException e) {
149             return "unknown state";
150         }
151     }
152     
153     /**
154      * Returns true if the status of the component is "STARTED"
155      * @param component the component
156      */

157     public static boolean isStarted (final Component component){
158         return getComponentState(component).equals("STARTED");
159     }
160     
161     /**
162      * Provides the array of the external interfaces of a component
163      * The array is sorted by alphabetical order of the names of the interfaces
164      * @param component the component
165      * @return the array of the external interfaces
166      */

167     public static Interface[] getSortExtItf(final Component component){
168         Object JavaDoc[] listExtItfObj = component.getFcInterfaces();
169         Interface[] listItfExtItf = new Interface[listExtItfObj.length];
170         List JavaDoc listExtItf = new ArrayList JavaDoc();
171         for(int i=0 ; i<listExtItfObj.length ; i++){
172             listExtItf.add(i, listExtItfObj[i]);
173         }
174         Collections.sort(listExtItf, new SortInterface());
175         for (int j=0 ; j<listExtItf.size() ; j++){
176             listItfExtItf[j] = (Interface)listExtItf.get(j);
177         }
178         return listItfExtItf;
179     }
180
181     /**
182      * Provides the array of the internal interfaces of a component
183      * @param component the component to introspect
184      * @return the array of its internal interfaces
185      */

186     public static Interface[] getIntItf (final Component component){
187         try {
188             ContentController cc = (ContentController)component.getFcInterface("content-controller");
189             Object JavaDoc[] listIntItfObj = cc.getFcInternalInterfaces();
190             Interface[] listIntItf = new Interface[listIntItfObj.length];
191             for (int i=0 ; i<listIntItfObj.length ; i++){
192                 listIntItf[i] = (Interface)listIntItfObj[i];
193             }
194             return listIntItf;
195         } catch (NoSuchInterfaceException e) {
196             System.out.println("No internal interfaces");
197             return null;
198         }
199     }
200     
201     /**
202      * Provides the name of an interface, summary name if it's a control interface ("content-controller" ==> "CC")
203      * @param itf the interface
204      * @return the name of the interface
205      */

206     public static String JavaDoc getInterfaceName(final Interface itf){
207         if (itf==null) return "unknown";
208         else if (isControlInterface(itf)) {
209             if (itf.getFcItfName().equals("component")) return "C";
210             else if (itf.getFcItfName().equals("factory")) return "F";
211             return (itf.getFcItfName().substring(0,1).toUpperCase() + "C");
212         }
213         return itf.getFcItfName();
214     }
215     
216     /**
217      * @param itf an interface
218      * @return true if the interface is a client interface
219      */

220     public static boolean isClientInterface (final Interface itf){
221         InterfaceType itType = (InterfaceType)itf.getFcItfType();
222         return itType.isFcClientItf();
223     }
224     
225     /**
226      * @param itf an interface
227      * @return true if itf is a control interface
228      */

229     public static boolean isControlInterface (final Interface itf){
230         return (itf.getFcItfName().endsWith("-controller"))||(itf.getFcItfName()=="component")||(itf.getFcItfName()=="factory");
231     }
232     
233     /**
234      * @param itf a client interface
235      * @return true if the client interface is a collection interface
236      */

237     public static boolean isCollectionInterface(final Interface itf){
238         InterfaceType itType = (InterfaceType)itf.getFcItfType();
239         return itType.isFcCollectionItf();
240     }
241     
242     /**
243      * @param itf a client interface
244      * @return true if the client interface is an optional interface
245      */

246     public static boolean isOptional(final Interface itf){
247         InterfaceType itType = (InterfaceType)itf.getFcItfType();
248         return itType.isFcOptionalItf();
249     }
250     
251     /**
252      * Provides an ArrayList of the (InterfaceType) of the collection interfaces
253      * Each collection will appear only one time in the ArrayList in order to represent a port per collection
254      * and not a port per client interface
255      * @param component the component to introspect
256      * @return the ArrayList of the (InterfaceType) of its collection interfaces
257      */

258     public static List JavaDoc getItfCollection (final Component component){
259         Interface[] listExtItf = getSortExtItf(component);
260         ArrayList JavaDoc listItfCollection = new ArrayList JavaDoc();
261         ArrayList JavaDoc listNameCollection = new ArrayList JavaDoc();
262         InterfaceType itType;
263         String JavaDoc nameItf;
264         for(int i=0 ; i<listExtItf.length ; i++){
265             if (isCollectionInterface(listExtItf[i])){
266                 itType = (InterfaceType)listExtItf[i].getFcItfType();
267                 nameItf = itType.getFcItfName();
268                 //check if the collection already exists - it must appear only once
269
if(!listNameCollection.contains(nameItf)){
270                     listNameCollection.add(nameItf);
271                     listItfCollection.add(itType);
272                 }
273             }
274         }
275         return listItfCollection;
276     }
277     
278     /**
279      * Provides the type of the port associated to an interface : client/collection-client/server/control
280      * @param itf the interface
281      * @return the type of the port associated
282      */

283     public static String JavaDoc getPortType (final Interface itf){
284         if (isClientInterface(itf)) {
285             if (isCollectionInterface(itf)) return PortType.COLLECTION_PORT;
286             else return PortType.CLIENT_PORT;
287         }
288         else if (isControlInterface(itf)) return PortType.CONTROLLER_PORT;
289         else return PortType.SERVER_PORT;
290     }
291     
292     /**
293      * Provides the signature of the interface
294      * @param itf the interface
295      * @return the signature of the interface
296      */

297     public static String JavaDoc getSignature(final Interface itf){
298         InterfaceType itType = (InterfaceType)itf.getFcItfType();
299         return itType.getFcItfSignature();
300     }
301     
302     /**
303      * Provides the destination component
304      * @param component the component "server" of the bind
305      * @param itf the client interface which is bound
306      * @return the component "client" of the bind
307      */

308     public static Component getTargetComponent (final Component component, final Interface itf){
309         try {
310             BindingController bc = (BindingController)component.getFcInterface("binding-controller");
311             Interface destItf = (Interface)bc.lookupFc(itf.getFcItfName());
312             if (destItf!=null)
313                 return destItf.getFcItfOwner();
314             else return null;
315         } catch (NoSuchInterfaceException e) {
316             return null;
317         }
318     }
319     
320     /**
321      * Provides the the destination interface
322      * @param component the component "server" of the bind
323      * @param itf the client interface which is bound
324      * @return the interface "client" of the bind
325      */

326     public static Interface getTargetInterface (final Component component, final Interface itf){
327         try {
328             BindingController bc = (BindingController)component.getFcInterface("binding-controller");
329             return (Interface)bc.lookupFc(itf.getFcItfName());
330         } catch (NoSuchInterfaceException e) {
331             return null;
332         }
333     }
334     
335     /**
336      * Provides a List of the neighbors 'server' components of a primitive component
337      * @param primitiveComponent the primitive component
338      * @return the list of its server neighbors
339      */

340     public static List JavaDoc getServerNeighbors(final Component primitiveComponent){
341         
342         //we don't draw the neighbors of a shared component because it appears in several composite components
343
if (isSharedComponent(primitiveComponent)) return new ArrayList JavaDoc();
344             
345         try {
346             SuperController sc = (SuperController)primitiveComponent.getFcInterface("super-controller");
347             Component[] listSuperComponents = sc.getFcSuperComponents();
348             Component superComponent = listSuperComponents[0];
349             Component[] listSubComponents = getSubComponents(superComponent);
350             Component subComponent;
351             Interface[] listExtItf;
352             Component targetComponent;
353             ArrayList JavaDoc neighborsList = new ArrayList JavaDoc();
354             //we put in the list the neighbors 'servers' of the primitiveComponent :
355
//we have to introspect each sub-component and look its client interfaces
356
for (int i=0 ; i<listSubComponents.length ; i++){
357                 subComponent = listSubComponents[i];
358                 listExtItf = getSortExtItf(subComponent);
359                 for (int j=0 ; j<listExtItf.length ; j++){
360                     if(isClientInterface(listExtItf[j])){
361                         targetComponent = getTargetComponent(subComponent, listExtItf[j]);
362                         if((targetComponent.equals(primitiveComponent))&&(!neighborsList.contains(subComponent)))
363                             neighborsList.add(subComponent);
364                     }
365                 }
366             }
367             return neighborsList;
368         } catch (NoSuchInterfaceException e) {
369             return new ArrayList JavaDoc();
370         }
371     }
372     
373     /**
374      * Provides a List of the neighbors 'client' components of a primitive component
375      * @param primitiveComponent the primitive component
376      * @return the list of its client neighbors
377      */

378     public static List JavaDoc getClientNeighbors(final Component primitiveComponent){
379         
380         //we don't draw the neighbors of a shared component because it appears in several composite components
381
if (isSharedComponent(primitiveComponent)) return new ArrayList JavaDoc();
382         
383         Interface[] listExtItf;
384         Component targetComponent;
385         ArrayList JavaDoc neighborsList = new ArrayList JavaDoc();
386         listExtItf = getSortExtItf(primitiveComponent);
387         for (int i=0 ; i<listExtItf.length ; i++){
388             if(isClientInterface(listExtItf[i])){
389                 targetComponent = getTargetComponent(primitiveComponent, listExtItf[i]);
390                 if((!neighborsList.contains(targetComponent))&&
391                    (!GraphInformations.getTargetInterface(primitiveComponent, listExtItf[i]).isFcInternalItf()))
392                         neighborsList.add(targetComponent);
393             }
394         }
395         return neighborsList;
396     }
397     
398     /**
399      * Sorts two interfaces by alphabetic order using their names.
400      *
401      * @version 0.1
402      */

403     public static class SortInterface implements Comparator JavaDoc {
404
405         protected int compare(Interface itf1, Interface itf2){
406             return itf1.getFcItfName().compareTo(itf2.getFcItfName());
407         }
408         
409         public int compare(Object JavaDoc o1, Object JavaDoc o2){
410             return compare((Interface)o1,(Interface)o2);
411         }
412
413     }
414     
415 }
416
Popular Tags