1 26 27 28 package org.objectweb.mobilitools.smi; 29 30 31 import java.util.Observable ; 32 import java.util.TreeMap ; 33 import java.util.SortedMap ; 34 import java.util.Iterator ; 35 import java.util.Vector ; 36 import java.util.Properties ; 37 import java.util.Enumeration ; 38 import org.objectweb.mobilitools.smi.api.Constants; 39 import org.objectweb.mobilitools.smi.idl.*; 40 import org.objectweb.mobilitools.util.corba.NameService; 41 import org.objectweb.mobilitools.util.corba.NameServiceException; 42 import org.omg.CfMAF.*; 43 import org.omg.CORBA.ORB ; 44 import org.omg.CORBA.Any ; 45 46 47 59 public class Finder extends Observable 60 { 61 static boolean standalone = false; 62 63 64 70 static public void main(String args[]) 71 { 72 ORB orb = ORB.init(args, null); 73 if (args.length == 0) 74 { 75 System.err.println("argument required: region_name"); 76 System.exit(-1); 77 } 78 else 79 { 80 standalone = true; 81 new Finder(args[args.length-1], orb); 82 System.out.println("MAFFinder for region " + args[0] + " is ready."); 83 orb.run(); 84 } 85 } 86 87 88 String my_ns_name; 90 91 String my_region; 93 94 _MAFFinderComplementImplBase my_finderTie; 96 97 NameService my_ns; 99 100 ORB my_orb; 102 103 SortedMap entries; 105 106 107 113 public Finder(String region) 114 { 115 this(region, ORB.init(new String [0], System.getProperties())); 116 } 117 118 119 127 public Finder(String region, ORB orb) 128 { 129 entries = new TreeMap (); 130 my_region = region; 131 my_orb = orb; 132 my_finderTie = new MAFFinder_impl(this); 133 orb.connect(my_finderTie); 134 my_ns_name = 135 System.getProperty(Constants.regionPrefixProp, Constants.regionPrefixDefault) + 136 "/" + 137 region + 138 "/" + 139 System.getProperty(Constants.finderNameProp, Constants.finderNameDefault); 140 try 141 { 142 my_ns = new NameService(orb); 143 my_ns.rebind(my_ns_name, my_finderTie); 144 } 145 catch (NameServiceException e) 146 { 147 e.printStackTrace(); 148 System.err.println("Exception with naming service: unable to bind name " + my_ns_name); 149 System.err.println(e.toString()); 150 } 151 } 152 153 154 157 public String getRegion() 158 { 159 return my_region; 160 } 161 162 163 168 public int exit() 169 { 170 int exitcode = -1; 171 try 172 { 173 my_ns.unbind(my_ns_name); 174 my_orb.disconnect(my_finderTie); 175 exitcode = 0; 176 } 177 catch (NameServiceException exception) 178 { 179 System.err.println(exception.toString()); 180 } 181 if (standalone) 182 { 183 System.exit(exitcode); 184 } 185 return(exitcode); 186 } 187 188 189 193 194 public void register(FinderEntry entry) 195 throws NameInvalid 196 { 197 org.objectweb.mobilitools.smi.api.Name key = entry.getName(); 198 if (key.getmafname().identity.length == 0) 199 { 200 throw new NameInvalid(); 201 } 202 synchronized(entries) 203 { 204 if (entries.containsKey(key)) 205 { 206 if (entry.isAgent()) 207 { 208 entries.remove(key); 209 } 210 else 211 { 212 throw new NameInvalid(); 213 } 214 } 215 entries.put(key, entry); 216 if (countObservers() > 0) 217 { 218 setChanged(); 219 try 220 { 221 notifyObservers(entries.values().toArray(new FinderEntry[0])); 222 } 223 catch (Throwable e) 224 { 225 e.printStackTrace(); 226 } 227 } 228 } 229 } 230 231 232 public Name[] lookup_names(Object profile) 233 { 234 Name[] result; 235 Object names = lookup(null, null, profile, true); 236 if (names instanceof Name[]) 237 { 238 result = (Name[])names; 239 } 240 else 241 { 242 result = new Name[0]; 243 } 244 return result; 245 } 246 247 248 public String [] lookup_locations(org.objectweb.mobilitools.smi.api.Name name, String location, Object profile) 249 throws EntryNotFound 250 { 251 String [] result; 252 Object locations = lookup(name, location, profile, false); 253 if (locations instanceof String []) 254 { 255 result = (String [])locations; 256 if (result.length == 0) 257 { 258 throw new EntryNotFound(); 259 } 260 } 261 else 262 { 263 throw new EntryNotFound(); 264 } 265 return result; 266 } 267 268 269 private Object lookup(org.objectweb.mobilitools.smi.api.Name name, String location, Object profile, boolean returnNames) 270 { 271 Object result = null; 272 if (name == null || name.getmafname().identity.length == 0) 273 { 274 synchronized (entries) 275 { 276 Iterator all = entries.values().iterator(); 277 Vector select = new Vector (); 278 while (all.hasNext()) 279 { 280 FinderEntry element = (FinderEntry)all.next(); 281 if (Misc.matchProfile(profile, element.getProfile()) && 282 (location == null || location.length() == 0 || location.equals(element.getLocation()))) 283 { 284 if (returnNames) 285 { 286 select.add(element.getName().getmafname()); 287 } 288 else 289 { 290 select.add(element.getLocation()); 291 } 292 } 293 } 294 if (returnNames) 295 { 296 result = new Name[select.size()]; 297 result = (Name[])(select.toArray((Name[])result)); 298 } 299 else 300 { 301 result = new String [select.size()]; 302 result = (String [])select.toArray((String [])result); 303 } 304 } 305 } 306 else 307 { 308 FinderEntry element = (FinderEntry)entries.get(name); 309 if (element != null) 310 { 311 result = new String [] { element.getLocation() }; 312 } 313 } 314 return result; 315 } 316 317 318 public void unregister(org.objectweb.mobilitools.smi.api.Name name) 319 throws EntryNotFound 320 { 321 if (entries.remove(name) == null) 322 { 323 throw new EntryNotFound(); 324 } 325 else if (countObservers() > 0) 326 { 327 setChanged(); 328 notifyObservers(entries.values().toArray(new FinderEntry[0])); 329 } 330 } 331 332 333 public AgentProfile getAgentProfile(org.objectweb.mobilitools.smi.api.Name name) 334 throws AgentNotFound 335 { 336 AgentProfile result = null; 337 FinderEntry entry = (FinderEntry)entries.get(name); 338 if (entry != null && entry.getProfile() instanceof AgentProfile) 339 { 340 result = (AgentProfile)entry.getProfile(); 341 } 342 if (result == null) 343 { 344 throw new AgentNotFound(); 345 } 346 else 347 { 348 return result; 349 } 350 } 351 352 353 public void printEntries() 354 { 355 Iterator values = entries.values().iterator(); 356 while (values.hasNext()) 357 { 358 FinderEntry entry = (FinderEntry)values.next(); 359 System.out.println(entry.toString() + ", location " + entry.getLocation()); 360 } 361 } 362 363 364 public ORB getORB() 365 { 366 return my_orb; 367 } 368 } 369 | Popular Tags |