Example usage for java.lang.reflect Proxy getInvocationHandler

List of usage examples for java.lang.reflect Proxy getInvocationHandler

Introduction

In this page you can find the example usage for java.lang.reflect Proxy getInvocationHandler.

Prototype

@CallerSensitive
public static InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException 

Source Link

Document

Returns the invocation handler for the specified proxy instance.

Usage

From source file:org.codehaus.enunciate.modules.xfire_client.EnunciatedSOAPWebServiceImpl.java

/**
 * Construct an enunciated SOAP web service that implements the specified interface.
 *
 * @param iface The interface.// w ww  .j av a  2  s .  c o  m
 * @param uuid The UUID of the interface.
 * @param endpoint The endpoint URL of the SOAP port.
 */
protected EnunciatedSOAPWebServiceImpl(Class iface, String uuid, String endpoint) {
    this.proxy = loadProxy(iface, uuid, endpoint);
    XFireProxy xfireProxy = (XFireProxy) Proxy.getInvocationHandler(proxy);
    this.client = xfireProxy.getClient();
    setMTOMEnabled(true);
}

From source file:org.kuali.rice.core.api.util.ClassLoaderUtils.java

/**
 * Unwraps the underlying object from the given proxy (which may itself be a proxy).  If the
 * given object is not a valid proxy, then null is returned.
 *//* w  w w.  j  a  va2  s.c om*/
private static Object unwrapFromProxyOnce(Object proxy) {
    if (proxy != null && Proxy.isProxyClass(proxy.getClass())) {
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);
        if (invocationHandler instanceof TargetedInvocationHandler) {
            return ((TargetedInvocationHandler) invocationHandler).getTarget();
        }
    }
    return null;
}

From source file:com.liferay.arkadiko.test.TestTwo.java

public void testDeployBundleWithImplementation() throws Exception {
    InterfaceOne interfaceOne = null;//from   www.j  ava 2s .  c  o m

    HasDependencyOnInterfaceOne bean = (HasDependencyOnInterfaceOne) _context
            .getBean(HasDependencyOnInterfaceOne.class.getName());

    // Test if the original impl is used

    interfaceOne = bean.getInterfaceOne();

    assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

    InvocationHandler ih = Proxy.getInvocationHandler(interfaceOne);

    assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
            ih instanceof ServiceTrackerInvocationHandler);

    ServiceTrackerInvocationHandler akih = (ServiceTrackerInvocationHandler) ih;

    assertTrue("currentService not equal to originalService",
            akih.getCurrentService() == akih.getOriginalService());

    // Install the bundle with the alternative impl

    Bundle installedBundle = installAndStart(_context, "/bundles/bundle-one/bundle-one.jar");

    try {
        // Test that the alternative impl is used

        interfaceOne = bean.getInterfaceOne();

        assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

        ih = Proxy.getInvocationHandler(interfaceOne);

        assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
                ih instanceof ServiceTrackerInvocationHandler);

        akih = (ServiceTrackerInvocationHandler) ih;

        assertFalse("currentService() is equal to originalService",
                akih.getCurrentService() == akih.getOriginalService());
    } finally {
        // Uninstall the bundle

        installedBundle.uninstall();
    }

    // Test again if the original impl is used

    interfaceOne = bean.getInterfaceOne();

    assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

    ih = Proxy.getInvocationHandler(interfaceOne);

    assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
            ih instanceof ServiceTrackerInvocationHandler);

    akih = (ServiceTrackerInvocationHandler) ih;

    assertTrue("currentService() is equal to originalService",
            akih.getCurrentService() == akih.getOriginalService());
}

From source file:com.liferay.arkadiko.test.TestSeven.java

public void testLoadOSGiOnlyDepedency() throws Exception {
    InterfaceOne interfaceOne = null;/*w w  w. j av a2 s . co  m*/

    HasDependencyOnInterfaceOne bean = (HasDependencyOnInterfaceOne) _context
            .getBean(HasDependencyOnInterfaceOne.class.getName());

    interfaceOne = bean.getInterfaceOne();

    assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

    InvocationHandler ih = Proxy.getInvocationHandler(interfaceOne);

    assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
            ih instanceof ServiceTrackerInvocationHandler);

    ServiceTrackerInvocationHandler akih = (ServiceTrackerInvocationHandler) ih;

    assertTrue("currentService not equal to originalService",
            akih.getCurrentService() == akih.getOriginalService());

    try {
        interfaceOne.getValue();

        Assert.fail("Should have thrown exception");
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
    }

    // Install the bundle with the dependency impl

    Bundle installedBundle = installAndStart(_context, "/bundles/bundle-one/bundle-one.jar");

    try {
        // Test to see that the dependency impl is used

        interfaceOne = bean.getInterfaceOne();

        assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

        ih = Proxy.getInvocationHandler(interfaceOne);

        assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
                ih instanceof ServiceTrackerInvocationHandler);

        akih = (ServiceTrackerInvocationHandler) ih;

        assertFalse("currentService is equal to originalService",
                akih.getCurrentService() == akih.getOriginalService());

        String testString = "test string";

        interfaceOne.setValue(testString);

        assertEquals("dependency impl returns the incorrect value", interfaceOne.getValue(), testString);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        installedBundle.uninstall();
    }

    interfaceOne = bean.getInterfaceOne();

    assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

    ih = Proxy.getInvocationHandler(interfaceOne);

    assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
            ih instanceof ServiceTrackerInvocationHandler);

    akih = (ServiceTrackerInvocationHandler) ih;

    assertTrue("currentService is equal to originalService",
            akih.getCurrentService() == akih.getOriginalService());

    try {
        interfaceOne.getValue();

        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
    }
}

From source file:net.sf.jdbcwrappers.spring.SpringTest.java

@Test
public void test() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "net/sf/jdbcwrappers/spring/beans.xml");
    try {/*from  w w w.j  av a2s  . c  o m*/
        DataSource ds = (DataSource) context.getBean("dataSource");
        assertTrue(Proxy.getInvocationHandler(ds) instanceof TrimmingDelegateInvocationHandler);
        // Attempt to get a connection
        ds.getConnection().close();
    } finally {
        context.close();
    }
}

From source file:org.qi4j.runtime.types.SerializableType.java

public Object toJSON(Object value) throws JSONException {
    // Check if we are serializing an Entity
    if (value instanceof EntityComposite) {
        // Store reference instead
        value = EntityReference.getEntityReference(value);
    } else if (value instanceof ValueComposite) {
        // Serialize ValueComposite JSON instead
        CompositeInstance instance = (CompositeInstance) Proxy.getInvocationHandler(value);
        ValueDescriptor descriptor = (ValueDescriptor) instance.descriptor();
        ValueType valueType = descriptor.valueType();
        try {//from   ww w  . j  a  va2 s  .  c om
            JSONObject object = (JSONObject) valueType.toJSON(value);
            object.put("_type", descriptor.type().getName());
            return object;
        } catch (JSONException e) {
            throw new IllegalStateException("Could not JSON serialize value", e);
        }
    }

    // Serialize value
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bout);
        out.writeUnshared(value);
        out.close();
        byte[] bytes = Base64Encoder.encode(bout.toByteArray(), true);
        String stringValue = new String(bytes, "UTF-8");
        return stringValue;
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not serialize value", e);
    }
}

From source file:com.fortuityframework.spring.broker.SpringEventListener.java

/**
 * @see com.fortuityframework.core.dispatch.EventListener#dispatchEvent(com.fortuityframework.core.dispatch.EventContext)
 *///from  w  w w  . j a  v a 2s .  c  o  m
@Override
public void dispatchEvent(EventContext<?> context) throws EventException {
    Object bean = applicationContext.getBean(beanName);

    try {
        if (!method.getDeclaringClass().isAssignableFrom(bean.getClass())) {
            try {
                Proxy.getInvocationHandler(bean).invoke(bean, method, new Object[] { context });
            } catch (Throwable e) {
                throw new EventException("Could not invoke proxy method", e);
            }
        } else {
            method.invoke(bean, context);
        }
    } catch (IllegalArgumentException e) {
        log.error("Could not invoke Spring event bean method", e);
        throw new EventException(e);
    } catch (IllegalAccessException e) {
        log.error("Could not invoke Spring event bean method", e);
        throw new EventException(e);
    } catch (InvocationTargetException e) {
        log.error("Could not invoke Spring event bean method", e);
        throw new EventException(e);
    }

}

From source file:com.liferay.arkadiko.test.TestThree.java

public void testDeployBundleWithImplementation() throws Exception {
    InterfaceOne interfaceOne = null;/*from  w ww .ja va  2s.  c  o m*/

    HasDependencyOnInterfaceOne bean, beanTwo, beanThree = null;

    // Install the bundle with the alternative impl

    Bundle installedBundle = installAndStart(_context, "/bundles/bundle-one/bundle-one.jar");

    try {
        // Test that the alternative impl is used by the first bean

        bean = (HasDependencyOnInterfaceOne) _context.getBean(HasDependencyOnInterfaceOne.class.getName());

        interfaceOne = bean.getInterfaceOne();

        assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

        InvocationHandler ih = Proxy.getInvocationHandler(interfaceOne);

        assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
                ih instanceof ServiceTrackerInvocationHandler);

        ServiceTrackerInvocationHandler akih = (ServiceTrackerInvocationHandler) ih;

        assertFalse("currentService is equal to originalService",
                akih.getCurrentService() == akih.getOriginalService());

        beanTwo = (HasDependencyOnInterfaceOne) _context
                .getBean(HasDependencyOnInterfaceOne.class.getName().concat("_TWO"));

        interfaceOne = beanTwo.getInterfaceOne();

        assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

        ih = Proxy.getInvocationHandler(interfaceOne);

        assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
                ih instanceof ServiceTrackerInvocationHandler);

        akih = (ServiceTrackerInvocationHandler) ih;

        assertFalse("currentService is equal to originalService",
                akih.getCurrentService() == akih.getOriginalService());

        beanThree = (HasDependencyOnInterfaceOne) _context
                .getBean(HasDependencyOnInterfaceOne.class.getName().concat("_THREE"));

        interfaceOne = beanThree.getInterfaceOne();

        assertFalse("interfaceOne is a proxy", Proxy.isProxyClass(interfaceOne.getClass()));
    } finally {
        installedBundle.uninstall();
    }

    interfaceOne = bean.getInterfaceOne();

    assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

    InvocationHandler ih = Proxy.getInvocationHandler(interfaceOne);

    assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
            ih instanceof ServiceTrackerInvocationHandler);

    ServiceTrackerInvocationHandler akih = (ServiceTrackerInvocationHandler) ih;

    assertTrue("currentService not equal to originalService",
            akih.getCurrentService() == akih.getOriginalService());

    interfaceOne = beanTwo.getInterfaceOne();

    assertTrue("interfaceOne is not a proxy", Proxy.isProxyClass(interfaceOne.getClass()));

    ih = Proxy.getInvocationHandler(interfaceOne);

    assertTrue("ih not instanceof AKServiceTrackerInvocationHandler",
            ih instanceof ServiceTrackerInvocationHandler);

    akih = (ServiceTrackerInvocationHandler) ih;

    assertTrue("currentService not equal to originalService",
            akih.getCurrentService() == akih.getOriginalService());

    interfaceOne = beanThree.getInterfaceOne();

    assertFalse("interfaceOne is a proxy", Proxy.isProxyClass(interfaceOne.getClass()));
}

From source file:org.resthub.rpc.AMQPProxy.java

/**
 * Handles the object invocation.//from   w  ww.j av  a2s  .  c o m
 *
 * @param proxy  the proxy object to invoke
 * @param method the method to call
 * @param args   the arguments to the proxy object
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String methodName = method.getName();
    Class<?>[] params = method.getParameterTypes();

    // equals and hashCode are special cased
    if (methodName.equals("equals") && params.length == 1 && params[0].equals(Object.class)) {
        Object value = args[0];
        if (value == null || !Proxy.isProxyClass(value.getClass())) {
            return Boolean.FALSE;
        }

        AMQPProxy handler = (AMQPProxy) Proxy.getInvocationHandler(value);

        return _factory.equals(handler._factory);
    } else if (methodName.equals("hashCode") && params.length == 0) {
        return _factory.hashCode();
    } else if (methodName.equals("toString") && params.length == 0) {
        return "[HessianProxy " + proxy.getClass() + "]";
    }

    ConnectionFactory connectionFactory = _factory.getConnectionFactory();

    Message response = sendRequest(connectionFactory, method, args);

    if (response == null) {
        throw new TimeoutException();
    }

    MessageProperties props = response.getMessageProperties();
    boolean compressed = "deflate".equals(props.getContentEncoding());

    InputStream is = new ByteArrayInputStream(response.getBody());
    if (compressed) {
        is = new InflaterInputStream(is, new Inflater(true));
    }

    return _factory.getSerializationHandler().readObject(method.getReturnType(), is);
}

From source file:org.apache.hadoop.hbase.ipc.ProtobufRpcClientEngine.java

@Override
public void stopProxy(IpcProtocol proxy) {
    if (proxy != null) {
        ((Invoker) Proxy.getInvocationHandler(proxy)).close();
    }
}