KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ist > coach > coachEmfCommon > Utils


1 package ist.coach.coachEmfCommon;
2
3 import org.omg.CosNaming.NameComponent JavaDoc;
4
5 public class Utils {
6
7     private static org.omg.CORBA.ORB JavaDoc orb = org.objectweb.ccm.CORBA.TheORB.getORB();
8
9     public static NameComponent JavaDoc[] string2name(String JavaDoc name_s) {
10
11         java.util.StringTokenizer JavaDoc depth_st, kind_st;
12         depth_st = new java.util.StringTokenizer JavaDoc(name_s, "/");
13
14         java.util.Vector JavaDoc name_seq_v = new java.util.Vector JavaDoc();
15         String JavaDoc token_str = null, token_name = null, token_kind = null;
16         for(int pos = 0; depth_st.hasMoreTokens(); pos++) {
17
18             token_str = depth_st.nextToken();
19             kind_st = new java.util.StringTokenizer JavaDoc(token_str, ".");
20
21             if (token_str.startsWith(".")) {
22                 token_name = new String JavaDoc();
23                 token_kind = kind_st.nextToken();
24             }
25             else {
26                 if (kind_st.hasMoreTokens())
27                     token_name = kind_st.nextToken();
28                 else
29                     token_name = new String JavaDoc();
30
31                 if (kind_st.hasMoreTokens())
32                     token_kind = kind_st.nextToken();
33                 else
34                     token_kind = new String JavaDoc();
35             }
36
37             name_seq_v.add(new NameComponent JavaDoc(token_name, token_kind));
38             //name_seq[pos] = new NameComponent(token_name, token_kind);
39
}
40
41         NameComponent JavaDoc[] name_seq = new NameComponent JavaDoc[name_seq_v.size()];
42         name_seq_v.copyInto(name_seq);
43         return name_seq;
44     }
45     public static String JavaDoc name2string(NameComponent JavaDoc[] name) {
46
47         String JavaDoc name_str = new String JavaDoc();
48         for (int i = 0; i < name.length; i++) {
49             name_str = name_str.concat(name[i].id + "." + name[i].kind);
50             if (i < name.length -1 ) {
51                 name_str = name_str.concat("/");
52             }
53         }
54         return name_str;
55     }
56
57     /* The kind represents the object type and is located in the name as
58      * the kind of the before last name component. Note that last contains
59      * facade id information.
60      */

61     public static String JavaDoc name2kind(String JavaDoc name) {
62
63         int delimEnd = name.lastIndexOf("/");
64         if (delimEnd < 0) {
65             //return new String();
66
//This is assumed to be a local root object
67
return new String JavaDoc("Organization");
68         }
69
70         int delimStart = name.substring(0, delimEnd).lastIndexOf(".");
71
72         if (delimStart < 0)
73             return new String JavaDoc();
74
75         return name.substring(delimStart+1, delimEnd);
76     }
77
78     public static String JavaDoc name2id(String JavaDoc name) {
79
80         int delimEnd = name.lastIndexOf("/");
81         if (delimEnd < 0) {
82             return new String JavaDoc();
83         }
84         delimEnd = name.substring(0, delimEnd).lastIndexOf(".");
85         int delimStart = name.substring(0, delimEnd).lastIndexOf("/");
86
87         if (delimStart < 0)
88             return new String JavaDoc();
89
90         return name.substring(delimStart+1, delimEnd);
91
92     }
93
94     /* The name of each object ends with a context that identifies the facade.
95         In detail, the last id should be "null" and the kind should contain
96         the identifier of the Facade. Remember that facades register directly under
97         the local root context, with id=FACADE and kind=the facade identifier
98     */

99     public static String JavaDoc name2facade(String JavaDoc name) {
100
101         int delimEnd = name.lastIndexOf("/");
102         if (delimEnd < 0) {
103             return new String JavaDoc();
104             //This is assumed to be a local root object
105
}
106
107         int delimStart = name.lastIndexOf(".");
108         return name.substring(delimStart+1);
109     }
110
111     public static boolean isChild(NameComponent JavaDoc[] child, NameComponent JavaDoc[] parent) {
112
113         for (int i = 0; i < parent.length - 2; i++) {
114             if (! (child[i].id.equals(parent[i].id)) ||
115               ! (child[i].kind.equals(parent[i].kind))) {
116
117                       return false;
118             }
119         }
120
121         return true;
122     }
123
124     public static boolean packageIsSupported(
125         String JavaDoc packageName,
126         String JavaDoc[] supportedPackages) {
127
128         boolean isSupported = false;
129         for (int i = 0; i < supportedPackages.length; i++) {
130             if (supportedPackages[i].equals(packageName)) {
131                 isSupported = true;
132                 break;
133             }
134         }
135
136         return isSupported;
137
138
139     }
140
141     public static synchronized void
142     pretty_log(ist.coach.coachEmfServices.EmfBasicLog.BasicLog logger,
143           String JavaDoc source, String JavaDoc sourceClass, short severity, String JavaDoc info) {
144
145         org.omg.CORBA.Any JavaDoc record = orb.create_any();
146         record.insert_string(info);
147         ist.coach.coachEmfServices.EmfBasicLog.NVPair[] log_data = new ist.coach.coachEmfServices.EmfBasicLog.NVPair[3];
148
149         log_data[0] = new ist.coach.coachEmfServices.EmfBasicLog.NVPair();
150         log_data[0].name = "Source";
151         org.omg.CORBA.Any JavaDoc name_value = orb.create_any();
152         name_value.insert_string(source);
153         log_data[0].value = name_value;
154
155         log_data[1] = new ist.coach.coachEmfServices.EmfBasicLog.NVPair();
156         log_data[1].name = "EventType";
157         org.omg.CORBA.Any JavaDoc type_value = orb.create_any();
158         type_value.insert_short(severity);
159         log_data[1].value = type_value;
160
161         log_data[2] = new ist.coach.coachEmfServices.EmfBasicLog.NVPair();
162         log_data[2].name = "SourceClass";
163         org.omg.CORBA.Any JavaDoc class_value = orb.create_any();
164         class_value.insert_string(sourceClass);
165         log_data[2].value = class_value;
166
167         try {
168           logger.write_record(record, log_data);
169         //System.err.println("pretty_log is called!..Logger is invoked OK");
170
}
171         catch(ist.coach.coachEmfServices.EmfBasicLog.LogFull fullEx) {
172           System.err.println("EmfBasicLog.LogFull Exception caught");
173         }
174         catch(ist.coach.coachEmfServices.EmfBasicLog.LogOffDuty dutyEx) {
175           System.err.println("EmfBasicLog.LogOffDuty Exception caught");
176         }
177         catch(ist.coach.coachEmfServices.EmfBasicLog.LogLocked lockedEx) {
178           System.err.println("EmfBasicLog.LogLocked Exception caught");
179         }
180         catch(ist.coach.coachEmfServices.EmfBasicLog.LogDisabled disabledEx) {
181           System.err.println("EmfBasicLog.LogDisabled Exception caught");
182         }
183
184
185     }
186 }
187
Popular Tags