KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > util > corba > CORBA


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.util.corba;
29
30
31 import org.omg.CORBA.*;
32 import java.io.*;
33 import java.applet.Applet JavaDoc;
34 import java.util.Properties JavaDoc;
35
36
37 /**
38  * MobiliTools $Name: $, $Id: CORBA.java,v 1.1.1.1 2003/03/28 14:48:06 dillense Exp $
39  * <P>
40  * Miscellaneous CORBA facilitators.
41 */

42 abstract public class CORBA
43 {
44     static ORB orbSingleton = null;
45
46
47     static public void setOrb(ORB orb)
48     {
49         orbSingleton = orb;
50     }
51
52
53     static public ORB getOrb()
54     {
55         if (orbSingleton == null)
56         {
57             orbSingleton = ORB.init();
58         }
59         return orbSingleton;
60     }
61
62
63     static public ORB setOrb(String JavaDoc[] args, Properties JavaDoc props)
64     {
65         return orbSingleton = ORB.init(args, props);
66     }
67
68
69     static public ORB setOrb(Applet JavaDoc applet, Properties JavaDoc props)
70     {
71         return orbSingleton = ORB.init(applet, props);
72     }
73
74
75     static public boolean refToFile(ORB orb, org.omg.CORBA.Object JavaDoc ref, String JavaDoc filename)
76     {
77         try
78         {
79             String JavaDoc refStr = orb.object_to_string(ref);
80             PrintWriter out = new PrintWriter(
81                 new FileOutputStream(filename));
82             out.println(refStr);
83             out.flush();
84         }
85         catch(IOException ex)
86         {
87             return false;
88         }
89         return true;
90     }
91
92
93     static public org.omg.CORBA.Object JavaDoc refFromFile(ORB orb, String JavaDoc filename)
94     {
95         String JavaDoc refStr = null;
96         try
97         {
98             BufferedReader in =
99                 new BufferedReader(new FileReader(filename));
100             refStr = in.readLine();
101         }
102         catch(IOException ex)
103         {
104             return null;
105         }
106         return orb.string_to_object(refStr);
107     }
108
109
110     static public Any object2any(ORB orb, java.lang.Object JavaDoc obj)
111         throws BAD_PARAM
112     {
113         Any result = orb.create_any();
114         if (obj instanceof Boolean JavaDoc)
115         {
116             result.insert_boolean(((Boolean JavaDoc)obj).booleanValue());
117         }
118         else if (obj instanceof Character JavaDoc)
119         {
120             result.insert_char(((Character JavaDoc)obj).charValue());
121         }
122         else if (obj instanceof Byte JavaDoc)
123         {
124             result.insert_octet(((Byte JavaDoc)obj).byteValue());
125         }
126         else if (obj instanceof String JavaDoc)
127         {
128             result.insert_string((String JavaDoc)obj);
129         }
130         else if (obj instanceof Short JavaDoc)
131         {
132             result.insert_short(((Short JavaDoc)obj).shortValue());
133         }
134         else if (obj instanceof Integer JavaDoc)
135         {
136             result.insert_long(((Integer JavaDoc)obj).intValue());
137         }
138         else if (obj instanceof Long JavaDoc)
139         {
140             result.insert_longlong(((Long JavaDoc)obj).longValue());
141         }
142         else if (obj instanceof Float JavaDoc)
143         {
144             result.insert_float(((Float JavaDoc)obj).floatValue());
145         }
146         else if (obj instanceof Double JavaDoc)
147         {
148             result.insert_double(((Double JavaDoc)obj).doubleValue());
149         }
150         else
151         {
152             throw new BAD_PARAM();
153         }
154         return result;
155     }
156
157
158     static public java.lang.Object JavaDoc any2object(Any any)
159     {
160         java.lang.Object JavaDoc result = null;
161         switch(any.type().kind().value())
162         {
163         case TCKind._tk_boolean:
164             result = new Boolean JavaDoc(any.extract_boolean());
165             break;
166         case TCKind._tk_char:
167             result = new Character JavaDoc(any.extract_char());
168             break;
169         case TCKind._tk_octet:
170             result = new Byte JavaDoc(any.extract_octet());
171             break;
172         case TCKind._tk_string:
173             result = any.extract_string();
174             break;
175         case TCKind._tk_short:
176             result = new Short JavaDoc(any.extract_short());
177             break;
178         case TCKind._tk_long:
179             result = new Integer JavaDoc(any.extract_long());
180             break;
181         case TCKind._tk_longlong:
182             result = new Long JavaDoc(any.extract_longlong());
183             break;
184         case TCKind._tk_float:
185             result = new Float JavaDoc(any.extract_float());
186             break;
187         case TCKind._tk_double:
188             result = new Float JavaDoc(any.extract_double());
189             break;
190         default:
191             throw new BAD_PARAM();
192         }
193         return result;
194     }
195 }
196
Popular Tags