Search in sources :

Example 91 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project Payara by payara.

the class WriteableView method removeNestedElements.

boolean removeNestedElements(Object object) {
    InvocationHandler h = Proxy.getInvocationHandler(object);
    if (!(h instanceof WriteableView)) {
        // h instanceof ConfigView
        ConfigBean bean = (ConfigBean) ((ConfigView) h).getMasterView();
        h = bean.getWriteableView();
        if (h == null) {
            ConfigBeanProxy writable;
            try {
                writable = currentTx.enroll((ConfigBeanProxy) object);
            } catch (TransactionFailure e) {
                // something is seriously wrong
                throw new RuntimeException(e);
            }
            h = Proxy.getInvocationHandler(writable);
        } else {
            // so oldValue was already processed
            return false;
        }
    }
    WriteableView writableView = (WriteableView) h;
    synchronized (writableView) {
        writableView.isDeleted = true;
    }
    boolean removed = false;
    for (Property property : writableView.bean.model.elements.values()) {
        if (property.isCollection()) {
            Object nested = writableView.getter(property, parameterizedType);
            ProtectedList list = (ProtectedList) nested;
            if (list.size() > 0) {
                list.clear();
                removed = true;
            }
        } else if (!property.isLeaf()) {
            // Element
            Object oldValue = writableView.getter(property, ConfigBeanProxy.class);
            if (oldValue != null) {
                writableView.setter(property, null, Dom.class);
                removed = true;
                removeNestedElements(oldValue);
            }
        }
    }
    return removed;
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler) Property(org.jvnet.hk2.config.ConfigModel.Property)

Example 92 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project Payara by payara.

the class ProfiledConnectionWrapper40 method getProxyObject.

// TODO refactor this method and move to a higher level
private <T> T getProxyObject(final Object actualObject, Class<T>[] ifaces) throws Exception {
    T result;
    InvocationHandler ih = new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            SQLTraceRecord record = new SQLTraceRecord();
            record.setMethodName(method.getName());
            record.setParams(args);
            record.setClassName(actualObject.getClass().getName());
            record.setThreadName(Thread.currentThread().getName());
            record.setThreadID(Thread.currentThread().getId());
            record.setTimeStamp(System.currentTimeMillis());
            try {
                long startTime = System.currentTimeMillis();
                Object methodResult = method.invoke(actualObject, args);
                record.setExecutionTime(System.currentTimeMillis() - startTime);
                return methodResult;
            } catch (InvocationTargetException ex) {
                Throwable cause = ex.getCause();
                if (cause != null) {
                    throw cause;
                } else {
                    throw ex;
                }
            } finally {
                sqlTraceDelegator.sqlTrace(record);
            }
        }
    };
    result = (T) Proxy.newProxyInstance(actualObject.getClass().getClassLoader(), ifaces, ih);
    return result;
}
Also used : SQLTraceRecord(org.glassfish.api.jdbc.SQLTraceRecord) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 93 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project Payara by payara.

the class ImplementorCacheDelegateImpl method createImplementor.

private Implementor createImplementor(RuntimeEndpointInfo targetEndpoint) throws Exception {
    Tie tie = (Tie) targetEndpoint.getTieClass().newInstance();
    Class seiClass = targetEndpoint.getRemoteInterface();
    Class implClass = targetEndpoint.getImplementationClass();
    Remote servant = null;
    if (seiClass.isAssignableFrom(implClass)) {
        // if servlet endpoint impl is a subtype of SEI, use an
        // instance as the servant.
        servant = (Remote) implClass.newInstance();
    } else {
        // Create a dynamic proxy that implements SEI (and optionally
        // ServiceLifecycle) and delegates to an instance of the
        // endpoint impl.
        Object implInstance = implClass.newInstance();
        InvocationHandler handler = new ServletImplInvocationHandler(implInstance);
        boolean implementsLifecycle = ServiceLifecycle.class.isAssignableFrom(implClass);
        Class[] proxyInterfaces = implementsLifecycle ? new Class[] { seiClass, ServiceLifecycle.class } : new Class[] { seiClass };
        servant = (Remote) Proxy.newProxyInstance(implClass.getClassLoader(), proxyInterfaces, handler);
    }
    tie.setTarget(servant);
    Implementor implementor = rpcFactory_.createImplementor(servletContext_, tie);
    implementor.init();
    return implementor;
}
Also used : Tie(com.sun.xml.rpc.spi.runtime.Tie) Implementor(com.sun.xml.rpc.spi.runtime.Implementor) Remote(java.rmi.Remote) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 94 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project Payara by payara.

the class PayaraClusteredCDIEventImpl method getQualifiers.

@Override
public Set<Annotation> getQualifiers() {
    if (qualifiers == null) {
        try {
            qualifiers = qualifiersPayload.getValue();
        } catch (IOException | ClassNotFoundException ex) {
            Logger.getLogger(PayaraClusteredCDIEventImpl.class.getName()).log(Level.INFO, "Unable to deserialize qualifiers received on the event ignoring...", ex);
        }
    }
    if (qualifiers != null) {
        Set<Annotation> result = new HashSet<>(qualifiers.size());
        for (InvocationHandler qualifier : qualifiers) {
            // ok we have the invocation handlers that have been serialized
            // we now need to create a Proxy for each
            // First find the type
            Method[] methods = Annotation.class.getMethods();
            Class<?> annotationClazz = null;
            for (Method method : methods) {
                if (method.getName().equals("annotationType")) {
                    try {
                        annotationClazz = (Class<?>) qualifier.invoke(null, method, new Object[0]);
                        // then create a proxy for the annotation type from the serialized Invocation Handler
                        result.add((Annotation) Proxy.newProxyInstance(Utility.getClassLoader(), new Class[] { annotationClazz }, qualifier));
                    } catch (Throwable ex) {
                        Logger.getLogger(PayaraClusteredCDIEventImpl.class.getName()).log(Level.INFO, "Problem determining the qualifier type of an Event ignoring", ex);
                    }
                }
            }
        }
        return result;
    } else {
        return Collections.EMPTY_SET;
    }
}
Also used : IOException(java.io.IOException) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 95 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project Payara by payara.

the class CommandRunnerTest method simpleDomain.

private static Domain simpleDomain() {
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            throw new UnsupportedOperationException("Feature-free dummy implementation for injection only");
        }
    };
    Domain d = (Domain) Proxy.newProxyInstance(Domain.class.getClassLoader(), new Class[] { Domain.class }, handler);
    return d;
}
Also used : BeforeClass(org.junit.BeforeClass) Method(java.lang.reflect.Method) Domain(com.sun.enterprise.config.serverbeans.Domain) InvocationHandler(java.lang.reflect.InvocationHandler)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)411 Method (java.lang.reflect.Method)232 Test (org.junit.Test)70 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 Proxy (java.lang.reflect.Proxy)28 ArrayList (java.util.ArrayList)25 Map (java.util.Map)23 IOException (java.io.IOException)19 Field (java.lang.reflect.Field)19 HashMap (java.util.HashMap)18 AccessibleObject (java.lang.reflect.AccessibleObject)16 List (java.util.List)16 BindingProvider (javax.xml.ws.BindingProvider)14 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 Before (org.junit.Before)10 Connection (java.sql.Connection)9 LinkedHashMap (java.util.LinkedHashMap)9 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)8 DexMakerTest (com.android.dx.DexMakerTest)7