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;
}
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;
}
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);
}
}
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);
}
}
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);
}
Aggregations