Package org.osgi.framework

Examples of org.osgi.framework.ServiceRegistration


        /* create the hashtable to put properties in */
        Hashtable props = new Hashtable();
        /* put service.pid property in hashtable */
        props.put("service.pid", testSuiteAllTests.getName());
        /* register service with the suite for all tests */
        ServiceRegistration serviceRegistration = context.registerService(
                TestSuite.class.getName(), testSuiteAllTests, props);

    }
View Full Code Here


    public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
    }

    public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
        if (object instanceof ServiceRegistration) {
            ServiceRegistration service = (ServiceRegistration) object;
            service.unregister();
        }
    }
View Full Code Here

        installedApis.remove(api.getId());
    }


    public void deleted(String pid) {
        ServiceRegistration oldRegistration = registrations.remove(pid);
        if (oldRegistration != null) {
            oldRegistration.unregister();
        }
    }
View Full Code Here

    public String getName() {
        return "Chef Service Factory";
    }

    public void updated(String pid, Dictionary properties) throws ConfigurationException {
        ServiceRegistration newRegistration = null;
        try {
            if (properties != null) {
                Properties props = new Properties();
                for (Enumeration e = properties.keys(); e.hasMoreElements(); ) {
                    Object key = e.nextElement();
                    Object val = properties.get(key);
                    props.put(key, val);
                }

                String api = (String) properties.get(ChefConstants.API);

                ProviderMetadata providerMetadata = null;
                ApiMetadata apiMetadata = null;
                if (!Strings.isNullOrEmpty(api) && installedApis.containsKey(api)) {
                    apiMetadata = installedApis.get(api);
                    validate(apiMetadata, properties);
                } else {

                    if (!Strings.isNullOrEmpty(api)) {
                        apiPids.put(api, pid);
                    }
                    pendingPids.put(pid, properties);
                    LOGGER.warn("Api {} is not currently installed. Service will resume once the the api is installed.", api);
                    return;
                }

                String id = (String) properties.get(ChefConstants.NAME);
                String clientName = (String) properties.get(ChefConstants.CLIENT_NAME);
                String clientKeyFile = (String) properties.get(ChefConstants.CLIENT_KEY_FILE);
                String clientCredential = (String) properties.get(ChefConstants.CLIENT_CREDENTIAL);
                String validatorName = (String) properties.get(ChefConstants.VALIDATOR_NAME);
                String validatorKeyFile = (String) properties.get(ChefConstants.VALIDATOR_KEY_FILE);
                String validatorCredential = (String) properties.get(ChefConstants.VALIDATOR_CREDENTIAL);
                String endpoint = (String) properties.get(ChefConstants.ENDPOINT);

                ChefService service = ChefHelper.createChefService(apiMetadata, id, clientName, clientCredential, clientKeyFile, validatorName, validatorCredential, validatorKeyFile, endpoint);
                newRegistration = bundleContext.registerService(
                        ChefService.class.getName(), service, properties);

                //If all goes well remove the pending pid.
                if (pendingPids.containsKey(pid)) {
                    activePids.put(pid, pendingPids.remove(pid));
                }
            }
        } catch (InvalidConfigurationException ex) {
            LOGGER.warn("Invalid configuration: {}", ex.getMessage());
        } catch (Exception ex) {
            LOGGER.error("Error creating compute service.", ex);
        } finally {
            ServiceRegistration oldRegistration = (newRegistration == null)
                    ? registrations.remove(pid)
                    : registrations.put(pid, newRegistration);
            if (oldRegistration != null) {
                oldRegistration.unregister();
            }
        }
    }
View Full Code Here

                chefServiceFactory, properties);
    }

    private void registerRecipeProviderForService(BundleContext context, String serviceId, ChefService chefService) {
        ChefRecipeProvider chefRecipeProvider = new ChefRecipeProvider(chefService);
        ServiceRegistration registration = context.registerService(RecipeProvider.class.getName(), chefRecipeProvider, null);
        registrationMap.put(serviceId, registration);
    }
View Full Code Here

        registrationMap.put(serviceId, registration);
    }

    private void unregisterRecipeProviderForService(BundleContext context, String serviceId, ChefService chefService) {
        if (registrationMap.containsKey(serviceId)) {
            ServiceRegistration registration = registrationMap.remove(serviceId);
            try {
                registration.unregister();
            } catch (Exception ex) {
                //ignore
            }
        }
    }
View Full Code Here

    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testTimedJob() throws Exception {
        final AtomicInteger counter = new AtomicInteger();

        final ServiceRegistration ehReg = this.registerJobConsumer(TOPIC, new JobConsumer() {

            @Override
            public JobResult process(final Job job) {
                if ( job.getTopic().equals(TOPIC) ) {
                    counter.incrementAndGet();
                }
                return JobResult.OK;
            }

        });
        try {
            final Date d = new Date();
            d.setTime(System.currentTimeMillis() + 3000); // run in 3 seconds

            // create scheduled job
            final ScheduledJobInfo info = this.getJobManager().createJob(TOPIC).schedule().at(d).add();
            assertNotNull(info);

            while ( counter.get() == 0 ) {
                this.sleep(1000);
            }
            assertEquals(0, this.getJobManager().getScheduledJobs().size()); // job is not scheduled anymore
            info.unschedule();
        } finally {
            ehReg.unregister();
        }
    }
View Full Code Here

     */
    private void setupChaosThreads(final List<Thread> threads,
            final AtomicLong finishedThreads) {
        final List<TopologyView> views = new ArrayList<TopologyView>();
        // register topology listener
        final ServiceRegistration reg = this.bc.registerService(TopologyEventListener.class.getName(), new TopologyEventListener() {

            @Override
            public void handleTopologyEvent(final TopologyEvent event) {
                if ( event.getType() == Type.TOPOLOGY_INIT ) {
                    views.add(event.getNewView());
                }
            }
        }, null);
        while ( views.isEmpty() ) {
            this.sleep(10);
        }
        reg.unregister();
        final TopologyView view = views.get(0);

        try {
            final ServiceReference[] refs = this.bc.getServiceReferences(TopologyEventListener.class.getName(),
                    "(objectClass=org.apache.sling.event.impl.jobs.config.JobManagerConfiguration)");
View Full Code Here

        final List<ServiceRegistration> registrations = new ArrayList<ServiceRegistration>();
        final List<Thread> threads = new ArrayList<Thread>();
        final AtomicLong finishedThreads = new AtomicLong();

        final ServiceRegistration eventHandler = this.registerEventHandler("org/apache/sling/event/notification/job/*",
                new EventHandler() {

                    @Override
                    public void handleEvent(final Event event) {
                        final String topic = (String) event.getProperty(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC);
                        if ( NotificationConstants.TOPIC_JOB_FINISHED.equals(event.getTopic())) {
                            finished.get(topic).incrementAndGet();
                        } else if ( NotificationConstants.TOPIC_JOB_ADDED.equals(event.getTopic())) {
                            added.get(topic).incrementAndGet();
                        }
                    }
                });
        try {
            // setup job consumers
            this.setupJobConsumers(registrations);

            // setup job creation tests
            this.setupJobCreationThreads(threads, jobManager, created, finishedThreads);

            this.setupChaosThreads(threads, finishedThreads);

            System.out.println("Starting threads...");
            // start threads
            for(final Thread t : threads) {
                t.start();
            }

            System.out.println("Sleeping for " + DURATION + " seconds to wait for threads to finish...");
            // for sure we can sleep for the duration
            this.sleep(DURATION * 1000);

            System.out.println("Polling for threads to finish...");
            // wait until threads are finished
            while ( finishedThreads.get() < threads.size() ) {
                this.sleep(100);
            }

            System.out.println("Waiting for job handling to finish...");
            final Set<String> allTopics = new HashSet<String>(topics);
            while ( !allTopics.isEmpty() ) {
                final Iterator<String> iter = allTopics.iterator();
                while ( iter.hasNext() ) {
                    final String topic = iter.next();
                    if ( finished.get(topic).get() == created.get(topic).get() ) {
                        iter.remove();
                    }
                }
                this.sleep(100);
            }
/* We could try to enable this with Oak again - but right now JR observation handler is too
* slow.
            System.out.println("Checking notifications...");
            for(final String topic : topics) {
                assertEquals("Checking topic " + topic, created.get(topic).get(), added.get(topic).get());
            }
*/

        } finally {
            eventHandler.unregister();
            for(final ServiceRegistration reg : registrations) {
                reg.unregister();
            }
        }

View Full Code Here

                                for (Class<?> adapterType : adapterTypes) {
                                    if (adapterType != implType) {
                                        adapterImplementations.add(adapterType, implType);
                                    }
                                }
                                ServiceRegistration reg = registerAdapterFactory(adapterTypes, annotation.adaptables(), implType, annotation.condition());
                                regs.add(reg);
                            }
                        }
                    } catch (ClassNotFoundException e) {
                        log.warn("Unable to load class", e);
View Full Code Here

TOP

Related Classes of org.osgi.framework.ServiceRegistration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.