Search in sources :

Example 1 with IPentahoObjectReference

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

the class AggregateObjectFactory method getObjectReferences.

@Override
public <T> List<IPentahoObjectReference<T>> getObjectReferences(Class<T> interfaceClass, IPentahoSession curSession, Map<String, String> properties) throws ObjectFactoryException {
    // Use a set to avoid duplicates
    Set<IPentahoObjectReference<T>> referenceSet = 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) {
                    referenceSet.addAll(found);
                }
            }
        }
    } finally {
        readLock.unlock();
    }
    // transform to a list to sort
    List<IPentahoObjectReference<T>> referenceList = new ArrayList<IPentahoObjectReference<T>>();
    referenceList.addAll(referenceSet);
    Collections.sort(referenceList, referencePriorityComparitor);
    return referenceList;
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 2 with IPentahoObjectReference

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

the class AggregateObjectFactory method getAll.

@Override
public <T> List<T> getAll(Class<T> interfaceClass, IPentahoSession curSession, Map<String, String> properties) throws ObjectFactoryException {
    List<IPentahoObjectReference<T>> referenceList = new ArrayList<IPentahoObjectReference<T>>();
    readLock.lock();
    try {
        for (IPentahoObjectFactory fact : factories) {
            if (fact.objectDefined(interfaceClass)) {
                List<IPentahoObjectReference<T>> refs = fact.getObjectReferences(interfaceClass, curSession, properties);
                if (refs != null) {
                    referenceList.addAll(refs);
                }
            }
        }
    } finally {
        readLock.unlock();
    }
    Collections.sort(referenceList, referencePriorityComparitor);
    // create final list of impls
    List<T> entryList = new ArrayList<T>();
    for (IPentahoObjectReference<T> ref : referenceList) {
        if (!entryList.contains(ref.getObject())) {
            entryList.add(ref.getObject());
        }
    }
    return entryList;
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) ArrayList(java.util.ArrayList)

Example 3 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> clazz, IPentahoSession curSession) throws ObjectFactoryException {
    Set<IPentahoObjectReference<T>> references = new HashSet<IPentahoObjectReference<T>>();
    readLock.lock();
    try {
        for (IPentahoObjectFactory fact : factories) {
            if (fact.objectDefined(clazz)) {
                IPentahoObjectReference<T> found = fact.getObjectReference(clazz, curSession);
                if (found != null) {
                    references.add(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 4 with IPentahoObjectReference

use of org.pentaho.platform.api.engine.IPentahoObjectReference 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 5 with IPentahoObjectReference

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

the class OSGIRuntimeObjectFactory method registerReference.

public <T> IPentahoObjectRegistration registerReference(final IPentahoObjectReference<?> reference, OSGIPentahoObjectRegistration existingRegistration, Class<?>... classes) {
    if (this.bundleContext == null) {
        ObjectRegistration runtimeRegistration = (ObjectRegistration) super.registerReference(reference, classes);
        OSGIPentahoObjectRegistration osgiPentahoObjectRegistration = new OSGIPentahoObjectRegistration(runtimeRegistration);
        synchronized (deferredRegistrations) {
            deferredRegistrations.add(osgiPentahoObjectRegistration);
        }
        return osgiPentahoObjectRegistration;
    }
    Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
    hashtable.putAll(reference.getAttributes());
    List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
    for (Class<?> aClass : classes) {
        try {
            // references unless the IPentahoObjectReference is a Singleton scope
            if (reference instanceof SingletonPentahoObjectReference || reference instanceof SpringPentahoObjectReference && (hashtable.get("scope").equals("singleton"))) {
                ServiceFactory<Object> factory = new ServiceFactory<Object>() {

                    @Override
                    public Object getService(Bundle bundle, ServiceRegistration<Object> serviceRegistration) {
                        return reference.getObject();
                    }

                    @Override
                    public void ungetService(Bundle bundle, ServiceRegistration<Object> serviceRegistration, Object o) {
                    }
                };
                if (hashtable.containsKey("priority")) {
                    hashtable.put(Constants.SERVICE_RANKING, hashtable.get("priority"));
                }
                ServiceRegistration<?> serviceRegistration = bundleContext.registerService(aClass.getName(), factory, hashtable);
                registrations.add(serviceRegistration);
            } else {
                // Publish it as an IPentahoObjectReference instead
                Hashtable<String, Object> referenceHashTable = new Hashtable<>(hashtable);
                referenceHashTable.put(REFERENCE_CLASS, aClass.getName());
                ServiceRegistration<?> serviceRegistration = bundleContext.registerService(IPentahoObjectReference.class.getName(), reference, referenceHashTable);
                registrations.add(serviceRegistration);
            }
        } catch (ClassCastException e) {
            logger.error("Error Retriving object from OSGI, Class is not as expected", e);
        }
    }
    if (existingRegistration != null) {
        existingRegistration.setRegistrations(registrations);
        return existingRegistration;
    } else {
        return new OSGIPentahoObjectRegistration(registrations);
    }
}
Also used : IPentahoObjectReference(org.pentaho.platform.api.engine.IPentahoObjectReference) ServiceFactory(org.osgi.framework.ServiceFactory) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) SingletonPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.references.SingletonPentahoObjectReference) ArrayList(java.util.ArrayList) IPentahoObjectRegistration(org.pentaho.platform.api.engine.IPentahoObjectRegistration) SpringPentahoObjectReference(org.pentaho.platform.engine.core.system.objfac.spring.SpringPentahoObjectReference) ServiceRegistration(org.osgi.framework.ServiceRegistration)

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