Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/alibaba/dubbo
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfei0201 committed Jul 28, 2012
2 parents c2c3441 + 9c1b508 commit d9562c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@
*
* @author william.liangf
* @author ding.lid
*
* @see Adaptive
* @see SPI
*
* @see com.alibaba.dubbo.common.extension.SPI
* @see com.alibaba.dubbo.common.extension.Adaptive
* @see com.alibaba.dubbo.common.extension.Activate
*/
public class ExtensionLoader<T> {

private static final Logger logger = LoggerFactory.getLogger(ExtensionLoader.class);

private static final String SERVICES_DIRECTORY = "META-INF/services/";
private static final String SERVICES_DIRECTORY = "META-INF/services/";

private static final String DUBBO_DIRECTORY = "META-INF/dubbo/";

Expand All @@ -74,28 +75,30 @@ public class ExtensionLoader<T> {

private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<Class<?>, Object>();

// ==============================

private final Class<?> type;

private final ExtensionFactory objectFactory;

private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<Class<?>, String>();

private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<Map<String,Class<?>>>();

private final Map<String, Activate> cachedActivates = new ConcurrentHashMap<String, Activate>();

private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<String, Holder<Object>>();


private volatile Class<?> cachedAdaptiveClass = null;

private final Holder<Object> cachedAdaptiveInstance = new Holder<Object>();
private volatile Throwable createAdaptiveInstanceError;

private Set<Class<?>> cachedWrapperClasses;


private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<String, Holder<Object>>();

private String cachedDefaultName;

private final Holder<Object> cachedAdaptiveInstance = new Holder<Object>();
private volatile Throwable createAdaptiveInstanceError;

private Set<Class<?>> cachedWrapperClasses;

private Map<String, IllegalStateException> exceptions = new ConcurrentHashMap<String, IllegalStateException>();

private final ExtensionFactory objectFactory;

private static <T> boolean withExtensionAnnotation(Class<T> type) {
return type.isAnnotationPresent(SPI.class);
Expand All @@ -107,7 +110,7 @@ public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
throw new IllegalArgumentException("Extension type == null");
if(!withExtensionAnnotation(type)) {
throw new IllegalArgumentException("Extension type(" + type +
") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
}

ExtensionLoader<T> loader = (ExtensionLoader<T>) EXTENSION_LOADERS.get(type);
Expand Down Expand Up @@ -178,7 +181,7 @@ public List<T> getActivateExtension(URL url, String key, String group) {
/**
* Get activate extensions.
*
* @see Activate
* @see com.alibaba.dubbo.common.extension.Activate
* @param url url
* @param values extension point names
* @param group group
Expand Down Expand Up @@ -268,7 +271,13 @@ public T getLoadedExtension(String name) {
}
return (T) holder.get();
}


/**
* 返回指定名字的扩展。如果指定名字的扩展不存在,则抛异常 {@link IllegalStateException}.
*
* @param name
* @return
*/
@SuppressWarnings("unchecked")
public T getExtension(String name) {
if (name == null || name.length() == 0)
Expand Down Expand Up @@ -347,26 +356,19 @@ public T getAdaptiveExtension() {
cachedAdaptiveInstance.set(instance);
} catch (Throwable t) {
createAdaptiveInstanceError = t;
rethrowAsRuntime(t, "fail to create adaptive instance: ");
throw new IllegalStateException("fail to create adaptive instance: " + t.toString(), t);
}
}
}
}
else {
rethrowAsRuntime(createAdaptiveInstanceError, "fail to create adaptive instance: ");
throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
}
}

return (T) instance;
}

private static void rethrowAsRuntime(Throwable t, String message) {
if(t instanceof RuntimeException)
throw (RuntimeException)t;
else
throw new IllegalStateException(message + t.toString(), t);
}

private IllegalStateException findException(String name) {
for (Map.Entry<String, IllegalStateException> entry : exceptions.entrySet()) {
if (entry.getKey().toLowerCase().contains(name.toLowerCase())) {
Expand Down Expand Up @@ -463,7 +465,8 @@ private Map<String, Class<?>> getExtensionClasses() {
}
return classes;
}


// 此方法已经getExtensionClasses方法同步过。
private Map<String, Class<?>> loadExtensionClasses() {
final SPI defaultAnnotation = type.getAnnotation(SPI.class);
if(defaultAnnotation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
public class Holder<T> {

private T value;
private volatile T value;

public void set(T value) {
this.value = value;
Expand Down

0 comments on commit d9562c6

Please sign in to comment.