org.eclipse.osgi_3.7.2.v20120110-1415

istry paramServiceRegistry, ServiceEvent paramServiceEvent, Map paramMap) {}
  
  public void call(Object hook, ServiceRegistration<?> hookRegistration)
    throws Exception
  {
    if ((hook instanceof EventListenerHook)) {
      ((EventListenerHook)hook).event(val$event, val$result);
    }
  }
  
  public String getHookClassName()
  {
    return ServiceRegistry.eventListenerHookName;
  }
  
  public String getHookMethodName()
  {
    return "event";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.6
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.serviceregistry;

import java.security.PrivilegedAction;

class ServiceRegistry$7
  implements PrivilegedAction<Object>
{
  final ServiceRegistry this$0;
  
  ServiceRegistry$7(ServiceRegistry paramServiceRegistry, ServiceRegistrationImpl paramServiceRegistrationImpl) {}
  
  public Object run()
  {
    this$0.notifyNewListenerHookPrivileged(val$registration);
    return null;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.7
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.serviceregistry;

import java.util.Collection;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.hooks.service.ListenerHook;

class ServiceRegistry$8
  implements HookContext
{
  final ServiceRegistry this$0;
  
  ServiceRegistry$8(ServiceRegistry paramServiceRegistry, Collection paramCollection) {}
  
  public void call(Object hook, ServiceRegistration<?> hookRegistration)
    throws Exception
  {
    if ((hook instanceof ListenerHook)) {
      ((ListenerHook)hook).added(val$listeners);
    }
  }
  
  public String getHookClassName()
  {
    return ServiceRegistry.listenerHookName;
  }
  
  public String getHookMethodName()
  {
    return "added";
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.8
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.serviceregistry;

import java.security.PrivilegedAction;
import java.util.Collection;

class ServiceRegistry$9
  implements PrivilegedAction<Object>
{
  final ServiceRegistry this$0;
  
  ServiceRegistry$9(ServiceRegistry paramServiceRegistry, Collection paramCollection, boolean paramBoolean) {}
  
  public Object run()
  {
    this$0.notifyListenerHooksPrivileged(val$listeners, val$added);
    return null;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.9
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.osgi.internal.serviceregistry;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.osgi.framework.debug.Debug;
import org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap;
import org.eclipse.osgi.framework.eventmgr.EventDispatcher;
import org.eclipse.osgi.framework.eventmgr.ListenerQueue;
import org.eclipse.osgi.framework.internal.core.AbstractBundle;
import org.eclipse.osgi.framework.internal.core.BundleContextImpl;
import org.eclipse.osgi.framework.internal.core.Framework;
import org.eclipse.osgi.framework.internal.core.Msg;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServicePermission;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.hooks.service.EventHook;
import org.osgi.framework.hooks.service.EventListenerHook;
import org.osgi.framework.hooks.service.FindHook;
import org.osgi.framework.hooks.service.ListenerHook;
import org.osgi.framework.hooks.service.ListenerHook.ListenerInfo;

public class ServiceRegistry
{
  public static final int SERVICEEVENT = 3;
  
  static
  {
    try {}catch (ClassNotFoundException localClassNotFoundException1)
    {
      throw new NoClassDefFoundError(localClassNotFoundException1.getMessage());
    }
    $assertionsDisabled = !Class.forName("org.eclipse.osgi.internal.serviceregistry.ServiceRegistry").desiredAssertionStatus();
  }
  
  static final String findHookName = FindHook.class.getName();
  static final String eventHookName = EventHook.class.getName();
  static final String eventListenerHookName = EventListenerHook.class.getName();
  static final String listenerHookName = ListenerHook.class.getName();
  private final Map<String, List<ServiceRegistrationImpl<?>>> publishedServicesByClass;
  private final List<ServiceRegistrationImpl<?>> allPublishedServices;
  private final Map<BundleContextImpl, List<ServiceRegistrationImpl<?>>> publishedServicesByContext;
  private long serviceid;
  private final Map<BundleContextImpl, CopyOnWriteIdentityMap<ServiceListener, FilteredServiceListener>> serviceEventListeners;
  private static final int initialCapacity = 50;
  private static final int initialSubCapacity = 10;
  private final Framework framework;
  static final boolean $assertionsDisabled;
  
  public ServiceRegistry(Framework framework)
  {
    this.framework = framework;
    serviceid = 1L;
    publishedServicesByClass = new HashMap(50);
    publishedServicesByContext = new HashMap(50);
    allPublishedServices = new ArrayList(50);
    serviceEventListeners = new HashMap(50);
  }
  
  public ServiceRegistrationImpl<?> registerService(BundleContextImpl context, String[] clazzes, Object service, Dictionary<String, ?> properties)
  {
    if (service == null)
    {
      if (Debug.DEBUG_SERVICES) {
        Debug.println("Service object is null");
      }
      throw new IllegalArgumentException(Msg.SERVICE_ARGUMENT_NULL_EXCEPTION);
    }
    int size = clazzes.length;
    if (size == 0)
    {
      if (Debug.DEBUG_SERVICES) {
        Debug.println("Classes array is empty");
      }
      throw new IllegalArgumentException(Msg.SERVICE_EMPTY_CLASS_LIST_EXCEPTION);
    }
    List<String> copy = new ArrayList(size);
    for (int i = 0; i < size; i++)
    {
      String clazz = clazzes[i].intern();
      if (!copy.contains(clazz)) {
        copy.add(clazz);
      }
    }
    size = copy.size();
    clazzes = (String[])copy.toArray(new String[size]);
    
    checkRegisterServicePermission(clazzes);
    if (!(service instanceof ServiceFactory))
    {
      String invalidService = checkServiceClass(clazzes, service);
      if (invalidService != null)
      {
        if (Debug.DEBUG_SERVICES) {
          Debug.println("Service object is not an instanceof " + invalidService);
        }
        throw new IllegalArgumentException(NLS.bind(Msg.SERVICE_NOT_INSTANCEOF_CLASS_EXCEPTION, invalidService));
      }
    }
    ServiceRegistrationImpl<?> registration = new ServiceRegistrationImpl(this, context, clazzes, service);
    registration.register(properties);
    if (copy.contains(listenerHookName)) {
      notifyNewListenerHook(registration);
    }
    return registration;
  }
  
  public ServiceReferenceImpl<?>[] getServiceReferences(BundleContextImpl context, String clazz, String filterstring, boolean allservices, boolean callHooks)
    throws InvalidSyntaxException
  {
    if (Debug.DEBUG_SERVICES) {
      Debug.println((allservices ? "getAllServiceReferences(" : "getServiceReferences(") + clazz + ", \"" + filterstring + "\")");
    }
    Filter filter = filterstring == null ? null : context.createFilter(filterstring);
    List<ServiceRegistrationImpl<?>> registrations = lookupServiceRegistrations(clazz, filter);
    List<ServiceReferenceImpl<?>> references = new ArrayList(registrations.size());
    for (Iterator localIterator = registrations.iterator(); localIterator.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)localIterator.next();
      try
      {
        reference = registration.getReferenceImpl();
      }
      catch (IllegalStateException localIllegalStateException)
      {
        ServiceReferenceImpl<?> reference;
        continue;
      }
      ServiceReferenceImpl<?> reference;
      if ((allservices) || (isAssignableTo(context, reference)))
      {
        try
        {
          checkGetServicePermission(reference);
        }
        catch (SecurityException localSecurityException)
        {
          continue;
        }
        references.add(reference);
      }
    }
    if (callHooks)
    {
      Collection<ServiceReference<?>> shrinkable = new ShrinkableCollection(references);
      notifyFindHooks(context, clazz, filterstring, allservices, shrinkable);
    }
    int size = references.size();
    if (size == 0) {
      return null;
    }
    return (ServiceReferenceImpl[])references.toArray(new ServiceReferenceImpl[size]);
  }
  
  public ServiceReferenceImpl<?>[] getServiceReferences(BundleContextImpl context, String clazz, String filterstring, boolean allservices)
    throws InvalidSyntaxException
  {
    return getServiceReferences(context, clazz, filterstring, allservices, true);
  }
  
  public ServiceReferenceImpl<?> getServiceReference(BundleContextImpl context, String clazz)
  {
    if (Debug.DEBUG_SERVICES) {
      Debug.println("getServiceReference(" + clazz + ")");
    }
    try
    {
      ServiceReferenceImpl[] references = getServiceReferences(context, clazz, null, false);
      if (references != null) {
        return references[0];
      }
    }
    catch (InvalidSyntaxException e)
    {
      if (Debug.DEBUG_GENERAL)
      {
        Debug.println("InvalidSyntaxException w/ null filter" + e.getMessage());
        Debug.printStackTrace(e);
      }
    }
    return null;
  }
  
  public Object getService(BundleContextImpl context, ServiceReferenceImpl<?> reference)
  {
    checkGetServicePermission(reference);
    return reference.getRegistration().getService(context);
  }
  
  public boolean ungetService(BundleContextImpl context, ServiceReferenceImpl<?> reference)
  {
    ServiceRegistrationImpl<?> registration = reference.getRegistration();
    
    return registration.ungetService(context);
  }
  
  public ServiceReferenceImpl<?>[] getRegisteredServices(BundleContextImpl context)
  {
    List<ServiceRegistrationImpl<?>> registrations = lookupServiceRegistrations(context);
    List<ServiceReferenceImpl<?>> references = new ArrayList(registrations.size());
    for (Iterator localIterator = registrations.iterator(); localIterator.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)localIterator.next();
      try
      {
        reference = registration.getReferenceImpl();
      }
      catch (IllegalStateException localIllegalStateException)
      {
        ServiceReferenceImpl<?> reference;
        continue;
      }
      ServiceReferenceImpl<?> reference;
      try
      {
        checkGetServicePermission(reference);
      }
      catch (SecurityException localSecurityException)
      {
        continue;
      }
      references.add(reference);
    }
    int size = references.size();
    if (size == 0) {
      return null;
    }
    return (ServiceReferenceImpl[])references.toArray(new ServiceReferenceImpl[size]);
  }
  
  public ServiceReferenceImpl<?>[] getServicesInUse(BundleContextImpl context)
  {
    Map<ServiceRegistrationImpl<?>, ServiceUse<?>> servicesInUse = context.getServicesInUseMap();
    if (servicesInUse == null) {
      return null;
    }
    List<ServiceRegistrationImpl<?>> registrations;
    synchronized (servicesInUse)
    {
      if (servicesInUse.isEmpty()) {
        return null;
      }
      registrations = new ArrayList(servicesInUse.keySet());
    }
    List<ServiceRegistrationImpl<?>> registrations;
    List<ServiceReferenceImpl<?>> references = new ArrayList(registrations.size());
    for (Iterator localIterator = registrations.iterator(); localIterator.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)localIterator.next();
      try
      {
        reference = registration.getReferenceImpl();
      }
      catch (IllegalStateException localIllegalStateException)
      {
        ServiceReferenceImpl<?> reference;
        continue;
      }
      ServiceReferenceImpl<?> reference;
      try
      {
        checkGetServicePermission(reference);
      }
      catch (SecurityException localSecurityException)
      {
        continue;
      }
      references.add(reference);
    }
    int size = references.size();
    if (size == 0) {
      return null;
    }
    return (ServiceReferenceImpl[])references.toArray(new ServiceReferenceImpl[size]);
  }
  
  public void unregisterServices(BundleContextImpl context)
  {
    for (Iterator localIterator = lookupServiceRegistrations(context).iterator(); localIterator.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)localIterator.next();
      try
      {
        registration.unregister();
      }
      catch (IllegalStateException localIllegalStateException) {}
    }
    removeServiceRegistrations(context);
  }
  
  public void releaseServicesInUse(BundleContextImpl context)
  {
    Map<ServiceRegistrationImpl<?>, ServiceUse<?>> servicesInUse = context.getServicesInUseMap();
    if (servicesInUse == null) {
      return;
    }
    List<ServiceRegistrationImpl<?>> registrations;
    synchronized (servicesInUse)
    {
      if (servicesInUse.isEmpty()) {
        return;
      }
      registrations = new ArrayList(servicesInUse.keySet());
    }
    List<ServiceRegistrationImpl<?>> registrations;
    if (Debug.DEBUG_SERVICES) {
      Debug.println("Releasing services");
    }
    for (Iterator localIterator = registrations.iterator(); localIterator.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)localIterator.next();
      registration.releaseService(context);
    }
  }
  
  public void addServiceListener(BundleContextImpl context, ServiceListener listener, String filter)
    throws InvalidSyntaxException
  {
    if (Debug.DEBUG_EVENTS)
    {
      String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener));
      Debug.println("addServiceListener[" + context.getBundleImpl() + "](" + listenerName + ", \"" + filter + "\")");
    }
    FilteredServiceListener filteredListener = new FilteredServiceListener(context, listener, filter);
    FilteredServiceListener oldFilteredListener;
    synchronized (serviceEventListeners)
    {
      CopyOnWriteIdentityMap<ServiceListener, FilteredServiceListener> listeners = (CopyOnWriteIdentityMap)serviceEventListeners.get(context);
      if (listeners == null)
      {
        listeners = new CopyOnWriteIdentityMap();
        serviceEventListeners.put(context, listeners);
      }
      oldFilteredListener = (FilteredServiceListener)listeners.put(listener, filteredListener);
    }
    FilteredServiceListener oldFilteredListener;
    if (oldFilteredListener != null)
    {
      oldFilteredListener.markRemoved();
      Collection<ListenerHook.ListenerInfo> removedListeners = Collections.singletonList(oldFilteredListener);
      notifyListenerHooks(removedListeners, false);
    }
    Collection<ListenerHook.ListenerInfo> addedListeners = Collections.singletonList(filteredListener);
    notifyListenerHooks(addedListeners, true);
  }
  
  public void removeServiceListener(BundleContextImpl context, ServiceListener listener)
  {
    if (Debug.DEBUG_EVENTS)
    {
      String listenerName = listener.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(listener));
      Debug.println("removeServiceListener[" + context.getBundleImpl() + "](" + listenerName + ")");
    }
    FilteredServiceListener oldFilteredListener;
    synchronized (serviceEventListeners)
    {
      Map<ServiceListener, FilteredServiceListener> listeners = (Map)serviceEventListeners.get(context);
      if (listeners == null) {
        return;
      }
      oldFilteredListener = (FilteredServiceListener)listeners.remove(listener);
    }
    FilteredServiceListener oldFilteredListener;
    if (oldFilteredListener == null) {
      return;
    }
    oldFilteredListener.markRemoved();
    Collection<ListenerHook.ListenerInfo> removedListeners = Collections.singletonList(oldFilteredListener);
    notifyListenerHooks(removedListeners, false);
  }
  
  public void removeAllServiceListeners(BundleContextImpl context)
  {
    Map<ServiceListener, FilteredServiceListener> removedListenersMap;
    synchronized (serviceEventListeners)
    {
      removedListenersMap = (Map)serviceEventListeners.remove(context);
    }
    Map<ServiceListener, FilteredServiceListener> removedListenersMap;
    if ((removedListenersMap == null) || (removedListenersMap.isEmpty())) {
      return;
    }
    Collection<FilteredServiceListener> removedListeners = removedListenersMap.values();
    for (Iterator localIterator = removedListeners.iterator(); localIterator.hasNext();)
    {
      FilteredServiceListener oldFilteredListener = (FilteredServiceListener)localIterator.next();
      oldFilteredListener.markRemoved();
    }
    notifyListenerHooks(asListenerInfos(removedListeners), false);
  }
  
  private static Collection<ListenerHook.ListenerInfo> asListenerInfos(Collection<? extends ListenerHook.ListenerInfo> c)
  {
    return c;
  }
  
  public void publishServiceEvent(final ServiceEvent event)
  {
    if (System.getSecurityManager() == null) {
      publishServiceEventPrivileged(event);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          publishServiceEventPrivileged(event);
          return null;
        }
      });
    }
  }
  
  void publishServiceEventPrivileged(ServiceEvent event)
  {
    synchronized (serviceEventListeners)
    {
      Map<BundleContextImpl, Set<Map.Entry<ServiceListener, FilteredServiceListener>>> listenerSnapshot = new HashMap(serviceEventListeners.size());
      for (Iterator localIterator1 = serviceEventListeners.entrySet().iterator(); localIterator1.hasNext();)
      {
        Map.Entry<BundleContextImpl, CopyOnWriteIdentityMap<ServiceListener, FilteredServiceListener>> entry = (Map.Entry)localIterator1.next();
        CopyOnWriteIdentityMap<ServiceListener, FilteredServiceListener> listeners = (CopyOnWriteIdentityMap)entry.getValue();
        if (!listeners.isEmpty()) {
          listenerSnapshot.put((BundleContextImpl)entry.getKey(), listeners.entrySet());
        }
      }
    }
    Map<BundleContextImpl, Set<Map.Entry<ServiceListener, FilteredServiceListener>>> listenerSnapshot;
    Collection<BundleContext> contexts = asBundleContexts(listenerSnapshot.keySet());
    notifyEventHooksPrivileged(event, contexts);
    if (listenerSnapshot.isEmpty()) {
      return;
    }
    Map<BundleContext, Collection<ListenerHook.ListenerInfo>> listeners = new ShrinkableValueCollectionMap(listenerSnapshot);
    notifyEventListenerHooksPrivileged(event, listeners);
    if (listenerSnapshot.isEmpty()) {
      return;
    }
    Object queue = framework.newListenerQueue();
    for (Iterator localIterator2 = listenerSnapshot.entrySet().iterator(); localIterator2.hasNext();)
    {
      Map.Entry<BundleContextImpl, Set<Map.Entry<ServiceListener, FilteredServiceListener>>> entry = (Map.Entry)localIterator2.next();
      
      EventDispatcher<ServiceListener, FilteredServiceListener, ServiceEvent> dispatcher = (EventDispatcher)entry.getKey();
      Set<Map.Entry<ServiceListener, FilteredServiceListener>> listenerSet = (Set)entry.getValue();
      ((ListenerQueue)queue).queueListeners(listenerSet, dispatcher);
    }
    ((ListenerQueue)queue).dispatchEventSynchronous(3, event);
  }
  
  private static Collection<BundleContext> asBundleContexts(Collection<? extends BundleContext> c)
  {
    return c;
  }
  
  synchronized long getNextServiceId()
  {
    long id = serviceid;
    serviceid = (id + 1L);
    return id;
  }
  
  void addServiceRegistration(BundleContextImpl context, ServiceRegistrationImpl<?> registration)
  {
    assert (Thread.holdsLock(this));
    
    List<ServiceRegistrationImpl<?>> contextServices = (List)publishedServicesByContext.get(context);
    if (contextServices == null)
    {
      contextServices = new ArrayList(10);
      publishedServicesByContext.put(context, contextServices);
    }
    contextServices.add(registration);
    String[] arrayOfString;
    int j = (arrayOfString = registration.getClasses()).length;
    for (int i = 0; i < j; i++)
    {
      String clazz = arrayOfString[i];
      List<ServiceRegistrationImpl<?>> services = (List)publishedServicesByClass.get(clazz);
      if (services == null)
      {
        services = new ArrayList(10);
        publishedServicesByClass.put(clazz, services);
      }
      int insertIndex = -Collections.binarySearch(services, registration) - 1;
      services.add(insertIndex, registration);
    }
    int insertIndex = -Collections.binarySearch(allPublishedServices, registration) - 1;
    allPublishedServices.add(insertIndex, registration);
  }
  
  void modifyServiceRegistration(BundleContextImpl context, ServiceRegistrationImpl<?> registration)
  {
    assert (Thread.holdsLock(this));
    String[] arrayOfString;
    int j = (arrayOfString = registration.getClasses()).length;
    for (int i = 0; i < j; i++)
    {
      String clazz = arrayOfString[i];
      List<ServiceRegistrationImpl<?>> services = (List)publishedServicesByClass.get(clazz);
      services.remove(registration);
      
      int insertIndex = -Collections.binarySearch(services, registration) - 1;
      services.add(insertIndex, registration);
    }
    allPublishedServices.remove(registration);
    
    int insertIndex = -Collections.binarySearch(allPublishedServices, registration) - 1;
    allPublishedServices.add(insertIndex, registration);
  }
  
  void removeServiceRegistration(BundleContextImpl context, ServiceRegistrationImpl<?> registration)
  {
    assert (Thread.holdsLock(this));
    
    List<ServiceRegistrationImpl<?>> contextServices = (List)publishedServicesByContext.get(context);
    if (contextServices != null) {
      contextServices.remove(registration);
    }
    String[] arrayOfString;
    int j = (arrayOfString = registration.getClasses()).length;
    for (int i = 0; i < j; i++)
    {
      String clazz = arrayOfString[i];
      List<ServiceRegistrationImpl<?>> services = (List)publishedServicesByClass.get(clazz);
      services.remove(registration);
      if (services.isEmpty()) {
        publishedServicesByClass.remove(clazz);
      }
    }
    allPublishedServices.remove(registration);
  }
  
  private List<ServiceRegistrationImpl<?>> lookupServiceRegistrations(String clazz, Filter filter)
  {
    synchronized (this)
    {
      List<ServiceRegistrationImpl<?>> result;
      if (clazz == null) {
        result = allPublishedServices;
      } else {
        result = (List)publishedServicesByClass.get(clazz);
      }
      if ((result == null) || (result.isEmpty()))
      {
        List<ServiceRegistrationImpl<?>> empty = Collections.EMPTY_LIST;
        return empty;
      }
      List<ServiceRegistrationImpl<?>> result = new ArrayList(result);
    }
    List<ServiceRegistrationImpl<?>> result;
    if (filter == null) {
      return result;
    }
    for (Iterator<ServiceRegistrationImpl<?>> iter = result.iterator(); iter.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)iter.next();
      try
      {
        reference = registration.getReferenceImpl();
      }
      catch (IllegalStateException localIllegalStateException)
      {
        ServiceReferenceImpl<?> reference;
        iter.remove();
        continue;
      }
      ServiceReferenceImpl<?> reference;
      if (!filter.match(reference)) {
        iter.remove();
      }
    }
    return result;
  }
  
  private synchronized List<ServiceRegistrationImpl<?>> lookupServiceRegistrations(BundleContextImpl context)
  {
    List<ServiceRegistrationImpl<?>> result = (List)publishedServicesByContext.get(context);
    if ((result == null) || (result.isEmpty()))
    {
      List<ServiceRegistrationImpl<?>> empty = Collections.EMPTY_LIST;
      return empty;
    }
    return new ArrayList(result);
  }
  
  private synchronized void removeServiceRegistrations(BundleContextImpl context)
  {
    publishedServicesByContext.remove(context);
  }
  
  private static void checkRegisterServicePermission(String[] names)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
      return;
    }
    int i = 0;
    for (int len = names.length; i < len; i++) {
      sm.checkPermission(new ServicePermission(names[i], "register"));
    }
  }
  
  private static void checkGetServicePermission(ServiceReference<?> reference)
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
      return;
    }
    sm.checkPermission(new ServicePermission(reference, "get"));
  }
  
  static boolean hasListenServicePermission(ServiceEvent event, BundleContextImpl context)
  {
    ProtectionDomain domain = context.getBundleImpl().getProtectionDomain();
    if (domain == null) {
      return true;
    }
    return domain.implies(new ServicePermission(event.getServiceReference(), "get"));
  }
  
  static String checkServiceClass(String[] clazzes, Object serviceObject)
  {
    ClassLoader cl = (ClassLoader)AccessController.doPrivileged(new PrivilegedAction()
    {
      public ClassLoader run()
      {
        return getClass().getClassLoader();
      }
    });
    int i = 0;
    for (int len = clazzes.length; i < len; i++) {
      try
      {
        Class<?> serviceClazz = cl == null ? Class.forName(clazzes[i]) : cl.loadClass(clazzes[i]);
        if (!serviceClazz.isInstance(serviceObject)) {
          return clazzes[i];
        }
      }
      catch (ClassNotFoundException localClassNotFoundException)
      {
        if (extensiveCheckServiceClass(clazzes[i], serviceObject.getClass())) {
          return clazzes[i];
        }
      }
    }
    return null;
  }
  
  private static boolean extensiveCheckServiceClass(String clazz, Class<?> serviceClazz)
  {
    if (clazz.equals(serviceClazz.getName())) {
      return false;
    }
    Class[] interfaces = serviceClazz.getInterfaces();
    int i = 0;
    for (int len = interfaces.length; i < len; i++) {
      if (!extensiveCheckServiceClass(clazz, interfaces[i])) {
        return false;
      }
    }
    Class<?> superClazz = serviceClazz.getSuperclass();
    if ((superClazz != null) && 
      (!extensiveCheckServiceClass(clazz, superClazz))) {
      return false;
    }
    return true;
  }
  
  static boolean isAssignableTo(BundleContextImpl context, ServiceReferenceImpl<?> reference)
  {
    Bundle bundle = context.getBundleImpl();
    String[] clazzes = reference.getClasses();
    int i = 0;
    for (int len = clazzes.length; i < len; i++) {
      if (!reference.isAssignableTo(bundle, clazzes[i])) {
        return false;
      }
    }
    return true;
  }
  
  private void notifyFindHooks(final BundleContextImpl context, final String clazz, final String filterstring, final boolean allservices, final Collection<ServiceReference<?>> result)
  {
    if (System.getSecurityManager() == null) {
      notifyFindHooksPrivileged(context, clazz, filterstring, allservices, result);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          notifyFindHooksPrivileged(context, clazz, filterstring, allservices, result);
          return null;
        }
      });
    }
  }
  
  void notifyFindHooksPrivileged(final BundleContextImpl context, final String clazz, final String filterstring, final boolean allservices, final Collection<ServiceReference<?>> result)
  {
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyServiceFindHooks(" + context.getBundleImpl() + "," + clazz + "," + filterstring + "," + allservices + "," + result + ")");
    }
    notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof FindHook)) {
          ((FindHook)hook).find(context, clazz, filterstring, allservices, result);
        }
      }
      
      public String getHookClassName()
      {
        return ServiceRegistry.findHookName;
      }
      
      public String getHookMethodName()
      {
        return "find";
      }
    });
  }
  
  private void notifyEventHooksPrivileged(final ServiceEvent event, final Collection<BundleContext> result)
  {
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyServiceEventHooks(" + event.getType() + ":" + event.getServiceReference() + "," + result + ")");
    }
    notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof EventHook)) {
          ((EventHook)hook).event(event, result);
        }
      }
      
      public String getHookClassName()
      {
        return ServiceRegistry.eventHookName;
      }
      
      public String getHookMethodName()
      {
        return "event";
      }
    });
  }
  
  private void notifyEventListenerHooksPrivileged(final ServiceEvent event, final Map<BundleContext, Collection<ListenerHook.ListenerInfo>> result)
  {
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyServiceEventListenerHooks(" + event.getType() + ":" + event.getServiceReference() + "," + result + ")");
    }
    notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof EventListenerHook)) {
          ((EventListenerHook)hook).event(event, result);
        }
      }
      
      public String getHookClassName()
      {
        return ServiceRegistry.eventListenerHookName;
      }
      
      public String getHookMethodName()
      {
        return "event";
      }
    });
  }
  
  public void notifyHooksPrivileged(HookContext hookContext)
  {
    BundleContextImpl systemBundleContext = framework.getSystemBundleContext();
    if (systemBundleContext == null) {
      return;
    }
    List<ServiceRegistrationImpl<?>> hooks = lookupServiceRegistrations(hookContext.getHookClassName(), null);
    for (Iterator localIterator = hooks.iterator(); localIterator.hasNext();)
    {
      ServiceRegistrationImpl<?> registration = (ServiceRegistrationImpl)localIterator.next();
      notifyHookPrivileged(systemBundleContext, registration, hookContext);
    }
  }
  
  /* Error */
  private void notifyHookPrivileged(BundleContextImpl context, ServiceRegistrationImpl<?> registration, HookContext hookContext)
  {
    // Byte code:
    //   0: aload_2
    //   1: aload_1
    //   2: invokevirtual 808	org/eclipse/osgi/internal/serviceregistry/ServiceRegistrationImpl:getSafeService	(Lorg/eclipse/osgi/framework/internal/core/BundleContextImpl;)Ljava/lang/Object;
    //   5: astore 4
    //   7: aload 4
    //   9: ifnonnull +4 -> 13
    //   12: return
    //   13: aload_3
    //   14: aload 4
    //   16: aload_2
    //   17: invokeinterface 882 3 0
    //   22: goto +148 -> 170
    //   25: astore 5
    //   27: getstatic 723	org/eclipse/osgi/framework/debug/Debug:DEBUG_HOOKS	Z
    //   30: ifeq +61 -> 91
    //   33: new 350	java/lang/StringBuffer
    //   36: dup
    //   37: aload 4
    //   39: invokevirtual 756	java/lang/Object:getClass	()Ljava/lang/Class;
    //   42: invokevirtual 749	java/lang/Class:getName	()Ljava/lang/String;
    //   45: invokestatic 760	java/lang/String:valueOf	(Ljava/lang/Object;)Ljava/lang/String;
    //   48: invokespecial 762	java/lang/StringBuffer:<init>	(Ljava/lang/String;)V
    //   51: ldc_w 318
    //   54: invokevirtual 766	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   57: aload_3
    //   58: invokeinterface 881 1 0
    //   63: invokevirtual 766	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   66: ldc_w 315
    //   69: invokevirtual 766	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   72: aload 5
    //   74: invokevirtual 770	java/lang/Throwable:getMessage	()Ljava/lang/String;
    //   77: invokevirtual 766	java/lang/StringBuffer:append	(Ljava/lang/String;)Ljava/lang/StringBuffer;
    //   80: invokevirtual 761	java/lang/StringBuffer:toString	()Ljava/lang/String;
    //   83: invokestatic 779	org/eclipse/osgi/framework/debug/Debug:println	(Ljava/lang/String;)V
    //   86: aload 5
    //   88: invokestatic 780	org/eclipse/osgi/framework/debug/Debug:printStackTrace	(Ljava/lang/Throwable;)V
    //   91: aload_0
    //   92: getfield 743	org/eclipse/osgi/internal/serviceregistry/ServiceRegistry:framework	Lorg/eclipse/osgi/framework/internal/core/Framework;
    //   95: invokevirtual 792	org/eclipse/osgi/framework/internal/core/Framework:getAdaptor	()Lorg/eclipse/osgi/framework/adaptor/FrameworkAdaptor;
    //   98: aload 5
    //   100: invokeinterface 879 2 0
    //   105: new 395	org/osgi/framework/ServiceException
    //   108: dup
    //   109: getstatic 727	org/eclipse/osgi/framework/internal/core/Msg:SERVICE_FACTORY_EXCEPTION	Ljava/lang/String;
    //   112: aload 4
    //   114: invokevirtual 756	java/lang/Object:getClass	()Ljava/lang/Class;
    //   117: invokevirtual 749	java/lang/Class:getName	()Ljava/lang/String;
    //   120: aload_3
    //   121: invokeinterface 881 1 0
    //   126: invokestatic 847	org/eclipse/osgi/util/NLS:bind	(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/String;
    //   129: aload 5
    //   131: invokespecial 851	org/osgi/framework/ServiceException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   134: astore 6
    //   136: aload_0
    //   137: getfield 743	org/eclipse/osgi/internal/serviceregistry/ServiceRegistry:framework	Lorg/eclipse/osgi/framework/internal/core/Framework;
    //   140: iconst_2
    //   141: aload_2
    //   142: invokevirtual 807	org/eclipse/osgi/internal/serviceregistry/ServiceRegistrationImpl:getBundle	()Lorg/osgi/framework/Bundle;
    //   145: aload 6
    //   147: invokevirtual 795	org/eclipse/osgi/framework/internal/core/Framework:publishFrameworkEvent	(ILorg/osgi/framework/Bundle;Ljava/lang/Throwable;)V
    //   150: aload_2
    //   151: aload_1
    //   152: invokevirtual 805	org/eclipse/osgi/internal/serviceregistry/ServiceRegistrationImpl:ungetService	(Lorg/eclipse/osgi/framework/internal/core/BundleContextImpl;)Z
    //   155: pop
    //   156: goto +20 -> 176
    //   159: astore 7
    //   161: aload_2
    //   162: aload_1
    //   163: invokevirtual 805	org/eclipse/osgi/internal/serviceregistry/ServiceRegistrationImpl:ungetService	(Lorg/eclipse/osgi/framework/internal/core/BundleContextImpl;)Z
    //   166: pop
    //   167: aload 7
    //   169: athrow
    //   170: aload_2
    //   171: aload_1
    //   172: invokevirtual 805	org/eclipse/osgi/internal/serviceregistry/ServiceRegistrationImpl:ungetService	(Lorg/eclipse/osgi/framework/internal/core/BundleContextImpl;)Z
    //   175: pop
    //   176: return
    // Line number table:
    //   Java source line #1234	-> byte code offset #0
    //   Java source line #1235	-> byte code offset #7
    //   Java source line #1236	-> byte code offset #12
    //   Java source line #1239	-> byte code offset #13
    //   Java source line #1240	-> byte code offset #25
    //   Java source line #1241	-> byte code offset #27
    //   Java source line #1242	-> byte code offset #33
    //   Java source line #1243	-> byte code offset #86
    //   Java source line #1246	-> byte code offset #91
    //   Java source line #1247	-> byte code offset #105
    //   Java source line #1248	-> byte code offset #136
    //   Java source line #1250	-> byte code offset #150
    //   Java source line #1249	-> byte code offset #159
    //   Java source line #1250	-> byte code offset #161
    //   Java source line #1251	-> byte code offset #167
    //   Java source line #1250	-> byte code offset #170
    //   Java source line #1252	-> byte code offset #176
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	177	0	this	ServiceRegistry
    //   0	177	1	context	BundleContextImpl
    //   0	177	2	registration	ServiceRegistrationImpl<?>
    //   0	177	3	hookContext	HookContext
    //   5	108	4	hook	Object
    //   25	105	5	t	Throwable
    //   134	12	6	se	org.osgi.framework.ServiceException
    //   159	9	7	localObject1	Object
    // Exception table:
    //   from	to	target	type
    //   13	22	25	java/lang/Throwable
    //   13	150	159	finally
  }
  
  private void notifyNewListenerHook(final ServiceRegistrationImpl<?> registration)
  {
    if (System.getSecurityManager() == null) {
      notifyNewListenerHookPrivileged(registration);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          notifyNewListenerHookPrivileged(registration);
          return null;
        }
      });
    }
  }
  
  void notifyNewListenerHookPrivileged(ServiceRegistrationImpl<?> registration)
  {
    BundleContextImpl systemBundleContext = framework.getSystemBundleContext();
    if (systemBundleContext == null) {
      return;
    }
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyServiceNewListenerHook(" + registration + ")");
    }
    Collection<ListenerHook.ListenerInfo> addedListeners = new ArrayList(50);
    synchronized (serviceEventListeners)
    {
      for (Iterator localIterator = serviceEventListeners.values().iterator(); localIterator.hasNext();)
      {
        CopyOnWriteIdentityMap<ServiceListener, FilteredServiceListener> listeners = (CopyOnWriteIdentityMap)localIterator.next();
        if (!listeners.isEmpty()) {
          addedListeners.addAll(listeners.values());
        }
      }
    }
    final Collection<ListenerHook.ListenerInfo> listeners = Collections.unmodifiableCollection(addedListeners);
    notifyHookPrivileged(systemBundleContext, registration, new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
        throws Exception
      {
        if ((hook instanceof ListenerHook)) {
          ((ListenerHook)hook).added(listeners);
        }
      }
      
      public String getHookClassName()
      {
        return ServiceRegistry.listenerHookName;
      }
      
      public String getHookMethodName()
      {
        return "added";
      }
    });
  }
  
  private void notifyListenerHooks(final Collection<ListenerHook.ListenerInfo> listeners, final boolean added)
  {
    if (System.getSecurityManager() == null) {
      notifyListenerHooksPrivileged(listeners, added);
    } else {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          notifyListenerHooksPrivileged(listeners, added);
          return null;
        }
      });
    }
  }
  
  void notifyListenerHooksPrivileged(final Collection<ListenerHook.ListenerInfo> listeners, final boolean added)
  {
    assert (!listeners.isEmpty());
    if (Debug.DEBUG_HOOKS) {
      Debug.println("notifyServiceListenerHooks(" + listeners + "," + (added ? "added" : "removed") + ")");
    }
    notifyHooksPrivileged(new HookContext()
    {
      public void call(Object hook, ServiceRegistration<?> hookRegistration)
   
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd