1 26 package org.objectweb.fractal.explorer.graph; 27 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 import java.util.HashMap ; 32 import java.util.List ; 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 55 56 public class GraphInformations { 57 58 59 64 public static Component getRootComponent (final String nameAppli){ 65 try { 66 Factory f = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND); 67 Object o = f.newComponent(nameAppli, new HashMap ()); 68 return (Component)o; 69 } catch (ADLException e) { 70 System.out.println("Application " + nameAppli + " not found"); 71 return null; 72 } 73 } 74 75 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 95 public static String 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 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 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 144 public static String 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 157 public static boolean isStarted (final Component component){ 158 return getComponentState(component).equals("STARTED"); 159 } 160 161 167 public static Interface[] getSortExtItf(final Component component){ 168 Object [] listExtItfObj = component.getFcInterfaces(); 169 Interface[] listItfExtItf = new Interface[listExtItfObj.length]; 170 List listExtItf = new ArrayList (); 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 186 public static Interface[] getIntItf (final Component component){ 187 try { 188 ContentController cc = (ContentController)component.getFcInterface("content-controller"); 189 Object [] 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 206 public static String 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 220 public static boolean isClientInterface (final Interface itf){ 221 InterfaceType itType = (InterfaceType)itf.getFcItfType(); 222 return itType.isFcClientItf(); 223 } 224 225 229 public static boolean isControlInterface (final Interface itf){ 230 return (itf.getFcItfName().endsWith("-controller"))||(itf.getFcItfName()=="component")||(itf.getFcItfName()=="factory"); 231 } 232 233 237 public static boolean isCollectionInterface(final Interface itf){ 238 InterfaceType itType = (InterfaceType)itf.getFcItfType(); 239 return itType.isFcCollectionItf(); 240 } 241 242 246 public static boolean isOptional(final Interface itf){ 247 InterfaceType itType = (InterfaceType)itf.getFcItfType(); 248 return itType.isFcOptionalItf(); 249 } 250 251 258 public static List getItfCollection (final Component component){ 259 Interface[] listExtItf = getSortExtItf(component); 260 ArrayList listItfCollection = new ArrayList (); 261 ArrayList listNameCollection = new ArrayList (); 262 InterfaceType itType; 263 String 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 if(!listNameCollection.contains(nameItf)){ 270 listNameCollection.add(nameItf); 271 listItfCollection.add(itType); 272 } 273 } 274 } 275 return listItfCollection; 276 } 277 278 283 public static String 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 297 public static String getSignature(final Interface itf){ 298 InterfaceType itType = (InterfaceType)itf.getFcItfType(); 299 return itType.getFcItfSignature(); 300 } 301 302 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 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 340 public static List getServerNeighbors(final Component primitiveComponent){ 341 342 if (isSharedComponent(primitiveComponent)) return new ArrayList (); 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 neighborsList = new ArrayList (); 354 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 (); 370 } 371 } 372 373 378 public static List getClientNeighbors(final Component primitiveComponent){ 379 380 if (isSharedComponent(primitiveComponent)) return new ArrayList (); 382 383 Interface[] listExtItf; 384 Component targetComponent; 385 ArrayList neighborsList = new ArrayList (); 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 403 public static class SortInterface implements Comparator { 404 405 protected int compare(Interface itf1, Interface itf2){ 406 return itf1.getFcItfName().compareTo(itf2.getFcItfName()); 407 } 408 409 public int compare(Object o1, Object o2){ 410 return compare((Interface)o1,(Interface)o2); 411 } 412 413 } 414 415 } 416 | Popular Tags |