Search in sources :

Example 11 with IPentahoObjectReference

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

the class BeanBuilderTest method testGetObjectWithoutDampeningTimeout.

@Test
public void testGetObjectWithoutDampeningTimeout() throws Exception {
    IPentahoObjectFactory pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    ISystemConfig systemConfig = mock(ISystemConfig.class);
    doReturn(systemConfig).when(pentahoObjectFactory).get(eq(ISystemConfig.class), any(IPentahoSession.class));
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    final IPentahoObjectReference objectReference = mock(IPentahoObjectReference.class);
    when(pentahoObjectFactory.objectDefined(eq(BeanTestInterface.class))).thenReturn(true);
    when(pentahoObjectFactory.getObjectReferences(eq(BeanTestInterface.class), any(IPentahoSession.class), any(Map.class))).thenReturn(null, new ArrayList<IPentahoObjectReference>() {

        {
            add(objectReference);
        }
    });
    final int testValue = 5;
    doReturn(new BeanTestInterface() {

        @Override
        public int testMethod() {
            return testValue;
        }
    }).when(objectReference).getObject();
    BeanBuilder beanBuilder = new BeanBuilder();
    beanBuilder.setType(BeanTestInterface.class.getName());
    beanBuilder.setAttributes(new HashMap<String, String>());
    Object object = beanBuilder.getObject();
    assertNotNull(object);
    assertTrue(object instanceof BeanTestInterface);
    assertEquals(testValue, ((BeanTestInterface) object).testMethod());
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ISystemConfig(org.pentaho.platform.api.engine.ISystemConfig) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with IPentahoObjectReference

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

the class PentahoSystemTest method initXMLFactories.

private void initXMLFactories(boolean factoriesInPentahoXML) throws Exception {
    // mock pentaho.xml
    final ISystemSettings settingsService = mock(ISystemSettings.class);
    if (factoriesInPentahoXML) {
        final Node testNode = mock(Node.class);
        final Node testNodeName = mock(Node.class);
        when(testNodeName.getText()).thenReturn(testSystemPropertyName);
        when(testNode.selectSingleNode(eq("@name"))).thenReturn(testNodeName);
        final Node testNodeImplementation = mock(Node.class);
        when(testNodeImplementation.getText()).thenReturn(testXMLValue);
        when(testNode.selectSingleNode(eq("@implementation"))).thenReturn(testNodeImplementation);
        when(settingsService.getSystemSettings(eq("xml-factories/factory-impl"))).thenReturn(new LinkedList() {

            {
                add(testNode);
            }
        });
    } else {
        when(settingsService.getSystemSettings(eq("xml-factories/factory-impl"))).thenReturn(new LinkedList());
    }
    when(settingsService.getSystemSetting(anyString(), anyString())).thenReturn("");
    PentahoSystem.setSystemSettingsService(settingsService);
    // mock java-system-properties.properties
    final IPentahoObjectFactory objectFactory = mock(IPentahoObjectFactory.class);
    final ISystemConfig systemConfig = mock(ISystemConfig.class);
    final IConfiguration configuration = mock(IConfiguration.class);
    when(configuration.getProperties()).thenReturn(new Properties() {

        {
            setProperty(testSystemPropertyName, testPropertyValue);
        }
    });
    when(systemConfig.getConfiguration(eq(PentahoSystem.JAVA_SYSTEM_PROPERTIES))).thenReturn(configuration);
    when(objectFactory.objectDefined(eq(ISystemConfig.class))).thenReturn(true);
    final IPentahoObjectReference pentahoObjectReference = mock(IPentahoObjectReference.class);
    when(pentahoObjectReference.getObject()).thenReturn(systemConfig);
    when(objectFactory.getObjectReferences(eq(ISystemConfig.class), any(IPentahoSession.class), any(Map.class))).thenReturn(new LinkedList() {

        {
            add(pentahoObjectReference);
        }
    });
    PentahoSystem.registerObjectFactory(objectFactory);
    PentahoSystem.init();
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Node(org.dom4j.Node) ISystemConfig(org.pentaho.platform.api.engine.ISystemConfig) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) IConfiguration(org.pentaho.platform.api.engine.IConfiguration) Properties(java.util.Properties) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 13 with IPentahoObjectReference

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

the class BeanBuilder method getObject.

/*
   * (non-Javadoc)
   * 
   * @see org.springframework.beans.factory.FactoryBean#getObject()
   */
public Object getObject() {
    try {
        if (resolvingBean.get() == this) {
            log.warn("Circular Reference detected in bean creation ( " + type + " : " + attributes + "). Very likely a published " + "pentaho bean is resolving itself. Ensure that the published attributes do not match that of the Pentaho " + "bean query. The system will attempt to find the next highest available bean, but at a performance " + "penilty");
            // attempt to find a lower priority bean for them
            Class cls = getClass().getClassLoader().loadClass(type.trim());
            resolvingBean.set(this);
            List<IPentahoObjectReference<?>> objectReferences = PentahoSystem.getObjectFactory().getObjectReferences(cls, PentahoSessionHolder.getSession(), attributes);
            resolvingBean.set(null);
            if (objectReferences.size() > 1) {
                // we have more than one, return the second highest
                return objectReferences.get(1).getObject();
            } else {
                // there's only one bean, this is a fatal situation
                throw new IllegalStateException("Fatal Circular reference in Pentaho Bean ( " + type + " : " + attributes + ")");
            }
        } else {
            final Class cls = getClass().getClassLoader().loadClass(type.trim());
            resolvingBean.set(this);
            Object val = null;
            IPentahoObjectReference objectReference = PentahoSystem.getObjectFactory().getObjectReference(cls, PentahoSessionHolder.getSession(), attributes);
            if (objectReference != null) {
                val = objectReference.getObject();
            }
            resolvingBean.set(null);
            if (val == null) {
                log.debug("No object was found to satisfy pen:bean request [" + type + " : " + attributes + "]");
                final int f_dampeningTimeout = getDampeningTimeout();
                // send back a proxy
                if (cls.isInterface() && dampeningTimeout > -1) {
                    log.debug("Request bean which wasn't found is interface-based. Instantiating a Proxy dampener");
                    val = Proxy.newProxyInstance(cls.getClassLoader(), new Class[] { cls }, new InvocationHandler() {

                        // class name to prevent locking somewhere else by the same string
                        String lock = "lock_" + getClass().getName();

                        Object target;

                        Thread watcher;

                        boolean dead = false;

                        private void startWatcherThread(final int millis) {
                            watcher = new Thread(new Runnable() {

                                @Override
                                public void run() {
                                    int countdown = millis;
                                    while (countdown > 0) {
                                        IPentahoObjectReference objectReference;
                                        try {
                                            objectReference = PentahoSystem.getObjectFactory().getObjectReference(cls, PentahoSessionHolder.getSession(), attributes);
                                            if (objectReference != null) {
                                                target = objectReference.getObject();
                                            }
                                        } catch (ObjectFactoryException e) {
                                            log.debug("Error fetching from PentahoSystem", e);
                                        }
                                        if (target != null) {
                                            synchronized (lock) {
                                                lock.notifyAll();
                                            }
                                            return;
                                        }
                                        countdown -= 100;
                                    }
                                    dead = true;
                                }
                            });
                            watcher.start();
                        }

                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            if (target == null && f_dampeningTimeout > 0) {
                                synchronized (lock) {
                                    if (watcher == null && !dead) {
                                        startWatcherThread(f_dampeningTimeout);
                                        lock.wait(f_dampeningTimeout);
                                    }
                                }
                            }
                            if (target == null) {
                                // with the ID equal to the simple name of the class
                                if (attributes.isEmpty() || (attributes.size() == 1 && attributes.containsKey("id"))) {
                                    target = getFallbackBySimpleName(cls, attributes);
                                }
                                if (target == null) {
                                    throw new IllegalStateException("Target of Bean was never resolved: " + cls.getName());
                                }
                            }
                            return method.invoke(target, args);
                        }
                    });
                } else if (!cls.isInterface() && (attributes.isEmpty() || (attributes.size() == 1 && attributes.containsKey("id")))) {
                    val = getFallbackBySimpleName(cls, attributes);
                }
            }
            return val;
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (ObjectFactoryException e) {
        throw new RuntimeException(e);
    }
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException)

Example 14 with IPentahoObjectReference

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

the class AggregateObjectFactory method getObjectReference.

@Override
public <T> IPentahoObjectReference<T> getObjectReference(Class<T> interfaceClass, IPentahoSession curSession, Map<String, String> properties) throws ObjectFactoryException {
    Set<IPentahoObjectReference<T>> references = new HashSet<IPentahoObjectReference<T>>();
    readLock.lock();
    try {
        for (IPentahoObjectFactory fact : factories) {
            if (fact.objectDefined(interfaceClass)) {
                List<IPentahoObjectReference<T>> found = fact.getObjectReferences(interfaceClass, curSession, properties);
                if (found != null) {
                    references.addAll(found);
                }
            }
        }
    } finally {
        readLock.unlock();
    }
    IPentahoObjectReference<T> highestRef = null;
    int highestRefPriority = -1;
    for (IPentahoObjectReference<T> ref : references) {
        int pri = computePriority(ref);
        if (pri > highestRefPriority) {
            highestRef = ref;
            highestRefPriority = pri;
        }
    }
    return highestRef;
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) HashSet(java.util.HashSet)

Example 15 with IPentahoObjectReference

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

the class OSGIObjectFactory method getObjectReferences.

@Override
public <T> List<IPentahoObjectReference<T>> getObjectReferences(Class<T> interfaceClass, IPentahoSession curSession, Map<String, String> properties) throws ObjectFactoryException {
    if (isBundleContextValid() == false) {
        return Collections.emptyList();
    }
    List<IPentahoObjectReference<T>> returnRefs = new ArrayList<IPentahoObjectReference<T>>();
    // make sure we check by reference first
    if (properties == null || !properties.containsKey(REFERENCE_CLASS)) {
        Map<String, String> props = new HashMap<>();
        if (properties != null) {
            props.putAll(properties);
        }
        props.put(REFERENCE_CLASS, interfaceClass.getName());
        List<IPentahoObjectReference<IPentahoObjectReference>> objectReferences = getObjectReferences(IPentahoObjectReference.class, curSession, props);
        for (IPentahoObjectReference<IPentahoObjectReference> objectReference : objectReferences) {
            returnRefs.add(objectReference.getObject());
        }
    }
    String filter = OSGIUtils.createFilter(properties);
    try {
        Collection<ServiceReference<T>> refs = context.getServiceReferences(interfaceClass, filter);
        if (refs == null || refs.size() == 0) {
            log.info("OSGI: did not find object: " + interfaceClass.getName());
            return returnRefs;
        }
        for (ServiceReference ref : refs) {
            returnRefs.add(new OsgiPentahoObjectReference<T>(this.context, interfaceClass, ref));
        }
        Collections.sort(returnRefs);
        return returnRefs;
    } catch (InvalidSyntaxException e) {
        e.printStackTrace();
    }
    return Collections.emptyList();
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServiceReference(org.osgi.framework.ServiceReference) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

IPentahoObjectReference (org.pentaho.platform.api.engine.IPentahoObjectReference)15 ArrayList (java.util.ArrayList)7 IPentahoObjectFactory (org.pentaho.platform.api.engine.IPentahoObjectFactory)6 Test (org.junit.Test)4 ServiceReference (org.osgi.framework.ServiceReference)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)3 Map (java.util.Map)2 ISystemConfig (org.pentaho.platform.api.engine.ISystemConfig)2 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)2 SingletonPentahoObjectReference (org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 Hashtable (java.util.Hashtable)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 Node (org.dom4j.Node)1 Bundle (org.osgi.framework.Bundle)1