Browse Source

青涩知夏->修改SPI配置

青涩知夏 4 years ago
parent
commit
575a27872c

+ 6 - 67
src/main/java/cn/nosum/common/annotation/Activate.java

@@ -1,19 +1,3 @@
-/*
- * 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 cn.nosum.common.annotation;
 
 
@@ -23,69 +7,24 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
-/**
- * Activate. This annotation is useful for automatically activate certain extensions with the given criteria,
- * for examples: <code>@Activate</code> can be used to load certain <code>Filter</code> extension when there are
- * multiple implementations.
- * <ol>
- * <li>{@link Activate#group()} specifies group criteria. Framework SPI defines the valid group values.
- * <li>{@link Activate#value()} specifies parameter key in {@link URL} criteria.
- * </ol>
- * SPI provider can call {@link ExtensionLoader#getActivateExtension(URL, String, String)} to find out all activated
- * extensions with the given criteria.
- *
- * @see SPI
- * @see URL
- * @see ExtensionLoader
- */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE, ElementType.METHOD})
 public @interface Activate {
-    /**
-     * Activate the current extension when one of the groups matches. The group passed into
-     * {@link ExtensionLoader#getActivateExtension(URL, String, String)} will be used for matching.
-     *
-     * @return group names to match
-     * @see ExtensionLoader#getActivateExtension(URL, String, String)
-     */
-    String[] group() default {};
-
-    /**
-     * Activate the current extension when the specified keys appear in the URL's parameters.
-     * <p>
-     * For example, given <code>@Activate("cache, validation")</code>, the current extension will be return only when
-     * there's either <code>cache</code> or <code>validation</code> key appeared in the URL's parameters.
-     * </p>
-     *
-     * @return URL parameter keys
-     * @see ExtensionLoader#getActivateExtension(URL, String)
-     * @see ExtensionLoader#getActivateExtension(URL, String, String)
-     */
-    String[] value() default {};
 
     /**
-     * Relative ordering info, optional
-     * Deprecated since 2.7.0
-     *
-     * @return extension list which should be put before the current one
+     * 所属的组,一般是类名
      */
-    @Deprecated
-    String[] before() default {};
+    String group() default "";
 
     /**
-     * Relative ordering info, optional
-     * Deprecated since 2.7.0
-     *
-     * @return extension list which should be put after the current one
+     * 是否激活
      */
-    @Deprecated
-    String[] after() default {};
+    boolean value() default false;
 
     /**
-     * Absolute ordering info, optional
-     *
-     * @return absolute ordering info
+     * 用于控制链路排序
      */
     int order() default 0;
+
 }

+ 0 - 61
src/main/java/cn/nosum/common/extension/ExtensionLoader.java

@@ -35,9 +35,7 @@ public class ExtensionLoader<T> {
     private static final ConcurrentMap<Class<?>, ExtensionLoader<?>> EXTENSION_LOADERS = new ConcurrentHashMap<>();
     private static final ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES = new ConcurrentHashMap<>();
     private final Holder<Map<String, Class<?>>> cachedClasses = new Holder<>();
-    private final ConcurrentMap<Class<?>, String> cachedNames = new ConcurrentHashMap<>();
     private final Holder<Object> cachedAdaptiveInstance = new Holder<>();
-    private final Map<String, Object> cachedActivates = new ConcurrentHashMap<>();
     private final ConcurrentMap<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<>();
     private Set<Class<?>> cachedWrapperClasses;
 
@@ -332,15 +330,6 @@ public class ExtensionLoader<T> {
         }
     }
 
-    /**
-     * cache name
-     */
-    private void cacheName(Class<?> clazz, String name) {
-        if (!cachedNames.containsKey(clazz)) {
-            cachedNames.put(clazz, name);
-        }
-    }
-
     private void loadClass(Map<String, Class<?>> extensionClasses, java.net.URL resourceURL, Class<?> clazz, String name) throws NoSuchMethodException {
         if (!type.isAssignableFrom(clazz)) {
             throw new IllegalStateException("Error occurred when loading extension class (interface: " +
@@ -351,56 +340,6 @@ public class ExtensionLoader<T> {
             cacheAdaptiveClass(clazz);
         } else if (isWrapperClass(clazz)) {
             cacheWrapperClass(clazz);
-        } else {
-            clazz.getConstructor();
-            if (StringUtils.isEmpty(name)) {
-                name = findAnnotationName(clazz);
-                if (name.length() == 0) {
-                    throw new IllegalStateException("No such extension name for the class " + clazz.getName() + " in the config " + resourceURL);
-                }
-            }
-
-            String[] names = NAME_SEPARATOR.split(name);
-            if (ArrayUtils.isNotEmpty(names)) {
-                cacheActivateClass(clazz, names[0]);
-                for (String n : names) {
-                    cacheName(clazz, n);
-                    saveInExtensionClass(extensionClasses, clazz, n);
-                }
-            }
-        }
-    }
-
-    private void saveInExtensionClass(Map<String, Class<?>> extensionClasses, Class<?> clazz, String name) {
-        Class<?> c = extensionClasses.get(name);
-        if (c == null) {
-            extensionClasses.put(name, clazz);
-        } else if (c != clazz) {
-            throw new IllegalStateException("Duplicate extension " + type.getName() + " name " + name + " on " + c.getName() + " and " + clazz.getName());
-        }
-    }
-    @SuppressWarnings("deprecation")
-    private String findAnnotationName(Class<?> clazz) {
-        cn.nosum.common.annotation.Extension extension = clazz.getAnnotation(cn.nosum.common.annotation.Extension.class);
-        if (extension == null) {
-            String name = clazz.getSimpleName();
-            if (name.endsWith(type.getSimpleName())) {
-                name = name.substring(0, name.length() - type.getSimpleName().length());
-            }
-            return name.toLowerCase();
-        }
-        return extension.value();
-    }
-
-    private void cacheActivateClass(Class<?> clazz, String name) {
-        Activate activate = clazz.getAnnotation(Activate.class);
-        if (activate != null) {
-            cachedActivates.put(name, activate);
-        } else {
-            cn.nosum.common.annotation.Activate oldActivate = clazz.getAnnotation( cn.nosum.common.annotation.Activate.class);
-            if (oldActivate != null) {
-                cachedActivates.put(name, oldActivate);
-            }
         }
     }
 

+ 14 - 4
src/main/java/cn/nosum/common/util/PropertiesUtil.java

@@ -4,10 +4,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.*;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.regex.Pattern;
 
 public class PropertiesUtil {
 	private final static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
@@ -34,8 +33,10 @@ public class PropertiesUtil {
 			properties.load(inputStream);
 			Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
 			for (Map.Entry<Object, Object> entry : entrySet) {
+				String entryKey=entry.getKey().toString().trim();
+				String value=entry.getValue().toString().trim();
 				if (!entry.getKey().toString().startsWith("#")) {
-					propertiesMap.put(((String) entry.getKey()).trim(), ((String) entry.getValue()).trim());
+					propertiesMap.put(entryKey, value);
 				}
 			}
 		} catch (IOException e) {
@@ -56,4 +57,13 @@ public class PropertiesUtil {
 	public static String getProperty(String name){
 		return propertiesMap.get(name);
 	}
+	public static List<String> getArraysProperty(String name){
+		List<String> values=new ArrayList<>();
+		for (Map.Entry<String, String> entry : propertiesMap.entrySet()) {
+			if (entry.getKey().startsWith(name)) {
+				values.add(entry.getValue());
+			}
+		}
+		return values;
+	}
 }

+ 1 - 1
src/main/java/cn/nosum/gateway/handler/PreProcessHandler.java

@@ -4,7 +4,7 @@ import cn.nosum.common.http.entity.Context;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.channel.SimpleChannelInboundHandler;
 
-public class PreProcessHandler extends SimpleChannelInboundHandler<Context>{
+public class PreProcessHandler extends SimpleChannelInboundHandler<Context> {
 
     @Override
     protected void channelRead0(ChannelHandlerContext ctx, Context context) throws Exception {

+ 4 - 1
src/main/java/cn/nosum/gateway/slot/DefaultProcessorSlotChain.java

@@ -1,8 +1,11 @@
 package cn.nosum.gateway.slot;
 
+import cn.nosum.common.annotation.Adaptive;
 import cn.nosum.common.http.entity.Context;
 
-public class DefaultProcessorSlotChain extends ProcessorSlotChain {
+
+@Adaptive
+public class LinkProcessorSlotChain implements ProcessorSlotChain {
 
     AbstractLinkedProcessorSlot<Context> first = new AbstractLinkedProcessorSlot<Context>() {
         @Override

+ 3 - 1
src/main/java/cn/nosum/gateway/slot/ProcessorSlotChain.java

@@ -1,8 +1,10 @@
 package cn.nosum.gateway.slot;
 
+import cn.nosum.common.annotation.SPI;
 import cn.nosum.common.http.entity.Context;
 
-public abstract class ProcessorSlotChain extends AbstractLinkedProcessorSlot<Context> {
+@SPI
+public interface ProcessorSlotChain extends ProcessorSlot<Context> {
 
     /**
      * Add a processor to the head of this slot chain.

+ 2 - 2
src/main/java/cn/nosum/gateway/slot/build/DefaultSlotChainBuilder.java

@@ -1,7 +1,7 @@
 package cn.nosum.gateway.slot.build;
 
 import cn.nosum.common.annotation.Adaptive;
-import cn.nosum.gateway.slot.DefaultProcessorSlotChain;
+import cn.nosum.common.extension.ExtensionLoader;
 import cn.nosum.gateway.slot.ProcessorSlotChain;
 import cn.nosum.gateway.slot.chain.FileProcessorSlotChain;
 import cn.nosum.gateway.slot.chain.LogProcessorSlotChain;
@@ -11,7 +11,7 @@ import cn.nosum.gateway.slot.chain.UrlProcessorSlotChain;
 public class DefaultSlotChainBuilder implements SlotChainBuilder {
     @Override
     public ProcessorSlotChain build() {
-        ProcessorSlotChain chain = new DefaultProcessorSlotChain();
+        ProcessorSlotChain chain = ExtensionLoader.getExtensionLoader(ProcessorSlotChain.class).getAdaptiveExtension();
         chain.addLast(new UrlProcessorSlotChain());
         chain.addLast(new LogProcessorSlotChain());
         chain.addLast(new FileProcessorSlotChain());

+ 1 - 1
src/main/java/cn/nosum/gateway/slot/build/SlotChainBuilder.java

@@ -7,7 +7,7 @@ import cn.nosum.gateway.slot.ProcessorSlotChain;
 /**
  * 处理槽构建器顶层接口
  */
-@SPI("link")
+@SPI("default")
 public interface SlotChainBuilder {
 
     /**

+ 1 - 0
src/main/resources/META-INF/gateway/cn.nosum.gateway.slot.ProcessorSlotChain

@@ -0,0 +1 @@
+link=cn.nosum.gateway.slot.LinkProcessorSlotChain

+ 1 - 1
src/main/resources/META-INF/gateway/cn.nosum.gateway.slot.build.SlotChainBuilder

@@ -1 +1 @@
-link=cn.nosum.gateway.slot.build.DefaultSlotChainBuilder
+default=cn.nosum.gateway.slot.build.DefaultSlotChainBuilder

+ 2 - 0
src/main/resources/application.properties

@@ -1,4 +1,6 @@
 container.port = 8888
+container.context.handler.class.name[0]=cn.nosum.gateway.handler.PreProcessHandler
+container.context.handler.class.name[1]=cn.nosum.gateway.handler.FinalProcessHandler
 cn.nosum.common.extension.ExtensionFactory = adaptive
 cn.nosum.gateway.container.GateWayContainer = netty
 cn.nosum.gateway.handler.build.HandlerBuilder = context