Java Code Examples for org.osgi.framework.BundleContext#addServiceListener()

The following examples show how to use org.osgi.framework.BundleContext#addServiceListener() . 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: Activator.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public void start(BundleContext context) throws Exception {
context.addServiceListener(new ServiceListener() {
			
	@Override
	public void serviceChanged(ServiceEvent event) {
		if (event.getType() == ServiceEvent.REGISTERED){
	    	component = new Component();
	    	
	    	Server server = new Server(Protocol.HTTP, 8182);
	    	component.getServers().add(server);
	    	component.setLogService(new LogService(false));
	    	
	    	final Application app = new ApiApplication();
	    	component.getDefaultHost().attachDefault(app);
	    	try {
	    		component.start();
	    	} catch (Exception e) {
	    		e.printStackTrace();
	    	}
		}
	}
}, "(objectclass=" + ApiStartServiceToken.class.getName() +")");
  }
 
Example 2
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void start(BundleContext bc) {

    lc = new LogConfigImpl(bc);
    lrsf = new LogReaderServiceFactory(bc, lc);
    lsf = new LogServiceFactory(lrsf);

    // Catch all framework error and place in the log.
    LogFrameworkListener lfl = new LogFrameworkListener(lrsf);
    bc.addFrameworkListener(lfl);
    bc.addBundleListener(lfl);
    bc.addServiceListener(lfl);

    // Register our services
    bc.registerService(LOG_READER_SERVICE_CLASS, lrsf, null);
    bc.registerService(LOG_SERVICE_CLASSES, lsf, null);
  }
 
Example 3
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void start ( final BundleContext context ) throws Exception
{
    this.context = context;
    addService ( context.getServiceReference ( HttpService.class ) );
    context.addServiceListener ( this, String.format ( "(%s=%s)", Constants.OBJECTCLASS, HttpService.class.getName () ) );
}
 
Example 4
Source File: Activator.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
@Override
public void start(BundleContext context) throws Exception {
    
    bstc = new BundleStartTimeCalculator(context.getBundle().getBundleId());
    context.addBundleListener(bstc);

    srcc = new ServiceRestartCountCalculator();
    context.addServiceListener(srcc);

    stc = new StartupTimeCalculator(context, bstc, srcc);
}
 
Example 5
Source File: ShellActivator.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * called, when the bundle is started.
 * 
 * @param context
 *            the bundle context.
 * 
 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
 */
public void start(final BundleContext context) throws Exception {
	ShellActivator.context = context;
	List<ShellCommandGroup> plugins = new ArrayList<ShellCommandGroup>();

	final ServiceReference<?> pkgAdminRef = context
			.getServiceReference("org.osgi.service.packageadmin.PackageAdmin");
	if (pkgAdminRef != null) {
		plugins.add(new PackageAdminCommandGroup(context
				.getService(pkgAdminRef)));
	}

	shell = new Shell(System.out, System.err,
			(ShellCommandGroup[]) plugins
					.toArray(new ShellCommandGroup[plugins.size()]));
	context.addServiceListener(shell, "(" + Constants.OBJECTCLASS + "="
			+ ShellCommandGroup.class.getName() + ")");

	final Collection<ServiceReference<ShellCommandGroup>> existingGroups = context
			.getServiceReferences(ShellCommandGroup.class, null);
	if (existingGroups != null) {
		for (final ServiceReference<ShellCommandGroup> group : existingGroups) {
			shell.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
					group));
		}
	}
}
 
Example 6
Source File: EventAdminActivator.java    From concierge with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 
 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
 */
public void start(BundleContext context) throws Exception {
	EventAdminActivator.context = context;
	eventAdmin = new EventAdminImpl();
	context.addBundleListener(eventAdmin);
	context.addServiceListener(eventAdmin);
	context.addFrameworkListener(eventAdmin);
	context.registerService(EventAdmin.class.getName(), eventAdmin, null);
}
 
Example 7
Source File: Activator.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void start(BundleContext bc) throws Exception  { 
  Activator.bc = bc; 
  log("starting");
  
  String filter = "(objectclass=" + DateService.class.getName() + ")"; 
  bc.addServiceListener(listener, filter); 
  
  ServiceReference references[] = bc.getServiceReferences(null, filter); 
  for (int i = 0; references != null && i < references.length; i++) 
    { 
      listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, 
                                               references[i])); 
    } 
}
 
Example 8
Source File: Repositories.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Repositories(BundleContext bc) {
  this.bc = bc;
  synchronized (this) {
    final String clazz = Repository.class.getName();
    try {
      bc.addServiceListener(this, "(objectClass=" + clazz + ")");
      for (ServiceReference<Repository> sr : bc.getServiceReferences(Repository.class, null)) {
        RepositoryInfo ri = new RepositoryInfo(sr);
        repos.put(ri, ri);
      }
    } catch (InvalidSyntaxException _ignore) { }
  }
  xmlRepoFactoryTracker = new ServiceTracker<XmlBackedRepositoryFactory, XmlBackedRepositoryFactory>(bc, XmlBackedRepositoryFactory.class.getName(), null);
  xmlRepoFactoryTracker.open();
}
 
Example 9
Source File: LogRef.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void init(BundleContext bc, boolean out)
{
  this.bc = bc;
  useOut = out;
  bundleId = bc.getBundle().getBundleId();
  try {
    bc.addServiceListener(this, logServiceFilter);
  } catch (InvalidSyntaxException e) {
    error("Failed to register log service listener (filter="
          + logServiceFilter + ")", e);
  }
}
 
Example 10
Source File: CMCommands.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CMCommands(BundleContext bc)
{
  super("configuration", "Configuration commands");
  this.bc = bc;
  refCA = bc.getServiceReference(ConfigurationAdmin.class);
  try {
    bc.addServiceListener(this,
                          "(objectClass="
                              + ConfigurationAdmin.class.getName() + ")");
  } catch (final InvalidSyntaxException ignored) {
  }
}
 
Example 11
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void start ( final BundleContext context ) throws Exception
{
    this.executor = new ExportedExecutorService ( "org.eclipse.scada.da.server.osgi", 1, 1, 1, TimeUnit.MINUTES );

    this.service = new HiveImpl ( context, this.executor );
    this.service.start ();

    final Dictionary<String, Object> properties = new Hashtable<String, Object> ();

    properties.put ( Constants.SERVICE_VENDOR, "Eclipse SCADA Project" );
    properties.put ( Constants.SERVICE_DESCRIPTION, "A common generic OSGi DA Hive" );
    properties.put ( "hive.id", this.service.getHiveId () );

    this.handle = context.registerService ( Hive.class, this.service, properties );

    context.addServiceListener ( this.listener = new ServiceListener () {

        @Override
        public void serviceChanged ( final ServiceEvent event )
        {
            switch ( event.getType () )
            {
                case ServiceEvent.REGISTERED:
                    Activator.this.addItem ( event.getServiceReference () );
                    break;
                case ServiceEvent.UNREGISTERING:
                    Activator.this.removeItem ( event.getServiceReference () );
                    break;
            }
        }
    }, "(" + Constants.OBJECTCLASS + "=" + DataItem.class.getName () + ")" );

    final Collection<ServiceReference<DataItem>> refs = context.getServiceReferences ( DataItem.class, null );
    if ( refs != null )
    {
        for ( final ServiceReference<DataItem> ref : refs )
        {
            addItem ( ref );
        }
    }

    this.poolTracker = new ObjectPoolTracker<DataItem> ( context, DataItem.class );
    this.poolTracker.open ();

    this.itemTracker = new AllObjectPoolServiceTracker<DataItem> ( this.poolTracker, new ObjectPoolListener<DataItem> () {

        @Override
        public void serviceRemoved ( final DataItem service, final Dictionary<?, ?> properties )
        {
            Activator.this.service.removeItem ( service );
        }

        @Override
        public void serviceModified ( final DataItem service, final Dictionary<?, ?> properties )
        {
        }

        @Override
        public void serviceAdded ( final DataItem service, final Dictionary<?, ?> properties )
        {
            Activator.this.service.addItem ( service, properties );
        }
    } );
    this.itemTracker.open ();
}
 
Example 12
Source File: Activator.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public void start(BundleContext context) throws Exception {
	
	System.err.println("Starting Admin bundle");
	
	context.addServiceListener(new ServiceListener() {
		
		@Override
		public void serviceChanged(ServiceEvent event) {
			System.err.println(event);
			if (event.getType() == ServiceEvent.REGISTERED){
				Application application = new AdminApplication();

				component = new Component();
				component.getServers().add(Protocol.HTTP, 8183);
				component.getClients().add(Protocol.FILE);

				boolean useAuth = Boolean.valueOf(Configuration.getInstance().getProperty("adminapi.use_authentication", "false"));
				
				if (useAuth) {
					String username = Configuration.getInstance().getProperty("adminapi.username", null);
					String password = Configuration.getInstance().getProperty("adminapi.password", null);
					
					ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "myRealm");
					MapVerifier verifier = new MapVerifier();
					verifier.getLocalSecrets().put(username, password.toCharArray());
					guard.setVerifier(verifier);
					guard.setNext(application);
					
					component.getDefaultHost().attachDefault(guard);
				} else {
					component.getDefaultHost().attachDefault(application);
				}
				
				try {
					component.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}, "(objectclass=" + ApiStartServiceToken.class.getName() +")");
}
 
Example 13
Source File: RegListenThread.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RegListenThread(BundleContext bc, PrintStream out) throws Exception {
  this.bc  = bc;
  this.out = out;
  bc.addServiceListener(this);
}