List of usage examples for java.lang.reflect Proxy newProxyInstance
private static Object newProxyInstance(Class<?> caller, Constructor<?> cons, InvocationHandler h)
From source file:Main.java
/** * Wraps an {@link ExecutorService} in such a way as to "protect" * it for calls to the {@link ExecutorService#shutdown()} or * {@link ExecutorService#shutdownNow()}. All other calls are delegated as-is * to the original service. <B>Note:</B> the exposed wrapped proxy will * answer correctly the {@link ExecutorService#isShutdown()} query if indeed * one of the {@code shutdown} methods was invoked. * * @param executorService The original service - ignored if {@code null} * @param shutdownOnExit If {@code true} then it is OK to shutdown the executor * so no wrapping takes place. * @return Either the original service or a wrapped one - depending on the * value of the <tt>shutdownOnExit</tt> parameter *///from w w w .ja v a2s . co m public static ExecutorService protectExecutorServiceShutdown(final ExecutorService executorService, boolean shutdownOnExit) { if (executorService == null || shutdownOnExit) { return executorService; } else { return (ExecutorService) Proxy.newProxyInstance(resolveDefaultClassLoader(executorService), new Class<?>[] { ExecutorService.class }, new InvocationHandler() { private final AtomicBoolean stopped = new AtomicBoolean(false); @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String name = method.getName(); if ("isShutdown".equals(name)) { return stopped.get(); } else if ("shutdown".equals(name)) { stopped.set(true); return null; // void... } else if ("shutdownNow".equals(name)) { stopped.set(true); return Collections.emptyList(); } else { return method.invoke(executorService, args); } } }); } }
From source file:ProxyAdapter.java
@SuppressWarnings("unchecked") public ProxyAdapter(Class... interfaces) { proxy = (T) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Method m;//from w w w. ja va 2 s . c om try { //Determine if the method has been defined in a subclass m = ProxyAdapter.this.getClass().getMethod(method.getName(), method.getParameterTypes()); m.setAccessible(true); } catch (Exception e) { //if not found throw new UnsupportedOperationException(method.toString(), e); } //Invoke the method found and return the result try { return m.invoke(ProxyAdapter.this, args); } catch (InvocationTargetException e) { throw e.getCause(); } } }); }
From source file:com.payu.ratel.client.inmemory.RatelServerProxyGenerator.java
@Override protected <T> T decorate(final T object, final Class<T> clazz) { return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { clazz }, new MonitoringInvocationHandler(object)); }
From source file:ch.sourcepond.maven.release.providers.BaseProvider.java
@SuppressWarnings("unchecked") @Override/* ww w . ja va2 s .com*/ public T get() { return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { getDelegateType() }, new InvocationHandler() { @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { notNull(getDelegate(), "Delegate object is not available!"); try { return method.invoke(getDelegate(), args); } catch (final InvocationTargetException e) { throw e.getTargetException(); } } }); }
From source file:org.force66.beantester.valuegens.InterfaceValueGenerator.java
@Override public Object[] makeValues() { return new Object[] { Proxy.newProxyInstance(interfaceType.getClassLoader(), new Class[] { interfaceType }, new GenericProxyHandler(interfaceType)) }; }
From source file:com.migratebird.datasource.SimpleDataSource.java
/** * Static factory that returns a data source providing access to the database using the driver with the given driver * class name, the given connection url, user name and password * * @param databaseInfo The database connection parameters, not null * @return a DataSource that gives access to the database *//*from ww w. j a va 2s . co m*/ public static DataSource createDataSource(DatabaseInfo databaseInfo) { String driverClassName = databaseInfo.getDriverClassName(); String url = databaseInfo.getUrl(); String userName = databaseInfo.getUserName(); String password = databaseInfo.getPassword(); logger.info("Creating data source. Driver: " + driverClassName + ", url: " + url + ", user: " + userName + ", password: <not shown>"); return (DataSource) Proxy.newProxyInstance(SimpleDataSource.class.getClassLoader(), new Class<?>[] { DataSource.class }, new SimpleDataSourceInvocationHandler(driverClassName, url, userName, password)); }
From source file:org.bpmscript.integration.internal.proxy.BpmScriptProxyFactoryBean.java
public Object getObject() throws Exception { return Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] { interfaceClass }, new BpmScriptProxy(bpmScriptFacade, definitionName, defaultTimeout, timeouts)); }
From source file:org.eclipse.ecr.core.api.TransactionalCoreSessionWrapper.java
public static CoreSession wrap(CoreSession session) { try {/* w ww. j a v a 2 s.co m*/ TransactionHelper.lookupTransactionManager(); } catch (NamingException e) { // no transactions, do not wrap return session; } ClassLoader cl = session.getClass().getClassLoader(); return (CoreSession) Proxy.newProxyInstance(cl, INTERFACES, new TransactionalCoreSessionWrapper(session)); }
From source file:org.resthub.rpc.AMQPHessianProxyFactoryBean.java
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); if (null == this.serviceInterface || !this.serviceInterface.isInterface()) { throw new IllegalArgumentException("Property 'serviceInterface' is required"); }/*from ww w . j ava 2s. c o m*/ AMQPHessianProxy handler = new AMQPHessianProxy(this); this.serviceProxy = Proxy.newProxyInstance(this.serviceInterface.getClassLoader(), new Class[] { this.serviceInterface }, handler); }
From source file:org.apache.jena.security.impl.CachedSecurityEvaluator.java
/** * Create an instance.//from w w w . j a v a2s . c o m * @param evaluator The security evaluator we are caching. * @param runAs The principal that we want to use when checking the permissions. * @return The proxied SecurityEvaluator. */ public static SecurityEvaluator getInstance(final SecurityEvaluator evaluator, final Principal runAs) { final Set<Class<?>> ifac = new LinkedHashSet<Class<?>>(); if (evaluator.getClass().isInterface()) { ifac.add(evaluator.getClass()); } ifac.addAll(ClassUtils.getAllInterfaces(evaluator.getClass())); return (SecurityEvaluator) Proxy.newProxyInstance(SecuredItemImpl.class.getClassLoader(), ifac.toArray(new Class<?>[ifac.size()]), new CachedSecurityEvaluator(evaluator, runAs)); }