Search in sources :

Example 6 with UnableToProxyException

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);
    }
}
Also used : ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 7 with UnableToProxyException

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);
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ProxyManager(org.apache.aries.proxy.ProxyManager) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException)

Example 8 with UnableToProxyException

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();
}
Also used : InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) IOException(java.io.IOException)

Example 9 with UnableToProxyException

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();
//////////////////////////////////////////////////////////
}
Also used : Label(org.objectweb.asm.Label) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) Method(org.objectweb.asm.commons.Method)

Example 10 with UnableToProxyException

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();
}
Also used : UnableToProxyException(org.apache.aries.proxy.UnableToProxyException) IOException(java.io.IOException)

Aggregations

UnableToProxyException (org.apache.aries.proxy.UnableToProxyException)12 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 FinalModifierException (org.apache.aries.proxy.FinalModifierException)2 ClassReader (org.objectweb.asm.ClassReader)2 Bundle (org.osgi.framework.Bundle)2 BundleWiring (org.osgi.framework.wiring.BundleWiring)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 ProxyManager (org.apache.aries.proxy.ProxyManager)1 WovenProxy (org.apache.aries.proxy.weaving.WovenProxy)1 Label (org.objectweb.asm.Label)1 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)1 Method (org.objectweb.asm.commons.Method)1 WeavingException (org.osgi.framework.hooks.weaving.WeavingException)1