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 30, 2012
2 parents ba9a64b + 5d39df0 commit edf5523
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,14 @@ private boolean isActive(Activate activate, URL url) {
}
return false;
}


/**
* 返回扩展点实例,如果没有指定的扩展点或是还没加载(即实例化)则返回<code>null</code>。注意:此方法不会触发扩展点的加载。
* <p />
* 一般应该调用{@link #getExtension(String)}方法获得扩展,这个方法会触发扩展点加载。
*
* @see #getExtension(String)
*/
@SuppressWarnings("unchecked")
public T getLoadedExtension(String name) {
if (name == null || name.length() == 0)
Expand All @@ -272,6 +279,17 @@ public T getLoadedExtension(String name) {
return (T) holder.get();
}

/**
* 返回已经加载的扩展点的名字。
* <p />
* 一般应该调用{@link #getSupportedExtensions()}方法获得扩展,这个方法会返回所有的扩展点。
*
* @see #getSupportedExtensions()
*/
public Set<String> getLoadedExtensions() {
return Collections.unmodifiableSet(new TreeSet<String>(cachedInstances.keySet()));
}

/**
* 返回指定名字的扩展。如果指定名字的扩展不存在,则抛异常 {@link IllegalStateException}.
*
Expand Down Expand Up @@ -329,10 +347,6 @@ public Set<String> getSupportedExtensions() {
Map<String, Class<?>> clazzes = getExtensionClasses();
return Collections.unmodifiableSet(new TreeSet<String>(clazzes.keySet()));
}

public Set<String> getLoadedExtensions() {
return Collections.unmodifiableSet(new TreeSet<String>(cachedInstances.keySet()));
}

/**
* 返回缺省的扩展点名,如果没有设置缺省则返回<code>null</code>。
Expand All @@ -341,7 +355,23 @@ public String getDefaultExtensionName() {
getExtensionClasses();
return cachedDefaultName;
}


public void addExtension(String name, Class<?> clazz) {
if(cachedNames.containsKey(name)) {
throw new IllegalStateException("Extension name " +
name + " already existed(Extension " + type + ")!");
}
if(!type.isAssignableFrom(clazz)) {
throw new IllegalStateException("Input type " +
clazz + "not implement Extension " + type);
}
if(clazz.isInterface()) {
throw new IllegalStateException("Input type " +
clazz + "can not be interface!");
}
cachedNames.put(clazz, name);
cachedClasses.get().put(name, clazz);
}

@SuppressWarnings("unchecked")
public T getAdaptiveExtension() {
Expand All @@ -365,7 +395,7 @@ public T getAdaptiveExtension() {
throw new IllegalStateException("fail to create adaptive instance: " + createAdaptiveInstanceError.toString(), createAdaptiveInstanceError);
}
}

return (T) instance;
}

Expand All @@ -375,9 +405,15 @@ private IllegalStateException findException(String name) {
return entry.getValue();
}
}
StringBuilder buf = new StringBuilder("No such extension " + type.getName() + " by name " + name + ", possible causes: ");
StringBuilder buf = new StringBuilder("No such extension " + type.getName() + " by name " + name);


int i = 1;
for (Map.Entry<String, IllegalStateException> entry : exceptions.entrySet()) {
if(i == 1) {
buf.append(", possible causes: ");
}

buf.append("\r\n(");
buf.append(i ++);
buf.append(") ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public AbortPolicyWithReport(String threadName, URL url) {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
String msg = String.format("Thread pool is EXHAUSTED!" +
" Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d), Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s!" ,
" Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," +
" Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!" ,
threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(), url.toIdentityString());
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
url.getProtocol(), url.getIp(), url.getPort());
logger.warn(msg);
throw new RejectedExecutionException(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.alibaba.dubbo.common.extensionloader.activate.impl.OrderActivateExtImpl1;
import com.alibaba.dubbo.common.extensionloader.activate.impl.OrderActivateExtImpl2;
import com.alibaba.dubbo.common.extensionloader.activate.impl.ValueActivateExtImpl;
import com.alibaba.dubbo.common.extensionloader.ext1.impl.Ext1Impl_ManualAdd;
import junit.framework.Assert;

import org.junit.Test;
Expand Down Expand Up @@ -125,7 +126,7 @@ public void test_getExtension_ExceptionNoExtension() throws Exception {
}

@Test
public void test_getExtension_ExceptionNoExtension_NameOnWrapperNoAffact() throws Exception {
public void test_getExtension_ExceptionNoExtension_WrapperNotAffactName() throws Exception {
try {
ExtensionLoader.getExtensionLoader(Ext5NoAdaptiveMethod.class).getExtension("XXX");
fail();
Expand Down Expand Up @@ -183,7 +184,24 @@ public void test_getSupportedExtensions_NoExtension() throws Exception {

}
}


@Test
public void test_AddExtension() throws Exception {
try {
ExtensionLoader.getExtensionLoader(Ext1.class).getExtension("Manual");
fail();
}
catch (IllegalStateException expected) {
assertThat(expected.getMessage(), containsString("No such extension com.alibaba.dubbo.common.extensionloader.ext1.Ext1 by name Manual"));
}

ExtensionLoader.getExtensionLoader(Ext1.class).addExtension("Manual", Ext1Impl_ManualAdd.class);
Ext1 ext = ExtensionLoader.getExtensionLoader(Ext1.class).getExtension("Manual");

assertThat(ext, instanceOf(Ext1Impl_ManualAdd.class));
assertEquals("Manual", ExtensionLoader.getExtensionLoader(Ext1.class).getExtensionName(Ext1Impl_ManualAdd.class));
}

@Test
public void test_getAdaptiveExtension_defaultExtension() throws Exception {
Ext1 ext = ExtensionLoader.getExtensionLoader(Ext1.class).getAdaptiveExtension();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.extensionloader.ext1.impl;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extensionloader.ext1.Ext1;

/**
* @author ding.lid
*
*/
public class Ext1Impl_ManualAdd implements Ext1 {
public String echo(URL url, String s) {
return "Ext1Impl3-echo";
}

public String yell(URL url, String s) {
return "Ext1Impl3-yell";
}

public String bang(URL url, int i) {
return "bang3";
}

}

0 comments on commit edf5523

Please sign in to comment.