KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > TIR > Access > TIRAccessMethods


1 /* $Id: TIRAccessMethods.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Access;
3
4
5 import java.rmi.RemoteException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import SOFA.SOFAnode.Made.TIR.ArrayDef;
10 import SOFA.SOFAnode.Made.TIR.CDLType;
11 import SOFA.SOFAnode.Made.TIR.Contained;
12 import SOFA.SOFAnode.Made.TIR.Container;
13 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
14 import SOFA.SOFAnode.Made.TIR.Identification;
15 import SOFA.SOFAnode.Made.TIR.InterfaceDef;
16 import SOFA.SOFAnode.Made.TIR.ProvideDef;
17 import SOFA.SOFAnode.Made.TIR.Repository;
18 import SOFA.SOFAnode.Made.TIR.RequireDef;
19 import SOFA.SOFAnode.Made.TIR.SequenceDef;
20 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
21 import SOFA.SOFAnode.Made.TIR.TypedefDef;
22
23 /** Some methods for simple access to TIR.
24   *
25   * @author Petr Hnetynka
26   */

27 public class TIRAccessMethods {
28
29   /** Returns reference to TIR. This method use java properties sofa.rmiport and sofa.rmihost.
30     *
31     * @return reference to TIR
32     * @throws java.net.MalformedURLException bad url
33     * @throws java.rmi.NotBoundException repository does not exists
34     * @throws java.rmi.RemoteException rmote exception
35     */

36   public static Repository getRepository() throws java.net.MalformedURLException JavaDoc, java.rmi.NotBoundException JavaDoc, java.rmi.RemoteException JavaDoc {
37     String JavaDoc rmiport = System.getProperty("sofa.rmiport","1099");
38     String JavaDoc rmihost = System.getProperty("sofa.rmihost","localhost");
39     return (Repository) java.rmi.Naming.lookup("//"+rmihost+":"+rmiport+"/Repository");
40   }
41   
42   /** Search repository object.
43     * @param rep repository
44     * @param lang defining language of object (currently only <tt>cdl</tt> is possible)
45     * @param name full name without version (e.g. ::cz::cuni::sofa::demo)
46     * @param version version of object
47     * @exception RemoteException remote exception
48     * @exception TIRExceptLock TIR is currently locked
49     */

50   public static Contained lookupContained(Repository rep, String JavaDoc lang, String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
51     ArrayList JavaDoc nm = new ArrayList JavaDoc();
52     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(name,"::");
53     while (st.hasMoreTokens()) {
54       nm.add(st.nextToken());
55     }
56
57     Contained[] cnt;
58     Container cont;
59     Contained next = null;
60     next = rep.lookup(lang,"");
61     if (next == null) {
62       return null;
63     }
64     cont = (Container) next;
65     int i;
66     for (i=0; i<nm.size()-1; i++) {
67       cnt = cont.lookup_name((String JavaDoc) nm.get(i));
68       if (cnt == null || cnt.length == 0) {
69         return null;
70       }
71       if (cnt.length == 1) {
72         next = cnt[0];
73       } else {
74         next = cont.lookup((String JavaDoc) nm.get(i), version);
75         if (next == null) {
76           return null;
77         }
78       }
79       if (!(next instanceof Container)) {
80         return null;
81       }
82       cont = (Container) next;
83     }
84     return cont.lookup((String JavaDoc) nm.get(i), version);
85   }
86   
87   /** Search repository object defined in CDL.
88     * @param rep repository
89     * @param name full name without version (e.g. ::cz::cuni::sofa::demo)
90     * @param version version of object
91     * @exception RemoteException remote exception
92     * @exception TIRExceptLock TIR is currently locked
93     */

94   public static Contained lookupCDLContained(Repository rep, String JavaDoc name, String JavaDoc version) throws RemoteException JavaDoc, TIRExceptLock {
95     return lookupContained(rep, "cdl", name, version);
96   }
97   
98   /** Search repository object defined in CDL.
99     * @param rep repository
100     * @param fullName full name with version (e.g. ::cz::cuni::sofa::demo?myhost!1)
101     * @exception RemoteException remote exception
102     * @exception TIRExceptLock TIR is currently locked
103     */

104   public static Contained lookupCDLContained(Repository rep, String JavaDoc fullName) throws RemoteException JavaDoc, TIRExceptLock {
105     int n = fullName.indexOf('?');
106     if (n != 0) {
107       return lookupContained(rep, "cdl", fullName.substring(0,n), fullName.substring(n+1,fullName.length()));
108     } else
109       return null;
110   }
111
112   /** Return all operations and attributes (also inherited) of interface.
113     * @param rep repository
114     * @param fullName full name with version of interface (e.g. ::cz::cuni::sofa::demo?myhost!1)
115     * @exception RemoteException remote exception
116     * @exception TIRExceptLock TIR is currently locked
117     */

118   public static Contained[] getOperAttrOfInterface(Repository rep, String JavaDoc fullName) throws RemoteException JavaDoc, TIRExceptLock {
119     Contained cnt = lookupCDLContained(rep, fullName);
120     if (cnt!=null && cnt.get_def_kind().value() == DefinitionKind.dk_Interface)
121       return getOperAttrOfInterface((InterfaceDef) cnt);
122     else
123       return null;
124   }
125   
126   /** Return all operations and attributes (also inherited) of interface.
127     * @param iface interface
128     * @exception RemoteException remote exception
129     * @exception TIRExceptLock TIR is currently locked
130     */

131   public static Contained[] getOperAttrOfInterface(InterfaceDef iface) throws RemoteException JavaDoc, TIRExceptLock {
132     ArrayList JavaDoc all = new ArrayList JavaDoc();
133     ArrayList JavaDoc scanned = new ArrayList JavaDoc();
134     Contained[] cnt = iface.contents(null);
135     for (int i=0; i<cnt.length; i++) {
136       int dk = cnt[i].get_def_kind().value();
137       if ( dk == DefinitionKind.dk_Attribute || dk == DefinitionKind.dk_Operation) {
138         all.add(cnt[i]);
139       }
140     }
141     scanned.add(iface);
142     
143     scanBase(all, scanned, iface);
144
145     Contained[] ret = new Contained [all.size()];
146     for (int i=0; i<ret.length; i++) {
147       ret[i] = (Contained) all.get(i);
148     }
149     return ret;
150   }
151
152   private static void scanBase(ArrayList JavaDoc all, ArrayList JavaDoc scanned, InterfaceDef iface) throws RemoteException JavaDoc, TIRExceptLock {
153     InterfaceDef[] base = iface.base_interfaces();
154     for (int i=0; i< base.length; i++) {
155       if (! isInScanned(scanned, base[i])) {
156         Contained[] cnt = base[i].contents(null);
157         for (int j=0; j<cnt.length; j++) {
158           int dk = cnt[j].get_def_kind().value();
159           if ( dk == DefinitionKind.dk_Attribute || dk == DefinitionKind.dk_Operation) {
160             all.add(cnt[j]);
161           }
162         }
163         scanned.add(base[i]);
164         scanBase(all, scanned, base[i]);
165       }
166     }
167   }
168
169   private static boolean isInScanned(ArrayList JavaDoc scanned, InterfaceDef iface) throws RemoteException JavaDoc {
170     Identification id = iface.get_identification();
171     for (int i=0; i<scanned.size(); i++) {
172       if (((InterfaceDef) scanned.get(i)).get_identification().is_equal(id))
173         return true;
174     }
175     return false;
176   }
177
178   /** Find interface of provision. It goes through typedefs and arrays.
179     *
180     * @param prov scanned provision
181     * @return provided interface
182     * @throws RemoteException remote exception
183     * @throws TIRExceptLock TIR is currently locked
184     */

185   public static InterfaceDef getIfaceOfProvides(ProvideDef prov) throws RemoteException JavaDoc, TIRExceptLock {
186     CDLType type = prov.provide();
187     while (!(type instanceof InterfaceDef)) {
188       int kind = type.get_def_kind().value();
189       switch (kind) {
190         case DefinitionKind.dk_Typedef:
191           type = ((TypedefDef) type).original_type();
192           break;
193         case DefinitionKind.dk_Array:
194           type = ((ArrayDef) type).element_type();
195           break;
196         case DefinitionKind.dk_Sequence:
197           type = ((SequenceDef) type).element_type();
198           break;
199       }
200     }
201     return (InterfaceDef) type;
202   }
203
204   /** Scan provision for arrays/sequences
205     *
206     * @param prov scanned provision
207     * @return true if provision contains arrays/sequences
208     * @throws RemoteException remote exception
209     * @throws TIRExceptLock TIR is currently locked
210     */

211   public static boolean containProvArrays(ProvideDef prov) throws RemoteException JavaDoc, TIRExceptLock {
212     CDLType type = prov.provide();
213     while (!(type instanceof InterfaceDef)) {
214       int kind = type.get_def_kind().value();
215       switch (kind) {
216         case DefinitionKind.dk_Typedef:
217           type = ((TypedefDef) type).original_type();
218           break;
219         case DefinitionKind.dk_Array:
220           return true;
221         case DefinitionKind.dk_Sequence:
222           return true;
223       }
224     }
225     return false;
226   }
227
228
229   /** Find interface of reqguirement. It goes through typedefs and arrays.
230     *
231     * @param prov scanned requirement
232     * @return required interface
233     * @throws RemoteException remote exception
234     * @throws TIRExceptLock TIR is currently locked
235     */

236   public static InterfaceDef getIfaceOfRequires(RequireDef prov) throws RemoteException JavaDoc, TIRExceptLock {
237     CDLType type = prov.require();
238     while (!(type instanceof InterfaceDef)) {
239       int kind = type.get_def_kind().value();
240       switch (kind) {
241         case DefinitionKind.dk_Typedef:
242           type = ((TypedefDef) type).original_type();
243           break;
244         case DefinitionKind.dk_Array:
245           type = ((ArrayDef) type).element_type();
246           break;
247         case DefinitionKind.dk_Sequence:
248           type = ((SequenceDef) type).element_type();
249           break;
250       }
251     }
252     return (InterfaceDef) type;
253   }
254
255   /** Scan requirement for arrays/sequences
256     *
257     * @param prov scanned requirement
258     * @return true if requirement contains arrays/sequences
259     * @throws RemoteException remote exception
260     * @throws TIRExceptLock TIR is currently locked
261     */

262   public static boolean containReqArrays(RequireDef prov) throws RemoteException JavaDoc, TIRExceptLock {
263     CDLType type = prov.require();
264     while (!(type instanceof InterfaceDef)) {
265       int kind = type.get_def_kind().value();
266       switch (kind) {
267         case DefinitionKind.dk_Typedef:
268           type = ((TypedefDef) type).original_type();
269           break;
270         case DefinitionKind.dk_Array:
271           return true;
272         case DefinitionKind.dk_Sequence:
273           return true;
274       }
275     }
276     return false;
277   }
278
279 /*
280   public static void main(String[] argv) {
281     try {
282       System.setSecurityManager(new java.rmi.RMISecurityManager());
283     
284       String rmiport = System.getProperty("tir.rmiport","1099");
285       String rmihost = System.getProperty("tir.rmihost","localhost");
286       Repository obj = (Repository) java.rmi.Naming.lookup("//"+rmihost+":"+rmiport+"/Repository");
287       Contained[] cnt = getOperAttrOfInterface(obj, argv[0]);
288       for (int i=0; i<cnt.length; i++) {
289         System.out.println(cnt[i].get_identification().absolute_name().name());
290       }
291     } catch (Exception e) {
292       System.out.println("Exception: " + e.getMessage());
293       e.printStackTrace();
294     }
295   }*/

296 }
297
Popular Tags