/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.common.serialize.support.dubbo;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import com.alibaba.dubbo.common.bytecode.ClassGenerator;
import com.alibaba.dubbo.common.io.UnsafeByteArrayInputStream;
import com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.serialize.support.java.CompactedObjectInputStream;
import com.alibaba.dubbo.common.serialize.support.java.CompactedObjectOutputStream;
import com.alibaba.dubbo.common.utils.ClassHelper;
import com.alibaba.dubbo.common.utils.IOUtils;
import com.alibaba.dubbo.common.utils.ReflectUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
/**
* Builder.
*
* @author qian.lei
*
* @param <T> type.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public abstract class Builder<T> implements GenericDataFlags
{
// Must be protected. by qian.lei
protected static Logger logger = LoggerFactory.getLogger(Builder.class);
private static final AtomicLong BUILDER_CLASS_COUNTER = new AtomicLong(0);
private static final String BUILDER_CLASS_NAME = Builder.class.getName();
private static final Map<Class<?>, Builder<?>> BuilderMap = new ConcurrentHashMap<Class<?>, Builder<?>>();
private static final Map<Class<?>, Builder<?>> nonSerializableBuilderMap = new ConcurrentHashMap<Class<?>, Builder<?>>();
private static final String FIELD_CONFIG_SUFFIX = ".fc";
private static final int MAX_FIELD_CONFIG_FILE_SIZE = 16 * 1024;
private static final Comparator<String> FNC = new Comparator<String>(){
public int compare(String n1, String n2){ return compareFieldName(n1, n2); }
};
private static final Comparator<Field> FC = new Comparator<Field>(){
public int compare(Field f1, Field f2){ return compareFieldName(f1.getName(), f2.getName()); }
};
private static final Comparator<Constructor> CC = new Comparator<Constructor>(){
public int compare(Constructor o1, Constructor o2){ return o1.getParameterTypes().length - o2.getParameterTypes().length; }
};
// class-descriptor mapper
private static final List<String> mDescList = new ArrayList<String>();
private static final Map<String, Integer> mDescMap = new ConcurrentHashMap<String, Integer>();
public static ClassDescriptorMapper DEFAULT_CLASS_DESCRIPTOR_MAPPER = new ClassDescriptorMapper(){
public String getDescriptor(int index)
{
if( index < 0 || index >= mDescList.size() )
return null;
return mDescList.get(index);
}
public int getDescriptorIndex(String desc)
{
Integer ret = mDescMap.get(desc);
return ret == null ? -1 : ret.intValue();
}
};
protected Builder(){}
abstract public Class<T> getType();
public void writeTo(T obj, OutputStream os) throws IOException
{
GenericObjectOutput out = new GenericObjectOutput(os);
writeTo(obj, out);
out.flushBuffer();
}
public T parseFrom(byte[] b) throws IOException
{
return parseFrom(new UnsafeByteArrayInputStream(b));
}
public T parseFrom(InputStream is) throws IOException
{
return parseFrom(new GenericObjectInput(is));
}
abstract public void writeTo(T obj, GenericObjectOutput out) throws IOException;
abstract public T parseFrom(GenericObjectInput in) throws IOException;
public static <T> Builder<T> register(Class<T> c, boolean isAllowNonSerializable)
{
if( c == Object.class || c.isInterface() )
return (Builder<T>)GenericBuilder;
if( c == Object[].class )
return (Builder<T>)GenericArrayBuilder;
Builder<T> b = (Builder<T>)BuilderMap.get(c);
if(null != b) return b;
boolean isSerializable = Serializable.class.isAssignableFrom(c);
if(!isAllowNonSerializable && !isSerializable) {
throw new IllegalStateException("Serialized class " + c.getName() +
" must implement java.io.Serializable (dubbo codec setting: isAllowNonSerializable = false)");
}
b = (Builder<T>)nonSerializableBuilderMap.get(c);
if(null != b) return b;
b = newBuilder(c);
if(isSerializable)
BuilderMap.put(c, b);
else
nonSerializableBuilderMap.put(c, b);
return b;
}
public static <T> Builder<T> register(Class<T> c)
{
return register(c, false);
}
public static <T> void register(Class<T> c, Builder<T> b)
{
if(Serializable.class.isAssignableFrom(c))
BuilderMap.put(c, b);
else
nonSerializableBuilderMap.put(c, b);
}
private static <T> Builder<T> newBuilder(Class<T> c)
{
if( c.isPrimitive() )
throw new RuntimeException("Can not create builder for primitive type: " + c);
if( logger.isInfoEnabled() )
logger.info("create Builder for class: " + c);
Builder<?> builder;
if( c.isArray() )
builder = newArrayBuilder(c);
else
builder = newObjectBuilder(c);
return (Builder<T>)builder;
}
private static Builder<?> newArrayBuilder(Class<?> c)
{
Class<?> cc = c.getComponentType();
if( cc.isInterface() )
return GenericArrayBuilder;
ClassLoader cl = ClassHelper.getCallerClassLoader(Builder.class);
String cn = ReflectUtils.getName(c), ccn = ReflectUtils.getName(cc); // get class name as int[][], double[].
String bcn = BUILDER_CLASS_NAME + "$bc" + BUILDER_CLASS_COUNTER.getAndIncrement();
int ix = cn.indexOf(']');
String s1 = cn.substring(0, ix), s2 = cn.substring(ix); // if name='int[][]' then s1='int[', s2='][]'
StringBuilder cwt = new StringBuilder("public void writeTo(Object obj, ").append(GenericObjectOutput.class.getName()).append(" out) throws java.io.IOException{"); // writeTo code.
StringBuilder cpf = new StringBuilder("public Object parseFrom(").append(GenericObjectInput.class.getName()).append(" in) throws java.io.IOException{"); // parseFrom code.
cwt.append("if( $1 == null ){ $2.write0(OBJECT_NULL); return; }");
cwt.append(cn).append(" v = (").append(cn).append(")$1; int len = v.length; $2.write0(OBJECT_VALUES); $2.writeUInt(len); for(int i=0;i<len;i++){ ");
cpf.append("byte b = $1.read0(); if( b == OBJECT_NULL ) return null; if( b != OBJECT_VALUES ) throw new java.io.IOException(\"Input format error, expect OBJECT_NULL|OBJECT_VALUES, get \" + b + \".\");");
cpf.append("int len = $1.readUInt(); if( len == 0 ) return new ").append(s1).append('0').append(s2).append("; ");
cpf.append(cn).append(" ret = new ").append(s1).append("len").append(s2).append("; for(int i=0;i<len;i++){ ");
Builder<?> builder = null;
if( cc.isPrimitive()
没有合适的资源?快使用搜索试试~ 我知道了~
Dubbox框架搭建的Demo
共803个文件
class:426个
java:309个
jar:10个
4星 · 超过85%的资源 需积分: 10 351 下载量 51 浏览量
2015-02-13
17:29:09
上传
评论 6
收藏 6.31MB RAR 举报
温馨提示
Dubbox框架搭建的Demo,本资源为Maven构建项目,本地测试通过。
资源推荐
资源详情
资源评论
收起资源包目录
Dubbox框架搭建的Demo (803个子文件)
com.alibaba.dubbo.cache.CacheFactory 197B
com.alibaba.dubbo.cache.CacheFactory 197B
URL.class 33KB
Builder.class 32KB
ExtensionLoader.class 30KB
ServiceConfig.class 25KB
ReflectUtils.class 24KB
RegistryDirectory.class 23KB
ReferenceConfig.class 20KB
AbstractRegistry.class 19KB
Yylex.class 18KB
JavaBeanSerializeUtil.class 18KB
PojoUtils.class 18KB
AbstractConfig.class 17KB
DubboProtocol.class 17KB
DubboBeanDefinitionParser.class 17KB
AbstractInterfaceConfig.class 16KB
FailbackRegistry.class 15KB
Bytes.class 15KB
Wrapper.class 15KB
RegistryProtocol.class 14KB
ClassGenerator.class 14KB
UrlUtils.class 14KB
AnnotationBean.class 13KB
RpcContext.class 13KB
ClassUtils.class 13KB
CallbackServiceCodec.class 13KB
Constants.class 13KB
JSON.class 12KB
StringUtils.class 11KB
RestProtocol.class 11KB
J2oVisitor.class 11KB
ServiceBean.class 10KB
Proxy.class 10KB
ProtocolConfig.class 10KB
GenericJSONConverter.class 9KB
ConfigUtils.class 9KB
DubboCodec.class 9KB
FutureFilter.class 9KB
NetUtils.class 9KB
GenericDataInput.class 8KB
Parameters.class 8KB
RpcInvocation.class 8KB
Mixin.class 8KB
InvokeTelnetHandler.class 7KB
CollectionUtils.class 7KB
ProviderConfig.class 7KB
ReferenceBean.class 7KB
CountTelnetHandler.class 7KB
LazyConnectExchangeClient.class 7KB
JdkCompiler$JavaFileManagerImpl.class 7KB
AbstractInvoker.class 6KB
DubboInvoker.class 6KB
TraceFilter.class 6KB
DecodeableRpcInvocation.class 6KB
RegistryConfig.class 6KB
GenericDataOutput.class 6KB
DubboProtocol$1.class 6KB
JavassistCompiler.class 6KB
JSONWriter.class 6KB
ReferenceCountExchangeClient.class 6KB
GenericObjectInput.class 6KB
JavaBeanDescriptor.class 6KB
AbstractProxyProtocol.class 6KB
JdkCompiler.class 6KB
RpcStatus.class 5KB
ClassHelper.class 5KB
LoggingFilter.class 5KB
AbstractServiceConfig.class 5KB
CompatibleTypeUtils.class 5KB
DecodeableRpcResult.class 5KB
Version.class 5KB
JacksonObjectInput.class 5KB
JSONObject.class 5KB
RegistryProtocol$OverrideListener.class 5KB
ApplicationConfig.class 5KB
MethodConfig.class 5KB
KryoFactory.class 5KB
LoggerFactory.class 5KB
JsonObjectInput.class 5KB
JdkLoggerAdapter.class 5KB
MemcachedProtocol$1.class 4KB
JettyHttpServer.class 4KB
IOUtils.class 4KB
RpcContextFilter.class 4KB
Main.class 4KB
Main.class 4KB
ReferenceConfigCache.class 4KB
LogTelnetHandler.class 4KB
ListTelnetHandler.class 4KB
JacksonObjectOutput.class 4KB
JSONArray.class 4KB
TraceTelnetHandler.class 4KB
FailsafeLogger.class 4KB
ClientsPageHandler.class 4KB
ChannelWrappedInvoker.class 4KB
FastJsonObjectInput.class 4KB
PortTelnetHandler.class 4KB
MemcachedProtocol.class 4KB
AtomicPositiveInteger.class 4KB
共 803 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
yedaoling
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页