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:com.xpn.xwiki.store.XWikiHibernateBaseStore.java

/**
 * Hack to get the real JDBC connection because hibernate 3.1 wraps the connection in a proxy and this creates a
 * memory leak/*w ww . ja  va2  s .c o  m*/
 */
private Connection getRealConnection(Session session) {
    Connection conn = session.connection();
    if (conn instanceof Proxy) {
        Object bcp = Proxy.getInvocationHandler(conn);
        if (bcp instanceof BorrowedConnectionProxy) {
            ConnectionManager cm = (ConnectionManager) XWiki.getPrivateField(bcp, "connectionManager");
            if (cm != null) {
                return cm.getConnection();
            }
        }
    }
    return conn;
}

From source file:org.geoserver.catalog.impl.CatalogImpl.java

public void save(WorkspaceInfo workspace) {
    validate(workspace, false);// www  . jav a2 s  .c om

    ModificationProxy h = (ModificationProxy) Proxy.getInvocationHandler(workspace);

    WorkspaceInfo ws = (WorkspaceInfo) h.getProxyObject();
    if (!workspace.getName().equals(ws.getName())) {
        synchronized (workspaces) {
            workspaces.remove(ws.getName());
            workspaces.put(workspace.getName(), ws);
        }
    }

    saved(workspace);
}

From source file:org.geoserver.catalog.ResourcePool.java

/**
 * Returns true if this object is saved in the catalog and not a modified proxy. We don't want to
 * cache the result of computations made against a dirty object, nor the ones made against an 
 * object that still haven't been saved//from   w  w w .  ja  v  a2s.c om
 * @param info
 * @return
 */
boolean isCacheable(CatalogInfo info) {
    // saved?
    if (info.getId() == null) {
        return false;
    }

    // dirty?
    if (Proxy.isProxyClass(info.getClass())) {
        Object invocationHandler = Proxy.getInvocationHandler(info);
        if (invocationHandler instanceof ModificationProxy
                && ((ModificationProxy) invocationHandler).isDirty()) {
            return false;
        }
    }

    return true;
}

From source file:org.geoserver.catalog.impl.CatalogImpl.java

protected void saved(CatalogInfo object) {
    //this object is a proxy
    ModificationProxy h = (ModificationProxy) Proxy.getInvocationHandler(object);

    //get the real object
    CatalogInfo real = (CatalogInfo) h.getProxyObject();

    //fire out what changed
    List propertyNames = h.getPropertyNames();
    List newValues = h.getNewValues();
    List oldValues = h.getOldValues();

    //TODO: protect this original object, perhaps with another proxy 
    fireModified(real, propertyNames, oldValues, newValues);

    //commit to the original object
    h.commit();/*from   w w w.j  a  va2  s. c om*/

    //resolve to do a sync on the object
    //syncIdWithName(real);

    //fire the post modify event
    firePostModified(real);
}

From source file:com.p5solutions.core.utils.ReflectionUtility.java

/**
 * Invoke.//from  ww w.  j a va  2 s.  c  o m
 * 
 * @param <O>
 *          the generic type
 * @param <I>
 *          the generic type
 * @param object
 *          the object
 * @param method
 *          the method
 * @param ignoreAccess
 *          the ignore access
 * @param args
 *          the args
 * @return the object
 */
@SuppressWarnings("unchecked")
public static <O, I> O invokeWithAccessWithArguments(I object, Method method, boolean ignoreAccess,
        Object... args) {
    if (object == null) {
        throw new RuntimeException("Object cannot be null when invoking its methods");
    }

    Object returnObject = null;
    Class<?> clazz = object.getClass();

    try {

        /* call the method */
        if (method != null) {
            if (AopUtils.isAopProxy(object)) {
                InvocationHandler handler = Proxy.getInvocationHandler(object);
                returnObject = handler.invoke(object, method, args);
            } else {
                boolean isAccessible = method.isAccessible();
                try {
                    if (!isAccessible && ignoreAccess) {
                        method.setAccessible(true);
                    }
                    returnObject = method.invoke(object, args);
                } finally {
                    if (ignoreAccess) {
                        method.setAccessible(isAccessible);
                    }
                }
            }
        } else {
            throw new RuntimeException("Method cannot be null");
        }
    } catch (Throwable e) {
        // get the target class if its a proxy
        clazz = AopUtils.getTargetClass(object);

        /* Logger that is available to subclasses */
        Log logger = LogFactory.getLog(clazz);
        // logger.error("Unable to invoke method " + method.getName() + " within "
        // + clazz.getCanonicalName() + "\n"
        // + getStackTrace(e));

        throw new RuntimeException(e);
    }

    return (O) returnObject;
}

From source file:org.geoserver.catalog.hibernate.HibCatalogImpl.java

@SuppressWarnings("unchecked")
protected void saved(CatalogInfo object) {
    // this object is a proxy
    if (!Proxy.isProxyClass(object.getClass())) {
        LOGGER.severe("WORKAROUND: CatalogInfo object is a " + object.getClass().getSimpleName()
                + " -- Faking delta values in fireModified()");

        // fire out what changed
        List propertyNames = new ArrayList();
        List newValues = new ArrayList();
        List oldValues = new ArrayList();

        // TODO: protect this original object, perhaps with another proxy
        fireModified(object, propertyNames, oldValues, newValues);
        firePostModified(object);//from w  ww  . jav  a  2  s .  c o m
        return;
    }

    ModificationProxy h = (ModificationProxy) Proxy.getInvocationHandler(object);

    // get the real object
    CatalogInfo real = (CatalogInfo) h.getProxyObject();

    // fire out what changed
    List propertyNames = h.getPropertyNames();
    List newValues = h.getNewValues();
    List oldValues = h.getOldValues();

    // TODO: protect this original object, perhaps with another proxy
    fireModified(real, propertyNames, oldValues, newValues);

    // commit to the original object
    h.commit();

    // resolve to do a sync on the object
    // syncIdWithName(real);

    // fire the post modify event
    firePostModified(real);
}

From source file:org.quartz.impl.jdbcjobstore.JobStoreSupport.java

/**
 * <p>//w ww  .jav a 2 s .  c o  m
 * Cleanup the given database connection.  This means restoring
 * any modified auto commit or transaction isolation connection
 * attributes, and then closing the underlying connection.
 * </p>
 * 
 * <p>
 * This is separate from closeConnection() because the Spring 
 * integration relies on being able to overload closeConnection() and
 * expects the same connection back that it originally returned
 * from the datasource. 
 * </p>
 * 
 * @see #closeConnection(Connection)
 */
protected void cleanupConnection(Connection conn) {
    if (conn != null) {
        if (conn instanceof Proxy) {
            Proxy connProxy = (Proxy) conn;

            InvocationHandler invocationHandler = Proxy.getInvocationHandler(connProxy);
            if (invocationHandler instanceof AttributeRestoringConnectionInvocationHandler) {
                AttributeRestoringConnectionInvocationHandler connHandler = (AttributeRestoringConnectionInvocationHandler) invocationHandler;

                connHandler.restoreOriginalAtributes();
                closeConnection(connHandler.getWrappedConnection());
                return;
            }
        }

        // Wan't a Proxy, or was a Proxy, but wasn't ours.
        closeConnection(conn);
    }
}