public static Object newProxyInstance(ClassLoader loader, Class>[] interfaces, InvocationHandler h) throws IllegalArgumentException {
//handler 对象不能是空
Objects.requireNonNull(h);
//被代理对象实现的所有接口
final Class>[] intfs = interfaces.clone();
//安全控制,可以不用管
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
/*
* Look up or generate the designated proxy class.
*/
//生成指定的代理对象的类,这个才是重点方法
Class> cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
final Constructor> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
//如果代理对象的类或者修饰符不是 public 的, 执行 PrivilegedAction.run 方法。这里设置当前构造函数为访问权限
if (!Modifier.isPublic(cl.getModifiers())) {
//启用特权,执行指定的 PrivilegedAction。
AccessController.doPrivileged(new PrivilegedAction() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
//使用 InvocationHandler 作为参数调用构造方法来获得代理类的实例
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
getProxyClass0() 源码:
private static Class> getProxyClass0(ClassLoader loader, Class>... interfaces) {
//被代理对象接口数量不能大于 65535
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
//class loader 实现的接口代理类定义已经存在,那么直接返回;否则通过 ProxyClassFactory 生成proxy class。(可以自己翻译)
return proxyClassCache.get(loader, interfaces);
}
创建proxyClassCache, 是直接new 的,指定 KeyFactory 和指定 ProxyClassFactory
//两个参数要说一下:分别对应 Key工厂和代理类工厂,看名字也知道生成key 和 生成代理类class对象的
private static final WeakCache[], Class>> proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());
注:所以下面的 WeakCache 泛型对应:[], Class>>
proxyClassCache.get() 源码
final class WeakCache {
//
private final ReferenceQueue refQueue = new ReferenceQueue<>();
//Map<类加载器,Map<接口数组对象key,代理类工厂Factory或代理类包装对象LookupValue>, 类加载器,已经加载的类class
//可以把 Supplier 看成生成类的class,调用get 方法就返回class
private final ConcurrentMap
private final class Factory implements Supplier {
//class loader
private final K key;
//Class[]
private final P parameter;
//根据key 生成的 subkey,是 valuesMap的key
private final Object subKey;
private final ConcurrentMap> valuesMap;
//省略构造函数
@Override
public synchronized V get() { // serialize access
// re-check
Supplier supplier = valuesMap.get(subKey);
if (supplier != this) {
// something changed while we were waiting:
// might be that we were replaced by a CacheValue
// or were removed because of failure ->
// return null to signal WeakCache.get() to retry
// the loop
//返回nul 是为了在外层(WeakCache.get()方法) 循环调用,可能会因为并发造成 supplier != this(这个Factory 是new 的)
return null;
}
// else still us (supplier == this)
// create new value
V value = null;
try {
//这里才调用 valueFactory 生成动态代理类class
//在这里又回到了 创建proxyClassCache的地方,创建proxyClassCache时指定的 ProxyClassFactory,前面做的工作都是缓存操作等。
value = Objects.requireNonNull(valueFactory.apply(key, parameter));
} finally {
if (value == null) { // remove us on failure
valuesMap.remove(subKey, this);
}
}
// the only path to reach here is with non-null value
assert value != null;
// wrap value with CacheValue (WeakReference)
CacheValue cacheValue = new CacheValue<>(value);
// put into reverseMap
reverseMap.put(cacheValue, Boolean.TRUE);
// try replacing us with CacheValue (this should always succeed)
//缓存value 值,这里缓存的就是 CacheValue
if (!valuesMap.replace(subKey, this, cacheValue)) {
throw new AssertionError("Should not reach here");
}
// successfully replaced us with new CacheValue -> return the value
// wrapped by it
return value;
}
}
ProxyClassFactory.apply() 方法是如何生成动态代理类的class
//ProxyClassFactory类是 Proxy 内部类
private static final class ProxyClassFactory implements BiFunction[], Class>>{
// prefix for all proxy class names
//动态代理类类名前缀
private static final String proxyClassNamePrefix = "$Proxy";
// next number to use for generation of unique proxy class names
//动态代理类类名前缀数字
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
public Class> apply(ClassLoader loader, Class>[] interfaces) {
Map, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
for (Class> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
Class> interfaceClass = null;
try {
//加载intf的 class 对象
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
//接口的class 不是同一个class loader 加载的
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
*/
//加载intf的 不是一个接口
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
*/
//有重复的 interfaceClass
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
//动态代理类的包名
String proxyPkg = null; // package to define proxy class in
//动态代理类的签名
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
//生成动态代理类的包名和签名
for (Class> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf('.');
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
//生成动态代理类的包名是空的,使用默认的包名
if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
/*
* Choose a name for the proxy class to generate.
*/
//生成动态代理类的类名,如:XX$Proxy1 XX$Proxy2
long num = nextUniqueNumber.getAndIncrement();
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/*
* Generate the specified proxy class.
*/
//生成动态代理类的字节数组
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(proxyName, interfaces, accessFlags);
try {
//根据类字节数组,加载类
return defineClass0(loader, proxyName, proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
/*
* A ClassFormatError here means that (barring bugs in the
* proxy class generation code) there was some other
* invalid aspect of the arguments supplied to the proxy
* class creation (such as virtual machine limitations
* exceeded).
*/
throw new IllegalArgumentException(e.toString());
}
}
}
什么是Hessian
The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary p
在Spark Shell上,通过创建HiveContext可以直接进行Hive操作
1. 操作Hive中已存在的表
[hadoop@hadoop bin]$ ./spark-shell
Spark assembly has been built with Hive, including Datanucleus jars on classpath
Welcom
JMS Message Delivery Reliability and Acknowledgement Patterns
http://wso2.com/library/articles/2013/01/jms-message-delivery-reliability-acknowledgement-patterns/
Transaction and redelivery in
转载请出自出处:http://eksliang.iteye.com/blog/2177567 一、游标
数据库使用游标返回find的执行结果。客户端对游标的实现通常能够对最终结果进行有效控制,从shell中定义一个游标非常简单,就是将查询结果分配给一个变量(用var声明的变量就是局部变量),便创建了一个游标,如下所示:
> var