Search in sources :

Example 1 with IPentahoInitializer

use of org.pentaho.platform.api.engine.IPentahoInitializer in project pentaho-platform by pentaho.

the class OSGIObjectFactory method getAll.

@Override
public <T> List<T> getAll(Class<T> interfaceClass, IPentahoSession session, Map<String, String> properties) throws ObjectFactoryException {
    if (isBundleContextValid() == false) {
        return null;
    }
    List<T> returnList = new ArrayList<T>();
    // make sure we check by reference first
    if (properties == null || !properties.containsKey(REFERENCE_CLASS)) {
        Map<String, String> props = new HashMap<String, String>();
        if (properties != null) {
            props.putAll(properties);
        }
        props.put(REFERENCE_CLASS, interfaceClass.getName());
        List<IPentahoObjectReference> all = getAll(IPentahoObjectReference.class, session, props);
        if (all != null) {
            for (IPentahoObjectReference iPentahoObjectReference : all) {
                returnList.add((T) iPentahoObjectReference.getObject());
            }
        }
    }
    String filter = OSGIUtils.createFilter(properties);
    try {
        Collection<ServiceReference<T>> refs = context.getServiceReferences(interfaceClass, filter);
        if (refs == null || refs.size() == 0) {
            log.info("\n\nOSGI: did not find object: " + interfaceClass.getName());
            return returnList;
        }
        for (ServiceReference ref : refs) {
            T obj = (T) context.getService(ref);
            if (obj instanceof IPentahoInitializer) {
                ((IPentahoInitializer) obj).init(session);
            }
            returnList.add(obj);
        }
        return returnList;
    } catch (InvalidSyntaxException e) {
        e.printStackTrace();
    }
    return returnList;
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) IPentahoInitializer(org.pentaho.platform.api.engine.IPentahoInitializer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with IPentahoInitializer

use of org.pentaho.platform.api.engine.IPentahoInitializer in project pentaho-platform by pentaho.

the class OSGIObjectFactory method get.

@Override
public <T> T get(Class<T> interfaceClass, IPentahoSession session, Map<String, String> properties) throws ObjectFactoryException {
    if (isBundleContextValid() == false) {
        return null;
    }
    try {
        Map<String, String> props = new HashMap<String, String>();
        if (properties != null) {
            props.putAll(properties);
        }
        props.put(REFERENCE_CLASS, interfaceClass.getName());
        Collection<ServiceReference<IPentahoObjectReference>> serviceReferences = this.context.getServiceReferences(IPentahoObjectReference.class, OSGIUtils.createFilter(props));
        if (serviceReferences != null && serviceReferences.size() > 0) {
            IPentahoObjectReference<T> obj = context.getService(serviceReferences.iterator().next());
            return obj.getObject();
        }
    } catch (InvalidSyntaxException e) {
        log.debug("Error retrieving from OSGI as ServiceReference, will try as bare type", e);
    }
    String filter = OSGIUtils.createFilter(properties);
    try {
        Collection<ServiceReference<T>> refs = context.getServiceReferences(interfaceClass, filter);
        ServiceReference ref;
        if (refs != null && refs.size() > 0) {
            ref = refs.toArray(new ServiceReference[refs.size()])[0];
        } else {
            ref = context.getServiceReference("" + interfaceClass.getName());
        }
        if (ref == null) {
            log.info("\n\nOSGI: did not find object: " + interfaceClass.getName());
            return null;
        }
        Object obj = context.getService(ref);
        if (obj instanceof IPentahoInitializer) {
            ((IPentahoInitializer) obj).init(session);
        }
        return (T) obj;
    } catch (InvalidSyntaxException e) {
        log.error("Error retrieving from OSGI ObjectFactory", e);
    }
    return null;
}
Also used : IPentahoInitializer(org.pentaho.platform.api.engine.IPentahoInitializer) HashMap(java.util.HashMap) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with IPentahoInitializer

use of org.pentaho.platform.api.engine.IPentahoInitializer in project pentaho-platform by pentaho.

the class SpringPentahoObjectReference method getObject.

@Override
@SuppressWarnings("unchecked")
public T getObject() {
    IPentahoSession sessionToUse = session != null ? session : PentahoSessionHolder.getSession();
    SpringScopeSessionHolder.SESSION.set(sessionToUse);
    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        Object obj = context.getBeanFactory().getBean(name);
        SpringScopeSessionHolder.SESSION.set(null);
        if (obj instanceof IPentahoInitializer) {
            ((IPentahoInitializer) obj).init(sessionToUse);
        }
        return (T) obj;
    } finally {
        Thread.currentThread().setContextClassLoader(originalClassLoader);
    }
}
Also used : IPentahoInitializer(org.pentaho.platform.api.engine.IPentahoInitializer) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 4 with IPentahoInitializer

use of org.pentaho.platform.api.engine.IPentahoInitializer in project pentaho-platform by pentaho.

the class SimpleObjectFactory method get.

@SuppressWarnings("unchecked")
public <T> T get(Class<T> interfaceClass, String key, IPentahoSession session) throws ObjectFactoryException {
    String classname = classnamesMap.get(key);
    try {
        Class implClass = Class.forName(classname);
        T t = (T) implClass.newInstance();
        if (t instanceof IPentahoInitializer) {
            ((IPentahoInitializer) t).init(session);
        }
        return t;
    } catch (Throwable th) {
        throw new ObjectFactoryException("Could not create instance for class " + classname, th);
    }
}
Also used : IPentahoInitializer(org.pentaho.platform.api.engine.IPentahoInitializer) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException)

Example 5 with IPentahoInitializer

use of org.pentaho.platform.api.engine.IPentahoInitializer in project pentaho-platform by pentaho.

the class OSGIObjectFactoryTest method testGetWInitializer.

@Test
public void testGetWInitializer() throws Exception {
    ServiceReference<IPentahoInitializer> ref = Mockito.mock(ServiceReference.class);
    when(mockContext.getServiceReferences(IPentahoInitializer.class, null)).thenReturn(Collections.singletonList(ref));
    final AtomicBoolean initialized = new AtomicBoolean(false);
    IPentahoInitializer initializer = new IPentahoInitializer() {

        @Override
        public void init(IPentahoSession session) {
            initialized.set(true);
        }
    };
    when(mockContext.getService(ref)).thenReturn(initializer);
    assertTrue("initializer should be rested", initialized.get() == false);
    IPentahoInitializer actual = factory.get(IPentahoInitializer.class, session);
    assertSame(initializer, actual);
    assertTrue("initializer not called", initialized.get());
    verify(mockContext).getServiceReferences(IPentahoInitializer.class, null);
    List<IPentahoInitializer> actuals = factory.getAll(IPentahoInitializer.class, session);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IPentahoInitializer(org.pentaho.platform.api.engine.IPentahoInitializer) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Test(org.junit.Test)

Aggregations

IPentahoInitializer (org.pentaho.platform.api.engine.IPentahoInitializer)5 HashMap (java.util.HashMap)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 ServiceReference (org.osgi.framework.ServiceReference)2 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 IPentahoObjectReference (org.pentaho.platform.api.engine.IPentahoObjectReference)1 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)1