Example usage for java.lang.reflect Proxy newProxyInstance

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

Introduction

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

Prototype

private static Object newProxyInstance(Class<?> caller, 
            Constructor<?> cons, InvocationHandler h) 

Source Link

Usage

From source file:org.apache.hadoop.hive.metastore.RetryingRawStore.java

public static RawStore getProxy(HiveConf hiveConf, Configuration conf, String rawStoreClassName, int id)
        throws MetaException {

    Class<? extends RawStore> baseClass = (Class<? extends RawStore>) MetaStoreUtils
            .getClass(rawStoreClassName);

    RetryingRawStore handler = new RetryingRawStore(hiveConf, conf, baseClass, id);

    return (RawStore) Proxy.newProxyInstance(RetryingRawStore.class.getClassLoader(), baseClass.getInterfaces(),
            handler);/*  w  w w  .j a v  a  2 s.co  m*/
}

From source file:com.amazonaws.services.lambda.invoke.LambdaInvokerFactory.java

/**
 * Creates a new Lambda invoker implementing the given interface and wrapping the given
 * {@code AWSLambda} client./*from  www .  ja v a2 s  . c  om*/
 *
 * @param interfaceClass
 *            the interface to implement
 * @param awsLambda
 *            the lambda client to use for making remote calls
 */
public static <T> T build(Class<T> interfaceClass, AWSLambda awsLambda) {
    Object proxy = Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] { interfaceClass },
            new LambdaInvocationHandler(interfaceClass, awsLambda));

    return interfaceClass.cast(proxy);
}

From source file:net.sf.ehcache.exceptionhandler.ExceptionHandlingDynamicCacheProxy.java

/**
 * A simple factory method to hide the messiness of creating the proxy from clients.
 *
 * @param ehcache the target cache//w ww.  j  a v a 2s  .com
 * @return a proxied Ehcache
 */
public static Ehcache createProxy(Ehcache ehcache) {
    return (Ehcache) Proxy.newProxyInstance(ehcache.getClass().getClassLoader(), new Class[] { Ehcache.class },
            new ExceptionHandlingDynamicCacheProxy(ehcache));
}

From source file:hermes.store.jdbc.JDBCConnectionPool.java

protected Connection newProxyInstance(Connection connection) {
    return (Connection) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Connection.class },
            new ObjectPoolProxy<Connection>(connection, this, "close"));
}

From source file:org.bytesoft.openjtcc.supports.dubbo.internal.RemoteInvocationServiceFactoryImpl.java

@Override
public RemoteInvocationService getRemoteInvocationService() {
    RemoteInvocationEndpointSelector handler = new RemoteInvocationEndpointSelector();
    handler.setApplicationContext(this.applicationContext);
    return (RemoteInvocationService) Proxy.newProxyInstance(RemoteInvocationService.class.getClassLoader(),
            new Class[] { RemoteInvocationService.class }, handler);
}

From source file:org.zenoss.zep.dao.impl.IndexMetadataDaoImpl.java

public IndexMetadataDaoImpl(DataSource ds, ZepInstance instance) {
    this.template = (SimpleJdbcOperations) Proxy.newProxyInstance(SimpleJdbcOperations.class.getClassLoader(),
            new Class<?>[] { SimpleJdbcOperations.class }, new SimpleJdbcTemplateProxy(ds));
    this.zepInstanceId = instance.getId();
}

From source file:com.someone.pizzaservice.infrastructure.BenchmarkProxyBeanPostprocessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Set<String> setOfBenchmarkedMethods = new HashSet<>();
    Method[] methods = bean.getClass().getMethods();
    for (Method m : methods) {
        if (m.getAnnotation(Benchmark.class) != null && m.getAnnotation(Benchmark.class).active()) {
            System.out.println(beanName);
            System.out.println(bean.getClass().getName());
            setOfBenchmarkedMethods.add(m.getName());
        }/*from w w  w  .j a va 2 s .  c  om*/
    }
    if (setOfBenchmarkedMethods.isEmpty()) {
        return bean;
    }
    // Here comes the tricky staff. If bean is enchanced by spring, getInterfaces no more provides actual interfaces
    // however superclass is an actual class, from which we still can get actual interfaces. Union of arrays provides us with actual 
    // interfaces in both cases.
    Class<?>[] interfaces = (Class<?>[]) ArrayUtils.addAll(bean.getClass().getInterfaces(),
            bean.getClass().getSuperclass().getInterfaces());
    InvocationHandler handler = new BenchmarkInvocationHandler(bean, setOfBenchmarkedMethods);
    bean = Proxy.newProxyInstance(bean.getClass().getClassLoader(), interfaces, handler);
    return bean;
}

From source file:com.netflix.iep.config.Configuration.java

@SuppressWarnings("unchecked")
public static <T> T newProxy(final Class<T> ctype, final String prefix) {
    InvocationHandler handler = new InvocationHandler() {
        @Override// w w w  .  j a  va  2 s  .  c o  m
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("get")) {
                return iConfiguration.get((args[0] == null) ? null : args[0].toString());
            } else {
                Class rt = method.getReturnType();
                String key = (prefix == null) ? method.getName() : prefix + "." + method.getName();
                if (IConfiguration.class.isAssignableFrom(rt)) {
                    return newProxy(rt, key);
                } else {
                    String value = iConfiguration.get(key);
                    if (value == null) {
                        DefaultValue anno = method.getAnnotation(DefaultValue.class);
                        value = (anno == null) ? null : anno.value();
                    }
                    if (value == null) {
                        if (rt.isPrimitive())
                            throw new IllegalStateException("no value for property " + method.getName());
                        return null;
                    }
                    return Strings.cast(rt, value);
                }
            }
        }
    };
    return (T) Proxy.newProxyInstance(ctype.getClassLoader(), new Class[] { ctype }, handler);
}

From source file:org.apache.hadoop.hbase.thrift2.ThriftHBaseServiceHandler.java

public static THBaseService.Iface newInstance(Configuration conf, ThriftMetrics metrics) {
    THBaseService.Iface handler = new ThriftHBaseServiceHandler(conf);
    return (THBaseService.Iface) Proxy.newProxyInstance(handler.getClass().getClassLoader(),
            new Class[] { THBaseService.Iface.class }, new THBaseServiceMetricsProxy(handler, metrics));
}

From source file:org.onebusaway.webapp.impl.TypedMessagesFactory.java

protected Object createInstance() throws IOException {
    String name = _messagesClass.getName();
    name = "/" + name.replace('.', '/') + ".properties";
    InputStream is = _messagesClass.getResourceAsStream(name);
    if (is == null)
        throw new IllegalStateException("Unable to find resources: " + name);
    Properties p = new Properties();
    p.load(is);/*ww  w.  j a  va  2 s.  c o m*/

    for (Method method : _messagesClass.getDeclaredMethods()) {
        if (!p.containsKey(method.getName()))
            _log.warn("missing message name: messagesType=" + _messagesClass.getName() + " property="
                    + method.getName());
    }

    Handler handler = new Handler(p);
    return Proxy.newProxyInstance(_messagesClass.getClassLoader(), new Class[] { _messagesClass }, handler);
}