use of org.apache.aries.proxy.UnableToProxyException in project aries by apache.
the class BeanRecipe method createProxyBean.
private Object createProxyBean(ReferenceRecipe rr) {
try {
VoidableCallable vc = new VoidableCallable();
rr.addVoidableChild(vc);
return blueprintContainer.getProxyManager().createDelegatingProxy(blueprintContainer.getBundleContext().getBundle(), rr.getProxyChildBeanClasses(), vc, vc.call());
} catch (UnableToProxyException e) {
throw new ComponentDefinitionException(e);
}
}
use of org.apache.aries.proxy.UnableToProxyException in project aries by apache.
the class ServiceHelper method proxyPrivileged.
private static Object proxyPrivileged(String interface1, String filter, boolean dynamicRebind, BundleContext ctx, ServicePair pair, int timeout) {
String[] interfaces = null;
if (interface1 != null) {
interfaces = new String[] { interface1 };
} else {
interfaces = (String[]) pair.ref.getProperty(Constants.OBJECTCLASS);
}
List<Class<?>> clazz = new ArrayList<Class<?>>(interfaces.length);
// We load the interface classes the service is registered under using the defining bundle.
// This is ok because the service must be able to see the classes to be registered using them.
// We then check to see if isAssignableTo on the reference works for the owning bundle and
// the interface name and only use the interface if true is returned there.
// This might seem odd, but equinox and felix return true for isAssignableTo if the
// Bundle provided does not import the package. This is under the assumption the
// caller will then use reflection. The upshot of doing it this way is that a utility
// bundle can be created which centralizes JNDI lookups, but the service will be used
// by another bundle. It is true that class space consistency is less safe, but we
// are enabling a slightly odd use case anyway.
// August 13th 2013: We've found valid use cases in which a Bundle is exporting
// services that the Bundle itself cannot load. We deal with this rare case by
// noting the classes that we failed to load. If as a result we have no classes
// to proxy, we try those classes again but instead pull the Class objects off
// the service rather than from the bundle exporting that service.
Bundle serviceProviderBundle = pair.ref.getBundle();
Bundle owningBundle = ctx.getBundle();
ProxyManager proxyManager = Activator.getProxyManager();
Collection<String> classesNotFound = new ArrayList<String>();
for (String interfaceName : interfaces) {
try {
Class<?> potentialClass = serviceProviderBundle.loadClass(interfaceName);
if (pair.ref.isAssignableTo(owningBundle, interfaceName)) {
clazz.add(potentialClass);
}
} catch (ClassNotFoundException e) {
classesNotFound.add(interfaceName);
}
}
if (clazz.isEmpty() && !classesNotFound.isEmpty()) {
Class<?>[] ifacesOnService = ctx.getService(pair.ref).getClass().getInterfaces();
for (String interfaceName : classesNotFound) {
Class<?> thisClass = null;
for (Class<?> c : getAllInterfaces(ifacesOnService)) {
if (c.getName().equals(interfaceName)) {
thisClass = c;
break;
}
}
if (thisClass != null) {
if (pair.ref.isAssignableTo(owningBundle, interfaceName)) {
clazz.add(thisClass);
}
}
}
}
if (clazz.isEmpty()) {
throw new IllegalArgumentException(Arrays.asList(interfaces).toString());
}
Callable<Object> ih = new JNDIServiceDamper(ctx, interface1, filter, pair, dynamicRebind, timeout);
try {
return proxyManager.createDelegatingProxy(serviceProviderBundle, clazz, ih, null);
} catch (UnableToProxyException e) {
throw new IllegalArgumentException(e);
} catch (RuntimeException e) {
throw new IllegalArgumentException(MESSAGES.getMessage("unable.to.create.proxy", pair.ref), e);
}
}
use of org.apache.aries.proxy.UnableToProxyException in project aries by apache.
the class AbstractWovenProxyAdapter method superHasNoArgsConstructor.
/**
* This method allows us to determine whether a superclass has a
* non-private no-args constructor without causing it to initialize.
* This avoids a potential ClassCircularityError on Mac VMs if the
* initialization references the subclass being woven. Odd, but seen
* in the wild!
*/
private final boolean superHasNoArgsConstructor(String superName, String name) {
ConstructorFinder cf = new ConstructorFinder();
try {
InputStream is = loader.getResourceAsStream(superName + ".class");
if (is == null)
throw new IOException();
new ClassReader(is).accept(cf, ClassReader.SKIP_FRAMES + ClassReader.SKIP_DEBUG + ClassReader.SKIP_CODE);
} catch (IOException ioe) {
UnableToProxyException u = new UnableToProxyException(name, ioe);
cannotLoadSuperClassException(superName, u);
}
return cf.hasNoArgsConstructor();
}
use of org.apache.aries.proxy.UnableToProxyException in project aries by apache.
the class AbstractWovenProxyAdapter method writeCreateNewProxyInstanceAndConstructor.
/**
* We write createNewProxyInstance separately because it isn't final, and is
* overridden on each class, we also write a constructor for this method to
* use if we don't have one.
*/
private final void writeCreateNewProxyInstanceAndConstructor() {
GeneratorAdapter methodAdapter = getMethodGenerator(ACC_PUBLIC, new Method("org_apache_aries_proxy_weaving_WovenProxy_createNewProxyInstance", WOVEN_PROXY_IFACE_TYPE, DISPATCHER_LISTENER_METHOD_ARGS));
// /////////////////////////////////////////////////////
// Implement the method
// Create and instantiate a new instance, then return it
methodAdapter.newInstance(typeBeingWoven);
methodAdapter.dup();
methodAdapter.loadArgs();
methodAdapter.invokeConstructor(typeBeingWoven, new Method("<init>", Type.VOID_TYPE, DISPATCHER_LISTENER_METHOD_ARGS));
methodAdapter.returnValue();
methodAdapter.endMethod();
//////////////////////////////////////////////////////////
// Write a protected no-args constructor for this class
methodAdapter = getMethodGenerator(ACC_PROTECTED | ACC_SYNTHETIC, ARGS_CONSTRUCTOR);
if (implementWovenProxy) {
methodAdapter.loadThis();
if (superHasNoArgsConstructor)
methodAdapter.invokeConstructor(superType, NO_ARGS_CONSTRUCTOR);
else {
if (hasNoArgsConstructor)
methodAdapter.invokeConstructor(typeBeingWoven, NO_ARGS_CONSTRUCTOR);
else
throw new RuntimeException(new UnableToProxyException(typeBeingWoven.getClassName(), String.format("The class %s and its superclass %s do not have no-args constructors and cannot be woven.", typeBeingWoven.getClassName(), superType.getClassName())));
}
methodAdapter.loadThis();
methodAdapter.loadArg(0);
methodAdapter.putField(typeBeingWoven, DISPATCHER_FIELD, DISPATCHER_TYPE);
methodAdapter.loadThis();
methodAdapter.loadArg(1);
methodAdapter.putField(typeBeingWoven, LISTENER_FIELD, LISTENER_TYPE);
} else {
//We just invoke the super with args
methodAdapter.loadThis();
methodAdapter.loadArgs();
methodAdapter.invokeConstructor(superType, ARGS_CONSTRUCTOR);
}
//Throw an NPE if the dispatcher is null, return otherwise
methodAdapter.loadArg(0);
Label returnValue = methodAdapter.newLabel();
methodAdapter.ifNonNull(returnValue);
methodAdapter.newInstance(NPE_TYPE);
methodAdapter.dup();
methodAdapter.push("The dispatcher must never be null!");
methodAdapter.invokeConstructor(NPE_TYPE, NPE_CONSTRUCTOR);
methodAdapter.throwException();
methodAdapter.mark(returnValue);
methodAdapter.returnValue();
methodAdapter.endMethod();
//////////////////////////////////////////////////////////
}
use of org.apache.aries.proxy.UnableToProxyException in project aries by apache.
the class InterfaceCombiningClassAdapter method generateBytes.
/**
* Generate the byte[] for our class
* @return
* @throws UnableToProxyException
*/
final byte[] generateBytes() throws UnableToProxyException {
if (!!!done) {
for (Class<?> c : interfaces) {
adapter.setCurrentMethodDeclaringType(Type.getType(c), true);
try {
AbstractWovenProxyAdapter.readClass(c, this);
} catch (IOException e) {
throw new UnableToProxyException(c, e);
}
}
Class<?> clazz = superclass;
while (clazz != null && (clazz.getModifiers() & Modifier.ABSTRACT) != 0) {
adapter.setCurrentMethodDeclaringType(Type.getType(clazz), false);
visitAbstractMethods(clazz);
clazz = clazz.getSuperclass();
}
adapter.setCurrentMethodDeclaringType(AbstractWovenProxyAdapter.OBJECT_TYPE, false);
visitObjectMethods();
adapter.visitEnd();
done = true;
}
return writer.toByteArray();
}
Aggregations