01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: */package org.objectweb.speedo.lib;
04:
05: import org.objectweb.fractal.api.Component;
06: import org.objectweb.fractal.api.NoSuchInterfaceException;
07: import org.objectweb.fractal.api.control.NameController;
08: import org.objectweb.fractal.util.Fractal;
09: import org.objectweb.util.monolog.api.BasicLevel;
10: import org.objectweb.util.monolog.api.Logger;
11:
12: import java.util.ArrayList;
13: import java.util.StringTokenizer;
14:
15: /**
16: * This class provides static method for helping the use of Fractal.
17: *
18: * @author S.Chassande-Barrioz
19: */
20: public class FractalHelper {
21:
22: /**
23: * Searches a sub component from a parent and a path in the architecture
24: * @param parent is the root composite component
25: * @param path is a dotted string representing a path in the architecture
26: * @param logger for the debug of the search
27: * @return the component if it has been found, otherwise null.
28: * @throws NoSuchInterfaceException when a component has no name (no
29: * NameController in fact)
30: */
31: public static Component getSubComponent(Component parent,
32: String path, Logger logger) throws NoSuchInterfaceException {
33: final boolean debug = logger != null
34: && logger.isLoggable(BasicLevel.DEBUG);
35: if (debug) {
36: logger.log(BasicLevel.DEBUG, "initial component: "
37: + Fractal.getNameController(parent).getFcName()
38: + ", path: " + path);
39: }
40: //run over the path
41: final StringTokenizer st = new StringTokenizer(path, ".", false);
42: Component res = parent;
43: while (st.hasMoreTokens()) {
44: String commponenentname = st.nextToken();
45: Component[] children = Fractal.getContentController(res)
46: .getFcSubComponents();
47: int i = 0;
48: boolean found = false;
49: //the list of sub component names (used for debug only)
50: final ArrayList subNames = (debug ? new ArrayList() : null);
51: while (!found && i < children.length) {
52: String subName = Fractal.getNameController(children[i])
53: .getFcName();
54: found = subName.equals(commponenentname);
55: if (!found) {
56: if (debug) {
57: subNames.add(subName);
58: }
59: i++;
60: } else {
61: res = children[i];
62: }
63: }
64: if (found) {
65: if (debug) {
66: logger.log(BasicLevel.DEBUG, "sub component '"
67: + commponenentname + "' found: " + res);
68: }
69: } else {
70: if (debug) {
71: logger.log(BasicLevel.DEBUG, "sub component '"
72: + commponenentname + "' not found (among: "
73: + subNames);
74: }
75: return null;
76: }
77: }
78: if (debug) {
79: logger.log(BasicLevel.DEBUG, "Path entirely found: " + res);
80: }
81: return res;
82: }
83: }
|