1 26 27 28 package org.objectweb.mobilitools.util.corba; 29 30 31 import java.io.*; 32 import java.net.URL ; 33 import java.util.Properties ; 34 import org.omg.CORBA.ORB ; 35 import mparser.*; 36 37 38 47 public class NSbinder 48 { 49 NameService ns; 50 ORB orb; 51 52 53 62 static public void main(String [] args) 63 { 64 try 65 { 66 NSbinder binder = new NSbinder(ORB.init(args, System.getProperties())); 67 if (args[args.length-2].equalsIgnoreCase("-prop")) 68 { 69 binder.bindFromPropFile(args[args.length-1]); 70 } 71 else if(args[args.length-2].equalsIgnoreCase("-xml")) 72 { 73 binder.bindFromXMLFile(args[args.length-1]); 74 } 75 else 76 { 77 System.out.println("usage: [ORB-specific args...] [-prop file] [-xml file]"); 78 } 79 } 80 catch (Throwable ex) 81 { 82 System.err.println(ex.toString()); 83 } 84 } 85 86 87 92 public NSbinder() 93 throws NameServiceException 94 { 95 this(ORB.init()); 96 } 97 98 99 105 public NSbinder(ORB orb) 106 throws NameServiceException 107 { 108 this.orb = orb; 109 ns = new NameService(orb); 110 } 111 112 113 126 public void bindFromXMLFile(String file) 127 throws Exception 128 { 129 WMparser parser = new WMparser(file); 130 ItemParsed item; 131 Properties props = new Properties (); 132 int bindCount = 0; 133 134 while (parser.hasMoreItems()) 135 { 136 item = parser.getNextItem(); 137 System.out.println(item.getName()); 138 if ((item.getType() == ItemParsed.ITEM_TYPE_ELEMT) && item.getName().equalsIgnoreCase("bind")) 139 { 140 ++bindCount; 141 } 142 else if (item.getType() == ItemParsed.ITEM_TYPE_ATTRB) 143 { 144 props.setProperty( 145 item.getName().substring("bind.".length()) + "." + String.valueOf(bindCount), 146 item.getValue()); 147 } 148 else if (item.getType() == ItemParsed.ITEM_TYPE_TEXTE) 149 { 150 System.out.println(item.getValue()); 151 } 152 else 153 { 154 System.out.println("XML deployment warning: ignoring \"" + item.getName() + "\""); 155 } 156 } 157 props.list(System.out); 158 bind(props); 159 } 160 161 162 168 public void bindFromPropFile(String file) 169 throws IOException 170 { 171 Properties props = new Properties (); 172 props.load(new FileInputStream(new File(file))); 173 bind(props); 174 } 175 176 177 189 public void bind(Properties props) 190 { 191 String name, url, ior, file; 192 String suffix; 193 194 for (int i=1 ; (name = props.getProperty("name." + (suffix = String.valueOf(i)))) != null ; ++i) 195 { 196 url = props.getProperty("url." + suffix); 197 ior = props.getProperty("ior." + suffix); 198 file = props.getProperty("file." + suffix); 199 try 200 { 201 if (url != null) 202 { 203 System.out.println("binding " + name + " to IOR from URL " + url); 204 ns.rebind( 205 name, 206 orb.string_to_object((new BufferedReader(new InputStreamReader((new URL (url)).openStream()))).readLine())); 207 } 208 else if (file != null) 209 { 210 System.out.println("binding " + name + " to IOR from file " + file); 211 ns.rebind( 212 name, 213 orb.string_to_object((new BufferedReader(new InputStreamReader((new URL ("file:" + file)).openStream()))).readLine())); 214 } 215 else if (ior != null) 216 { 217 System.out.println("binding " + name + " to IOR " + ior); 218 ns.rebind(name, orb.string_to_object(ior)); 219 } 220 else 221 { 222 System.out.println("binding " + name + " to new context"); 223 ns.makePath(name); 224 } 225 } 226 catch (Exception e) 227 { 228 System.err.println(e.toString()); 229 } 230 } 231 } 232 } 233 | Popular Tags |