List of usage examples for java.lang.reflect Proxy newProxyInstance
private static Object newProxyInstance(Class<?> caller, Constructor<?> cons, InvocationHandler h)
From source file:com.msopentech.odatajclient.proxy.api.impl.EntityContainerInvocationHandler.java
@Override @SuppressWarnings("unchecked") public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if (isSelfMethod(method, args)) { return invokeSelfMethod(method, args); } else if ("flush".equals(method.getName()) && ArrayUtils.isEmpty(args)) { new Container(client, factory).flush(); return ClassUtils.returnVoid(); } else {//from ww w .j a v a 2 s.c o m final Annotation[] methodAnnots = method.getAnnotations(); // 1. access top-level entity sets if (methodAnnots.length == 0) { final Class<?> returnType = method.getReturnType(); return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { returnType }, EntitySetInvocationHandler.getInstance(returnType, this)); } // 2. invoke function imports else if (methodAnnots[0] instanceof Operation) { final com.msopentech.odatajclient.engine.metadata.edm.v3.EntityContainer container = getFactory() .getMetadata().getSchema(schemaName).getEntityContainer(entityContainerName); final com.msopentech.odatajclient.engine.metadata.edm.v3.FunctionImport funcImp = container .getFunctionImport(((Operation) methodAnnots[0]).name()); final URIBuilder uriBuilder = client.getURIBuilder(factory.getServiceRoot()) .appendFunctionImportSegment(URIUtils.rootFunctionImportURISegment(container, funcImp)); return functionImport((Operation) methodAnnots[0], method, args, uriBuilder.build(), funcImp); } else { throw new UnsupportedOperationException("Method not found: " + method); } } }
From source file:co.paralleluniverse.galaxy.core.BackupImpl.java
static BackupMonitor createMonitor(MonitoringType monitoringType, String name) { if (monitoringType == null) return (BackupMonitor) Proxy.newProxyInstance(Cache.class.getClassLoader(), new Class<?>[] { BackupMonitor.class }, DegenerateInvocationHandler.INSTANCE); else//w w w . ja va2 s .co m switch (monitoringType) { case JMX: return new JMXBackupMonitor(name); case METRICS: return new MetricsBackupMonitor(); } throw new IllegalArgumentException("Unknown MonitoringType " + monitoringType); }
From source file:com.futureplatforms.kirin.internal.attic.ProxyGenerator.java
public <T> T javascriptProxyForRequest(final JSONObject obj, Class<T> baseInterface, Class<?>... otherClasses) { Class<?>[] allClasses;/* w ww .j a v a2s .com*/ if (otherClasses.length == 0) { allClasses = new Class[] { baseInterface }; } else { allClasses = new Class[otherClasses.length + 1]; allClasses[0] = baseInterface; System.arraycopy(otherClasses, 0, allClasses, 1, otherClasses.length); } InvocationHandler h = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); Class<?> returnType = method.getReturnType(); if (obj.has(methodName)) { String id = obj.optString("__id"); if (id != null) { // so assume it's a callback if (void.class.equals(returnType)) { mKirinHelper.jsCallbackObjectMethod(id, methodName, (Object[]) args); } else { return mKirinHelper.jsSyncCallbackObjectMethod(id, returnType, methodName, (Object[]) args); } } return null; } String propertyName = findGetter(methodName); if (propertyName != null) { return handleGetter(obj, returnType, propertyName); } if ("toString".equals(methodName) && String.class.equals(returnType)) { return obj.toString(); } return null; } }; Object proxy = Proxy.newProxyInstance(baseInterface.getClassLoader(), allClasses, h); return baseInterface.cast(proxy); }
From source file:MethodCountingHandler.java
/** * Gets proxy to depending upon debug status in Log4J. * /*from www . j a va 2 s .com*/ * @return The proxy to the implementation. */ public static final SomeClass getDynamicSomeClassProxy() { SomeClassImpl impl = new SomeClassImpl(System.getProperty("user.name")); InvocationHandler handler = new MethodCountingHandler(impl); Class[] interfaces = new Class[] { SomeClass.class }; ClassLoader loader = SomeClassFactory.class.getClassLoader(); SomeClass proxy = (SomeClass) Proxy.newProxyInstance(loader, interfaces, handler); return proxy; }
From source file:org.dimitrovchi.conf.service.ServiceParameterUtils.java
public static <P> P parameters(final String prefix, final Environment environment) { final AnnotationParameters aParameters = annotationParameters(); return (P) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), aParameters.annotations.toArray(new Class[aParameters.annotations.size()]), new InvocationHandler() { @Override//from w ww .j a v a 2 s . c o m public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if ("toString".equals(method.getName())) { return reflectToString(prefix, proxy); } return environment.getProperty(prefix + "." + method.getName(), (Class) method.getReturnType(), method.getDefaultValue()); } }); }
From source file:com.netflix.bluespar.amazon.security.AmazonClientProvider.java
public AmazonAutoScaling getAutoScaling(AmazonCredentials amazonCredentials, String region) { AmazonAutoScalingClient client = new AmazonAutoScalingClient(amazonCredentials.getCredentials()); if (edda == null || edda.length() == 0) { return client; } else {/* w w w . j av a 2s. c om*/ return (AmazonAutoScaling) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { AmazonAutoScaling.class }, getInvocationHandler(client, region, amazonCredentials)); } }
From source file:org.apache.openjpa.azure.AzureDistributedBrokerFactory.java
protected void synchronizeMappings(final ClassLoader loader, final Slice slice, final String action, final String props, final Collection<Class<?>> classes) { final JDBCConfiguration conf = (JDBCConfiguration) Proxy.newProxyInstance( AzureSliceConfiguration.class.getClassLoader(), new Class<?>[] { AzureSliceConfiguration.class }, new JDBCConfInterceptor( new AzureSliceConfigurationImpl(slice, (AzureConfiguration) getConfiguration()))); final MappingTool tool = new AzureMappingTool(conf, action, false); Configurations.configureInstance(tool, conf, props, "SynchronizeMappings"); // initialize the schema for (Class<?> cls : classes) { try {//from w w w . j a v a2 s . com tool.run(cls); } catch (IllegalArgumentException iae) { throw new UserException(_loc.get("bad-synch-mappings", action, Arrays.asList(MappingTool.ACTIONS)), iae); } } tool.record(); }
From source file:org.red5.server.script.rhino.RhinoScriptUtils.java
/** * Create a new Rhino-scripted object from the given script source. * // w w w . j a va2s. c om * @param scriptSource * the script source text * @param interfaces * the interfaces that the scripted Java object is supposed to * implement * @param extendedClass * @return the scripted Java object * @throws ScriptCompilationException * in case of Rhino parsing failure * @throws java.io.IOException */ @SuppressWarnings("rawtypes") public static Object createRhinoObject(String scriptSource, Class[] interfaces, Class extendedClass) throws ScriptCompilationException, IOException, Exception { if (log.isDebugEnabled()) { log.debug("Script Engine Manager: " + mgr.getClass().getName()); } ScriptEngine engine = mgr.getEngineByExtension("js"); if (null == engine) { log.warn("Javascript is not supported in this build"); } // set engine scope namespace Bindings nameSpace = engine.getBindings(ScriptContext.ENGINE_SCOPE); // add the logger to the script nameSpace.put("log", log); // compile the wrapper script CompiledScript wrapper = ((Compilable) engine).compile(jsWrapper); nameSpace.put("Wrapper", wrapper); // get the function name ie. class name / ctor String funcName = RhinoScriptUtils.getFunctionName(scriptSource); if (log.isDebugEnabled()) { log.debug("New script: " + funcName); } // set the 'filename' nameSpace.put(ScriptEngine.FILENAME, funcName); if (null != interfaces) { nameSpace.put("interfaces", interfaces); } if (null != extendedClass) { if (log.isDebugEnabled()) { log.debug("Extended: " + extendedClass.getName()); } nameSpace.put("supa", extendedClass.newInstance()); } // // compile the script CompiledScript script = ((Compilable) engine).compile(scriptSource); // eval the script with the associated namespace Object o = null; try { o = script.eval(); } catch (Exception e) { log.error("Problem evaluating script", e); } if (log.isDebugEnabled()) { log.debug("Result of script call: " + o); } // script didnt return anything we can use so try the wrapper if (null == o) { wrapper.eval(); } else { wrapper.eval(); o = ((Invocable) engine).invokeFunction("Wrapper", new Object[] { engine.get(funcName) }); if (log.isDebugEnabled()) { log.debug("Result of invokeFunction: " + o); } } return Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), interfaces, new RhinoObjectInvocationHandler(engine, o)); }
From source file:org.apache.olingo.ext.proxy.commons.EntityContainerInvocationHandler.java
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { if (isSelfMethod(method)) { return invokeSelfMethod(method, args); } else if ("flush".equals(method.getName()) && ArrayUtils.isEmpty(args)) { service.getPersistenceManager().flush(); return ClassUtils.returnVoid(); } else if ("flushAsync".equals(method.getName()) && ArrayUtils.isEmpty(args)) { return service.getPersistenceManager().flushAsync(); } else if ("operations".equals(method.getName()) && ArrayUtils.isEmpty(args)) { final Class<?> returnType = method.getReturnType(); return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { returnType }, OperationInvocationHandler.getInstance(this)); } else {/*w w w . j av a 2 s . c o m*/ final Class<?> returnType = method.getReturnType(); if (returnType.isAnnotationPresent(EntitySet.class)) { return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { returnType }, EntitySetInvocationHandler.getInstance(returnType, service)); } else if (returnType .isAnnotationPresent(org.apache.olingo.ext.proxy.api.annotations.EntityType.class)) { return getSingleton(method); } throw new NoSuchMethodException(method.getName()); } }
From source file:cat.albirar.framework.dynabean.impl.DefaultDynaBeanFactory.java
/** * Proxyfy the dynaBean instace./* ww w. j a v a 2 s. c om*/ * * @param typeToImplement The type to implement * @param dynaBean The dynabean * @return */ @SuppressWarnings("unchecked") private <T> T createDynaBean(DynaBeanImpl<T> dynaBean) { return (T) Proxy.newProxyInstance(dynaBean.getClass().getClassLoader(), new Class[] { dynaBean.getImplementedType() }, dynaBean); }