KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > client > proxy > export


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.client.proxy;
15
16 import java.io.*;
17 import java.util.*;
18 import org.omg.CORBA.*;
19 import org.omg.CosTrading.*;
20 import org.omg.CosTrading.ProxyPackage.*;
21 import org.omg.CosTradingRepos.*;
22 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
23 /**
24  * Exports proxy offers
25  */

26
27 public class export
28 {
29     public static void main(String JavaDoc[] args)
30     {
31         if (args.length != 1)
32         {
33             usage();
34         }
35
36         File targetFile = new File(args[0]);
37
38         if (! targetFile.exists())
39         {
40             System.err.println("File " + args[0] + " does not exist");
41             usage();
42         }
43
44         if (! targetFile.isFile())
45         {
46             System.err.println(args[0] + " is not a file");
47             usage();
48         }
49
50         ORB orb = null;
51
52         Proxy proxy = null;
53         Lookup target = null;
54         ServiceTypeRepository repos = null;
55         try
56         {
57             org.omg.CORBA.Object JavaDoc obj = null;
58
59             orb = ORB.init(args,null);
60
61             //resolve trader
62
obj = orb.resolve_initial_references("TradingService");
63             if (obj == null)
64             {
65                 System.out.println("Invalid lookup object");
66                 System.exit(1);
67             }
68
69             Lookup lookup = LookupHelper.narrow(obj);
70             proxy = lookup.proxy_if();
71             repos = ServiceTypeRepositoryHelper.narrow(lookup.type_repos());
72
73             // read the IOR for the Lookup target object from a file
74
FileReader fr = new FileReader(targetFile);
75             BufferedReader in = new BufferedReader(fr);
76             String JavaDoc targetRef = in.readLine();
77             fr.close();
78
79
80             obj = orb.string_to_object(targetRef);
81             if (obj == null)
82             {
83                 System.out.println("Invalid target object");
84                 System.exit(1);
85             }
86
87             target = LookupHelper.narrow(obj);
88         }
89         catch (Exception JavaDoc e)
90         {
91             e.printStackTrace();
92             System.exit(1);
93         }
94
95         try
96         {
97             Random rand = new Random();
98
99         PropStruct[] _props = new PropStruct[3];
100         _props[0] = new PropStruct();
101         _props[0].name = "name";
102         _props[0].value_type = orb.get_primitive_tc(TCKind.tk_string);
103         _props[0].mode = PropertyMode.PROP_MANDATORY;
104         
105         _props[1] = new PropStruct();
106         _props[1].name = "cost";
107         _props[1].value_type = orb.get_primitive_tc(TCKind.tk_double);
108         _props[1].mode = PropertyMode.PROP_MANDATORY;
109
110         _props[2] = new PropStruct();
111         _props[2].name = "version";
112         _props[2].value_type = orb.get_primitive_tc(TCKind.tk_string);
113         _props[2].mode = PropertyMode.PROP_MANDATORY;
114
115         repos.add_type("SubSvc", "IDL:SubSvc:1.0", _props, new String JavaDoc[0]);
116
117
118             for (int i = 0; i < 10; i++)
119             {
120                 Property[] props = new Property[3];
121
122                 int num = 0;
123                 TypeCode tc;
124
125                 // the "name" property
126
props[num] = new Property();
127                 props[num].name = "name";
128                 props[num].value = orb.create_any();
129                 props[num].value.insert_string("proxy #" + i);
130                 num++;
131
132                 // the "cost" property
133
props[num] = new Property();
134                 props[num].name = "cost";
135                 props[num].value = orb.create_any();
136                 props[num].value.insert_double(Math.abs(rand.nextDouble()));
137                 num++;
138
139                 // the "version" property
140
props[num] = new Property();
141                 props[num].name = "version";
142                 props[num].value = orb.create_any();
143                 props[num].value.insert_string("1.0" + i);
144                 num++;
145
146                 String JavaDoc recipe = "$(cost) < 1.50 && $(version) = '1.03'";
147
148                 org.omg.CosTrading.Policy[] policies = new org.omg.CosTrading.Policy[2];
149                 policies[0] = new org.omg.CosTrading.Policy();
150                 policies[0].name = "policy1";
151                 policies[0].value = orb.create_any();
152                 policies[0].value.insert_boolean(true);
153                 policies[1] = new org.omg.CosTrading.Policy();
154                 policies[1].name = "policy2";
155                 policies[1].value = orb.create_any();
156                 policies[1].value.insert_ulong(i);
157
158                 boolean ifMatchAll = (i % 2 == 0);
159
160                 String JavaDoc id = proxy.export_proxy(target, "SubSvc", props, ifMatchAll,
161                                                recipe, policies);
162                 System.out.println("Offer id = " + id);
163             }
164         }
165         catch (IllegalServiceType e) {
166             System.out.println("Illegal service type: " + e.type);
167         }
168         catch (UnknownServiceType e) {
169             System.out.println("Unknown service type: " + e.type);
170         }
171         catch (InvalidLookupRef e) {
172             System.out.println("Invalid target object");
173         }
174         catch (IllegalPropertyName e) {
175             System.out.println("Illegal property name: " + e.name);
176         }
177         catch (PropertyTypeMismatch e) {
178             System.out.println("Property type mismatch: " + e.prop.name);
179         }
180         catch (ReadonlyDynamicProperty e) {
181             System.out.println("Readonly dynamic property: " + e.name);
182         }
183         catch (MissingMandatoryProperty e) {
184             System.out.println("Missing mandatory property: " + e.name);
185         }
186         catch (IllegalRecipe e) {
187             System.out.println("Illegal recipe: " + e.recipe);
188         }
189         catch (DuplicatePropertyName e) {
190             System.out.println("Duplicate property: " + e.name);
191         }
192         catch (DuplicatePolicyName e) {
193             System.out.println("Duplicate policy: " + e.name);
194         }
195         catch (Exception JavaDoc e) {
196             System.out.println("Exception: " + e);
197         }
198         
199
200         System.exit(0);
201     }
202
203
204     protected static void usage()
205     {
206         System.out.println("Usage: org.jacorb.trading.client.proxy.export <proxy-iorfile>");
207         System.exit(1);
208     }
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
Popular Tags