@Override
 public void run() {
   while (!stop) {
     ServiceObjects<String> serviceObjects = ctx.getServiceObjects(ref);
     try {
       String svc = serviceObjects.getService();
       if (!"bar".equals(svc)) System.out.println("Bad Service!");
       else counter++;
       //                serviceObjects.ungetService(svc);
     } finally {
       ctx.ungetService(ref);
     }
   }
   System.out.println("Thread done (" + getClass().getSimpleName() + ")");
 }
 private void close() {
   for (Entry<S, Integer> e : services.entrySet()) {
     for (int i = e.getValue().intValue(); i > 0; i--) {
       so.ungetService(e.getKey());
     }
   }
   services.clear();
   cciService = null;
 }
 /**
  * Returns a service object for the {@link #getServiceReference() associated} service.
  *
  * @see org.osgi.framework.ServiceObjects#getService()
  */
 public S getService() {
   synchronized (rls) {
     if (cci.cc.getState() == ComponentConfiguration.STATE_DEACTIVE) {
       throw new IllegalStateException("Reference unbound");
     }
     if (rls.isEmpty()) {
       return null;
     }
   }
   S service = so.getService();
   synchronized (rls) {
     Integer refCnt = services.get(service);
     refCnt = new Integer(refCnt != null ? refCnt.intValue() + 1 : 1);
     services.put(service, refCnt);
   }
   return service;
 }
 /**
  * Releases a service object for the {@link #getServiceReference() associated} service.
  *
  * @see org.osgi.framework.ServiceObjects#ungetService()
  */
 public void ungetService(S service) {
   synchronized (rls) {
     if (rls.isEmpty()) {
       throw new IllegalStateException("Reference unbound");
     }
     Integer refCnt = services.get(service);
     if (refCnt != null) {
       int cnt = refCnt.intValue() - 1;
       if (cnt == 0) {
         services.remove(service);
       } else {
         services.put(service, new Integer(cnt));
       }
     } else {
       throw new IllegalArgumentException("Service not from this ComponentServiceObjects");
     }
   }
   so.ungetService(service);
 }
 @Override
 public boolean getServiceObject(
     ComponentContextImpl<S> key, BundleContext context, SimpleLogger logger) {
   final T service =
       key.getComponentServiceObjectsHelper()
           .getPrototypeRefInstance(this.getRef(), serviceObjects);
   if (service == null) {
     setFailed();
     logger.log(
         LogService.LOG_WARNING,
         "Could not get service from serviceobjects for ref {0}",
         new Object[] {getRef()},
         null);
     return false;
   }
   if (!setServiceObject(key, service)) {
     // Another thread got the service before, so unget our
     serviceObjects.ungetService(service);
   }
   return true;
 }