Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Catch exception which throws on the listener notifying when loading extension success. #229

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,16 @@ private ExtensionClass<T> buildClass(Extension extension, Class<? extends T> imp
}

private void loadSuccess(String alias, ExtensionClass<T> extensionClass) {
all.put(alias, extensionClass);
if (listener != null) {
listener.onLoad(extensionClass); // 加载完毕,通知监听器
try {
listener.onLoad(extensionClass); // 加载完毕,通知监听器
all.put(alias, extensionClass);
} catch (Exception e) {
LOGGER.error("Error when load extension of extensible " + interfaceClass + " with alias: "
+ alias + ".", e);
}
} else {
all.put(alias, extensionClass);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ public void testLoadFromFile() throws Exception {
Assert.assertNotNull(extensionClass);
Assert.assertTrue(extensionClass.getOrder() == 123);

loader = new ExtensionLoader<Filter>(Filter.class, false, new TestErrorLoadListener());
loader.loadFromFile("META-INF/ext3/");
Assert.assertFalse(loader.getAllExtensions().isEmpty());
extensionClass = loader.getExtensionClass("rightxx0");
Assert.assertNull(extensionClass);
extensionClass = loader.getExtensionClass("rightxx1");
Assert.assertNotNull(extensionClass);
Assert.assertTrue(extensionClass.getOrder() == 128);

loader = new ExtensionLoader<Filter>(Filter.class, false, new TestLoadListener());
loader.loadFromFile("META-INF/ext4/");
Assert.assertFalse(loader.getAllExtensions().isEmpty());
extensionClass = loader.getExtensionClass("rightxx0");
Assert.assertNotNull(extensionClass);
Assert.assertTrue(extensionClass.getOrder() == 123);
extensionClass = loader.getExtensionClass("rightxx1");
Assert.assertNotNull(extensionClass);
Assert.assertTrue(extensionClass.getOrder() == 128);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
@Extension("rightxx1")
@Extension(value = "rightxx1", order = 128)
public class RightFilter1 extends Filter {
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.rpc.ext;

import com.alipay.sofa.rpc.filter.Filter;

/**
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class TestErrorLoadListener implements ExtensionLoaderListener<Filter> {
@Override
public void onLoad(ExtensionClass extensionClass) {
if ("rightxx0".equals(extensionClass.getAlias())) {
throw new NullPointerException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.alipay.sofa.rpc.ext;

import com.alipay.sofa.rpc.filter.Filter;

/**
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class TestLoadListener implements ExtensionLoaderListener<Filter> {
@Override
public void onLoad(ExtensionClass extensionClass) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.alipay.sofa.rpc.ext.RightFilter0
com.alipay.sofa.rpc.ext.RightFilter1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.alipay.sofa.rpc.ext.RightFilter0
com.alipay.sofa.rpc.ext.RightFilter1