You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/10/09 02:30:10 UTC

[GitHub] [shardingsphere-elasticjob] TeslaCN commented on a change in pull request #1548: Redesign SPI factory to make them into one class (#1537)

TeslaCN commented on a change in pull request #1548:
URL: https://github.com/apache/shardingsphere-elasticjob/pull/1548#discussion_r502144791



##########
File path: elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/spi/ElasticJobServiceLoader.java
##########
@@ -76,4 +81,58 @@ private static Object newServiceInstance(final Class<?> clazz) {
             throw new ServiceLoaderInstantiationException(clazz, ex.getCause());
         }
     }
+
+    /**
+     * Register typeSPI service.
+     *
+     * @param typedService specific service type
+     * @param <T> type of service
+     */
+    public static <T> void registerTypedService(final Class<T> typedService) {
+        if (!TypedSPI.class.isAssignableFrom(typedService)) {
+            throw new IllegalArgumentException("Cannot register @" + typedService.getName() + "as a typed service, because its not a subClass of @" + typedService);
+        }
+        if (TYPED_SERVICES.containsKey(typedService)) {
+            return;
+        }
+        ServiceLoader.load(typedService).forEach(each -> registerTypedServiceClass(typedService, (TypedSPI) each));
+    }
+
+    private static <T> void registerTypedServiceClass(final Class<T> typedService, final TypedSPI instance) {
+        TYPED_SERVICES.computeIfAbsent(typedService, unused -> new ConcurrentHashMap<>()).putIfAbsent(instance.getType(), instance);
+        TYPED_SERVICE_CLASSES.computeIfAbsent(typedService, unused -> new ConcurrentHashMap<>()).putIfAbsent(instance.getType(), instance.getClass());
+    }
+
+    /**
+     * Get cached instance.
+     *
+     * @param typedService service type
+     * @param type         specific service type
+     * @param <T>          specific type of service
+     * @return cached service instance
+     */
+    public static <T extends TypedSPI> T getCachedInstance(final Class<T> typedService, final String type) {
+        T instance = TYPED_SERVICES.containsKey(typedService) ? (T) TYPED_SERVICES.get(typedService).get(type) : null;
+        if (instance == null) {

Review comment:
       Have constants on the left and variable on the right in = and equals conditional expressions; Have variable on the left and constants on the right in greater than and less than conditional expressions.
   Ref https://shardingsphere.apache.org/community/en/contribute/code-conduct/

##########
File path: elasticjob-infra/elasticjob-infra-common/src/main/java/org/apache/shardingsphere/elasticjob/infra/spi/ElasticJobServiceLoader.java
##########
@@ -76,4 +81,58 @@ private static Object newServiceInstance(final Class<?> clazz) {
             throw new ServiceLoaderInstantiationException(clazz, ex.getCause());
         }
     }
+
+    /**
+     * Register typeSPI service.
+     *
+     * @param typedService specific service type
+     * @param <T> type of service
+     */
+    public static <T> void registerTypedService(final Class<T> typedService) {
+        if (!TypedSPI.class.isAssignableFrom(typedService)) {
+            throw new IllegalArgumentException("Cannot register @" + typedService.getName() + "as a typed service, because its not a subClass of @" + typedService);
+        }
+        if (TYPED_SERVICES.containsKey(typedService)) {
+            return;
+        }
+        ServiceLoader.load(typedService).forEach(each -> registerTypedServiceClass(typedService, (TypedSPI) each));
+    }
+
+    private static <T> void registerTypedServiceClass(final Class<T> typedService, final TypedSPI instance) {
+        TYPED_SERVICES.computeIfAbsent(typedService, unused -> new ConcurrentHashMap<>()).putIfAbsent(instance.getType(), instance);
+        TYPED_SERVICE_CLASSES.computeIfAbsent(typedService, unused -> new ConcurrentHashMap<>()).putIfAbsent(instance.getType(), instance.getClass());
+    }
+
+    /**
+     * Get cached instance.
+     *
+     * @param typedService service type
+     * @param type         specific service type
+     * @param <T>          specific type of service
+     * @return cached service instance
+     */
+    public static <T extends TypedSPI> T getCachedInstance(final Class<T> typedService, final String type) {
+        T instance = TYPED_SERVICES.containsKey(typedService) ? (T) TYPED_SERVICES.get(typedService).get(type) : null;
+        if (instance == null) {
+            throw new JobConfigurationException("Cannot find a cached typed service instance by the interface: @" + typedService.getName() + "and type: " + type);
+        }
+        return instance;
+    }
+
+    /**
+     * New typed instance.
+     *
+     * @param typedService service type
+     * @param type         specific service type
+     * @param <T>          specific type of service
+     * @return specific typed service instance
+     */
+    public static <T extends TypedSPI> T newTypedServiceInstance(final Class<T> typedService, final String type) {
+        Class<?> instanceClass = TYPED_SERVICE_CLASSES.containsKey(typedService) ? TYPED_SERVICE_CLASSES.get(typedService).get(type) : null;
+        if (instanceClass == null) {

Review comment:
       Have constants on the left and variable on the right in = and equals conditional expressions; Have variable on the left and constants on the right in greater than and less than conditional expressions.
   Ref https://shardingsphere.apache.org/community/en/contribute/code-conduct/




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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