KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > binding > Util


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia.control.binding;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.Interface;
28 import org.objectweb.fractal.api.NoSuchInterfaceException;
29 import org.objectweb.fractal.api.control.BindingController;
30 import org.objectweb.fractal.api.control.ContentController;
31 import org.objectweb.fractal.api.control.SuperController;
32 import org.objectweb.fractal.api.type.InterfaceType;
33
34 import java.util.ArrayList JavaDoc;
35 import java.util.HashSet JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Set JavaDoc;
38
39 /**
40  * Provides static utility methods related to {@link BindingController}.
41  */

42
43 public class Util {
44
45   // -------------------------------------------------------------------------
46
// Private constructor
47
// -------------------------------------------------------------------------
48

49   private Util () {
50   }
51
52   // -------------------------------------------------------------------------
53
// Static methods
54
// -------------------------------------------------------------------------
55

56   /**
57    * Returns the client interfaces that are bound to the given server interface.
58    *
59    * @param serverItf a server interface.
60    * @return the client interfaces that are bound to serverItf.
61    * @throws Exception if a problem occurs.
62    */

63
64   public static Set JavaDoc getFcClientItfsBoundTo (final Interface serverItf)
65     throws Exception JavaDoc
66   {
67     Set JavaDoc result = new HashSet JavaDoc();
68     Object JavaDoc[] comps = getFcPotentialClientsOf(serverItf).toArray();
69     for (int i = 0; i < comps.length; ++i) {
70       Component comp = (Component)comps[i];
71       List JavaDoc clientItfs = getFcClientItfsBoundTo(comp, serverItf);
72       for (int j = 0; j < clientItfs.size(); ++j) {
73         result.add(clientItfs.get(j));
74       }
75     }
76     return result;
77   }
78
79   /**
80    * Returns the potential client components of the given server interface.
81    *
82    * @param serverItf a server interface.
83    * @return the potential client components of the given server interface.
84    * @throws Exception if a problem occurs.
85    */

86
87   public static Set JavaDoc getFcPotentialClientsOf (final Interface serverItf)
88     throws Exception JavaDoc
89   {
90     Set JavaDoc compSet = new HashSet JavaDoc();
91     Component serverComp = serverItf.getFcItfOwner();
92     if (serverItf.isFcInternalItf()) {
93       ContentController cc;
94       try {
95         cc = (ContentController)serverComp.getFcInterface("content-controller");
96       } catch (NoSuchInterfaceException e) {
97         throw new Exception JavaDoc("Cannot create shortcuts");
98       }
99       Component[] comps = cc.getFcSubComponents();
100       for (int i = 0; i < comps.length; ++i) {
101         compSet.add(comps[i]);
102       }
103       compSet.add(serverComp);
104     } else {
105       SuperController sc;
106       try {
107         sc = (SuperController)serverComp.getFcInterface("super-controller");
108       } catch (NoSuchInterfaceException e) {
109         throw new Exception JavaDoc("Cannot create shortcuts");
110       }
111       Component[] superComps = sc.getFcSuperComponents();
112       for (int i = 0; i < superComps.length; ++i) {
113         compSet.add(superComps[i]);
114         ContentController cc;
115         try {
116           cc = (ContentController)superComps[i].
117             getFcInterface("content-controller");
118         } catch (NoSuchInterfaceException e) {
119           throw new Exception JavaDoc("Cannot create shortcuts");
120         }
121         Component[] subComps = cc.getFcSubComponents();
122         for (int j = 0; j < subComps.length; ++j) {
123           compSet.add(subComps[j]);
124         }
125       }
126     }
127     return compSet;
128   }
129
130   /**
131    * Returns the client interfaces of the given component that are bound to
132    * the given server interface.
133    *
134    * @param component a component.
135    * @param serverItf a server interface.
136    * @return the client interfaces of <tt>component</tt> that are bound to
137    * <tt>serverItf</tt>.
138    * @throws Exception if a problem occurs.
139    */

140
141   public static List JavaDoc getFcClientItfsBoundTo (
142     final Component component,
143     final Interface serverItf) throws Exception JavaDoc
144   {
145     List JavaDoc itfList = new ArrayList JavaDoc();
146     BindingController bc;
147     try {
148       bc = (BindingController)component.getFcInterface("binding-controller");
149     } catch (NoSuchInterfaceException e) {
150       return itfList;
151     }
152
153     // search in external client interfaces
154
Object JavaDoc[] itfs = component.getFcInterfaces();
155     for (int i = 0; i < itfs.length; ++i) {
156       Interface itf;
157       InterfaceType itfType;
158       try {
159         itf = (Interface)itfs[i];
160         itfType = (InterfaceType)itf.getFcItfType();
161       } catch (ClassCastException JavaDoc e) {
162         throw new Exception JavaDoc("Cannot create shortcuts");
163       }
164       if (!itfType.isFcClientItf()) {
165         continue;
166       }
167       Object JavaDoc sItf;
168       try {
169         sItf = bc.lookupFc(itf.getFcItfName());
170       } catch (NoSuchInterfaceException e) {
171         continue;
172       }
173       if (serverItf.equals(sItf)) {
174         itfList.add(itf);
175       }
176     }
177
178     ContentController cc;
179     try {
180       cc = (ContentController)component.getFcInterface("content-controller");
181     } catch (NoSuchInterfaceException e) {
182       return itfList;
183     }
184
185     // search in internal client interfaces
186
itfs = cc.getFcInternalInterfaces();
187     for (int i = 0; i < itfs.length; ++i) {
188       Interface itf;
189       InterfaceType itfType;
190       try {
191         itf = (Interface)itfs[i];
192         itfType = (InterfaceType)itf.getFcItfType();
193       } catch (ClassCastException JavaDoc e) {
194         throw new Exception JavaDoc("Cannot create shortcuts");
195       }
196       if (!itfType.isFcClientItf()) {
197         continue;
198       }
199       Object JavaDoc sItf;
200       try {
201         sItf = bc.lookupFc(itf.getFcItfName());
202       } catch (NoSuchInterfaceException e) {
203         continue;
204       }
205       if (serverItf.equals(sItf)) {
206         itfList.add(itf);
207       }
208     }
209     return itfList;
210   }
211 }
212
Popular Tags