org.eclipse.equinox.event_1.2.100.v20110502

16:43:51.777 INFO  jd.cli.Main - Decompiling org.eclipse.equinox.event_1.2.100.v20110502.jar
package org.eclipse.equinox.internal.event;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.EventAdmin;

public class Activator
  implements BundleActivator
{
  private static final String PROP_USE_DS = "equinox.use.ds";
  private ServiceRegistration<EventAdmin> eventAdminService;
  private EventComponent eventAdmin;
  
  public void start(BundleContext bundleContext)
    throws InvalidSyntaxException
  {
    if (Boolean.valueOf(bundleContext.getProperty("equinox.use.ds")).booleanValue()) {
      return;
    }
    String serviceName = EventAdmin.class.getName();
    Filter serviceFilter = bundleContext.createFilter("(objectclass=" + serviceName + ")");
    
    ServiceReference[] refs = bundleContext.getBundle().getRegisteredServices();
    if (refs != null) {
      for (int i = 0; i < refs.length; i++) {
        if (serviceFilter.match(refs[i])) {
          return;
        }
      }
    }
    eventAdmin = new EventComponent();
    eventAdmin.activate(bundleContext);
    eventAdminService = bundleContext.registerService(EventAdmin.class, eventAdmin, null);
  }
  
  public void stop(BundleContext bundleContext)
  {
    if (eventAdmin != null)
    {
      eventAdminService.unregister();
      eventAdmin.deactivate(bundleContext);
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.Activator
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import java.security.Permission;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.osgi.framework.eventmgr.CopyOnWriteIdentityMap;
import org.eclipse.osgi.framework.eventmgr.EventManager;
import org.eclipse.osgi.framework.eventmgr.ListenerQueue;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.event.TopicPermission;

public class EventAdminImpl
  implements EventAdmin
{
  private final LogTracker log;
  private final EventHandlerTracker handlers;
  private volatile EventManager eventManager;
  
  EventAdminImpl(BundleContext context)
  {
    log = new LogTracker(context, System.out);
    handlers = new EventHandlerTracker(context, log);
  }
  
  void start()
  {
    log.open();
    ThreadGroup eventGroup = new ThreadGroup("Equinox Event Admin");
    eventGroup.setDaemon(true);
    eventManager = new EventManager(EventAdminMsg.EVENT_ASYNC_THREAD_NAME, eventGroup);
    handlers.open();
  }
  
  void stop()
  {
    handlers.close();
    eventManager.close();
    eventManager = null;
    log.close();
  }
  
  public void postEvent(Event event)
  {
    dispatchEvent(event, true);
  }
  
  public void sendEvent(Event event)
  {
    dispatchEvent(event, false);
  }
  
  private void dispatchEvent(Event event, boolean isAsync)
  {
    EventManager currentManager = eventManager;
    if (currentManager == null) {
      return;
    }
    if (event == null) {
      log.log(1, EventAdminMsg.EVENT_NULL_EVENT);
    }
    String topic = event.getTopic();
    try
    {
      checkTopicPermissionPublish(topic);
    }
    catch (SecurityException e)
    {
      String msg = NLS.bind(EventAdminMsg.EVENT_NO_TOPICPERMISSION_PUBLISH, event.getTopic());
      log.log(1, msg);
      
      throw e;
    }
    Set<EventHandlerWrapper> eventHandlers = handlers.getHandlers(topic);
    if (eventHandlers.isEmpty()) {
      return;
    }
    SecurityManager sm = System.getSecurityManager();
    Permission perm = sm == null ? null : new TopicPermission(topic, "subscribe");
    
    Map<EventHandlerWrapper, Permission> listeners = new CopyOnWriteIdentityMap();
    for (Iterator localIterator = eventHandlers.iterator(); localIterator.hasNext();)
    {
      EventHandlerWrapper wrapper = (EventHandlerWrapper)localIterator.next();
      listeners.put(wrapper, perm);
    }
    ListenerQueue<EventHandlerWrapper, Permission, Event> listenerQueue = new ListenerQueue(currentManager);
    
    listenerQueue.queueListeners(listeners.entrySet(), handlers);
    if (isAsync) {
      listenerQueue.dispatchEventAsynchronous(0, event);
    } else {
      listenerQueue.dispatchEventSynchronous(0, event);
    }
  }
  
  private void checkTopicPermissionPublish(String topic)
    throws SecurityException
  {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
      return;
    }
    sm.checkPermission(new TopicPermission(topic, "publish"));
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.EventAdminImpl
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import org.eclipse.osgi.util.NLS;

public class EventAdminMsg
  extends NLS
{
  private static final String BUNDLE_NAME = "org.eclipse.equinox.internal.event.ExternalMessages";
  public static String EVENT_ASYNC_THREAD_NAME;
  public static String EVENT_NULL_EVENT;
  public static String EVENT_NO_TOPICPERMISSION_PUBLISH;
  public static String EVENT_DISPATCH_HANDLER_EXCEPTION;
  public static String EVENT_INVALID_HANDLER_FILTER;
  public static String EVENT_INVALID_HANDLER_TOPICS;
  
  static
  {
    NLS.initializeMessages("org.eclipse.equinox.internal.event.ExternalMessages", EventAdminMsg.class);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.EventAdminMsg
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import org.eclipse.equinox.internal.event.mapper.EventRedeliverer;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public class EventComponent
  implements EventAdmin
{
  private EventRedeliverer eventRedeliverer;
  private EventAdminImpl eventAdmin;
  
  void activate(BundleContext context)
  {
    eventAdmin = new EventAdminImpl(context);
    eventAdmin.start();
    eventRedeliverer = new EventRedeliverer(context);
    eventRedeliverer.open();
  }
  
  void deactivate(BundleContext context)
  {
    eventRedeliverer.close();
    eventAdmin.stop();
  }
  
  public void postEvent(Event event)
  {
    eventAdmin.postEvent(event);
  }
  
  public void sendEvent(Event event)
  {
    eventAdmin.sendEvent(event);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.EventComponent
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import java.security.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osgi.framework.eventmgr.EventDispatcher;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

public class EventHandlerTracker
  extends ServiceTracker<EventHandler, EventHandlerWrapper>
  implements EventDispatcher<EventHandlerWrapper, Permission, Event>
{
  private final LogService log;
  private final List<EventHandlerWrapper> globalWildcard;
  private final Map<String, List<EventHandlerWrapper>> partialWildcard;
  private final Map<String, List<EventHandlerWrapper>> topicName;
  
  public EventHandlerTracker(BundleContext context, LogService log)
  {
    super(context, EventHandler.class.getName(), null);
    this.log = log;
    globalWildcard = new ArrayList();
    partialWildcard = new HashMap();
    topicName = new HashMap();
  }
  
  public EventHandlerWrapper addingService(ServiceReference<EventHandler> reference)
  {
    EventHandlerWrapper wrapper = new EventHandlerWrapper(reference, context, log);
    synchronized (this)
    {
      if (wrapper.init()) {
        bucket(wrapper);
      }
    }
    return wrapper;
  }
  
  public void modifiedService(ServiceReference<EventHandler> reference, EventHandlerWrapper service)
  {
    synchronized (this)
    {
      unbucket(service);
      if (service.init())
      {
        bucket(service);
        return;
      }
    }
    service.flush();
  }
  
  public void removedService(ServiceReference<EventHandler> reference, EventHandlerWrapper service)
  {
    synchronized (this)
    {
      unbucket(service);
    }
    service.flush();
  }
  
  private void bucket(EventHandlerWrapper wrapper)
  {
    String[] topics = wrapper.getTopics();
    int length = topics == null ? 0 : topics.length;
    for (int i = 0; i < length; i++)
    {
      String topic = topics[i];
      if (topic.equals("*"))
      {
        globalWildcard.add(wrapper);
      }
      else if (topic.endsWith("/*"))
      {
        String key = topic.substring(0, topic.length() - 2);
        List<EventHandlerWrapper> wrappers = (List)partialWildcard.get(key);
        if (wrappers == null)
        {
          wrappers = new ArrayList();
          partialWildcard.put(key, wrappers);
        }
        wrappers.add(wrapper);
      }
      else
      {
        List<EventHandlerWrapper> wrappers = (List)topicName.get(topic);
        if (wrappers == null)
        {
          wrappers = new ArrayList();
          topicName.put(topic, wrappers);
        }
        wrappers.add(wrapper);
      }
    }
  }
  
  private void unbucket(EventHandlerWrapper wrapper)
  {
    String[] topics = wrapper.getTopics();
    int length = topics == null ? 0 : topics.length;
    for (int i = 0; i < length; i++)
    {
      String topic = topics[i];
      if (topic.equals("*"))
      {
        globalWildcard.remove(wrapper);
      }
      else if (topic.endsWith("/*"))
      {
        String key = topic.substring(0, topic.length() - 2);
        List<EventHandlerWrapper> wrappers = (List)partialWildcard.get(key);
        if (wrappers != null)
        {
          wrappers.remove(wrapper);
          if (wrappers.isEmpty()) {
            partialWildcard.remove(key);
          }
        }
      }
      else
      {
        List<EventHandlerWrapper> wrappers = (List)topicName.get(topic);
        if (wrappers != null)
        {
          wrappers.remove(wrapper);
          if (wrappers.isEmpty()) {
            topicName.remove(topic);
          }
        }
      }
    }
  }
  
  public synchronized Set<EventHandlerWrapper> getHandlers(String topic)
  {
    Set<EventHandlerWrapper> handlers = new HashSet();
    
    handlers.addAll(globalWildcard);
    if (partialWildcard.size() > 0)
    {
      int index = topic.lastIndexOf('/');
      while (index >= 0)
      {
        String subTopic = topic.substring(0, index);
        List<EventHandlerWrapper> wrappers = (List)partialWildcard.get(subTopic);
        if (wrappers != null) {
          handlers.addAll(wrappers);
        }
        index = subTopic.lastIndexOf('/');
      }
    }
    List<EventHandlerWrapper> wrappers = (List)topicName.get(topic);
    if (wrappers != null) {
      handlers.addAll(wrappers);
    }
    return handlers;
  }
  
  public void dispatchEvent(EventHandlerWrapper eventListener, Permission listenerObject, int eventAction, Event eventObject)
  {
    eventListener.handleEvent(eventObject, listenerObject);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.EventHandlerTracker
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

class EventHandlerWrapper$LogTopicException
  extends RuntimeException
{
  private static final long serialVersionUID = -2386940335620739632L;
  
  public EventHandlerWrapper$LogTopicException(Throwable cause)
  {
    super(cause);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.EventHandlerWrapper.LogTopicException
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import java.security.Permission;
import java.util.Collection;
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.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.osgi.service.log.LogService;

public class EventHandlerWrapper
{
  private final ServiceReference<EventHandler> reference;
  private final LogService log;
  private final BundleContext context;
  private EventHandler handler;
  private String[] topics;
  private Filter filter;
  
  public EventHandlerWrapper(ServiceReference<EventHandler> reference, BundleContext context, LogService log)
  {
    this.reference = reference;
    this.context = context;
    this.log = log;
  }
  
  public synchronized boolean init()
  {
    topics = null;
    filter = null;
    
    Object o = reference.getProperty("event.topics");
    if ((o instanceof String)) {
      topics = new String[] { (String)o };
    } else if ((o instanceof String[])) {
      topics = ((String[])o);
    } else if ((o instanceof Collection)) {
      try
      {
        Collection<String> c = (Collection)o;
        topics = ((String[])c.toArray(new String[c.size()]));
      }
      catch (ArrayStoreException e)
      {
        log.log(1, NLS.bind(EventAdminMsg.EVENT_INVALID_HANDLER_TOPICS, o), e);
      }
    }
    if (topics == null) {
      return false;
    }
    o = reference.getProperty("event.filter");
    if ((o instanceof String)) {
      try
      {
        filter = context.createFilter((String)o);
      }
      catch (InvalidSyntaxException e)
      {
        log.log(1, NLS.bind(EventAdminMsg.EVENT_INVALID_HANDLER_FILTER, o), e);
        return false;
      }
    }
    return true;
  }
  
  public void flush()
  {
    synchronized (this)
    {
      if (handler == null) {
        return;
      }
      handler = null;
    }
    try
    {
      context.ungetService(reference);
    }
    catch (IllegalStateException localIllegalStateException) {}
  }
  
  public synchronized String[] getTopics()
  {
    return topics;
  }
  
  private EventHandler getHandler()
  {
    synchronized (this)
    {
      if (handler != null) {
        return handler;
      }
    }
    EventHandler tempHandler = null;
    try
    {
      tempHandler = (EventHandler)context.getService(reference);
    }
    catch (IllegalStateException localIllegalStateException1) {}
    synchronized (this)
    {
      if (handler == null)
      {
        handler = tempHandler;
        return handler;
      }
      tempHandler = handler;
    }
    try
    {
      context.ungetService(reference);
    }
    catch (IllegalStateException localIllegalStateException2) {}
    return tempHandler;
  }
  
  private synchronized Filter getFilter()
  {
    return filter;
  }
  
  public void handleEvent(Event event, Permission perm)
  {
    Bundle bundle = reference.getBundle();
    if (bundle == null) {
      return;
    }
    Filter eventFilter = getFilter();
    if ((eventFilter != null) && (!event.matches(eventFilter))) {
      return;
    }
    if ((perm != null) && (!bundle.hasPermission(perm))) {
      return;
    }
    EventHandler handlerService = getHandler();
    if (handlerService == null) {
      return;
    }
    try
    {
      handlerService.handleEvent(event);
    }
    catch (Throwable t)
    {
      if (event.getTopic().startsWith("org/osgi/service/log/LogEntry"))
      {
        Object exception = event.getProperty("exception");
        if ((exception instanceof LogTopicException)) {
          return;
        }
        t = new LogTopicException(t);
      }
      log.log(1, NLS.bind(EventAdminMsg.EVENT_DISPATCH_HANDLER_EXCEPTION, event, handlerService), t);
    }
  }
  
  static class LogTopicException
    extends RuntimeException
  {
    private static final long serialVersionUID = -2386940335620739632L;
    
    public LogTopicException(Throwable cause)
    {
      super();
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.EventHandlerWrapper
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import java.io.PrintStream;
import java.util.Calendar;
import java.util.Date;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

public class LogTracker
  extends ServiceTracker<LogService, LogService>
  implements LogService
{
  protected static final String clazz = "org.osgi.service.log.LogService";
  private final PrintStream out;
  
  public LogTracker(BundleContext context, PrintStream out)
  {
    super(context, "org.osgi.service.log.LogService", null);
    this.out = out;
  }
  
  public void log(int level, String message)
  {
    log(null, level, message, null);
  }
  
  public void log(int level, String message, Throwable exception)
  {
    log(null, level, message, exception);
  }
  
  public void log(ServiceReference reference, int level, String message)
  {
    log(reference, level, message, null);
  }
  
  public synchronized void log(ServiceReference reference, int level, String message, Throwable exception)
  {
    ServiceReference[] references = getServiceReferences();
    if (references != null)
    {
      int size = references.length;
      for (int i = 0; i < size; i++)
      {
        LogService service = (LogService)getService(references[i]);
        if (service != null) {
          try
          {
            service.log(reference, level, message, exception);
          }
          catch (Exception localException) {}
        }
      }
      return;
    }
    noLogService(level, message, exception, reference);
  }
  
  protected void noLogService(int level, String message, Throwable throwable, ServiceReference<?> reference)
  {
    if (out != null) {
      synchronized (out)
      {
        String timestamp = getDate(new Date());
        out.print(timestamp + " ");
        switch (level)
        {
        case 4: 
          out.print(LogTrackerMsg.Debug);
          
          break;
        case 3: 
          out.print(LogTrackerMsg.Info);
          
          break;
        case 2: 
          out.print(LogTrackerMsg.Warning);
          
          break;
        case 1: 
          out.print(LogTrackerMsg.Error);
          
          break;
        default: 
          out.print("[");
          out.print(LogTrackerMsg.Unknown_Log_level);
          out.print("]: ");
        }
        out.println(message);
        if (reference != null) {
          out.println(reference);
        }
        if (throwable != null) {
          throwable.printStackTrace(out);
        }
      }
    }
  }
  
  private String getDate(Date date)
  {
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    StringBuffer sb = new StringBuffer();
    appendPaddedInt(c.get(1), 4, sb).append('-');
    appendPaddedInt(c.get(2) + 1, 2, sb).append('-');
    appendPaddedInt(c.get(5), 2, sb).append(' ');
    appendPaddedInt(c.get(11), 2, sb).append(':');
    appendPaddedInt(c.get(12), 2, sb).append(':');
    appendPaddedInt(c.get(13), 2, sb).append('.');
    appendPaddedInt(c.get(14), 3, sb);
    return sb.toString();
  }
  
  private StringBuffer appendPaddedInt(int value, int pad, StringBuffer buffer)
  {
    
    if (pad == 0) {
      return buffer.append(Integer.toString(value));
    }
    int padding = (int)Math.pow(10.0D, pad);
    if (value >= padding) {
      return buffer.append(Integer.toString(value));
    }
    while ((padding > value) && (padding > 1))
    {
      buffer.append('0');
      padding /= 10;
    }
    buffer.append(value);
    return buffer;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.LogTracker
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event;

import org.eclipse.osgi.util.NLS;

public class LogTrackerMsg
  extends NLS
{
  private static final String BUNDLE_NAME = "org.eclipse.equinox.internal.event.LogMessages";
  public static String Unknown_Log_level;
  public static String Info;
  public static String Warning;
  public static String Error;
  public static String Debug;
  
  static
  {
    NLS.initializeMessages("org.eclipse.equinox.internal.event.LogMessages", LogTrackerMsg.class);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.LogTrackerMsg
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event.mapper;

import java.util.HashMap;
import java.util.Map;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public class BundleEventAdapter
  extends EventAdapter
{
  public static final String HEADER = "org/osgi/framework/BundleEvent";
  public static final String INSTALLED = "INSTALLED";
  public static final String STOPPED = "STOPPED";
  public static final String STARTED = "STARTED";
  public static final String UPDATED = "UPDATED";
  public static final String UNINSTALLED = "UNINSTALLED";
  public static final String RESOLVED = "RESOLVED";
  public static final String UNRESOLVED = "UNRESOLVED";
  private BundleEvent event;
  
  public BundleEventAdapter(BundleEvent event, EventAdmin eventAdmin)
  {
    super(eventAdmin);
    this.event = event;
  }
  
  public Event convert()
  {
    String typename = null;
    switch (event.getType())
    {
    case 1: 
      typename = "INSTALLED";
      break;
    case 4: 
      typename = "STOPPED";
      break;
    case 2: 
      typename = "STARTED";
      break;
    case 8: 
      typename = "UPDATED";
      break;
    case 16: 
      typename = "UNINSTALLED";
      break;
    case 32: 
      typename = "RESOLVED";
      break;
    case 64: 
      typename = "UNRESOLVED";
      break;
    default: 
      typename = Integer.toString(event.getType());
    }
    String topic = "org/osgi/framework/BundleEvent/" + typename;
    Map<String, Object> properties = new HashMap();
    Bundle bundle = event.getBundle();
    if (bundle == null) {
      throw new RuntimeException("BundleEvent.getBundle() returns null");
    }
    putBundleProperties(properties, bundle);
    properties.put("event", event);
    Event converted = new Event(topic, properties);
    return converted;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.mapper.BundleEventAdapter
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event.mapper;

public abstract interface Constants
{
  public static final String BUNDLE = "bundle";
  public static final String BUNDLE_ID = "bundle.id";
  public static final String BUNDLE_SYMBOLICNAME = "bundle.symbolicName";
  public static final String EVENT = "event";
  public static final String EXCEPTION = "exception";
  public static final String EXCEPTION_CLASS = "exception.class";
  public static final String EXCEPTION_MESSAGE = "exception.message";
  public static final String MESSAGE = "message";
  public static final String SERVICE = "service";
  public static final String SERVICE_ID = "service.id";
  public static final String SERVICE_OBJECTCLASS = "service.objectClass";
  public static final String SERVICE_PID = "service.pid";
  public static final String TIMESTAMP = "timestamp";
  public static final char TOPIC_SEPARATOR = '/';
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.mapper.Constants
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event.mapper;

import java.util.Map;
import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public abstract class EventAdapter
{
  final EventAdmin eventAdmin;
  
  public EventAdapter(EventAdmin eventAdmin)
  {
    this.eventAdmin = eventAdmin;
  }
  
  public abstract Event convert();
  
  public void redeliver()
  {
    Event converted = convert();
    if (converted != null) {
      redeliverInternal(converted);
    }
  }
  
  protected void redeliverInternal(Event converted)
  {
    eventAdmin.postEvent(converted);
  }
  
  public void putBundleProperties(Map<String, Object> properties, Bundle bundle)
  {
    properties.put("bundle.id", new Long(bundle.getBundleId()));
    String symbolicName = bundle.getSymbolicName();
    if (symbolicName != null) {
      properties.put("bundle.symbolicName", symbolicName);
    }
    properties.put("bundle", bundle);
  }
  
  public void putExceptionProperties(Map<String, Object> properties, Throwable t)
  {
    properties.put("exception", t);
    properties.put("exception.class", t.getClass().getName());
    String message = t.getMessage();
    if (message != null) {
      properties.put("exception.message", t.getMessage());
    }
  }
  
  public void putServiceReferenceProperties(Map<String, Object> properties, ServiceReference<?> ref)
  {
    properties.put("service", ref);
    properties.put("service.id", ref.getProperty("service.id"));
    Object o = ref.getProperty("service.pid");
    if ((o != null) && ((o instanceof String))) {
      properties.put("service.pid", o);
    }
    Object o2 = ref.getProperty("objectClass");
    if ((o2 != null) && ((o2 instanceof String[]))) {
      properties.put("service.objectClass", o2);
    }
  }
  
  public String[] classes2strings(Class<?>[] classes)
  {
    if ((classes == null) || (classes.length == 0)) {
      return null;
    }
    String[] strings = new String[classes.length];
    for (int i = 0; i < classes.length; i++) {
      strings[i] = classes[i].getName();
    }
    return strings;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.mapper.EventAdapter
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event.mapper;

import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;

public class EventRedeliverer
  implements FrameworkListener, BundleListener, ServiceListener
{
  private final ServiceTracker<EventAdmin, EventAdmin> eventAdminTracker;
  private static final boolean DEBUG = false;
  private BundleContext bc;
  
  public EventRedeliverer(BundleContext bc)
  {
    this.bc = bc;
    eventAdminTracker = new ServiceTracker(bc, EventAdmin.class.getName(), null);
  }
  
  public void close()
  {
    eventAdminTracker.close();
    bc.removeFrameworkListener(this);
    bc.removeBundleListener(this);
    bc.removeServiceListener(this);
  }
  
  public void open()
  {
    eventAdminTracker.open();
    
    bc.addFrameworkListener(this);
    bc.addBundleListener(this);
    bc.addServiceListener(this);
  }
  
  private EventAdmin getEventAdmin()
  {
    return (EventAdmin)eventAdminTracker.getService();
  }
  
  public void frameworkEvent(FrameworkEvent event)
  {
    EventAdmin eventAdmin = getEventAdmin();
    if (eventAdmin != null) {
      new FrameworkEventAdapter(event, eventAdmin).redeliver();
    } else {
      printNoEventAdminError();
    }
  }
  
  private void printNoEventAdminError() {}
  
  public void bundleChanged(BundleEvent event)
  {
    EventAdmin eventAdmin = getEventAdmin();
    if (eventAdmin != null) {
      new BundleEventAdapter(event, eventAdmin).redeliver();
    } else {
      printNoEventAdminError();
    }
  }
  
  public void serviceChanged(ServiceEvent event)
  {
    EventAdmin eventAdmin = getEventAdmin();
    if (eventAdmin != null) {
      new ServiceEventAdapter(event, eventAdmin).redeliver();
    } else {
      printNoEventAdminError();
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.mapper.EventRedeliverer
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event.mapper;

import java.util.HashMap;
import java.util.Map;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkEvent;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public class FrameworkEventAdapter
  extends EventAdapter
{
  public static final String HEADER = "org/osgi/framework/FrameworkEvent";
  public static final String STARTLEVEL_CHANGED = "STARTLEVEL_CHANGED";
  public static final String STARTED = "STARTED";
  public static final String PACKAGES_REFRESHED = "PACKAGES_REFRESHED";
  public static final String ERROR = "ERROR";
  protected FrameworkEvent event;
  
  public FrameworkEventAdapter(FrameworkEvent event, EventAdmin eventAdmin)
  {
    super(eventAdmin);
    this.event = event;
  }
  
  public Event convert()
  {
    String typename = null;
    switch (event.getType())
    {
    case 2: 
      typename = "ERROR";
      break;
    case 4: 
      typename = "PACKAGES_REFRESHED";
      break;
    case 1: 
      typename = "STARTED";
      break;
    case 8: 
      typename = "STARTLEVEL_CHANGED";
      break;
    case 3: 
    case 5: 
    case 6: 
    case 7: 
    default: 
      return null;
    }
    String topic = "org/osgi/framework/FrameworkEvent/" + typename;
    Map<String, Object> properties = new HashMap();
    Bundle bundle = event.getBundle();
    if (bundle != null) {
      putBundleProperties(properties, bundle);
    }
    Throwable t = event.getThrowable();
    if (t != null) {
      putExceptionProperties(properties, t);
    }
    properties.put("event", event);
    Event converted = new Event(topic, properties);
    return converted;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.mapper.FrameworkEventAdapter
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.event.mapper;

import java.util.HashMap;
import java.util.Map;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

public class ServiceEventAdapter
  extends EventAdapter
{
  public static final String HEADER = "org/osgi/framework/ServiceEvent";
  public static final String UNREGISTERING = "UNREGISTERING";
  public static final String MODIFIED = "MODIFIED";
  public static final String REGISTERED = "REGISTERED";
  private ServiceEvent event;
  
  public ServiceEventAdapter(ServiceEvent event, EventAdmin eventAdmin)
  {
    super(eventAdmin);
    this.event = event;
  }
  
  public Event convert()
  {
    String typename = null;
    switch (event.getType())
    {
    case 1: 
      typename = "REGISTERED";
      break;
    case 2: 
      typename = "MODIFIED";
      break;
    case 4: 
      typename = "UNREGISTERING";
      break;
    case 3: 
    default: 
      return null;
    }
    String topic = "org/osgi/framework/ServiceEvent/" + typename;
    Map<String, Object> properties = new HashMap();
    ServiceReference<?> ref = event.getServiceReference();
    if (ref != null) {
      putServiceReferenceProperties(properties, ref);
    }
    properties.put("event", event);
    Event converted = new Event(topic, properties);
    return converted;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.event.mapper.ServiceEventAdapter
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
1

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