KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > TradingService


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

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

13 package org.jacorb.trading;
14
15 import java.io.*;
16 import java.util.*;
17
18 import org.apache.avalon.framework.configuration.*;
19
20 import org.omg.CORBA.*;
21 import org.omg.CosTrading.*;
22 import org.omg.CosTradingRepos.ServiceTypeRepositoryPackage.*;
23
24 import org.jacorb.trading.impl.*;
25 import org.jacorb.trading.db.DatabaseMgr;
26 import org.jacorb.trading.db.OfferDatabase;
27 import org.jacorb.trading.db.TypeDatabase;
28
29 import org.jacorb.trading.util.*;
30
31
32 public class TradingService
33 {
34     private static final String JavaDoc s_defaultDbpath = "db";
35     private static org.jacorb.config.Configuration configuration = null;
36     private static ORB orb;
37
38     protected TradingService()
39     {
40     }
41
42     public static ORB getORB()
43     {
44     return orb;
45     }
46
47     public TradingService(DatabaseMgr dbMgr, String JavaDoc iorfile)
48     {
49     // see if we have an interface repository
50
org.omg.CORBA.Repository JavaDoc intRep = null;
51     org.omg.CORBA.Object JavaDoc obj = null;
52
53     try
54     {
55         obj = orb.resolve_initial_references("InterfaceRepository");
56     }
57     catch ( Exception JavaDoc e )
58         {
59         // ignore - no interface repository available
60
}
61
62     try
63     {
64         org.omg.PortableServer.POA JavaDoc poa =
65                 org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
66         poa.the_POAManager().activate();
67
68         if (obj != null)
69         intRep = org.omg.CORBA.RepositoryHelper.narrow(obj);
70     }
71     catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc e) {
72         // ignore
73
}
74     catch (org.omg.CORBA.SystemException JavaDoc e) {
75         // ignore
76
}
77     catch (org.omg.CORBA.UserException JavaDoc e) {
78         // ignore
79
}
80
81     // retrieve the database objects
82
OfferDatabase offerDb = dbMgr.getOfferDatabase();
83     TypeDatabase typeDb = dbMgr.getTypeDatabase();
84
85     // create the service type repository implementation
86
RepositoryImpl typeRepos = new RepositoryImpl(typeDb, intRep);
87     typeRepos._this_object( orb );
88
89     // create and initialize the attributes objects
90
SupportAttrib supportAttrib = new SupportAttrib();
91     supportAttrib.setModifiableProperties(getProperty("jtrader.modifiable_properties", true));
92     supportAttrib.setDynamicProperties(getProperty("jtrader.dynamic_properties",true));
93     supportAttrib.setProxyOffers(getProperty("jtrader.proxy_offers",true));
94     supportAttrib.setTypeRepos(typeRepos._this());
95
96     ImportAttrib importAttrib = new ImportAttrib();
97     importAttrib.setDefSearchCard(getProperty("jtrader.def_search_card",Integer.MAX_VALUE));
98     importAttrib.setMaxSearchCard(getProperty("jtrader.max_search_card",Integer.MAX_VALUE));
99     importAttrib.setDefMatchCard(getProperty("jtrader.def_match_card",Integer.MAX_VALUE));
100     importAttrib.setMaxMatchCard(getProperty("jtrader.max_match_card",Integer.MAX_VALUE));
101     importAttrib.setDefReturnCard(getProperty("jtrader.def_return_card",Integer.MAX_VALUE));
102     importAttrib.setMaxReturnCard(getProperty("jtrader.max_return_card",Integer.MAX_VALUE));
103     importAttrib.setMaxList(getProperty("jtrader.max_list",Integer.MAX_VALUE));
104
105     //////////////////////////////////////////////////////////////// new!
106
importAttrib.setDefHopCount(getProperty("jtrader.def_hop_count",Integer.MAX_VALUE));
107     importAttrib.setMaxHopCount(getProperty("jtrader.max_hop_count",Integer.MAX_VALUE));
108     importAttrib.setDefFollowPolicy(getProperty("jtrader.def_follow_policy",FollowOption.always));
109     importAttrib.setMaxFollowPolicy(getProperty("jtrader.max_follow_policy",FollowOption.always));
110     
111
112     LinkAttrib linkAttrib = new LinkAttrib();
113     linkAttrib.setMaxLinkFollowPolicy(getProperty("jtrader.max_link_follow_policy",FollowOption.always));
114     //////////////////////////////////////////////////////////////// new!
115

116     TraderComp traderComp = new TraderComp();
117
118
119     // create the trader components
120

121     RegisterImpl reg =
122         new RegisterImpl(traderComp, supportAttrib, offerDb, intRep);
123     reg._this_object(orb);
124     traderComp.setRegisterInterface(reg._this());
125
126     LinkImpl link =
127             new LinkImpl(traderComp,
128                          supportAttrib,
129                          linkAttrib);
130
131     link._this_object(orb);
132     traderComp.setLinkInterface(link._this());
133
134     LookupImpl lookup =
135             new LookupImpl(traderComp, supportAttrib,
136                            importAttrib, offerDb, link,
137                            ((org.jacorb.orb.ORB)orb).getConfiguration());
138
139     lookup._this_object(orb);
140     traderComp.setLookupInterface(lookup._this());
141
142     // creating request_id_stem out of inet-address. Might as well use any other
143
// way of creating a unique string
144
byte[] stem = orb.object_to_string(lookup._this()).getBytes();
145
146
147     AdminImpl admin = new AdminImpl(traderComp, supportAttrib,
148                     importAttrib, linkAttrib,
149                     offerDb, stem);
150     admin._this_object(orb);
151     traderComp.setAdminInterface(admin._this());
152
153     ProxyImpl proxy = new ProxyImpl(traderComp, supportAttrib, offerDb);
154     proxy._this_object(orb);
155     traderComp.setProxyInterface(proxy._this());
156     
157     // write the IOR of the Lookup object (if necessary)
158
if (iorfile != null) {
159         try {
160         FileOutputStream out = new FileOutputStream(iorfile);
161         PrintWriter pw = new PrintWriter(out);
162         pw.println(orb.object_to_string(lookup._this()));
163         pw.flush();
164         out.close();
165         }
166         catch (IOException e) {
167         System.err.println("Unable to write IOR to file " + iorfile);
168         System.exit(1);
169         }
170     }
171     }
172
173     /**
174      * main
175      */

176
177     public static void main(String JavaDoc[] args)
178     {
179     String JavaDoc iorfile = null;
180     String JavaDoc dbpath = s_defaultDbpath;
181
182     if( args.length != 1 && args.length != 3)
183         usage();
184
185     iorfile = args[0];
186
187     if( args.length == 3 )
188     {
189         if( args[1].equals("-d"))
190         dbpath = args[2];
191         else
192         usage();
193     }
194
195     // validate the database pathname
196

197     File f = new File(dbpath);
198     if (! f.exists()) {
199         System.out.println("The directory " + dbpath + " does not exist");
200         System.exit(1);
201     }
202     else if (! f.isDirectory()) {
203         System.out.println("The path " + dbpath + " is not a directory");
204         System.exit(1);
205     }
206
207     // initialize the ORB
208
orb = ORB.init(args,null);
209
210         // JacORB-specific service configuration
211
configuration =
212             (org.jacorb.config.Configuration)((org.jacorb.orb.ORB)orb).getConfiguration();
213
214     // create the appropriate database manager object
215
DatabaseMgr dbMgr;
216
217     ///////// Simple database implementation
218
dbMgr = new org.jacorb.trading.db.simple.SimpleDatabaseMgr(dbpath);
219
220     ///////// ObjectStore PSE database implementation
221
//dbMgr = new org.jacorb.trading.db.pse.PSEDatabaseMgr(dbpath);
222

223     new TradingService(dbMgr, iorfile);
224     orb.run();
225     
226     dbMgr.shutdown();
227     System.exit(0);
228     }
229
230
231     protected static void usage()
232     {
233     System.err.println("Usage: org.jacorb.trading.TradingService <iorfile> [-d dbpath]");
234     System.exit(1);
235     }
236
237     private int getProperty(String JavaDoc prop_name, int default_val)
238     {
239     String JavaDoc _res = configuration.getAttribute(prop_name, null);
240     int _value = default_val;
241     if (_res != null)
242         {
243         try
244             {
245         _value = Integer.parseInt(_res);
246         }catch (Exception JavaDoc _e){
247         // sth wrong, ignore
248
}
249     }
250     return _value;
251     }
252
253     private FollowOption getProperty(String JavaDoc prop_name, FollowOption default_val){
254     String JavaDoc _res = configuration.getAttribute(prop_name, null);
255     int _value = default_val.value();
256     if (_res != null){
257         try{
258         _value = Integer.parseInt(_res);
259         }catch (Exception JavaDoc _e){
260         // sth wrong, ignore
261
}
262     }
263     return FollowOption.from_int(_value);
264     }
265
266     private boolean getProperty(String JavaDoc prop_name, boolean default_val)
267     {
268     String JavaDoc _res = configuration.getAttribute(prop_name, null);
269     boolean _value = default_val;
270     if (_res != null)
271     {
272         try
273         {
274         _value = Boolean.valueOf(_res).booleanValue();
275         }
276         catch (Exception JavaDoc _e)
277         {
278         // sth wrong, ignore
279
}
280     }
281     return _value;
282     }
283 }
284
285
286
Popular Tags