You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/06/21 11:22:54 UTC

[GitHub] liubao68 closed pull request #772: [SCB-194] Spring default scan main class package

liubao68 closed pull request #772: [SCB-194] Spring default scan main class package
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/772
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/resources/META-INF/spring/cse.bean.xml b/core/src/main/resources/META-INF/spring/cse.bean.xml
index 44817fbbd..8ae429abd 100644
--- a/core/src/main/resources/META-INF/spring/cse.bean.xml
+++ b/core/src/main/resources/META-INF/spring/cse.bean.xml
@@ -23,7 +23,7 @@
 		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>
   <!-- <context:spring-configured /> -->
-  <context:component-scan base-package="org.apache.servicecomb"/>
+  <context:component-scan base-package="${scb-scan-package:org.apache.servicecomb}"/>
 
   <bean class="org.apache.servicecomb.core.config.ConfigurationSpringInitializer">
     <property name="configId" value="config"/>
diff --git a/foundations/foundation-common/pom.xml b/foundations/foundation-common/pom.xml
index d1a369f8b..dced02a31 100644
--- a/foundations/foundation-common/pom.xml
+++ b/foundations/foundation-common/pom.xml
@@ -47,11 +47,6 @@
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-log4j12</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
@@ -76,5 +71,19 @@
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>foundation-test-scaffolding</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java
index cb1536ee1..103ac79e0 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/BeanUtils.java
@@ -17,13 +17,22 @@
 
 package org.apache.servicecomb.foundation.common.utils;
 
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.aop.TargetClassAware;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
+
 public final class BeanUtils {
   public static final String DEFAULT_BEAN_RESOURCE = "classpath*:META-INF/spring/*.bean.xml";
 
+  public static final String SCB_SCAN_PACKAGE = "scb-scan-package";
+
+  private static final String SCB_PACKAGE = "org.apache.servicecomb";
+
   private static ApplicationContext context;
 
   private BeanUtils() {
@@ -33,10 +42,41 @@ public static void init() {
     init(DEFAULT_BEAN_RESOURCE);
   }
 
+
   public static void init(String... configLocations) {
+    prepareServiceCombScanPackage();
+
     context = new ClassPathXmlApplicationContext(configLocations);
   }
 
+  public static void prepareServiceCombScanPackage() {
+    Set<String> scanPackags = new LinkedHashSet<>();
+    // add exists settings
+    String exists = System.getProperty(SCB_SCAN_PACKAGE);
+    if (exists != null) {
+      for (String exist : exists.trim().split(",")) {
+        if (!exist.isEmpty()) {
+          scanPackags.add(exist.trim());
+        }
+      }
+    }
+
+    // ensure servicecomb package exist
+    scanPackags.add(SCB_PACKAGE);
+
+    // add main class package
+    Class<?> mainClass = JvmUtils.findMainClass();
+    if (mainClass != null) {
+      String pkg = mainClass.getPackage().getName();
+      if (!pkg.startsWith(SCB_PACKAGE)) {
+        scanPackags.add(pkg);
+      }
+    }
+
+    // finish
+    System.setProperty(SCB_SCAN_PACKAGE, StringUtils.join(scanPackags, ","));
+  }
+
   public static ApplicationContext getContext() {
     return context;
   }
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JvmUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JvmUtils.java
new file mode 100644
index 000000000..e2b2b8617
--- /dev/null
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JvmUtils.java
@@ -0,0 +1,53 @@
+/*
+ * 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 org.apache.servicecomb.foundation.common.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+
+public final class JvmUtils {
+  private static final Logger LOGGER = LoggerFactory.getLogger(JvmUtils.class);
+
+  // available for oracle jdk/ open jdk, and maybe others
+  @VisibleForTesting
+  static final String SUN_JAVA_COMMAND = "sun.java.command";
+
+  private JvmUtils() {
+  }
+
+  /**
+   *
+   * @return main class or null, never throw exception
+   */
+  public static Class<?> findMainClass() {
+    String command = System.getProperty(SUN_JAVA_COMMAND);
+    if (command == null || command.isEmpty()) {
+      return null;
+    }
+
+    // command is main class and args
+    String mainClass = command.trim().split(" ")[0];
+    try {
+      return Class.forName(mainClass);
+    } catch (Throwable e) {
+      LOGGER.warn("\"{}\" is not a valid class.", mainClass, e);
+      return null;
+    }
+  }
+}
\ No newline at end of file
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java
index fc7afcf8c..c9a28c185 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestBeanUtils.java
@@ -17,9 +17,15 @@
 package org.apache.servicecomb.foundation.common.utils;
 
 import org.aspectj.lang.annotation.Aspect;
+import org.junit.AfterClass;
 import org.junit.Assert;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+import mockit.Expectations;
+import mockit.Mocked;
 
 public class TestBeanUtils {
   static interface Intf {
@@ -34,6 +40,16 @@
   static class MyAspect {
   }
 
+  @BeforeClass
+  public static void setup() {
+    System.clearProperty(BeanUtils.SCB_SCAN_PACKAGE);
+  }
+
+  @AfterClass
+  public static void tearDown() {
+    System.clearProperty(BeanUtils.SCB_SCAN_PACKAGE);
+  }
+
   @Test
   public void test() {
     Intf target = new Impl();
@@ -45,4 +61,78 @@ public void test() {
     Assert.assertEquals(Impl.class, BeanUtils.getImplClassFromBean(proxy));
     Assert.assertEquals(Impl.class, BeanUtils.getImplClassFromBean(new Impl()));
   }
+
+  @Test
+  public void prepareServiceCombScanPackage_noExist_noMain() {
+    System.clearProperty(BeanUtils.SCB_SCAN_PACKAGE);
+    new Expectations(JvmUtils.class) {
+      {
+        JvmUtils.findMainClass();
+        result = null;
+      }
+    };
+
+    BeanUtils.prepareServiceCombScanPackage();
+
+    Assert.assertEquals("org.apache.servicecomb", System.getProperty(BeanUtils.SCB_SCAN_PACKAGE));
+  }
+
+  @Test
+  public void prepareServiceCombScanPackage_noExist_scbMain() {
+    System.clearProperty(BeanUtils.SCB_SCAN_PACKAGE);
+    new Expectations(JvmUtils.class) {
+      {
+        JvmUtils.findMainClass();
+        result = TestBeanUtils.class;
+      }
+    };
+
+    BeanUtils.prepareServiceCombScanPackage();
+
+    Assert.assertEquals("org.apache.servicecomb", System.getProperty(BeanUtils.SCB_SCAN_PACKAGE));
+  }
+
+  @Test
+  public void prepareServiceCombScanPackage_noExist_otherMain() {
+    System.clearProperty(BeanUtils.SCB_SCAN_PACKAGE);
+    new Expectations(JvmUtils.class) {
+      {
+        JvmUtils.findMainClass();
+        result = String.class;
+      }
+    };
+
+    BeanUtils.prepareServiceCombScanPackage();
+
+    Assert.assertEquals("org.apache.servicecomb,java.lang", System.getProperty(BeanUtils.SCB_SCAN_PACKAGE));
+  }
+
+  @Test
+  public void prepareServiceCombScanPackage_exist() {
+    System.setProperty(BeanUtils.SCB_SCAN_PACKAGE, "a.b,,c.d");
+    new Expectations(JvmUtils.class) {
+      {
+        JvmUtils.findMainClass();
+        result = null;
+      }
+    };
+
+    BeanUtils.prepareServiceCombScanPackage();
+
+    Assert.assertEquals("a.b,c.d,org.apache.servicecomb", System.getProperty(BeanUtils.SCB_SCAN_PACKAGE));
+  }
+
+  @Test
+  public void init(@Mocked ClassPathXmlApplicationContext context) {
+    System.clearProperty(BeanUtils.SCB_SCAN_PACKAGE);
+    new Expectations(JvmUtils.class) {
+      {
+        JvmUtils.findMainClass();
+        result = TestBeanUtils.class;
+      }
+    };
+    BeanUtils.init();
+
+    Assert.assertEquals("org.apache.servicecomb", System.getProperty(BeanUtils.SCB_SCAN_PACKAGE));
+  }
 }
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java
new file mode 100644
index 000000000..d16792f3e
--- /dev/null
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/TestJvmUtils.java
@@ -0,0 +1,71 @@
+/*
+ * 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 org.apache.servicecomb.foundation.common.utils;
+
+import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestJvmUtils {
+  static String orgCmd = System.getProperty(JvmUtils.SUN_JAVA_COMMAND);
+
+  @Before
+  public void setup() {
+    System.clearProperty(JvmUtils.SUN_JAVA_COMMAND);
+  }
+
+  @AfterClass
+  public static void tearDown() {
+    if (orgCmd == null) {
+      System.clearProperty(JvmUtils.SUN_JAVA_COMMAND);
+      return;
+    }
+
+    System.setProperty(JvmUtils.SUN_JAVA_COMMAND, orgCmd);
+  }
+
+  @Test
+  public void findMainClass_notExist() {
+    Assert.assertNull(JvmUtils.findMainClass());
+  }
+
+  @Test
+  public void findMainClass_existButEmpty() {
+    System.setProperty(JvmUtils.SUN_JAVA_COMMAND, "");
+    Assert.assertNull(JvmUtils.findMainClass());
+  }
+
+  @Test
+  public void findMainClass_invalid() {
+    LogCollector logCollector = new LogCollector();
+
+    System.setProperty(JvmUtils.SUN_JAVA_COMMAND, "invalidCls");
+
+    Assert.assertNull(JvmUtils.findMainClass());
+    Assert.assertEquals("\"invalidCls\" is not a valid class.", logCollector.getEvents().get(0).getMessage());
+    logCollector.teardown();
+  }
+
+  @Test
+  public void findMainClass_normal() {
+    System.setProperty(JvmUtils.SUN_JAVA_COMMAND, TestJvmUtils.class.getName() + " arg");
+
+    Assert.assertEquals(TestJvmUtils.class, JvmUtils.findMainClass());
+  }
+}
diff --git a/foundations/foundation-test-scaffolding/src/main/java/io/vertx/core/impl/SyncContext.java b/foundations/foundation-test-scaffolding/src/main/java/io/vertx/core/impl/SyncContext.java
index 7eecbfd81..eb039afd6 100644
--- a/foundations/foundation-test-scaffolding/src/main/java/io/vertx/core/impl/SyncContext.java
+++ b/foundations/foundation-test-scaffolding/src/main/java/io/vertx/core/impl/SyncContext.java
@@ -74,7 +74,7 @@ public void runOnContext(Handler<Void> task) {
   @Override
   <T> void executeBlocking(Action<T> action, Handler<Future<T>> blockingCodeHandler,
       Handler<AsyncResult<T>> resultHandler,
-      Executor exec, TaskQueue queue, PoolMetrics metrics) {
+      Executor exec, TaskQueue queue, @SuppressWarnings("rawtypes") PoolMetrics metrics) {
     syncExecuteBlocking(blockingCodeHandler, resultHandler);
   }
 }
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java
index ac6811925..7a07853d2 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestStandardHttpServletResponseEx.java
@@ -50,11 +50,6 @@
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
-  private void setExceptionExpected() {
-    expectedException.expect(Error.class);
-    expectedException.expectMessage(Matchers.is("not supported method"));
-  }
-
   @Before
   public void setup() {
     responseEx = new StandardHttpServletResponseEx(response);
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
index 8ae82dbf6..07de74aa7 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
@@ -367,7 +367,6 @@ public void sendPart_inputStreamBreak(@Mocked Part part, @Mocked InputStream inp
     future.get();
   }
 
-  @SuppressWarnings("unchecked")
   @Test
   public void sendPart_ReadStreamPart(@Mocked ReadStreamPart part) {
     CompletableFuture<Void> future = new CompletableFuture<>();
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
index 6bd814638..586da6bb2 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
@@ -50,8 +50,10 @@ public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBoo
 
   @Override
   public void destroy() {
-    DefaultMonitorRegistry.getInstance().unregister(registry);
-    globalRegistry.remove(registry);
+    if (registry != null) {
+      DefaultMonitorRegistry.getInstance().unregister(registry);
+      globalRegistry.remove(registry);
+    }
   }
 
   public Registry getRegistry() {
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java
index 4b2a1c3ff..f5e77b092 100644
--- a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java
@@ -54,4 +54,10 @@ public void init() {
     Assert.assertEquals(0, registries.size());
     Assert.assertEquals(0, DefaultMonitorRegistry.getInstance().getRegisteredMonitors().size());
   }
+
+  @Test
+  public void destroy_notInit() {
+    // should not throw exception
+    registryInitializer.destroy();
+  }
 }
diff --git a/transports/transport-rest/transport-rest-client/src/main/resources/META-INF/spring/cse.transport.rest.bean.xml b/transports/transport-rest/transport-rest-client/src/main/resources/META-INF/spring/cse.transport.rest.bean.xml
deleted file mode 100644
index a21116eca..000000000
--- a/transports/transport-rest/transport-rest-client/src/main/resources/META-INF/spring/cse.transport.rest.bean.xml
+++ /dev/null
@@ -1,25 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  ~ 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.
-  -->
-
-<beans xmlns="http://www.springframework.org/schema/beans"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="
-        http://www.springframework.org/schema/beans
-        classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd">
-
-</beans>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services