Skip to content

Commit

Permalink
Increase availability, make @adaptive try default extension when the …
Browse files Browse the repository at this point in the history
…specified one couldn't be found. (apache#2159)

* Increase availability, make @adaptive try default extension when the specified one couldn't be found.

* Add warn log for @adaptive when using default extension.

* Optimization, append to StringBuilder directly.
  • Loading branch information
chickenlj authored and jerrick-zhu committed Aug 16, 2018
1 parent 7277f5e commit 8879afa
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ private String createAdaptiveExtensionClassCode() {
codeBuilder.append("\nimport ").append(ExtensionLoader.class.getName()).append(";");
codeBuilder.append("\npublic class ").append(type.getSimpleName()).append("$Adaptive").append(" implements ").append(type.getCanonicalName()).append(" {");

codeBuilder.append("\nprivate static final org.apache.dubbo.common.logger.Logger logger = org.apache.dubbo.common.logger.LoggerFactory.getLogger(ExtensionLoader.class);");
codeBuilder.append("\nprivate java.util.concurrent.atomic.AtomicInteger count = new java.util.concurrent.atomic.AtomicInteger(0);\n");

for (Method method : methods) {
Class<?> rt = method.getReturnType();
Class<?>[] pts = method.getParameterTypes();
Expand Down Expand Up @@ -910,9 +913,12 @@ private String createAdaptiveExtensionClassCode() {
type.getName(), Arrays.toString(value));
code.append(s);

s = String.format("\n%s extension = (%<s)%s.getExtensionLoader(%s.class).getExtension(extName);",
type.getName(), ExtensionLoader.class.getSimpleName(), type.getName());
code.append(s);
code.append(String.format("\n%s extension = null;\n try {\nextension = (%<s)%s.getExtensionLoader(%s.class).getExtension(extName);\n}catch(Exception e){\n",
type.getName(), ExtensionLoader.class.getSimpleName(), type.getName()));
code.append(String.format("if (count.incrementAndGet() == 1) {\nlogger.warn(\"Failed to find extension named \" + extName + \" for type %s, will use default extension %s instead.\", e);\n}\n",
type.getName(), defaultExtName));
code.append(String.format("extension = (%s)%s.getExtensionLoader(%s.class).getExtension(\"%s\");\n}",
type.getName(), ExtensionLoader.class.getSimpleName(), type.getName(), defaultExtName));

// return statement
if (!rt.equals(void.class)) {
Expand Down

0 comments on commit 8879afa

Please sign in to comment.