Package org.osgi.framework

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


        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

     * 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

     * 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

    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

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

     * The job is executed once and finished successfully.
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testSimpleJobWithIdExecution() throws Exception {
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        return JobResult.OK;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, "myid1", 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 {
            jcReg.unregister();
        }
    }
View Full Code Here

     * The job execution always fails
     */
    @Test(timeout = DEFAULT_TEST_TIMEOUT)
    public void testForceCancelJob() throws Exception {
        final Barrier cb = new Barrier(2);
        final ServiceRegistration jcReg = this.registerJobConsumer(TOPIC,
                new JobConsumer() {

                    @Override
                    public JobResult process(Job job) {
                        cb.block();
                        sleep(1000);
                        return JobResult.FAILED;
                    }
                });
        try {
            final JobManager jobManager = this.getJobManager();
            jobManager.addJob(TOPIC, "myid3", null);
            cb.block();

            assertEquals(1, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
            // job is currently sleeping, but force cancel always waits!
            final Event e = jobManager.findJob("sling/test", Collections.singletonMap(JobUtil.PROPERTY_JOB_NAME, (Object)"myid3"));
            assertNotNull(e);
            jobManager.forceRemoveJob((String)e.getProperty(ResourceHelper.PROPERTY_JOB_ID));
            // the job is now removed
            assertEquals(0, jobManager.findJobs(JobManager.QueryType.ALL, "sling/test", -1, (Map<String, Object>[])null).size());
            final Collection<Job> col = jobManager.findJobs(JobManager.QueryType.HISTORY, "sling/test", -1, (Map<String, Object>[])null);
            try {
                assertEquals(1, col.size());
            } finally {
                for(final Job j : col) {
                    jobManager.removeJobById(j.getId());
                }
            }
        } finally {
            jcReg.unregister();
        }
    }
View Full Code Here

     */
    protected ServiceRegistration registerEventHandler(final String topic,
            final EventHandler handler) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(EventConstants.EVENT_TOPIC, topic);
        final ServiceRegistration reg = this.bc.registerService(EventHandler.class.getName(),
                handler, props);
        return reg;
    }
View Full Code Here

     */
    protected ServiceRegistration registerJobConsumer(final String topic,
            final JobConsumer handler) {
        final Dictionary<String, Object> props = new Hashtable<String, Object>();
        props.put(JobConsumer.PROPERTY_TOPICS, topic);
        final ServiceRegistration reg = this.bc.registerService(JobConsumer.class.getName(),
                handler, props);
        return reg;
    }
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.