Package org.osgi.framework

Examples of org.osgi.framework.ServiceRegistration


            {
                replacedLatch.countDown();
            }
        };

        ServiceRegistration reg = m_context.registerService(HttpSessionAttributeListener.class.getName(), listener, null);

        register("/session", new TestServlet()
        {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
            {
                try
                {
                    HttpSession session = req.getSession();

                    session.setAttribute("foo", "bar");

                    assertTrue(addedLatch.await(5, TimeUnit.SECONDS));

                    session.setAttribute("foo", "qux");

                    assertTrue(replacedLatch.await(5, TimeUnit.SECONDS));

                    session.removeAttribute("foo");

                    assertTrue(removedLatch.await(5, TimeUnit.SECONDS));

                    resp.setStatus(SC_OK);
                }
                catch (InterruptedException e)
                {
                    resp.sendError(SC_SERVICE_UNAVAILABLE, e.getMessage());
                }
                finally
                {
                    resp.flushBuffer();
                }
            }
        });

        try
        {
            assertContent(SC_OK, null, new URL("http://localhost:8080/session"));
        }
        finally
        {
            reg.unregister();
        }
    }
View Full Code Here


        else if (svcObj == null)
        {
            throw new IllegalArgumentException("Service object cannot be null.");
        }

        ServiceRegistration reg = null;

        // Check to make sure that the service object is
        // an instance of all service classes; ignore if
        // service object is a service factory.
        if (!(svcObj instanceof ServiceFactory))
        {
            for (int i = 0; i < classNames.length; i++)
            {
                Class clazz = Util.loadClassUsingClass(svcObj.getClass(), classNames[i], m_secureAction);
                if (clazz == null)
                {
                    throw new IllegalArgumentException(
                        "Cannot cast service: " + classNames[i]);
                }
                else if (!clazz.isAssignableFrom(svcObj.getClass()))
                {
                    throw new IllegalArgumentException(
                        "Service object is not an instance of \""
                        + classNames[i] + "\".");
                }
            }
        }

        reg = m_registry.registerService(context, classNames, svcObj, dict);

        // Check to see if this a listener hook; if so, then we need
        // to invoke the callback with all existing service listeners.
        if (ServiceRegistry.isHook(
            classNames, org.osgi.framework.hooks.service.ListenerHook.class, svcObj))
        {
            org.osgi.framework.hooks.service.ListenerHook lh =
                (org.osgi.framework.hooks.service.ListenerHook)
                    getService(this, reg.getReference(), false);
            if (lh != null)
            {
                try
                {
                    m_secureAction.invokeServiceListenerHookAdded(
                        lh, m_dispatcher.getAllServiceListeners());
                }
                catch (Throwable th)
                {
                    m_logger.log(reg.getReference(), Logger.LOG_WARNING,
                        "Problem invoking service registry hook", th);
                }
                finally
                {
                    m_registry.ungetService(this, reg.getReference(), null);
                }
            }
        }

        // Fire service event.
        fireServiceEvent(new ServiceEvent(ServiceEvent.REGISTERED, reg.getReference()), null);

        return reg;
    }
View Full Code Here

    /* (non-Javadoc)
     * @see org.osgi.framework.BundleContext#getServiceReference(java.lang.String)
     */
    public ServiceReference getServiceReference( String name )
    {
        ServiceRegistration sr = ( ServiceRegistration ) services.get( name );
        return ( sr != null ) ? sr.getReference() : null;
    }
View Full Code Here

     * @see org.osgi.framework.BundleContext#registerService(java.lang.String[], java.lang.Object, java.util.Dictionary)
     */
    public ServiceRegistration registerService( String[] names, Object service, Dictionary props )
    {
        props.put( Constants.OBJECTCLASS, names );
        ServiceRegistration sr = new MockServiceRegistration( this, service, names, props );

        for ( int i = 0; i < names.length; i++ )
        {
            services.put( names[i], sr );
        }

        fireServiceEvent( sr.getReference(), ServiceEvent.REGISTERED );

        return sr;
    }
View Full Code Here

    {
        final String pid = "org.apache.felix.http";
        ServiceTracker tracker = new ServiceTracker(m_context, ConfigurationAdmin.class.getName(), null);
        tracker.open();

        ServiceRegistration reg = null;
        org.osgi.service.cm.Configuration config = null;

        try
        {
            ConfigurationAdmin configAdmin = (ConfigurationAdmin) tracker.waitForService(TimeUnit.SECONDS.toMillis(5));
            assertNotNull("No configuration admin service found?!", configAdmin);

            final CountDownLatch latch = new CountDownLatch(1);
            final int configEvent = (props != null) ? ConfigurationEvent.CM_UPDATED : ConfigurationEvent.CM_DELETED;

            config = configAdmin.getConfiguration(pid, null);

            reg = m_context.registerService(ConfigurationListener.class.getName(), new ConfigurationListener()
            {
                @Override
                public void configurationEvent(ConfigurationEvent event)
                {
                    if (pid.equals(event.getPid()) && event.getType() == configEvent)
                    {
                        latch.countDown();
                    }
                }
            }, null);

            if (props != null)
            {
                config.update(props);
            }
            else
            {
                config.delete();
            }

            assertTrue("Configuration not provisioned in time!", latch.await(5, TimeUnit.SECONDS));

            // Needed to get Jetty restarted...
            TimeUnit.MILLISECONDS.sleep(150);

            return config;
        }
        finally
        {
            if (reg != null)
            {
                reg.unregister();
            }

            tracker.close();
        }
    }
View Full Code Here

        };

        assertEquals("Precondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(ListenerHook.class).size());
        ServiceRegistration reg = sr.registerService(c, new String [] {EventHook.class.getName()}, hook, new Hashtable());
        assertEquals(1, sr.getHooks(EventHook.class).size());
        assertTrue(sr.getHooks(EventHook.class).iterator().next() instanceof ServiceReference);
        assertSame(reg.getReference(), sr.getHooks(EventHook.class).iterator().next());
        assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
        assertEquals("Postcondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Postcondition failed", 0, sr.getHooks(ListenerHook.class).size());

        sr.unregisterService(b, reg);
View Full Code Here

        ServiceFactory sf = (ServiceFactory) sfControl.getMock();

        assertEquals("Precondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(ListenerHook.class).size());
        ServiceRegistration reg = sr.registerService(c, new String [] {EventHook.class.getName()}, sf, new Hashtable());
        assertEquals(1, sr.getHooks(EventHook.class).size());
        assertSame(reg.getReference(), sr.getHooks(EventHook.class).iterator().next());
        assertSame(sf, ((ServiceRegistrationImpl) reg).getService());
        assertEquals("Postcondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Postcondition failed", 0, sr.getHooks(ListenerHook.class).size());

        sr.unregisterService(b, reg);
View Full Code Here

        };

        assertEquals("Precondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(ListenerHook.class).size());
        ServiceRegistration reg = sr.registerService(c, new String [] {FindHook.class.getName()}, hook, new Hashtable());
        assertEquals(1, sr.getHooks(FindHook.class).size());
        assertSame(reg.getReference(), sr.getHooks(FindHook.class).iterator().next());
        assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
        assertEquals("Postcondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Postcondition failed", 0, sr.getHooks(ListenerHook.class).size());

        sr.unregisterService(b, reg);
View Full Code Here

        ServiceFactory sf = (ServiceFactory) sfControl.getMock();

        assertEquals("Precondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(ListenerHook.class).size());
        ServiceRegistration reg = sr.registerService(c, new String [] {FindHook.class.getName()}, sf, new Hashtable());
        assertEquals(1, sr.getHooks(FindHook.class).size());
        assertSame(reg.getReference(), sr.getHooks(FindHook.class).iterator().next());
        assertSame(sf, ((ServiceRegistrationImpl) reg).getService());
        assertEquals("Postcondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Postcondition failed", 0, sr.getHooks(ListenerHook.class).size());

        sr.unregisterService(b, reg);
View Full Code Here

        };

        assertEquals("Precondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(FindHook.class).size());
        assertEquals("Precondition failed", 0, sr.getHooks(ListenerHook.class).size());
        ServiceRegistration reg = sr.registerService(c, new String [] {ListenerHook.class.getName()}, hook, new Hashtable());
        assertEquals(1, sr.getHooks(ListenerHook.class).size());
        assertSame(reg.getReference(), sr.getHooks(ListenerHook.class).iterator().next());
        assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
        assertEquals("Postcondition failed", 0, sr.getHooks(EventHook.class).size());
        assertEquals("Postcondition failed", 0, sr.getHooks(FindHook.class).size());

        sr.unregisterService(b, 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.