Java Code Examples for org.osgi.framework.ServiceEvent#getServiceReference()

The following examples show how to use org.osgi.framework.ServiceEvent#getServiceReference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RDF2GoActivator.java    From semweb4j with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void handleRegisteredEvent(ServiceEvent event) {
	ServiceReference ref = event.getServiceReference();
	ModelFactory factory = (ModelFactory)this.bc.getService(ref);
	String currClass = getCurrentFactoryClassName();
	String newClass = factory.getClass().getName();
	if(this.currentFactorySR == null) {
		log.debug("Registering " + factory.getClass().getName() + " as default ModelFactory");
		this.currentFactorySR = ref;
		RDF2Go.register(factory);
	} else if(this.defaultFactoryClassName != null
	        && !currClass.equals(this.defaultFactoryClassName)
	        && newClass.equals(this.defaultFactoryClassName)) {
		RDF2Go.register((ModelFactory)null);
		this.bc.ungetService(this.currentFactorySR);
		
		factory = (ModelFactory)this.bc.getService(ref);
		this.currentFactorySR = ref;
		RDF2Go.register(factory);
		log.debug("RDF2Go uses " + newClass + " as default ModelFactory");
	} else {
		this.bc.ungetService(ref);
	}
}
 
Example 2
Source File: CMCommands.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent event)
{
  switch (event.getType()) {
  case ServiceEvent.REGISTERED:
    @SuppressWarnings("unchecked")
    final ServiceReference<ConfigurationAdmin> sr =
      (ServiceReference<ConfigurationAdmin>) event.getServiceReference();
    if (refCA != sr) {
      refCA = sr;
    }
    break;
  case ServiceEvent.MODIFIED:
    break;
  case ServiceEvent.UNREGISTERING:
    if (refCA != null) {
      refCA = null;
    }
    break;
  default:
    break;
  }
}
 
Example 3
Source File: LogFrameworkListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * The service event callback method inserts all service events into the
 * log.
 * 
 * Event of types REGISTERED, UNREGISTERED are assigned the log
 * level info.
 * 
 * Events of type MODIFIED are assigned the log level DEBUG.
 * 
 * @param se
 *            the service event that has occurred.
 */
public void serviceChanged(ServiceEvent se) {
    ServiceReference<?> sr = se.getServiceReference();
    Bundle bundle = sr.getBundle();
    String msg = null;
    int level = LogService.LOG_INFO;
    switch (se.getType()) {
    case ServiceEvent.REGISTERED:
        msg = "ServiceEvent REGISTERED";
        break;
    case ServiceEvent.UNREGISTERING:
        msg = "ServiceEvent UNREGISTERING";
        break;
    case ServiceEvent.MODIFIED:
        msg = "ServiceEvent MODIFIED";
        level = LogService.LOG_DEBUG;
        break;
    }
    lrsf.log(new LogEntryImpl(bundle, sr, level, msg));
}
 
Example 4
Source File: ComponentServiceListener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent event)
{
  ServiceReference<?> sr = event.getServiceReference();
  for (String cn : (String [])sr.getProperty(Constants.OBJECTCLASS)) {
    Set<ReferenceListener> rls = serviceListeners.get(cn);
    if (rls != null) {
      for (ReferenceListener rl : rls) {
        rl.serviceEvent(sr, event);
      }
    }
  }
  List<Runnable> postrun = afterServiceEvent.get(event);
  if (postrun != null) {
    for (Runnable r : postrun) {
      r.run();
    }
  }
}
 
Example 5
Source File: RDF2GoActivator.java    From semweb4j with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void handleUnregisteredEvent(ServiceEvent event) {
	ServiceReference ref = event.getServiceReference();
	String currClass = getCurrentFactoryClassName();
	if(ref == this.currentFactorySR) {
		RDF2Go.register((ModelFactory)null);
		this.currentFactorySR = null;
		log.debug("RDF2Go unregistered the ModelFactory " + currClass
		        + " as default ModelFactory. No ModelFactory available now. "
		        + "The Bundle of the ModelFactory was unregistered.");
	}
	this.bc.ungetService(ref);
	initalizeListener();
}
 
Example 6
Source File: ServiceTracker.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * {@code ServiceListener} method for the {@code ServiceTracker} class.
 * This method must NOT be synchronized to avoid deadlock potential.
 * 
 * @param event {@code ServiceEvent} object from the framework.
 */
final public void serviceChanged(final ServiceEvent event) {
	/*
	 * Check if we had a delayed call (which could happen when we
	 * close).
	 */
	if (closed) {
		return;
	}
	@SuppressWarnings("unchecked")
	final ServiceReference<S> reference = (ServiceReference<S>) event.getServiceReference();
	if (DEBUG) {
		System.out.println("ServiceTracker.Tracked.serviceChanged[" + event.getType() + "]: " + reference);
	}

	switch (event.getType()) {
		case ServiceEvent.REGISTERED :
		case ServiceEvent.MODIFIED :
			track(reference, event);
			/*
			 * If the customizer throws an unchecked exception, it is
			 * safe to let it propagate
			 */
			break;
		case ServiceEvent.MODIFIED_ENDMATCH :
		case ServiceEvent.UNREGISTERING :
			untrack(reference, event);
			/*
			 * If the customizer throws an unchecked exception, it is
			 * safe to let it propagate
			 */
			break;
	}
}
 
Example 7
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
String toString(ServiceEvent evt)
{
  if (null==evt) return " - NONE - ";

  ServiceReference sr = evt.getServiceReference();
  String[] objectClasses = (String[]) sr.getProperty(Constants.OBJECTCLASS);
  String res = toStringEventType(evt.getType());

  for (int i=0; i<objectClasses.length; i++){
    if (i>0) res += ", ";
    res += objectClasses[i];
  }
  return res;
}
 
Example 8
Source File: RegListenThread.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public synchronized void serviceChanged(ServiceEvent se) {
  ServiceReference srlocal = se.getServiceReference();
  try {
    Thread.sleep(500); // sleep for longer time that the registering thread sleeps
                       // as we need to be hanging here to create 
                       // a lock for the registering thread
  }
  catch (Exception ex) {
    out.println("### Frame test bundle :FRAME210A exception, in RegListenThread");
    ex.printStackTrace(out);
    status = false;
  }

  bc.getService(srlocal);
}
 
Example 9
Source File: ConfigurationAdminFactory.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent event)
{
  final ServiceReference<?> sr = event.getServiceReference();
  final int eventType = event.getType();
  final String[] objectClasses = (String[]) sr.getProperty("objectClass");
  for (final String objectClasse : objectClasses) {
    serviceChanged(sr, eventType, objectClasse);
  }
}
 
Example 10
Source File: LogReaderDispatcher.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent ev)
{
  @SuppressWarnings("unchecked")
  final
  ServiceReference<LogReaderService> sr =
    (ServiceReference<LogReaderService>) ev.getServiceReference();

  final LogReaderService lr =
    logReaders.containsKey(sr) ? logReaders.get(sr) : (LogReaderService) bc
        .getService(sr);

  if (null != lr) {
    switch (ev.getType()) {
    case ServiceEvent.REGISTERED:
      lr.addLogListener(this);
      logReaders.put(sr, lr);
      break;
    case ServiceEvent.MODIFIED:
      break;
    case ServiceEvent.UNREGISTERING:
      lr.removeLogListener(this);
      logReaders.remove(sr);
      bc.ungetService(sr);
      break;
    }
  }
}
 
Example 11
Source File: Command.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent e) {
    if (e.getServiceReference() == commandGroupRef) {
        synchronized (this) {
            // Wait for run command
        }
    }
}
 
Example 12
Source File: LogRef.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Service listener entry point. Releases the log service object if one has
 * been fetched.
 * 
 * @param evt
 *          Service event
 */
public void serviceChanged(ServiceEvent evt)
{
  if (evt.getServiceReference() == logSR
      && evt.getType() == ServiceEvent.UNREGISTERING) {
    ungetLogService();
  }
}
 
Example 13
Source File: UserAdminImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void serviceChanged(ServiceEvent event) {
    ServiceReference sr = event.getServiceReference();
    switch (event.getType()) {
    case ServiceEvent.REGISTERED:
        listeners.addElement(sr);
        if (Activator.log.doDebug())
            Activator.log.debug("UserAdminListener found: " + sr);
        break;
    case ServiceEvent.UNREGISTERING:
        if (listeners.removeElement(sr) && Activator.log.doDebug())
            Activator.log.debug("UserAdminListener gone: " + sr);
        break;
    }
}
 
Example 14
Source File: ServiceTracker.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@code ServiceListener} method for the {@code ServiceTracker} class.
 * This method must NOT be synchronized to avoid deadlock potential.
 * 
 * @param event {@code ServiceEvent} object from the framework.
 */
final public void serviceChanged(final ServiceEvent event) {
	/*
	 * Check if we had a delayed call (which could happen when we
	 * close).
	 */
	if (closed) {
		return;
	}
	@SuppressWarnings("unchecked")
	final ServiceReference<S> reference = (ServiceReference<S>) event.getServiceReference();
	if (DEBUG) {
		System.out.println("ServiceTracker.Tracked.serviceChanged[" + event.getType() + "]: " + reference);
	}

	switch (event.getType()) {
		case ServiceEvent.REGISTERED :
		case ServiceEvent.MODIFIED :
			track(reference, event);
			/*
			 * If the customizer throws an unchecked exception, it is
			 * safe to let it propagate
			 */
			break;
		case ServiceEvent.MODIFIED_ENDMATCH :
		case ServiceEvent.UNREGISTERING :
			untrack(reference, event);
			/*
			 * If the customizer throws an unchecked exception, it is
			 * safe to let it propagate
			 */
			break;
	}
}
 
Example 15
Source File: ServiceImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public synchronized void serviceChanged ( final ServiceEvent event )
{
    logger.debug ( "Service changed: {}", event );
    final ServiceReference<?> ref = event.getServiceReference ();

    try
    {
        switch ( event.getType () )
        {
            case ServiceEvent.REGISTERED:
                checkAddConditionQuery ( ref );
                checkAddEventQuery ( ref );
                break;
            case ServiceEvent.UNREGISTERING:
                final String id = getQueryId ( ref );
                final MonitorQuery query = this.conditionQueryRefs.remove ( id );
                if ( query != null )
                {
                    removeMonitorQuery ( id, query );
                    this.context.ungetService ( ref );
                }
                final EventQuery eventQuery = this.eventQueryRefs.remove ( id );
                if ( eventQuery != null )
                {
                    removeEventQuery ( id, eventQuery );
                    this.context.ungetService ( ref );
                }
                break;
        }
    }
    catch ( final Exception e )
    {
        logger.warn ( "Failed to handle service change", e );
    }
}
 
Example 16
Source File: NetigsoServices.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent ev) {
    final ServiceReference ref = ev.getServiceReference();
    if (ev.getType() == ServiceEvent.REGISTERED) {
        MainLookup.register(ref, this);
    }
    if (ev.getType() == ServiceEvent.UNREGISTERING) {
        MainLookup.unregister(ref, this);
    }
}
 
Example 17
Source File: Repositories.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void serviceChanged(ServiceEvent event) {
  @SuppressWarnings("unchecked")
  RepositoryInfo ri = new RepositoryInfo((ServiceReference<Repository>) event.getServiceReference());
  synchronized (this) {
    switch (event.getType()) {
    case ServiceEvent.REGISTERED:
      repos.put(ri, ri);
      break;
    case ServiceEvent.MODIFIED:
      for (Iterator<RepositoryInfo> i = repos.keySet().iterator(); i.hasNext();) {
        RepositoryInfo old = i.next();
        if (old.getId() == ri.getId()) {
          i.remove();
          repos.put(ri, ri);
        }
      }
      break;
    case ServiceEvent.MODIFIED_ENDMATCH:
    case ServiceEvent.UNREGISTERING:
      repos.remove(ri);
      break;      
    }
    changeCount++;
  }
  for (RepositoryListener rl : listeners) {
    switch (event.getType()) {
    case ServiceEvent.REGISTERED:
      rl.addedRepo(ri);
      repos.put(ri, ri);
      break;
    case ServiceEvent.MODIFIED:
      rl.modifiedRepo(ri);
      break;
    case ServiceEvent.MODIFIED_ENDMATCH:
    case ServiceEvent.UNREGISTERING:
      rl.removedRepo(ri);
      break;      
    }
  }
}
 
Example 18
Source File: ServiceListenerTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void serviceChanged(ServiceEvent evt) {
  events.add(evt);
  out.println("ServiceEvent: " +toString(evt) );
  if (ServiceEvent.UNREGISTERING==evt.getType()) {
    ServiceReference sr = evt.getServiceReference();

    // Validate that no bundle is marked as using the service
    Bundle[] usingBundles = sr.getUsingBundles();
    if (checkUsingBundles && null!=usingBundles) {
      teststatus = false;
      printUsingBundles(sr, "*** Using bundles (unreg) should be null "
                        +"but is: ");
    }

    // Check if the service can be fetched
    Object service = bc.getService(sr);
    usingBundles = sr.getUsingBundles();
    if (UNREGISTERSERVICE_VALID_DURING_UNREGISTERING) {
      // In this mode the service shall be obtainable during
      // unregistration.
      if (null==service) {
        teststatus = false;
        out.print("*** Service should be available to ServiceListener "
                  +"while handling unregistering event.");
      }
      out.println("Service (unreg): " +service);
      if (checkUsingBundles && usingBundles.length!=1) {
        teststatus = false;
        printUsingBundles(sr,
                          "*** One using bundle expected "
                          +"(unreg, after getService), found: ");
      } else {
        printUsingBundles(sr, "Using bundles (unreg, after getService): ");
      }
    } else {
      // In this mode the service shall NOT be obtainable during
      // unregistration.
      if (null!=service) {
        teststatus = false;
        out.print("*** Service should not be available to ServiceListener "
                  +"while handling unregistering event.");
      }
      if (checkUsingBundles && null!=usingBundles) {
        teststatus = false;
        printUsingBundles(sr,
                          "*** Using bundles (unreg, after getService), "
                          +"should be null but is: ");
      } else {
        printUsingBundles(sr,
                          "Using bundles (unreg, after getService): null");
      }
    }
    bc.ungetService(sr);

    // Check that the UNREGISTERING service can not be looked up
    // using the service registry.
    try {
      Long sid = (Long)sr.getProperty(Constants.SERVICE_ID);
      String sidFilter = "(" +Constants.SERVICE_ID +"=" +sid +")";
      ServiceReference[] srs = bc.getServiceReferences((String)null, sidFilter);
      if (null==srs || 0==srs.length) {
        out.println("ServiceReference for UNREGISTERING service is not"
                    +" found in the service registry; ok.");
      } else {
        teststatus = false;
        out.println("*** ServiceReference for UNREGISTERING"
                    +" service, "
                    + sr
                    +", not found in the service registry; fail.");
        out.print("Found the following Service references: ");
        for (int i=0; null!=srs && i<srs.length; i++) {
          if (i>0) out.print(", ");
          out.print(srs[i]);
        }
        out.println();
      }
    } catch (Throwable t) {
      teststatus = false;
      out.println("*** Unexpected excpetion when trying to lookup a"
                  +" service while it is in state UNREGISTERING; "
                  +t);
      t.printStackTrace(out);
    }
  }
}
 
Example 19
Source File: ContentHandlerWrapper.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
ContentHandlerWrapper(FrameworkContext       framework,
	String                 mimetype) {

  this.framework = framework;
  this.mimetype  = mimetype;

  filter =
    "(&" +
    "(" + Constants.OBJECTCLASS + "=" +
    ContentHandler.class.getName() + ")" +
    "(" + URLConstants.URL_CONTENT_MIMETYPE + "=" + mimetype +
    ")" +
    ")";

  final ServiceListener serviceListener =
    new ServiceListener() {
      public void serviceChanged(ServiceEvent evt) {
        @SuppressWarnings("unchecked")
        final
        ServiceReference<ContentHandler> ref =
            (ServiceReference<ContentHandler>) evt.getServiceReference();

        switch (evt.getType()) {
        case ServiceEvent.MODIFIED:
          // fall through
        case ServiceEvent.REGISTERED:
          if (best == null) {
            updateBest();
            return ;
          }

          if (compare(best, ref) > 0) {
            best = ref;
          }
          break;
        case ServiceEvent.MODIFIED_ENDMATCH:
          // fall through
        case ServiceEvent.UNREGISTERING:
          if (best.equals(ref)) {
            best = null;
          }
        }
      }
    };

  try {
    framework.systemBundle.bundleContext.addServiceListener(serviceListener, filter);

  } catch (final Exception e) {
    throw new IllegalArgumentException("Could not register service listener for content handler: " + e);
  }

  if (framework.debug.url) {
    framework.debug.println("created wrapper for " + mimetype + ", filter=" + filter);
  }
}
 
Example 20
Source File: Axis2ConfigServiceListener.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
public void serviceChanged(ServiceEvent event) {
    ServiceReference reference = event.getServiceReference();
    processRegisteredAxis2ConfigServices(reference, event.getType());
}