KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > Reference


1 /* $Id: Reference.java,v 1.3 2004/05/20 14:23:50 bures Exp $ */
2 package SOFA.Connector;
3
4 import java.util.StringTokenizer JavaDoc;
5 import java.util.Vector JavaDoc;
6
7 import SOFA.Connector.ECG.Profiles.CORBAProfile;
8 import SOFA.Connector.ECG.Profiles.LocalProfile;
9 import SOFA.Connector.ECG.Profiles.RMIProfile;
10
11 /** Connector reference.
12   */

13 public final class Reference implements java.io.Serializable JavaDoc, cz.cuni.sofa.lib.Streamable {
14   
15   /** Type of the connector - CSProcCall, EventPassing, DataStream */
16   public String JavaDoc conn_type;
17   /** Profiles */
18   public TaggedProfile[] profiles;
19
20   public Reference () {
21   }
22
23   public Reference (final String JavaDoc conn_type, final TaggedProfile[] profiles) {
24     this.conn_type = conn_type;
25     this.profiles = profiles;
26   }
27
28   public void _write(cz.cuni.sofa.lib.OutputStream os) throws java.io.IOException JavaDoc {
29     os.write_string(conn_type);
30     int len = 0;
31     for (int i=0; i<profiles.length; i++) {
32       if (profiles[i] instanceof RMIProfile || profiles[i] instanceof CORBAProfile) {
33         len++;
34       }
35     }
36     os.write_long(len);
37     for (int i=0; i<profiles.length; i++) {
38       if (profiles[i] instanceof RMIProfile) {
39         os.write_string(profiles[i].tag);
40         os.write_string( ((RMIProfile) profiles[i]).URL);
41       }
42       if (profiles[i] instanceof CORBAProfile) {
43         os.write_string(profiles[i].tag);
44         os.write_string( ((CORBAProfile) profiles[i]).IOR);
45       }
46     }
47   }
48   
49   public void _read(cz.cuni.sofa.lib.InputStream is) throws java.io.IOException JavaDoc {
50     conn_type = is.read_string();
51     int len = is.read_long();
52     profiles = new TaggedProfile [len];
53     for (int i=0; i<len; i++) {
54       String JavaDoc tag = is.read_string();
55       if (tag.compareTo("RMI")==0) {
56         profiles[i] = new RMIProfile(is.read_string());
57       }
58       if (tag.compareTo("CORBA")==0) {
59         profiles[i] = new CORBAProfile(is.read_string());
60       }
61     }
62   }
63
64   public String JavaDoc toString() {
65     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(conn_type);
66     sb.append("-");
67     for (int i=0; i<profiles.length; i++) {
68       if (profiles[i] instanceof RMIProfile) {
69         sb.append(profiles[i].tag);
70         sb.append(";");
71         sb.append( ((RMIProfile) profiles[i]).URL);
72         sb.append("-");
73       }
74       if (profiles[i] instanceof CORBAProfile) {
75         sb.append(profiles[i].tag);
76         sb.append("x");
77         sb.append( ((CORBAProfile) profiles[i]).IOR);
78         sb.append("-");
79       }
80       if (profiles[i] instanceof LocalProfile) {
81         sb.append(profiles[i].tag);
82         sb.append("-");
83       }
84
85     }
86     return sb.toString();
87   }
88
89   /**
90    * Reconstructs a reference from a string previously created by the {@link #toString()} method.
91    * Added by PP.
92    * @param s a string describing the reference in a defined format.
93    * @return the reference being described by the string.
94    */

95   public static Reference fromString (String JavaDoc s) {
96     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, "-");
97     String JavaDoc type = st.nextToken();
98     Vector JavaDoc profiles = new Vector JavaDoc();
99
100     while (st.hasMoreTokens()) {
101       String JavaDoc token = st.nextToken();
102       if (token.equals("LOCAL")) {
103         profiles.add(new LocalProfile());
104         continue;
105       }
106       if (token.startsWith("RMI")) {
107         String JavaDoc url = token.substring(4); // 4 == "RMI;".length(), see #toString()
108
profiles.add(new RMIProfile(url));
109         continue;
110       }
111       if (token.startsWith("CORBA")) {
112         String JavaDoc ior = token.substring(6); // 6 == "CORBAx".length(), see #toString()
113
profiles.add(new CORBAProfile(ior));
114         continue;
115       }
116     }
117     return new Reference(type, (TaggedProfile [])profiles.toArray(new TaggedProfile[profiles.size()]));
118   }
119
120 }
121
Popular Tags