1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.lang.reflect.Array ; 15 import java.util.*; 16 import org.eclipse.osgi.framework.debug.Debug; 17 import org.eclipse.osgi.framework.util.Headers; 18 import org.osgi.framework.*; 19 20 35 public class ServiceRegistrationImpl implements ServiceRegistration { 37 38 protected ServiceReferenceImpl reference; 39 40 41 protected Framework framework; 42 43 44 protected BundleContextImpl context; 45 46 47 protected AbstractBundle bundle; 48 49 51 protected ArrayList contextsUsing; 52 53 54 protected String [] clazzes; 55 56 57 protected Object service; 58 59 60 protected Properties properties; 61 62 63 protected long serviceid; 64 65 66 protected int serviceranking; 67 68 69 protected Object registrationLock = new Object (); 70 71 72 protected int state = REGISTERED; 73 public static final int REGISTERED = 0x00; 74 public static final int UNREGISTERING = 0x01; 75 public static final int UNREGISTERED = 0x02; 76 77 82 protected ServiceRegistrationImpl(BundleContextImpl context, String [] clazzes, Object service, Dictionary properties) { 83 this.context = context; 84 this.bundle = context.bundle; 85 this.framework = context.framework; 86 this.clazzes = clazzes; 87 this.service = service; 88 this.contextsUsing = null; 89 this.reference = new ServiceReferenceImpl(this); 90 91 synchronized (framework.serviceRegistry) { 92 serviceid = framework.getNextServiceId(); 93 this.properties = createProperties(properties); 94 95 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 96 Debug.println("registerService[" + bundle + "](" + this + ")"); } 98 99 framework.serviceRegistry.publishService(context, this); 100 } 101 102 103 framework.publishServiceEvent(ServiceEvent.REGISTERED, reference); 104 } 105 106 136 public void unregister() { 137 synchronized (registrationLock) { 138 if (state != REGISTERED) 139 { 140 throw new IllegalStateException (Msg.SERVICE_ALREADY_UNREGISTERED_EXCEPTION); 141 } 142 143 144 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 145 Debug.println("unregisterService[" + bundle + "](" + this + ")"); } 147 148 synchronized (framework.serviceRegistry) { 149 framework.serviceRegistry.unpublishService(context, this); 150 } 151 152 state = UNREGISTERING; 153 } 154 155 156 framework.publishServiceEvent(ServiceEvent.UNREGISTERING, reference); 157 158 159 service = null; 160 state = UNREGISTERED; 161 162 int size = 0; 163 BundleContextImpl[] users = null; 164 165 synchronized (registrationLock) { 166 if (contextsUsing != null) { 167 size = contextsUsing.size(); 168 169 if (size > 0) { 170 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 171 Debug.println("unregisterService: releasing users"); } 173 users = (BundleContextImpl[]) contextsUsing.toArray(new BundleContextImpl[size]); 174 } 175 } 176 } 177 178 179 for (int i = 0; i < size; i++) { 180 releaseService(users[i]); 181 } 182 183 contextsUsing = null; 184 185 reference = null; 186 context = null; 187 188 191 } 192 193 201 public org.osgi.framework.ServiceReference getReference() { 202 207 if (reference == null) { 208 throw new IllegalStateException (Msg.SERVICE_ALREADY_UNREGISTERED_EXCEPTION); 209 } 210 211 return (reference); 212 } 213 214 236 public void setProperties(Dictionary props) { 237 synchronized (registrationLock) { 238 if (state != REGISTERED) 239 { 240 throw new IllegalStateException (Msg.SERVICE_ALREADY_UNREGISTERED_EXCEPTION); 241 } 242 243 this.properties = createProperties(props); 244 } 245 246 247 framework.publishServiceEvent(ServiceEvent.MODIFIED, reference); 248 } 249 250 257 protected Properties createProperties(Dictionary props) { 258 Properties properties = new Properties(props); 259 260 properties.set(Constants.OBJECTCLASS, clazzes, true); 261 properties.set(Constants.SERVICE_ID, new Long (serviceid), true); 262 properties.setReadOnly(); 263 Object ranking = properties.getProperty(Constants.SERVICE_RANKING); 264 265 serviceranking = (ranking instanceof Integer ) ? ((Integer ) ranking).intValue() : 0; 266 267 return (properties); 268 } 269 270 282 protected Object getProperty(String key) { 283 synchronized (registrationLock) { 284 return (properties.getProperty(key)); 285 } 286 } 287 288 298 protected String [] getPropertyKeys() { 299 synchronized (registrationLock) { 300 return (properties.getPropertyKeys()); 301 } 302 } 303 304 313 protected AbstractBundle getBundle() { 314 if (reference == null) { 315 return (null); 316 } 317 318 return (bundle); 319 } 320 321 327 protected Object getService(BundleContextImpl user) { 328 synchronized (registrationLock) { 329 if (state == UNREGISTERED) 330 { 331 return (null); 332 } 333 334 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 335 Debug.println("getService[" + user.bundle + "](" + this + ")"); } 337 338 Hashtable servicesInUse = user.servicesInUse; 339 340 ServiceUse use = (ServiceUse) servicesInUse.get(reference); 341 342 if (use == null) { 343 use = new ServiceUse(user, this); 344 345 Object service = use.getService(); 346 347 if (service != null) { 348 servicesInUse.put(reference, use); 349 350 if (contextsUsing == null) { 351 contextsUsing = new ArrayList(10); 352 } 353 354 contextsUsing.add(user); 355 } 356 357 return (service); 358 } else { 359 return (use.getService()); 360 } 361 } 362 } 363 364 372 protected boolean ungetService(BundleContextImpl user) { 373 synchronized (registrationLock) { 374 if (state == UNREGISTERED) { 375 return (false); 376 } 377 378 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 379 String bundle = (user.bundle == null) ? "" : user.bundle.toString(); Debug.println("ungetService[" + bundle + "](" + this + ")"); } 382 383 Hashtable servicesInUse = user.servicesInUse; 384 385 if (servicesInUse != null) { 386 ServiceUse use = (ServiceUse) servicesInUse.get(reference); 387 388 if (use != null) { 389 if (use.ungetService()) { 390 391 servicesInUse.remove(reference); 392 393 contextsUsing.remove(user); 394 } 395 return (true); 396 } 397 } 398 399 return (false); 400 } 401 } 402 403 408 protected void releaseService(BundleContextImpl user) { 409 synchronized (registrationLock) { 410 if (reference == null) 411 { 412 return; 413 } 414 415 if (Debug.DEBUG && Debug.DEBUG_SERVICES) { 416 String bundle = (user.bundle == null) ? "" : user.bundle.toString(); Debug.println("releaseService[" + bundle + "](" + this + ")"); } 419 420 Hashtable servicesInUse = user.servicesInUse; 421 422 if (servicesInUse != null) { 423 ServiceUse use = (ServiceUse) servicesInUse.remove(reference); 424 425 if (use != null) { 426 use.releaseService(); 427 if (contextsUsing != null) 430 contextsUsing.remove(user); 431 } 432 } 433 } 434 } 435 436 441 protected AbstractBundle[] getUsingBundles() { 442 synchronized (registrationLock) { 443 if (state == UNREGISTERED) 444 return (null); 445 446 if (contextsUsing == null) 447 return (null); 448 449 int size = contextsUsing.size(); 450 if (size == 0) 451 return (null); 452 453 454 AbstractBundle[] bundles = new AbstractBundle[size]; 455 for (int i = 0; i < size; i++) 456 bundles[i] = ((BundleContextImpl) contextsUsing.get(i)).bundle; 457 458 return (bundles); 459 } 460 } 461 462 467 public String toString() { 468 String [] clazzes = this.clazzes; 469 int size = clazzes.length; 470 StringBuffer sb = new StringBuffer (50 * size); 471 472 sb.append('{'); 473 474 for (int i = 0; i < size; i++) { 475 if (i > 0) { 476 sb.append(", "); } 478 sb.append(clazzes[i]); 479 } 480 481 sb.append("}="); sb.append(properties); 483 484 return (sb.toString()); 485 } 486 487 490 static class Properties extends Headers { 491 496 private Properties(int size, Dictionary props) { 497 super(size); 498 499 if (props != null) { 500 synchronized (props) { 501 Enumeration keysEnum = props.keys(); 502 503 while (keysEnum.hasMoreElements()) { 504 Object key = keysEnum.nextElement(); 505 506 if (key instanceof String ) { 507 String header = (String ) key; 508 509 setProperty(header, props.get(header)); 510 } 511 } 512 } 513 } 514 } 515 516 521 protected Properties(Dictionary props) { 522 this((props == null) ? 2 : props.size() + 2, props); 523 } 524 525 532 protected Object getProperty(String key) { 533 return (cloneValue(get(key))); 534 } 535 536 541 protected synchronized String [] getPropertyKeys() { 542 int size = size(); 543 544 String [] keynames = new String [size]; 545 546 Enumeration keysEnum = keys(); 547 548 for (int i = 0; i < size; i++) { 549 keynames[i] = (String ) keysEnum.nextElement(); 550 } 551 552 return (keynames); 553 } 554 555 562 protected synchronized Object setProperty(String key, Object value) { 563 return (set(key, cloneValue(value))); 564 } 565 566 576 protected static Object cloneValue(Object value) { 577 if (value == null) 578 return null; 579 if (value instanceof String ) 580 return (value); 581 if (value instanceof Number ) 582 return value; 583 if (value instanceof Character ) 584 return value; 585 if (value instanceof Boolean ) 586 return value; 587 588 Class clazz = value.getClass(); 589 if (clazz.isArray()) { 590 Class type = clazz.getComponentType(); 592 int len = Array.getLength(value); 593 Object clonedArray = Array.newInstance(type, len); 594 System.arraycopy(value, 0, clonedArray, 0, len); 595 return clonedArray; 596 } 597 try { 599 return (clazz.getMethod("clone", null).invoke(value, null)); } catch (Exception e) { 601 602 } catch (Error e) { 603 604 if (value instanceof Vector) 605 return (((Vector) value).clone()); 606 if (value instanceof Hashtable) 607 return (((Hashtable) value).clone()); 608 } 609 return (value); 610 } 611 612 public synchronized String toString() { 613 String keys[] = getPropertyKeys(); 614 615 int size = keys.length; 616 617 StringBuffer sb = new StringBuffer (20 * size); 618 619 sb.append('{'); 620 621 int n = 0; 622 for (int i = 0; i < size; i++) { 623 String key = keys[i]; 624 if (!key.equals(Constants.OBJECTCLASS)) { 625 if (n > 0) 626 sb.append(", "); 628 sb.append(key); 629 sb.append('='); 630 Object value = get(key); 631 if (value.getClass().isArray()) { 632 sb.append('['); 633 int length = Array.getLength(value); 634 for (int j = 0; j < length; j++) { 635 if (j > 0) 636 sb.append(','); 637 sb.append(Array.get(value, j)); 638 } 639 sb.append(']'); 640 } else { 641 sb.append(value); 642 } 643 n++; 644 } 645 } 646 647 sb.append('}'); 648 649 return (sb.toString()); 650 } 651 } 652 } 653 | Popular Tags |