Examples of ServiceRegistration


Examples of org.osgi.framework.ServiceRegistration

    @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

Examples of org.osgi.framework.ServiceRegistration

     */
    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

Examples of org.osgi.framework.ServiceRegistration

        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

Examples of org.osgi.framework.ServiceRegistration

                                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

Examples of org.osgi.framework.ServiceRegistration

        when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
        when(bundleContext.registerService(anyString(), anyObject(), any(Dictionary.class))).then(new Answer<ServiceRegistration>() {
            @Override
            public ServiceRegistration answer(InvocationOnMock invocation) throws Throwable {
                final Dictionary<String, Object> props = (Dictionary<String, Object>)invocation.getArguments()[2];
                ServiceRegistration reg = mock(ServiceRegistration.class);
                ServiceReference ref = mock(ServiceReference.class);
                when(reg.getReference()).thenReturn(ref);
                when(ref.getProperty(anyString())).thenAnswer(new Answer<Object>() {
                    @Override
                    public Object answer(InvocationOnMock invocation) throws Throwable {
                        String key = (String)invocation.getArguments()[0];
                        return props.get(key);
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

        final SlingPostOperation service = (SlingPostOperation) this.bundleContext.getService(serviceReference);
        final PostOperationProxy proxy = new PostOperationProxy(service);

        final BundleContext bundleContext = serviceReference.getBundle().getBundleContext();
        final Dictionary<String, Object> props = copyServiceProperties(serviceReference);
        final ServiceRegistration reg = bundleContext.registerService(
            PostOperation.SERVICE_NAME, proxy, props);

        log.debug("Registering {}", proxy);
        synchronized (this.proxies) {
            this.proxies.put(serviceReference, reg);
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     * Update proxy service registration properties
     * <p>
     * Called by serviceChanged
     */
    private void update(final ServiceReference serviceReference) {
        final ServiceRegistration proxyRegistration;
        synchronized (this.proxies) {
            proxyRegistration = this.proxies.get(serviceReference);
        }

        if (proxyRegistration != null) {
            log.debug("Updating {}", proxyRegistration);
            proxyRegistration.setProperties(copyServiceProperties(serviceReference));
        }
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     * Unregister proxy and unget SlingPostOperation service
     * <p>
     * Called by serviceChanged
     */
    private void unregister(final ServiceReference serviceReference) {
        final ServiceRegistration proxyRegistration;
        synchronized (this.proxies) {
            proxyRegistration = this.proxies.remove(serviceReference);
        }

        if (proxyRegistration != null) {
            log.debug("Unregistering {}", proxyRegistration);
            this.bundleContext.ungetService(serviceReference);
            proxyRegistration.unregister();
        }
    }
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

    public void testServiceRegistration() throws InvalidSyntaxException {
        // prepare test services
        String clazz1 = String.class.getName();
        Object service1 = new Object();
        Dictionary properties1 = getServiceProperties(null);
        ServiceRegistration reg1 = this.bundleContext.registerService(clazz1, service1, properties1);

        String[] clazzes2 = new String[] { String.class.getName(), Integer.class.getName() };
        Object service2 = new Object();
        Dictionary properties2 = getServiceProperties(null);
        ServiceRegistration reg2 = this.bundleContext.registerService(clazzes2, service2, properties2);

        String clazz3 = Integer.class.getName();
        Object service3 = new Object();
        Dictionary properties3 = getServiceProperties(100L);
        ServiceRegistration reg3 = this.bundleContext.registerService(clazz3, service3, properties3);

        // test get service references
        ServiceReference refString = this.bundleContext.getServiceReference(String.class.getName());
        assertSame(reg1.getReference(), refString);

        ServiceReference refInteger = this.bundleContext.getServiceReference(Integer.class.getName());
        assertSame(reg3.getReference(), refInteger);

        ServiceReference[] refsString = this.bundleContext.getServiceReferences(String.class.getName(), null);
        assertEquals(2, refsString.length);
        assertSame(reg1.getReference(), refsString[0]);
        assertSame(reg2.getReference(), refsString[1]);

        ServiceReference[] refsInteger = this.bundleContext.getServiceReferences(Integer.class.getName(), null);
        assertEquals(2, refsInteger.length);
        assertSame(reg3.getReference(), refsInteger[0]);
        assertSame(reg2.getReference(), refsInteger[1]);

        ServiceReference[] allRefsString = this.bundleContext.getAllServiceReferences(String.class.getName(), null);
        assertArrayEquals(refsString, allRefsString);
View Full Code Here

Examples of org.osgi.framework.ServiceRegistration

     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobExecutionUsingBridge() throws Exception {
        final Barrier cb = new Barrier(2);

        final ServiceRegistration reg = this.registerEventHandler(TOPIC,
                new EventHandler() {
                    @Override
                    public void handleEvent(Event event) {
                        JobUtil.acknowledgeJob(event);
                        JobUtil.finishedJob(event);
                        cb.block();
                    }

                 });

        try {
            this.eventAdmin.sendEvent(getJobEvent(null));
            assertTrue("No event received in the given time.", cb.block(5));
            cb.reset();
            assertFalse("Unexpected event received in the given time.", cb.block(5));
        } finally {
            reg.unregister();
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.