use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.
the class BlueprintRepository method getAllRecipes.
public Set<Recipe> getAllRecipes(String... names) {
ExecutionContext oldContext = ExecutionContext.Holder.setContext(this);
try {
Set<Recipe> allRecipes = new HashSet<Recipe>();
Collection<String> topLevel = names != null && names.length > 0 ? Arrays.asList(names) : recipes.keySet();
for (String name : topLevel) {
internalGetAllRecipes(allRecipes, getRecipe(name));
}
return allRecipes;
} finally {
ExecutionContext.Holder.setContext(oldContext);
}
}
use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.
the class BlueprintContainerImpl method injectBeanInstance.
public void injectBeanInstance(BeanMetadata bmd, Object o) throws IllegalArgumentException, ComponentDefinitionException {
ExecutionContext origContext = ExecutionContext.Holder.setContext((ExecutionContext) getRepository());
try {
ComponentMetadata cmd = componentDefinitionRegistry.getComponentDefinition(bmd.getId());
if (cmd == null || cmd != bmd) {
throw new IllegalArgumentException(bmd.getId() + " not found in blueprint container");
}
Recipe r = this.getRepository().getRecipe(bmd.getId());
if (r instanceof BeanRecipe) {
BeanRecipe br = (BeanRecipe) r;
if (!br.getType().isInstance(o)) {
throw new IllegalArgumentException("Instance class " + o.getClass().getName() + " is not an instance of " + br.getClass());
}
br.setProperties(o);
} else {
throw new IllegalArgumentException(bmd.getId() + " does not refer to a BeanRecipe");
}
} finally {
ExecutionContext.Holder.setContext(origContext);
}
}
use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.
the class AbstractServiceReferenceRecipe method getExtendedOsgiFilter.
private String getExtendedOsgiFilter() {
if (filterRecipe != null) {
Object object;
BlueprintRepository repository = ((BlueprintContainerImpl) blueprintContainer).getRepository();
ExecutionContext oldContext = null;
try {
oldContext = ExecutionContext.Holder.setContext(repository);
object = filterRecipe.create();
} finally {
ExecutionContext.Holder.setContext(oldContext);
}
if (object != null) {
String flt = object.toString();
if (flt != null && flt.length() > 0) {
if (!flt.startsWith("(")) {
flt = "(" + flt + ")";
}
return flt;
}
}
}
return null;
}
use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.
the class ReflectionUtilsTest method before.
@BeforeClass
public static void before() throws ClassNotFoundException {
mockBlueprint = EasyMock.createNiceMock(ExtendedBlueprintContainer.class);
final Capture<String> nameCapture = new Capture<String>();
EasyMock.expect(mockBlueprint.loadClass(EasyMock.capture(nameCapture))).andAnswer(new IAnswer<Class<?>>() {
public Class<?> answer() throws Throwable {
return Thread.currentThread().getContextClassLoader().loadClass(nameCapture.getValue());
}
});
EasyMock.replay(mockBlueprint);
ExecutionContext.Holder.setContext(new ExecutionContext() {
public void addPartialObject(String name, Object object) {
}
public boolean containsObject(String name) {
return false;
}
public Object convert(Object value, ReifiedType type) throws Exception {
if (type.getRawClass().equals(Inconvertible.class))
throw new Exception();
else if (type.getRawClass().equals(String.class))
return String.valueOf(value);
else if (type.getRawClass().equals(List.class)) {
if (value == null)
return null;
else if (value instanceof Collection)
return new ArrayList((Collection) value);
else
throw new Exception();
} else if (value == null)
return null;
else if (type.getRawClass().isInstance(value))
return value;
else
throw new Exception();
}
public boolean canConvert(Object value, ReifiedType type) {
if (value instanceof Inconvertible)
return false;
else if (type.getRawClass().equals(String.class))
return true;
else if (type.getRawClass().equals(List.class) && (value == null || value instanceof Collection))
return true;
else
return false;
}
public Object getObject(String name) {
return null;
}
public Object getPartialObject(String name) {
return null;
}
public Recipe getRecipe(String name) {
return null;
}
public Class loadClass(String className) throws ClassNotFoundException {
return null;
}
public Recipe pop() {
return null;
}
public void push(Recipe recipe) throws CircularDependencyException {
}
public void removePartialObject(String name) {
}
public Future<Object> addFullObject(String name, Future<Object> object) {
return null;
}
});
}
use of org.apache.aries.blueprint.di.ExecutionContext in project aries by apache.
the class ServiceRecipe method createService.
private void createService() {
try {
if (service == null) {
LOGGER.debug("Creating service instance");
//We can't use the BlueprintRepository because we don't know what interfaces
//to use yet! We have to be a bit smarter.
ExecutionContext old = ExecutionContext.Holder.setContext(blueprintContainer.getRepository());
try {
Object o = serviceRecipe.create();
if (o instanceof Convertible) {
o = blueprintContainer.getRepository().convert(o, new ReifiedType(Object.class));
validateClasses(o);
} else if (o instanceof UnwrapperedBeanHolder) {
UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) o;
if (holder.unwrapperedBean instanceof ServiceFactory) {
//If a service factory is used, make sure the proxy classes implement this
//interface so that later on, internalGetService will create the real
//service from it.
LOGGER.debug("{} implements ServiceFactory, creating proxy that also implements this", holder.unwrapperedBean);
Collection<Class<?>> cls = getClassesForProxying(holder.unwrapperedBean);
cls.add(blueprintContainer.loadClass("org.osgi.framework.ServiceFactory"));
o = BeanRecipe.wrap(holder, cls);
} else {
validateClasses(holder.unwrapperedBean);
o = BeanRecipe.wrap(holder, getClassesForProxying(holder.unwrapperedBean));
}
} else if (!(o instanceof ServiceFactory)) {
validateClasses(o);
}
service = o;
} catch (Exception e) {
LOGGER.error("Error retrieving service from " + this, e);
throw new ComponentDefinitionException(e);
} finally {
ExecutionContext.Holder.setContext(old);
}
LOGGER.debug("Service created: {}", service);
}
// When the service is first requested, we need to create listeners and call them
if (!initialServiceRegistration && listeners == null) {
LOGGER.debug("Creating listeners");
if (listenersRecipe != null) {
listeners = (List) createRecipe(listenersRecipe);
} else {
listeners = Collections.emptyList();
}
LOGGER.debug("Listeners created: {}", listeners);
if (registration.get() != null) {
LOGGER.debug("Calling listeners for initial service registration");
for (ServiceListener listener : listeners) {
listener.register(service, registrationProperties);
}
} else {
LOGGER.debug("Calling listeners for initial service unregistration");
for (ServiceListener listener : listeners) {
listener.unregister(service, registrationProperties);
}
}
}
} catch (RuntimeException e) {
LOGGER.error("Error retrieving service from " + this, e);
throw e;
}
}
Aggregations