1 /*==================================================================== 2 3 Objectweb Browser 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): Romain Rouvoy. 23 Contributor(s): ______________________________________. 24 25 --------------------------------------------------------------------- 26 $Id: FcBrowser.java,v 1.1 2004/04/20 16:37:28 moroy Exp $ 27 ====================================================================*/ 28 29 package org.objectweb.util.browser.plugins.fractal; 30 31 import org.objectweb.fractal.api.Component; 32 import org.objectweb.fractal.api.Interface; 33 import org.objectweb.fractal.api.NoSuchInterfaceException; 34 import org.objectweb.fractal.api.control.AttributeController; 35 import org.objectweb.fractal.api.control.BindingController; 36 import org.objectweb.fractal.api.control.ContentController; 37 import org.objectweb.fractal.api.control.LifeCycleController; 38 import org.objectweb.fractal.api.control.NameController; 39 import org.objectweb.fractal.api.control.SuperController; 40 import org.objectweb.fractal.api.factory.Factory; 41 import org.objectweb.fractal.api.factory.GenericFactory; 42 import org.objectweb.fractal.api.factory.InstantiationException; 43 import org.objectweb.fractal.api.type.InterfaceType; 44 import org.objectweb.fractal.api.type.TypeFactory; 45 46 /** 47 * Utilities class for Fractal. 48 * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>, 49 * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a> 50 * @version 0.1 51 */ 52 public class FcBrowser 53 { 54 /** 55 * Provides the interface of the primitive component to which the itt is binded. 56 * @param its the indirect interface. 57 * @return the primitive interface. 58 */ 59 public static Interface getPrimitiveItf(final Interface itf) { 60 Component cpt = itf.getFcItfOwner(); 61 try { 62 BindingController bc = getBindingController(cpt); 63 Interface internal = (Interface) bc.lookupFc(itf.getFcItfName()); 64 return getPrimitiveItf(internal); 65 } catch (NoSuchInterfaceException ex) { 66 return itf; 67 } 68 } 69 70 /** 71 * Provides the primitive component that own the interface itf. 72 * @param itf the indirect interface. 73 * @return the primitive component. 74 */ 75 public static Component getPrimitiveCpt(final Interface itf) { 76 return getPrimitiveItf(itf).getFcItfOwner(); 77 } 78 79 /** 80 * Provides the value of the life cycle state associated to the interface. 81 * @param itf the interface to introspect. 82 * @return the state of the interface ("STARTED" if started, "STOPPED" else). 83 */ 84 public static String getLifeCycleState(final Interface itf) { 85 try { 86 LifeCycleController lc = getLifeCycleController(getPrimitiveCpt(itf)); 87 return lc.getFcState(); 88 } catch (NoSuchInterfaceException ex) { 89 return LifeCycleController.STARTED ; 90 } 91 } 92 93 /** 94 * Provides the reference to the component associated to this object. 95 * @param obj the base object reference. 96 * @return the associated component. 97 */ 98 public static Component getComponent(final Object obj) { 99 if (obj instanceof Component) 100 return (Component)obj; 101 else 102 return ((Interface)obj).getFcItfOwner(); 103 } 104 105 /** 106 * Provides the name of the object prefixed by the component name. 107 * @param obj the object to identify. 108 * @return the associated name. 109 */ 110 public static String getPrefixedName(final Object obj) { 111 if (obj instanceof Component) 112 return getName((Component)obj); 113 else 114 return getName(getComponent(obj))+"."+getName(obj); 115 } 116 117 /** 118 * Provides the name of an object. 119 * @param obj the object to identify. 120 * @return the associated name ('unknown' if no name). 121 */ 122 public static String getName(final Object obj) { 123 if (obj instanceof Component) 124 return getName((Component)obj); 125 else if (obj instanceof Interface) 126 return getName((Interface)obj); 127 else 128 return "unknown" ; 129 } 130 131 public static String getName(final InterfaceType itf) { 132 return itf.getFcItfName(); 133 } 134 135 /** 136 * Provides the name of an interface. 137 * @param itf the interface to identify. 138 * @return the name of the interface. 139 */ 140 public static String getName(final Interface itf) { 141 return itf.getFcItfName(); 142 } 143 144 /** 145 * Provides the name of a component. 146 * @param cpt the component to identify. 147 * @return the name of the component (the name of the associated 148 * interface if no NameController). 149 */ 150 public static String getName(final Component cpt) { 151 try { 152 NameController nc = getNameController(cpt); 153 if (nc != null) return nc.getFcName() ; 154 } catch (NoSuchInterfaceException e) { } 155 if (cpt instanceof Interface) 156 return getName((Interface)cpt); 157 else 158 return "unknown" ; 159 } 160 161 162 /** 163 * Returns a bootstrap component to create other components. This method 164 * just calls the corresponding method of the 165 * <tt>org.objectweb.fractal.api.Fractal</tt> class. 166 * 167 * @return a bootstrap component to create other components. 168 * @throws InstantiationException if the bootstrap component cannot be 169 * created. 170 */ 171 public static Component getBootstrapComponent () 172 throws InstantiationException 173 { 174 try { 175 return org.objectweb.fractal.api.Fractal.getBootstrapComponent(); 176 } catch(NullPointerException ex) { 177 throw new InstantiationException("client interface not bound"); 178 } 179 } 180 181 /** 182 * Returns the {@link AttributeController} interface of the given component. 183 * 184 * @param component a component. 185 * @return the {@link AttributeController} interface of the given component. 186 * @throws NoSuchInterfaceException if there is no such interface. 187 */ 188 public static AttributeController getAttributeController (final Component component) 189 throws NoSuchInterfaceException 190 { 191 try { 192 return (AttributeController)component.getFcInterface("attribute-controller"); 193 } catch(NullPointerException ex) { 194 throw new NoSuchInterfaceException("client interface not bound"); 195 } 196 } 197 198 /** 199 * Returns the {@link BindingController} interface of the given component. 200 * 201 * @param component a component. 202 * @return the {@link BindingController} interface of the given component. 203 * @throws NoSuchInterfaceException if there is no such interface. 204 */ 205 public static BindingController getBindingController (final Component component) 206 throws NoSuchInterfaceException 207 { 208 try { 209 return (BindingController)component.getFcInterface("binding-controller"); 210 } catch(NullPointerException ex){ 211 throw new NoSuchInterfaceException("client interface not bound"); 212 } 213 } 214 215 /** 216 * Returns the {@link ContentController} interface of the given component. 217 * 218 * @param component a component. 219 * @return the {@link ContentController} interface of the given component. 220 * @throws NoSuchInterfaceException if there is no such interface. 221 */ 222 public static ContentController getContentController (final Component component) 223 throws NoSuchInterfaceException 224 { 225 try { 226 return (ContentController)component.getFcInterface("content-controller"); 227 } catch(NullPointerException ex){ 228 throw new NoSuchInterfaceException("client interface not bound"); 229 } 230 } 231 232 /** 233 * Returns the {@link SuperController} interface of the given component. 234 * 235 * @param component a component. 236 * @return the {@link SuperController} interface of the given component. 237 * @throws NoSuchInterfaceException if there is no such interface. 238 */ 239 public static SuperController getSuperController (final Component component) 240 throws NoSuchInterfaceException 241 { 242 try { 243 return (SuperController)component.getFcInterface("super-controller"); 244 } catch(NullPointerException ex){ 245 throw new NoSuchInterfaceException("client interface not bound"); 246 } 247 } 248 249 /** 250 * Returns the {@link NameController} interface of the given component. 251 * 252 * @param component a component. 253 * @return the {@link NameController} interface of the given component. 254 * @throws NoSuchInterfaceException if there is no such interface. 255 */ 256 public static NameController getNameController (final Component component) 257 throws NoSuchInterfaceException 258 { 259 try { 260 return (NameController)component.getFcInterface("name-controller"); 261 } catch(NullPointerException ex){ 262 throw new NoSuchInterfaceException("client interface not bound"); 263 } 264 } 265 266 /** 267 * Returns the {@link LifeCycleController} interface of the given component. 268 * 269 * @param component a component. 270 * @return the {@link LifeCycleController} interface of the given component. 271 * @throws NoSuchInterfaceException if there is no such interface. 272 */ 273 public static LifeCycleController getLifeCycleController ( 274 final Component component) throws NoSuchInterfaceException 275 { 276 try { 277 return (LifeCycleController)component.getFcInterface("lifecycle-controller"); 278 } catch(NullPointerException ex){ 279 throw new NoSuchInterfaceException("client interface not bound"); 280 } 281 } 282 283 /** 284 * Returns the {@link Factory} interface of the given component. 285 * 286 * @param component a component. 287 * @return the {@link Factory} interface of the given component. 288 * @throws NoSuchInterfaceException if there is no such interface. 289 */ 290 public static Factory getFactory (final Component component) 291 throws NoSuchInterfaceException 292 { 293 try { 294 return (Factory)component.getFcInterface("factory"); 295 } catch(NullPointerException ex){ 296 throw new NoSuchInterfaceException("client interface not bound"); 297 } 298 } 299 300 /** 301 * Returns the {@link GenericFactory} interface of the given component. 302 * 303 * @param component a component. 304 * @return the {@link GenericFactory} interface of the given component. 305 * @throws NoSuchInterfaceException if there is no such interface. 306 */ 307 public static GenericFactory getGenericFactory (final Component component) 308 throws NoSuchInterfaceException 309 { 310 try { 311 return (GenericFactory)component.getFcInterface("generic-factory"); 312 } catch(NullPointerException ex) { 313 throw new NoSuchInterfaceException("client interface not bound"); 314 } 315 } 316 317 /** 318 * Returns the {@link TypeFactory} interface of the given component. 319 * 320 * @param component a component. 321 * @return the {@link TypeFactory} interface of the given component. 322 * @throws NoSuchInterfaceException if there is no such interface. 323 */ 324 public static TypeFactory getTypeFactory (final Component component) 325 throws NoSuchInterfaceException 326 { 327 try { 328 return (TypeFactory)component.getFcInterface("type-factory"); 329 }catch(NullPointerException ex){ 330 throw new NoSuchInterfaceException("client interface not bound"); 331 } 332 } 333 }