1 4 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 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 iorfile) 48 { 49 org.omg.CORBA.Repository intRep = null; 51 org.omg.CORBA.Object obj = null; 52 53 try 54 { 55 obj = orb.resolve_initial_references("InterfaceRepository"); 56 } 57 catch ( Exception e ) 58 { 59 } 61 62 try 63 { 64 org.omg.PortableServer.POA 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 e) { 72 } 74 catch (org.omg.CORBA.SystemException e) { 75 } 77 catch (org.omg.CORBA.UserException e) { 78 } 80 81 OfferDatabase offerDb = dbMgr.getOfferDatabase(); 83 TypeDatabase typeDb = dbMgr.getTypeDatabase(); 84 85 RepositoryImpl typeRepos = new RepositoryImpl(typeDb, intRep); 87 typeRepos._this_object( orb ); 88 89 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 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 116 TraderComp traderComp = new TraderComp(); 117 118 119 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 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 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 176 177 public static void main(String [] args) 178 { 179 String iorfile = null; 180 String 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 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 orb = ORB.init(args,null); 209 210 configuration = 212 (org.jacorb.config.Configuration)((org.jacorb.orb.ORB)orb).getConfiguration(); 213 214 DatabaseMgr dbMgr; 216 217 dbMgr = new org.jacorb.trading.db.simple.SimpleDatabaseMgr(dbpath); 219 220 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 prop_name, int default_val) 238 { 239 String _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 _e){ 247 } 249 } 250 return _value; 251 } 252 253 private FollowOption getProperty(String prop_name, FollowOption default_val){ 254 String _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 _e){ 260 } 262 } 263 return FollowOption.from_int(_value); 264 } 265 266 private boolean getProperty(String prop_name, boolean default_val) 267 { 268 String _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 _e) 277 { 278 } 280 } 281 return _value; 282 } 283 } 284 285 286 | Popular Tags |