Search in sources :

Example 1 with Logger

use of org.apache.felix.ipojo.util.Logger in project felix by apache.

the class SmartProxyTest method testProxiesOfJavaClasses.

/**
 * Tests if we can proxies classes from java.* package.
 * Indeed, a recent JVM bug fix introduces a bug:
 * <code>
 * [ERROR] test : Cannot create the proxy object
 * java.lang.SecurityException: Prohibited package name: java.awt
 * </code>
 */
public void testProxiesOfJavaClasses() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
    Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
    Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
    Mockito.when(context.getBundle()).thenReturn(bundle);
    ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
    Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
    InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
    Mockito.when(im.getContext()).thenReturn(context);
    Mockito.when(im.getFactory()).thenReturn(factory);
    DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Logger logger = new Logger(context, "test", Logger.INFO);
    Mockito.when(handler.getLogger()).thenReturn(logger);
    // Try with java.List
    Dependency dependency = new Dependency(handler, "a_field", List.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // OK
    Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
    Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof List);
    dependency.stop();
    // Try with javax.sql.CommonDataSource
    dependency = new Dependency(handler, "a_field", javax.sql.DataSource.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // OK
    Assert.assertNotNull(dependency.onGet(new Object(), "a_field", null));
    Assert.assertTrue(dependency.onGet(new Object(), "a_field", null) instanceof javax.sql.DataSource);
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) Logger(org.apache.felix.ipojo.util.Logger) MockBundle(org.apache.felix.ipojo.test.MockBundle) List(java.util.List) ArrayList(java.util.ArrayList) BundleContext(org.osgi.framework.BundleContext)

Example 2 with Logger

use of org.apache.felix.ipojo.util.Logger in project felix by apache.

the class SmartProxyTest method testCannotProxyAbstractAndConcreteClasses.

/**
 * Check that we don't create smart proxies for concrete and abstract classes.
 */
public void testCannotProxyAbstractAndConcreteClasses() {
    Bundle bundle = new MockBundle(Dependency.class.getClassLoader());
    BundleContext context = (BundleContext) Mockito.mock(BundleContext.class);
    Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
    Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
    Mockito.when(context.getBundle()).thenReturn(bundle);
    ComponentFactory factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
    Mockito.when(factory.getBundleClassLoader()).thenReturn(Dependency.class.getClassLoader());
    InstanceManager im = (InstanceManager) Mockito.mock(InstanceManager.class);
    Mockito.when(im.getContext()).thenReturn(context);
    Mockito.when(im.getFactory()).thenReturn(factory);
    DependencyHandler handler = (DependencyHandler) Mockito.mock(DependencyHandler.class);
    Mockito.when(handler.getInstanceManager()).thenReturn(im);
    Logger logger = new Logger(context, "test", Logger.INFO);
    Mockito.when(handler.getLogger()).thenReturn(logger);
    Dependency dependency = new Dependency(handler, "a_field", ArrayList.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // No service
    Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
    dependency.stop();
    // Try with an Object.
    dependency = new Dependency(handler, "a_field", Object.class, null, false, false, false, true, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null);
    dependency.start();
    // OK
    Assert.assertNull(dependency.onGet(new Object(), "a_field", null));
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Bundle(org.osgi.framework.Bundle) InstanceManager(org.apache.felix.ipojo.InstanceManager) ComponentFactory(org.apache.felix.ipojo.ComponentFactory) MockBundle(org.apache.felix.ipojo.test.MockBundle) Logger(org.apache.felix.ipojo.util.Logger) BundleContext(org.osgi.framework.BundleContext)

Example 3 with Logger

use of org.apache.felix.ipojo.util.Logger in project felix by apache.

the class ProvidedServiceHandlerTest method setUp.

public void setUp() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    context = (BundleContext) Mockito.mock(BundleContext.class);
    Mockito.when(context.getProperty(DependencyHandler.PROXY_TYPE_PROPERTY)).thenReturn(null);
    Mockito.when(context.getProperty(Logger.IPOJO_LOG_LEVEL_PROP)).thenReturn(null);
    Mockito.when(context.getBundle()).thenReturn(new MockBundle(this.getClass().getClassLoader()));
    factory = (ComponentFactory) Mockito.mock(ComponentFactory.class);
    Mockito.when(factory.getBundleClassLoader()).thenReturn(ProvidedServiceHandler.class.getClassLoader());
    logger = Mockito.spy(new Logger(context, "TEST", Logger.INFO));
    Mockito.when(factory.getLogger()).thenReturn(logger);
    im = (InstanceManager) Mockito.mock(InstanceManager.class);
    Mockito.when(im.getContext()).thenReturn(context);
    Mockito.when(im.getGlobalContext()).thenReturn(context);
    Mockito.when(im.getFactory()).thenReturn(factory);
    Mockito.when(im.getInstanceName()).thenReturn("an.instance");
    desc = (ComponentTypeDescription) Mockito.mock(ComponentTypeDescription.class);
    Mockito.when(desc.getFactory()).thenReturn(factory);
    Mockito.when(desc.getBundleContext()).thenReturn(context);
    handler = new ProvidedServiceHandler();
    handler.setFactory(factory);
    // Attach the handler
    Method method = PrimitiveHandler.class.getDeclaredMethod("attach", new Class[] { ComponentInstance.class });
    method.setAccessible(true);
    method.invoke(handler, new Object[] { im });
}
Also used : MockBundle(org.apache.felix.ipojo.test.MockBundle) Method(java.lang.reflect.Method) Logger(org.apache.felix.ipojo.util.Logger)

Example 4 with Logger

use of org.apache.felix.ipojo.util.Logger in project felix by apache.

the class Extender method start.

/**
 * The iPOJO bundle is starting.
 * This method configures the iPOJO system (internal dispatcher and bundle processing). Then it initiates the
 * bundle processing.
 * <p/>
 * To optimize the processing, we process the iPOJO bundle first.
 *
 * @param context the iPOJO's bundle bundle context
 * @throws Exception something terrible happen during startup
 */
public void start(BundleContext context) throws Exception {
    m_context = context;
    m_bundle = context.getBundle();
    m_logger = new Logger(m_context, "IPOJO-Main-Extender");
    enablingDispatcher(context, m_logger);
    enablingSynchronousProcessing(context, m_logger);
    // Create the dispatcher only if required.
    if (DISPATCHER_ENABLED) {
        EventDispatcher.create(context);
    }
    // Initialize ConfigurationTracker
    ConfigurationTracker.initialize();
    // Initialize the queue event proxy if wanted
    ReplayQueueEventProxy proxy = null;
    if (Boolean.getBoolean(BOOTSTRAP_QUEUE_DEBUG_PROPERTY)) {
        proxy = new ReplayQueueEventProxy();
        context.registerService(QueueEventProxy.class, proxy, null);
    }
    BundleProcessor extensionBundleProcessor = new ExtensionBundleProcessor(m_logger);
    BundleProcessor componentsProcessor = new ComponentsBundleProcessor(m_logger);
    BundleProcessor configurationProcessor = new ConfigurationProcessor(m_logger);
    if (SYNCHRONOUS_PROCESSING_ENABLED) {
        m_queueService = new EnforcedQueueService(new HeaderPreferenceSelection(), new SynchronousQueueService(context), Preference.SYNC, m_logger);
        // If required, add the event proxy
        if (proxy != null) {
            m_queueService.addQueueListener(proxy);
        }
    } else {
        // Build a thread factory that will groups extender's thread together
        ThreadFactory threadFactory = new GroupThreadFactory(new ThreadGroup("iPOJO Extender"));
        threadFactory = new NamingThreadFactory(threadFactory);
        threadFactory = new PrefixedThreadFactory(threadFactory, "[iPOJO] ");
        // Create the queue services
        SynchronousQueueService sync = new SynchronousQueueService(context);
        ExecutorQueueService async = new ExecutorQueueService(context, Integer.getInteger(ExecutorQueueService.THREADPOOL_SIZE_PROPERTY, // default to 1 if no system property is set
        1), threadFactory);
        m_queueService = new PreferenceQueueService(new HeaderPreferenceSelection(), sync, async);
        extensionBundleProcessor = new QueuingActivationProcessor(extensionBundleProcessor, m_queueService);
        componentsProcessor = new QueuingActivationProcessor(componentsProcessor, m_queueService);
        configurationProcessor = new QueuingActivationProcessor(configurationProcessor, m_queueService);
        // If required, add the event proxy to both real services
        if (proxy != null) {
            sync.addQueueListener(proxy);
            async.addQueueListener(proxy);
        }
    }
    m_queueService.start();
    // Start linking
    m_linker = new DeclarationLinker(context, m_queueService);
    m_linker.start();
    m_processor = ChainedBundleProcessor.create(extensionBundleProcessor, componentsProcessor, configurationProcessor);
    m_processor.start();
    // Begin by initializing core handlers
    m_processor.activate(m_bundle);
    m_tracker = new BundleTracker(context, Bundle.ACTIVE, new BundleTrackerCustomizer() {

        public Object addingBundle(final Bundle bundle, final BundleEvent event) {
            if (bundle.getBundleId() == m_bundle.getBundleId()) {
                // Not interested in our own bundle
                return null;
            }
            m_processor.activate(bundle);
            return bundle;
        }

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

        public void removedBundle(final Bundle bundle, final BundleEvent event, final Object object) {
            m_processor.deactivate(bundle);
        }
    });
    m_tracker.open();
    m_declarationService = new DeclarationServiceFactory(context);
    m_declarationService.start();
    m_logger.log(Logger.INFO, "iPOJO Main Extender started");
}
Also used : NamingThreadFactory(org.apache.felix.ipojo.extender.internal.queue.NamingThreadFactory) GroupThreadFactory(org.apache.felix.ipojo.extender.internal.queue.GroupThreadFactory) PrefixedThreadFactory(org.apache.felix.ipojo.extender.internal.queue.PrefixedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) NamingThreadFactory(org.apache.felix.ipojo.extender.internal.queue.NamingThreadFactory) HeaderPreferenceSelection(org.apache.felix.ipojo.extender.internal.queue.pref.HeaderPreferenceSelection) GroupThreadFactory(org.apache.felix.ipojo.extender.internal.queue.GroupThreadFactory) BundleTracker(org.osgi.util.tracker.BundleTracker) SynchronousQueueService(org.apache.felix.ipojo.extender.internal.queue.SynchronousQueueService) Logger(org.apache.felix.ipojo.util.Logger) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer) EnforcedQueueService(org.apache.felix.ipojo.extender.internal.queue.pref.enforce.EnforcedQueueService) PreferenceQueueService(org.apache.felix.ipojo.extender.internal.queue.pref.PreferenceQueueService) DeclarationServiceFactory(org.apache.felix.ipojo.extender.internal.declaration.service.DeclarationServiceFactory) DeclarationLinker(org.apache.felix.ipojo.extender.internal.linker.DeclarationLinker) ReplayQueueEventProxy(org.apache.felix.ipojo.extender.internal.queue.debug.ReplayQueueEventProxy) ExecutorQueueService(org.apache.felix.ipojo.extender.internal.queue.ExecutorQueueService) PrefixedThreadFactory(org.apache.felix.ipojo.extender.internal.queue.PrefixedThreadFactory)

Aggregations

Logger (org.apache.felix.ipojo.util.Logger)4 MockBundle (org.apache.felix.ipojo.test.MockBundle)3 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)2 InstanceManager (org.apache.felix.ipojo.InstanceManager)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 DeclarationServiceFactory (org.apache.felix.ipojo.extender.internal.declaration.service.DeclarationServiceFactory)1 DeclarationLinker (org.apache.felix.ipojo.extender.internal.linker.DeclarationLinker)1 ExecutorQueueService (org.apache.felix.ipojo.extender.internal.queue.ExecutorQueueService)1 GroupThreadFactory (org.apache.felix.ipojo.extender.internal.queue.GroupThreadFactory)1 NamingThreadFactory (org.apache.felix.ipojo.extender.internal.queue.NamingThreadFactory)1 PrefixedThreadFactory (org.apache.felix.ipojo.extender.internal.queue.PrefixedThreadFactory)1 SynchronousQueueService (org.apache.felix.ipojo.extender.internal.queue.SynchronousQueueService)1 ReplayQueueEventProxy (org.apache.felix.ipojo.extender.internal.queue.debug.ReplayQueueEventProxy)1 HeaderPreferenceSelection (org.apache.felix.ipojo.extender.internal.queue.pref.HeaderPreferenceSelection)1 PreferenceQueueService (org.apache.felix.ipojo.extender.internal.queue.pref.PreferenceQueueService)1