This form avoids superfluous object creation when the logger
22 | * is disabled for the DEBUG level.
23 | *
24 | * @param format the format string
25 | * @param arg the argument
26 | */
27 | public void debug(String format, Object arg);
28 |
29 | /**
30 | * Log an exception at the DEBUG level with an accompanying message.
31 | *
32 | * @param msg the message accompanying the exception
33 | * @param t the exception to be logged
34 | */
35 | void debug(String msg, Throwable t);
36 |
37 | /**
38 | * Log a message at the INFO level.
39 | *
40 | * @param msg the message string to be logged
41 | */
42 | void info(String msg);
43 |
44 | /**
45 | * Log a message at the INFO level according to the specified format
46 | * and argument.
47 | *
48 | *
This form avoids superfluous object creation when the logger
49 | * is disabled for the INFO level.
50 | *
51 | * @param format the format string
52 | * @param arg the argument
53 | */
54 | public void info(String format, Object arg);
55 |
56 | /**
57 | * Log an exception at the INFO level with an accompanying message.
58 | *
59 | * @param msg the message accompanying the exception
60 | * @param t the exception to be logged
61 | */
62 | void info(String msg, Throwable t);
63 |
64 | /**
65 | * Log a message at the ERROR level.
66 | *
67 | * @param msg the message string to be logged
68 | */
69 | void error(String msg);
70 |
71 | /**
72 | * Log an exception at the ERROR level.
73 | *
74 | * @param msg the message accompanying the exception
75 | * @param t the exception to log
76 | */
77 | void error(String msg, Throwable t);
78 |
79 | /**
80 | * Log a message at the ERROR level according to the specified format
81 | * and argument.
82 | *
83 | *
This form avoids superfluous object creation when the logger
84 | * is disabled for the ERROR level.
85 | *
86 | * @param format the format string
87 | * @param arg the argument
88 | */
89 | public void error(String format, Object arg);
90 |
91 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/logs/LoggerFactory.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.logs;
2 |
3 | import com.bj58.argo.internal.DefaultLoggerFactory;
4 | import com.google.inject.ImplementedBy;
5 |
6 | @ImplementedBy(DefaultLoggerFactory.class)
7 | public interface LoggerFactory {
8 |
9 | /**
10 | *
11 | * @param name the name of the Logger to return
12 | * @return a Logger instance
13 | */
14 | Logger getLogger(String name);
15 |
16 | Logger getLogger(Class> clazz);
17 | }
18 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/route/Action.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.route;
2 |
3 | /**
4 | * 封装的action方法
5 | */
6 | public interface Action {
7 | /**
8 | * 确定优先级,路由时根据优先级进行匹配
9 | * @return 优先级
10 | */
11 | double order();
12 |
13 | /**
14 | * 匹配并且执行
15 | * @param bag 当前路由信息
16 | * @return 匹配或执行的结果
17 | */
18 | RouteResult matchAndInvoke(RouteBag bag);
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/route/RouteBag.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.route;
2 |
3 | import com.bj58.argo.BeatContext;
4 | import com.bj58.argo.utils.PathUtils;
5 |
6 | /**
7 | * 路由信息
8 | */
9 | public class RouteBag {
10 |
11 | public static RouteBag create(BeatContext beat) {
12 | return new RouteBag(beat);
13 | }
14 |
15 | private final BeatContext beat;
16 | private final boolean isGet;
17 | private final boolean isPost;
18 | private final String path;
19 | private final String simplyPath;
20 |
21 | private RouteBag(BeatContext beat) {
22 | this.beat = beat;
23 |
24 | path = beat.getClient().getRelativeUrl();
25 | simplyPath = PathUtils.simplyWithoutSuffix(path);
26 |
27 | String requestMethod = beat.getRequest().getMethod().toUpperCase();
28 | isPost = "POST".equals(requestMethod);
29 | isGet = !isPost;
30 | }
31 |
32 | public BeatContext getBeat() {
33 | return beat;
34 | }
35 |
36 | public boolean isGet() {
37 | return isGet;
38 | }
39 |
40 | public boolean isPost() {
41 | return isPost;
42 | }
43 |
44 | public String getPath() {
45 | return path;
46 | }
47 |
48 | public String getSimplyPath() {
49 | return simplyPath;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/route/RouteResult.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.route;
2 |
3 | import com.bj58.argo.ActionResult;
4 |
5 | /**
6 | * 路由处理的结果
7 | */
8 | public class RouteResult {
9 |
10 | public static RouteResult unMatch() {
11 | return new RouteResult(false, ActionResult.NULL);
12 | }
13 |
14 | public static RouteResult invoked(ActionResult result) {
15 | return new RouteResult(ActionResult.NULL != result, result);
16 | }
17 |
18 | private final boolean success;
19 | private final ActionResult result;
20 |
21 | private RouteResult(boolean success, ActionResult result) {
22 | this.success = success;
23 | this.result = result;
24 | }
25 |
26 | public boolean isSuccess() {
27 | return success;
28 | }
29 |
30 | public ActionResult getResult() {
31 | return result;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/route/Router.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.route;
2 |
3 | import com.bj58.argo.BeatContext;
4 | import com.bj58.argo.ActionResult;
5 | import com.bj58.argo.internal.DefaultRouter;
6 | import com.google.inject.ImplementedBy;
7 |
8 | /**
9 | * 路由器,根据每个请求的url进行匹配找到合适的
10 | * @see Action
11 | * 来执行
12 | */
13 | @ImplementedBy(DefaultRouter.class)
14 | //@Singleton
15 | public interface Router {
16 |
17 | public ActionResult route(BeatContext beat);
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/route/StaticActionAnnotation.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.route;
2 |
3 | import com.google.inject.BindingAnnotation;
4 |
5 | import java.lang.annotation.ElementType;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 | import java.lang.annotation.Target;
9 |
10 | /**
11 | * 用于Argo内部注入用
12 | *
13 | */
14 | @BindingAnnotation
15 | @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
16 | @Retention(RetentionPolicy.RUNTIME)
17 | public @interface StaticActionAnnotation {
18 | }
19 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/servlet/ArgoDispatcher.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.servlet;
2 |
3 | import com.bj58.argo.BeatContext;
4 | import com.bj58.argo.internal.DefaultArgoDispatcher;
5 | import com.google.inject.ImplementedBy;
6 |
7 | import javax.servlet.http.HttpServletRequest;
8 | import javax.servlet.http.HttpServletResponse;
9 |
10 | /**
11 | *
12 | * 用于处理request请求调度的核心类
13 | *
14 | */
15 | @ImplementedBy(DefaultArgoDispatcher.class)
16 | public interface ArgoDispatcher {
17 |
18 | void init();
19 |
20 | void service(HttpServletRequest request, HttpServletResponse response);
21 |
22 | void destroy();
23 |
24 | public HttpServletRequest currentRequest();
25 |
26 | public HttpServletResponse currentResponse();
27 |
28 | BeatContext currentBeatContext();
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/servlet/ArgoDispatcherFactory.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.servlet;
2 |
3 | import com.bj58.argo.Argo;
4 | import com.bj58.argo.convention.GroupConvention;
5 | import com.bj58.argo.convention.GroupConventionFactory;
6 |
7 | import javax.servlet.ServletContext;
8 |
9 |
10 | public class ArgoDispatcherFactory {
11 |
12 | public static ArgoDispatcher create(ServletContext servletContext){
13 |
14 | // xxx:这是一处硬编码
15 | GroupConvention groupConvention = GroupConventionFactory.getGroupConvention();
16 |
17 | return Argo.instance.init(servletContext, groupConvention);
18 |
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/servlet/ArgoFilter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.servlet;
2 |
3 | import javax.servlet.*;
4 | import javax.servlet.annotation.WebFilter;
5 | import javax.servlet.annotation.WebInitParam;
6 | import javax.servlet.http.HttpServletRequest;
7 | import javax.servlet.http.HttpServletResponse;
8 | import java.io.IOException;
9 |
10 | /**
11 | * 利用Filter来实行调度
12 | */
13 | @WebFilter(urlPatterns = {"/*"},
14 | dispatcherTypes = {DispatcherType.REQUEST},
15 | initParams = {@WebInitParam(name = "encoding", value = "UTF-8")}
16 | )
17 | public class ArgoFilter implements Filter {
18 |
19 | private ArgoDispatcher dispatcher;
20 |
21 | @Override
22 | public void init(FilterConfig filterConfig) throws ServletException {
23 |
24 |
25 | ServletContext servletContext = filterConfig.getServletContext();
26 |
27 | try {
28 | dispatcher = ArgoDispatcherFactory.create(servletContext);
29 | dispatcher.init();
30 | } catch (Exception e) {
31 |
32 | servletContext.log("failed to argo initialize, system exit!!!", e);
33 | System.exit(1);
34 |
35 | }
36 |
37 | }
38 |
39 | @Override
40 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
41 |
42 | HttpServletRequest httpReq = (HttpServletRequest) request;
43 | HttpServletResponse httpResp = (HttpServletResponse) response;
44 |
45 | dispatcher.service(httpReq, httpResp);
46 |
47 | }
48 |
49 | @Override
50 | public void destroy() {
51 | dispatcher.destroy();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/AntPathStringMatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.thirdparty;
22 |
23 | import java.util.LinkedList;
24 | import java.util.List;
25 | import java.util.Map;
26 | import java.util.regex.Matcher;
27 | import java.util.regex.Pattern;
28 |
29 | /**
30 | * Ant-style的路径匹配工具类
31 | *
32 | * @author Service Platform Architecture Team (spat@58.com)
33 | */
34 | class AntPathStringMatcher {
35 |
36 | private static final Pattern GLOB_PATTERN = Pattern.compile("\\?|\\*|\\{([^/]+?)\\}");
37 |
38 | private static final String DEFAULT_VARIABLE_PATTERN = "(.*)";
39 |
40 | private final Pattern pattern;
41 |
42 | private String str;
43 |
44 | private final List variableNames = new LinkedList();
45 |
46 | private final Map uriTemplateVariables;
47 |
48 | /** Construct a new instance of the AntPatchStringMatcher. */
49 | AntPathStringMatcher(String pattern, String str, Map uriTemplateVariables) {
50 | this.str = str;
51 | this.uriTemplateVariables = uriTemplateVariables;
52 | this.pattern = createPattern(pattern);
53 | }
54 |
55 | private Pattern createPattern(String pattern) {
56 | StringBuilder patternBuilder = new StringBuilder();
57 | Matcher m = GLOB_PATTERN.matcher(pattern);
58 | int end = 0;
59 | while (m.find()) {
60 | patternBuilder.append(quote(pattern, end, m.start()));
61 | String match = m.group();
62 | if ("?".equals(match)) {
63 | patternBuilder.append('.');
64 | }
65 | else if ("*".equals(match)) {
66 | patternBuilder.append(".*");
67 | }
68 | else if (match.startsWith("{") && match.endsWith("}")) {
69 | int colonIdx = match.indexOf(':');
70 | if (colonIdx == -1) {
71 | patternBuilder.append(DEFAULT_VARIABLE_PATTERN);
72 | variableNames.add(m.group(1));
73 | }
74 | else {
75 | String variablePattern = match.substring(colonIdx + 1, match.length() - 1);
76 | patternBuilder.append('(');
77 | patternBuilder.append(variablePattern);
78 | patternBuilder.append(')');
79 | String variableName = match.substring(1, colonIdx);
80 | variableNames.add(variableName);
81 | }
82 | }
83 | end = m.end();
84 | }
85 | patternBuilder.append(quote(pattern, end, pattern.length()));
86 | return Pattern.compile(patternBuilder.toString());
87 | }
88 |
89 | private String quote(String s, int start, int end) {
90 | if (start == end) {
91 | return "";
92 | }
93 | return Pattern.quote(s.substring(start, end));
94 | }
95 |
96 | /**
97 | * Main entry point.
98 | *
99 | * @return true if the string matches against the pattern, or false otherwise.
100 | */
101 | public boolean matchStrings() {
102 | Matcher matcher = pattern.matcher(str);
103 | if (matcher.matches()) {
104 | if (uriTemplateVariables != null) {
105 | for (int i = 1; i <= matcher.groupCount(); i++) {
106 | String name = this.variableNames.get(i - 1);
107 | String value = matcher.group(i);
108 | uriTemplateVariables.put(name, value);
109 | }
110 | }
111 | return true;
112 | }
113 | else {
114 | return false;
115 | }
116 | }
117 |
118 | }
119 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/ClassUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.thirdparty;
22 |
23 |
24 | import com.google.common.base.Preconditions;
25 |
26 | import java.io.File;
27 | import java.io.FileFilter;
28 | import java.io.IOException;
29 | import java.net.JarURLConnection;
30 | import java.net.URL;
31 | import java.net.URLDecoder;
32 | import java.util.ArrayList;
33 | import java.util.Enumeration;
34 | import java.util.LinkedHashSet;
35 | import java.util.List;
36 | import java.util.Set;
37 | import java.util.jar.JarEntry;
38 | import java.util.jar.JarFile;
39 |
40 |
41 |
42 | /**
43 | *
44 | * ClassUtil
45 | *
46 | * 扫描指定包(包括jar)下的class文件
47 | *
48 | * @author Service Platform Architecture Team (spat@58.com)
49 | */
50 | public class ClassUtil {
51 |
52 | /**
53 | * Return all interfaces that the given class implements as array,
54 | * including ones implemented by superclasses.
55 | *
If the class itself is an interface, it gets returned as sole interface.
56 | * @param clazz the class to analyze for interfaces
57 | * @return all interfaces that the given object implements as array
58 | */
59 | public static Class>[] getAllInterfacesForClass(Class> clazz) {
60 | return getAllInterfacesForClass(clazz, null);
61 | }
62 | /**
63 | * Return all interfaces that the given class implements as array,
64 | * including ones implemented by superclasses.
65 | *
If the class itself is an interface, it gets returned as sole interface.
66 | * @param clazz the class to analyze for interfaces
67 | * @param classLoader the ClassLoader that the interfaces need to be visible in
68 | * (may be null when accepting all declared interfaces)
69 | * @return all interfaces that the given object implements as array
70 | */
71 | public static Class>[] getAllInterfacesForClass(Class> clazz, ClassLoader classLoader) {
72 | Set ifcs = getAllInterfacesForClassAsSet(clazz, classLoader);
73 | return ifcs.toArray(new Class[ifcs.size()]);
74 | }
75 | /**
76 | * Return all interfaces that the given class implements as Set,
77 | * including ones implemented by superclasses.
78 | *
If the class itself is an interface, it gets returned as sole interface.
79 | * @param clazz the class to analyze for interfaces
80 | * @param classLoader the ClassLoader that the interfaces need to be visible in
81 | * (may be null when accepting all declared interfaces)
82 | * @return all interfaces that the given object implements as Set
83 | */
84 | public static Set getAllInterfacesForClassAsSet(Class clazz, ClassLoader classLoader) {
85 | Preconditions.checkNotNull(clazz, "Class must not be null");
86 | if (clazz.isInterface() && isVisible(clazz, classLoader)) {
87 | return Collections.singleton(clazz);
88 | }
89 | Set interfaces = new LinkedHashSet();
90 | while (clazz != null) {
91 | Class>[] ifcs = clazz.getInterfaces();
92 | for (Class> ifc : ifcs) {
93 | interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader));
94 | }
95 | clazz = clazz.getSuperclass();
96 | }
97 | return interfaces;
98 | }
99 |
100 |
101 | /**
102 | * Check whether the given class is visible in the given ClassLoader.
103 | * @param clazz the class to check (typically an interface)
104 | * @param classLoader the ClassLoader to check against (may be null,
105 | * in which case this method will always return true)
106 | */
107 | public static boolean isVisible(Class> clazz, ClassLoader classLoader) {
108 | if (classLoader == null) {
109 | return true;
110 | }
111 | try {
112 | Class> actualClass = classLoader.loadClass(clazz.getName());
113 | return (clazz == actualClass);
114 | // Else: different interface class found...
115 | }
116 | catch (ClassNotFoundException ex) {
117 | // No interface class found...
118 | return false;
119 | }
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/HttpUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package com.bj58.argo.thirdparty;
23 |
24 | import javax.servlet.http.HttpServletRequest;
25 |
26 | /**
27 | * @author Service Platform Architecture Team (spat@58.com)
28 | */
29 | public class HttpUtils {
30 |
31 | /**
32 | * Part of HTTP content type header.
33 | */
34 | public static final String MULTIPART = "multipart/";
35 |
36 | /**
37 | * Utility method that determines whether the currentRequest contains multipart
38 | * content.
39 | *
40 | * @param request The servlet currentRequest to be evaluated. Must be non-null.
41 | *
42 | * @return true if the currentRequest is multipart;
43 | * false otherwise.
44 | *
45 | * @author Sean C. Sullivan
46 | */
47 | public static final boolean isMultipartContent(
48 | HttpServletRequest request) {
49 | if (!"post".equals(request.getMethod().toLowerCase())) {
50 | return false;
51 | }
52 | String contentType = request.getContentType();
53 | if (contentType == null) {
54 | return false;
55 | }
56 | if (contentType.toLowerCase().startsWith(MULTIPART)) {
57 | return true;
58 | }
59 | return false;
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/jetty/ByteArrayOutputStream2.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.thirdparty.jetty;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.nio.charset.Charset;
5 |
6 | /* ------------------------------------------------------------ */
7 | /** ByteArrayOutputStream with public internals
8 |
9 | *
10 | */
11 | public class ByteArrayOutputStream2 extends ByteArrayOutputStream
12 | {
13 | public ByteArrayOutputStream2(){super();}
14 | public ByteArrayOutputStream2(int size){super(size);}
15 | public byte[] getBuf(){return buf;}
16 | public int getCount(){return count;}
17 | public void setCount(int count){this.count = count;}
18 |
19 | public void reset(int minSize)
20 | {
21 | reset();
22 | if (buf.lengthm)
39 | return new String(buf,m,pos-m,StringUtil.__UTF8_CHARSET);
40 |
41 | return null;
42 | }
43 |
44 | if (b=='\r')
45 | {
46 | int p=pos;
47 |
48 | // if we have seen CRLF before, hungrily consume LF
49 | if (_seenCRLF && pos0)
94 | {
95 | _skipLF=false;
96 | if (_seenCRLF)
97 | {
98 | int b = super.read();
99 | if (b==-1)
100 | return -1;
101 |
102 | if (b!='\n')
103 | {
104 | buf[off]=(byte)(0xff&b);
105 | return 1+super.read(buf,off+1,len-1);
106 | }
107 | }
108 | }
109 |
110 | return super.read(buf,off,len);
111 | }
112 |
113 |
114 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/jetty/StringUtil.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.thirdparty.jetty;
2 |
3 | import java.nio.charset.Charset;
4 |
5 |
6 | /** Fast String Utilities.
7 | *
8 | * These string utilities provide both conveniance methods and
9 | * performance improvements over most standard library versions. The
10 | * main aim of the optimizations is to avoid object creation unless
11 | * absolutely required.
12 | *
13 | *
14 | */
15 | public class StringUtil
16 | {
17 | private final static StringMap CHARSETS= new StringMap(true);
18 |
19 | public static final String ALL_INTERFACES="0.0.0.0";
20 | public static final String CRLF="\015\012";
21 | public static final String __LINE_SEPARATOR=
22 | System.getProperty("line.separator","\n");
23 |
24 | public static final String __ISO_8859_1="ISO-8859-1";
25 | public final static String __UTF8="UTF-8";
26 | public final static String __UTF16="UTF-16";
27 |
28 | public final static Charset __UTF8_CHARSET;
29 | public final static Charset __ISO_8859_1_CHARSET;
30 | public final static Charset __UTF16_CHARSET;
31 |
32 | static
33 | {
34 | __UTF8_CHARSET=Charset.forName(__UTF8);
35 | __ISO_8859_1_CHARSET=Charset.forName(__ISO_8859_1);
36 | __UTF16_CHARSET=Charset.forName(__UTF16);
37 |
38 | CHARSETS.put("UTF-8",__UTF8);
39 | CHARSETS.put("UTF8",__UTF8);
40 | CHARSETS.put("UTF-16",__UTF16);
41 | CHARSETS.put("UTF16",__UTF16);
42 | CHARSETS.put("ISO-8859-1",__ISO_8859_1);
43 | CHARSETS.put("ISO_8859_1",__ISO_8859_1);
44 | }
45 |
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/jetty/Utf8StringBuffer.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.thirdparty.jetty;
2 | /* ------------------------------------------------------------ */
3 | /**
4 | * UTF-8 StringBuffer.
5 | *
6 | * This class wraps a standard {@link java.lang.StringBuffer} and provides methods to append
7 | * UTF-8 encoded bytes, that are converted into characters.
8 | *
9 | * This class is stateful and up to 4 calls to {@link #append(byte)} may be needed before
10 | * state a character is appended to the string buffer.
11 | *
12 | * The UTF-8 decoding is done by this class and no additional buffers or Readers are used.
13 | * The UTF-8 code was inspired by http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
14 | */
15 | public class Utf8StringBuffer extends Utf8Appendable
16 | {
17 | final StringBuffer _buffer;
18 |
19 | public Utf8StringBuffer()
20 | {
21 | super(new StringBuffer());
22 | _buffer = (StringBuffer)_appendable;
23 | }
24 |
25 | public Utf8StringBuffer(int capacity)
26 | {
27 | super(new StringBuffer(capacity));
28 | _buffer = (StringBuffer)_appendable;
29 | }
30 |
31 | @Override
32 | public int length()
33 | {
34 | return _buffer.length();
35 | }
36 |
37 | @Override
38 | public void reset()
39 | {
40 | super.reset();
41 | _buffer.setLength(0);
42 | }
43 |
44 | public StringBuffer getStringBuffer()
45 | {
46 | checkState();
47 | return _buffer;
48 | }
49 |
50 | @Override
51 | public String toString()
52 | {
53 | checkState();
54 | return _buffer.toString();
55 | }
56 | }
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/thirdparty/jetty/Utf8StringBuilder.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.thirdparty.jetty;
2 | /* ------------------------------------------------------------ */
3 | /** UTF-8 StringBuilder.
4 | *
5 | * This class wraps a standard {@link java.lang.StringBuilder} and provides methods to append
6 | * UTF-8 encoded bytes, that are converted into characters.
7 | *
8 | * This class is stateful and up to 4 calls to {@link #append(byte)} may be needed before
9 | * state a character is appended to the string buffer.
10 | *
11 | * The UTF-8 decoding is done by this class and no additional buffers or Readers are used.
12 | * The UTF-8 code was inspired by http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
13 | *
14 | */
15 | public class Utf8StringBuilder extends Utf8Appendable
16 | {
17 | final StringBuilder _buffer;
18 |
19 | public Utf8StringBuilder()
20 | {
21 | super(new StringBuilder());
22 | _buffer=(StringBuilder)_appendable;
23 | }
24 |
25 | public Utf8StringBuilder(int capacity)
26 | {
27 | super(new StringBuilder(capacity));
28 | _buffer=(StringBuilder)_appendable;
29 | }
30 |
31 | @Override
32 | public int length()
33 | {
34 | return _buffer.length();
35 | }
36 |
37 | @Override
38 | public void reset()
39 | {
40 | super.reset();
41 | _buffer.setLength(0);
42 | }
43 |
44 | public StringBuilder getStringBuilder()
45 | {
46 | checkState();
47 | return _buffer;
48 | }
49 |
50 | @Override
51 | public String toString()
52 | {
53 | checkState();
54 | return _buffer.toString();
55 | }
56 |
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/NetUtils.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils;
2 |
3 | import com.bj58.argo.ArgoException;
4 | import sun.net.util.IPAddressUtil;
5 |
6 | import java.net.InetAddress;
7 | import java.net.UnknownHostException;
8 |
9 | public class NetUtils {
10 |
11 | private NetUtils() {}
12 |
13 |
14 | /**
15 | * 将ip地址由文本转换为byte数组
16 | * @param ipText
17 | * @return
18 | */
19 | public static byte[] getDigitalFromText(String ipText) {
20 | byte[] ip = IPAddressUtil.textToNumericFormatV4(ipText);
21 |
22 | if (ip != null)
23 | return ip;
24 |
25 | ip = IPAddressUtil.textToNumericFormatV6(ipText);
26 |
27 | if (ip != null)
28 | return ip;
29 |
30 | throw ArgoException.raise(new UnknownHostException("[" + ipText + "]"));
31 | }
32 |
33 | public static InetAddress getInetAddressFromText(String ipText) {
34 | byte[] ip = getDigitalFromText(ipText);
35 |
36 | try {
37 | return InetAddress.getByAddress(ip);
38 | } catch (UnknownHostException e) {
39 | throw ArgoException.raise(new UnknownHostException("[" + ipText + "]"));
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/OnlyOnceCondition.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils;
2 |
3 | import com.bj58.argo.ArgoException;
4 |
5 | import java.util.concurrent.atomic.AtomicBoolean;
6 |
7 | /**
8 | * 保证只执行一次
9 | * @author renjun
10 | */
11 | public class OnlyOnceCondition {
12 |
13 | public static OnlyOnceCondition create(String message) {
14 | return new OnlyOnceCondition(message);
15 | }
16 |
17 | private final String message;
18 | private OnlyOnceCondition(String message) {
19 | this.message = message;
20 | }
21 |
22 | private final AtomicBoolean hasChecked = new AtomicBoolean(false);
23 | public void check() {
24 | if (!hasChecked.compareAndSet(false, true))
25 | throw ArgoException
26 | .newBuilder(message)
27 | .build();
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/Pair.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils;
2 |
3 | /**
4 | * @author renjun
5 | */
6 | public class Pair {
7 |
8 | public static Pair build(K k, V v) {
9 | return new Pair(k, v);
10 | }
11 |
12 | private final K key;
13 | private final V value;
14 |
15 | public Pair(K k, V v) {
16 | this.key = k;
17 | this.value = v;
18 | }
19 |
20 | public K getKey() {
21 | return this.key;
22 | }
23 |
24 | public V getValue() {
25 | return this.value;
26 | }
27 |
28 | public java.lang.String toString() {
29 | return "Pair " + key + ": " + value;
30 |
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/TouchTimer.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils;
2 |
3 | import java.util.concurrent.Executor;
4 | import java.util.concurrent.atomic.AtomicBoolean;
5 |
6 | /**
7 | *
8 | * 一个不需要定时线程的定时器,减少线程量
9 | *
10 | * @author renjun
11 | */
12 | public class TouchTimer {
13 |
14 | private final long interval;
15 |
16 | private final Runnable run;
17 |
18 | private final Executor executor;
19 |
20 | private volatile long lastTime = 0;
21 | private AtomicBoolean isRun = new AtomicBoolean(false);
22 |
23 | public static TouchTimer build(long interval, Runnable run, Executor executor) {
24 | return new TouchTimer(interval, run, executor);
25 | }
26 |
27 | public TouchTimer(long interval, Runnable run, Executor executor) {
28 | this.interval = interval;
29 | this.run = run;
30 | this.executor = executor;
31 | }
32 |
33 | public void touch() {
34 |
35 | long time = System.currentTimeMillis();
36 | if (isRun.get())
37 | return;
38 |
39 | if (time - lastTime < interval)
40 | return;
41 |
42 | execute();
43 |
44 | lastTime = time;
45 |
46 | }
47 |
48 | public void execute() {
49 |
50 | if(!isRun.compareAndSet(false, true))
51 | return;
52 |
53 | executor.execute(new Runnable() {
54 | @Override
55 | public void run() {
56 | immediateRun();
57 | }
58 | });
59 |
60 | }
61 |
62 | public void immediateRun() {
63 | try {
64 | if (isRun.get())
65 | return;
66 |
67 | executor.execute(run);
68 | } finally {
69 | lastTime = System.currentTimeMillis();
70 | isRun.set(false);
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/BooleanConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Boolean.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class BooleanConverter implements Converter {
9 |
10 | public Boolean convert(String s) {
11 | return Boolean.parseBoolean(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/ByteConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Byte.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class ByteConverter implements Converter {
9 |
10 | public Byte convert(String s) {
11 | return Byte.parseByte(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/CharacterConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Character.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class CharacterConverter implements Converter {
9 |
10 | public Character convert(String s) {
11 | if (s.length()==0)
12 | throw new IllegalArgumentException("Cannot convert empty string to char.");
13 | return s.charAt(0);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/Converter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to any given type.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | *
8 | * @param Generic type of converted result.
9 | */
10 | public interface Converter {
11 |
12 | /**
13 | * Convert a not-null String to specified object.
14 | */
15 | T convert(String s);
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/ConverterFactory.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | /**
7 | * Factory for all converters.
8 | *
9 | * fixMe: 可以改成注入
10 | *
11 | * @author Michael Liao (askxuefeng@gmail.com)
12 | */
13 | public class ConverterFactory {
14 |
15 | // private static final Log log = LogFactory.getLog(ConverterFactory.class);
16 |
17 | private Map, Converter>> map = new HashMap, Converter>>();
18 |
19 | public ConverterFactory() {
20 | loadInternal();
21 | }
22 |
23 | void loadInternal() {
24 |
25 | Converter> c = new StringConverter();
26 | map.put(String.class, c);
27 |
28 | c = new BooleanConverter();
29 | map.put(boolean.class, c);
30 | map.put(Boolean.class, c);
31 |
32 | c = new CharacterConverter();
33 | map.put(char.class, c);
34 | map.put(Character.class, c);
35 |
36 | c = new ByteConverter();
37 | map.put(byte.class, c);
38 | map.put(Byte.class, c);
39 |
40 | c = new ShortConverter();
41 | map.put(short.class, c);
42 | map.put(Short.class, c);
43 |
44 | c = new IntegerConverter();
45 | map.put(int.class, c);
46 | map.put(Integer.class, c);
47 |
48 | c = new LongConverter();
49 | map.put(long.class, c);
50 | map.put(Long.class, c);
51 |
52 | c = new FloatConverter();
53 | map.put(float.class, c);
54 | map.put(Float.class, c);
55 |
56 | c = new DoubleConverter();
57 | map.put(double.class, c);
58 | map.put(Double.class, c);
59 | }
60 |
61 |
62 | public boolean canConvert(Class> clazz) {
63 | return clazz.equals(String.class) || map.containsKey(clazz);
64 | }
65 |
66 | public Object convert(Class> clazz, String s) {
67 | Converter> c = map.get(clazz);
68 | return c.convert(s);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/DoubleConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Double.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class DoubleConverter implements Converter {
9 |
10 | public Double convert(String s) {
11 | return Double.parseDouble(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/FloatConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Float.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class FloatConverter implements Converter {
9 |
10 | public Float convert(String s) {
11 | return Float.parseFloat(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/IntegerConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Integer.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class IntegerConverter implements Converter {
9 |
10 | public Integer convert(String s) {
11 | return Integer.parseInt(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/LongConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Long.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class LongConverter implements Converter {
9 |
10 | public Long convert(String s) {
11 | return Long.parseLong(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/ShortConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Short.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class ShortConverter implements Converter {
9 |
10 | public Short convert(String s) {
11 | return Short.parseShort(s);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/StringConverter.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.utils.converter;
2 |
3 | /**
4 | * Convert String to Integer.
5 | *
6 | * @author Michael Liao (askxuefeng@gmail.com)
7 | */
8 | public class StringConverter implements Converter {
9 |
10 | public String convert(String s) {
11 | return s;
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/core/src/main/java/com/bj58/argo/utils/converter/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Package that contains all converters which convert java.lang.String to
3 | * specified object.
4 | */
5 | package com.bj58.argo.utils.converter;
6 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/ArgoDispatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo;
22 |
23 | import org.testng.annotations.Test;
24 |
25 | /**
26 | * @author Service Platform Architecture Team (spat@58.com)
27 | */
28 | public class ArgoDispatcherTest {
29 |
30 | // ArgoDispatcher dispatcher = ArgoDispatcher.instance;
31 | //
32 | // @Test
33 | // public void testInit() {
34 | // dispatcher.init(null);
35 | // }
36 | //
37 | // @Test(expectedExceptions = ArgoException.class)
38 | // public void testInitMore() {
39 | // dispatcher.init(null);
40 | // dispatcher.init(null);
41 | // }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/ArgoExceptionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo;
22 |
23 | import org.testng.annotations.Test;
24 | import org.testng.Assert;
25 |
26 | /**
27 | * @author Service Platform Architecture Team (spat@58.com)
28 | */
29 | public class ArgoExceptionTest {
30 |
31 | @Test
32 | public void testMessage() {
33 | ArgoException ex = ArgoException.newBuilder("This is a message.")
34 | .build();
35 |
36 | Assert.assertEquals("This is a message.", ex.getMessage());
37 | Assert.assertNull(ex.getCause());
38 | }
39 |
40 |
41 | @Test
42 | public void testCause() {
43 | Throwable ta = new RuntimeException("runtime exception.");
44 | ArgoException ex = ArgoException.newBuilder(ta).build();
45 | Assert.assertEquals("", ex.getMessage());
46 | Assert.assertEquals(ta, ex.getCause());
47 | }
48 |
49 | @Test
50 | public void testBoth() {
51 | Throwable ta = new RuntimeException();
52 | String message = "This is a message.";
53 |
54 | ArgoException ex = ArgoException.newBuilder(message, ta)
55 | .build();
56 |
57 | Assert.assertEquals(message, ex.getMessage());
58 | Assert.assertEquals(ta, ex.getCause());
59 |
60 | }
61 |
62 | @Test
63 | public void testContext() {
64 | ArgoException ex = ArgoException.newBuilder()
65 | .addContextVariable("url", "http://weibo.com/duoway/")
66 | .addContextVariable("email", "jun.ren@gmail.com")
67 | .build();
68 |
69 | Assert.assertEquals("\ncontext: {url=http://weibo.com/duoway/, email=jun.ren@gmail.com}", ex.getMessage());
70 |
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/ArgoFilterTest.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo;
2 |
3 | import static org.mockito.Mockito.mock;
4 |
5 | import javax.servlet.ServletException;
6 |
7 | import org.testng.annotations.Test;
8 |
9 |
10 | public class ArgoFilterTest {
11 |
12 | @Test
13 | public void testInit() throws ServletException {
14 | // FilterConfig filterConfig = mock(FilterConfig.class);
15 | //
16 | //// GroupConventionAnnotation groupConventionAnnotation = mock(GroupConventionAnnotation.class);
17 | //// when(groupConventionAnnotation.groupConfigFolder()).thenReturn("/opt/argo");
18 | //// when(groupConventionAnnotation.projectConfigFolder()).thenReturn("{groupConfigFolder}/{projectId}");
19 | //// when(groupConventionAnnotation.projectLogFolder()).thenReturn("{groupConfigFolder}/logs/{projectId}");
20 | //// when(groupConventionAnnotation.groupPackagesPrefix()).thenReturn("com.bj58.argo");
21 | //// when(groupConventionAnnotation.projectConventionClass()).thenReturn("com.bj58.argo.MyProjectConvention");
22 | //// when(groupConventionAnnotation.controllerPattern()).thenReturn("com\\.bj58\\..*\\.controllers\\..*Controller");
23 | //// when(groupConventionAnnotation.viewsFolder()).thenReturn("/WEB-INF/classes/views/");
24 | //// when(groupConventionAnnotation.staticResourcesFolder()).thenReturn("/WEB-INF/classes/static/");
25 | //
26 | // ServletContext servletContext = mock(ServletContext.class);
27 | // when(filterConfig.getServletContext()).thenReturn(servletContext);
28 | // when(servletContext.getContextPath()).thenReturn("/oss/web/post.58.com");
29 | //
30 | //
31 | // ArgoFilter argoFilter = new ArgoFilter();
32 | // argoFilter.init(filterConfig);
33 |
34 | }
35 |
36 |
37 |
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/ArgoTest.java:
--------------------------------------------------------------------------------
1 | ///*
2 | //* Copyright Beijing 58 Information Technology Co.,Ltd.
3 | //*
4 | //* Licensed to the Apache Software Foundation (ASF) under one
5 | //* or more contributor license agreements. See the NOTICE file
6 | //* distributed with this work for additional information
7 | //* regarding copyright ownership. The ASF licenses this file
8 | //* to you under the Apache License, Version 2.0 (the
9 | //* "License"); you may not use this file except in compliance
10 | //* with the License. You may obtain a copy of the License at
11 | //*
12 | //* http://www.apache.org/licenses/LICENSE-2.0
13 | //*
14 | //* Unless required by applicable law or agreed to in writing,
15 | //* software distributed under the License is distributed on an
16 | //* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | //* KIND, either express or implied. See the License for the
18 | //* specific language governing permissions and limitations
19 | //* under the License.
20 | //*/
21 | //package com.bj58.argo;
22 | //
23 | //import static org.mockito.Mockito.mock;
24 | //
25 | //import java.io.File;
26 | //
27 | //import javax.servlet.ServletContext;
28 | //
29 | //import org.testng.Assert;
30 | //import org.testng.annotations.Test;
31 | //
32 | //import com.bj58.argo.convention.GroupConvention;
33 | //
34 | ///**
35 | // * @author Service Platform Architecture Team (spat@58.com)
36 | // */
37 | //public class ArgoTest {
38 | //
39 | //// @Test(expectedExceptions = ArgoException.class
40 | //// , expectedExceptionsMessageRegExp = "Argo has been initialized.")
41 | //// public void testInitMore() {
42 | //// Argo.instance.init();
43 | //// }
44 | // @Test
45 | // public void testSingleton() {
46 | // Argo argo1 = Argo.instance;
47 | // Argo argo2 = Argo.instance;
48 | //
49 | // Assert.assertEquals(argo1, argo2);
50 | // }
51 | //
52 | // @Test(expectedExceptions=ArgoException.class,expectedExceptionsMessageRegExp="Argo has been initialized.")
53 | // public void testInitOnlyOnce() {
54 | // ServletContext servletContext = mock(ServletContext.class);
55 | // GroupConvention groupConvention = mock(GroupConvention.class);
56 | // Argo.instance.init(servletContext, groupConvention);
57 | // Argo.instance.init(servletContext, groupConvention);
58 | // }
59 | //
60 | // public static class MyGroupConvention implements GroupConvention {
61 | //
62 | // @Override
63 | // public File rootFolder() {
64 | // // TODO Auto-generated method stub
65 | // return null;
66 | // }
67 | //
68 | // @Override
69 | // public File logPath() {
70 | // // TODO Auto-generated method stub
71 | // return null;
72 | // }
73 | //
74 | // @Override
75 | // public File projectConfigFolder() {
76 | // // TODO Auto-generated method stub
77 | // return null;
78 | // }
79 | //
80 | // @Override
81 | // public String currentProjectId() {
82 | // // TODO Auto-generated method stub
83 | // return null;
84 | // }
85 | //
86 | // @Override
87 | // public void assemblyInject(AssemblyInjector injector) {
88 | // // TODO Auto-generated method stub
89 | //
90 | // }
91 | //
92 | // @Override
93 | // public File viewsFolder() {
94 | // // TODO Auto-generated method stub
95 | // return null;
96 | // }
97 | //
98 | // @Override
99 | // public File staticResourcesFolder() {
100 | // // TODO Auto-generated method stub
101 | // return null;
102 | // }
103 | //
104 | // }
105 | //}
106 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/HadesModuleTest.java:
--------------------------------------------------------------------------------
1 | ///*
2 | //* Copyright Beijing 58 Information Technology Co.,Ltd.
3 | //*
4 | //* Licensed to the Apache Software Foundation (ASF) under one
5 | //* or more contributor license agreements. See the NOTICE file
6 | //* distributed with this work for additional information
7 | //* regarding copyright ownership. The ASF licenses this file
8 | //* to you under the Apache License, Version 2.0 (the
9 | //* "License"); you may not use this file except in compliance
10 | //* with the License. You may obtain a copy of the License at
11 | //*
12 | //* http://www.apache.org/licenses/LICENSE-2.0
13 | //*
14 | //* Unless required by applicable law or agreed to in writing,
15 | //* software distributed under the License is distributed on an
16 | //* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | //* KIND, either express or implied. See the License for the
18 | //* specific language governing permissions and limitations
19 | //* under the License.
20 | //*/
21 | //package com.bj58.argo;
22 | //
23 | //import BeatContext;
24 | //import GroupConvention;
25 | //import AssemblyInjector;
26 | //import ArgoModule;
27 | //import com.google.inject.Injector;
28 | //import org.testng.Assert;
29 | //import org.testng.annotations.Test;
30 | //
31 | //import javax.servlet.http.HttpServletRequest;
32 | //import java.io.File;
33 | //
34 | //import static org.mockito.Mockito.*;
35 | //
36 | ///**
37 | // * @author Service Platform Architecture Team (spat@58.com)
38 | // */
39 | //public class HadesModuleTest {
40 | //
41 | // @Test
42 | // public void testModule() {
43 | //
44 | // Argo argo = mock(Argo.class);
45 | // GroupConvention groupConvention = mock(GroupConvention.class);
46 | // BeatContext beatContext = mock(BeatContext.class);
47 | //
48 | // HadesModule module = new HadesModule(argo, groupConvention);
49 | //
50 | // Injector injector = module.getInjector();
51 | //
52 | // when(argo.beatContext()).thenReturn(beatContext);
53 | //
54 | // HttpServletRequest request = mock(HttpServletRequest.class);
55 | // when(argo.request()).thenReturn(request);
56 | //
57 | // Assert.assertEquals(module.provideBeatContext(), beatContext);
58 | // Assert.assertEquals(module.provideHttpServletRequest(), request);
59 | //
60 | //
61 | // BeatContext actualBeat = injector.getInstance(BeatContext.class);
62 | // Assert.assertEquals(actualBeat, beatContext);
63 | //
64 | // }
65 | //
66 | //
67 | //
68 | //
69 | // GroupConvention groupConvention = new GroupConvention() {
70 | // @Override
71 | // public File groupConfigFolder() {
72 | // return new File("/opt/argo");
73 | // }
74 | //
75 | // @Override
76 | // public File projectLogFolder() {
77 | // return new File("/opt/argo/logs");
78 | // }
79 | //
80 | // @Override
81 | // public File projectConfigFolder() {
82 | // return new File("/opt/argo/me");
83 | // }
84 | //
85 | // @Override
86 | // public String currentProjectId() {
87 | // return "myprojectId";
88 | // }
89 | //
90 | // @Override
91 | // public void assemblyInject(AssemblyInjector injector) {
92 | //
93 | // }
94 | //
95 | // @Override
96 | // public File viewsFolder() {
97 | // return new File("/opt/web/my/WEB-INF/classes/create");
98 | // }
99 | //
100 | // @Override
101 | // public File staticResourcesFolder() {
102 | // return new File("/opt/web/my/WEB-INF/classes/static");
103 | // }
104 | // };
105 | //}
106 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/client/IpAddressUtilTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.client;
22 |
23 |
24 | import com.google.common.net.InetAddresses;
25 | import org.testng.Assert;
26 | import org.testng.annotations.Test;
27 | import sun.net.util.IPAddressUtil;
28 |
29 | import java.net.Inet4Address;
30 | import java.net.InetAddress;
31 | import java.net.UnknownHostException;
32 |
33 | /**
34 | * @author Service Platform Architecture Team (spat@58.com)
35 | */
36 | public class IpAddressUtilTest {
37 |
38 | @Test
39 | public void testIpV4() {
40 |
41 | byte[] ip = IPAddressUtil.textToNumericFormatV4("211.151.115.9");
42 |
43 | Assert.assertEquals(ip, new byte[]{211 - 256, 151 - 256, 115, 9});
44 |
45 | ip = IPAddressUtil.textToNumericFormatV4("bj.58.com");
46 |
47 | Assert.assertNull(ip);
48 | }
49 |
50 | @Test
51 | public void test() {
52 |
53 | }
54 |
55 | @Test
56 | public void testIsPrivateAddress() throws UnknownHostException {
57 | // byte[] rawAddress = { 10, 0, 0, 5 };
58 | //
59 | // Inet4Address inet4Address = (Inet4Address) InetAddress.getByAddress(rawAddress);
60 | //
61 | // System.out.println(inet4Address.isAnyLocalAddress());
62 | // System.out.println(inet4Address.isSiteLocalAddress());
63 | //
64 | // rawAddress = new byte[] { 192 - 256, 168 - 256, 0, 5 };
65 | //
66 | // inet4Address = (Inet4Address) InetAddress.getByAddress(rawAddress);
67 | //
68 | // System.out.println(inet4Address.isAnyLocalAddress());
69 | // System.out.println(inet4Address.isSiteLocalAddress());
70 | //
71 | // rawAddress = new byte[] { 211 - 256, 151 - 256, 70, 5 };
72 | //
73 | // inet4Address = (Inet4Address) InetAddress.getByAddress(rawAddress);
74 | //
75 | // System.out.println(inet4Address.isAnyLocalAddress());
76 | // System.out.println(inet4Address.isSiteLocalAddress());
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/convention/GroupConventionAnnotationTest.java:
--------------------------------------------------------------------------------
1 | //package com.bj58.argo.convention;
2 | //
3 | //import org.testng.Assert;
4 | //
5 | //import org.testng.annotations.Test;
6 | //
7 | //public class GroupConventionAnnotationTest {
8 | //
9 | // //采用注解默认值
10 | // @GroupConventionAnnotation
11 | // public static class AnnotationDefaultClass {
12 | // }
13 | //
14 | // @Test(description="default")
15 | // public void testDefaultAnnotation() {
16 | // GroupConventionAnnotation ga = AnnotationDefaultClass.class.getAnnotation(GroupConventionAnnotation.class);
17 | // Assert.assertEquals(ga.groupConfigFolder(),"/opt/argo");
18 | // Assert.assertEquals(ga.projectConfigFolder(),"{groupConfigFolder}/{projectId}");
19 | // Assert.assertEquals(ga.projectLogFolder(),"{groupConfigFolder}/logs/{projectId}");
20 | // Assert.assertEquals(ga.groupPackagesPrefix(),"com.bj58.argo");
21 | // Assert.assertEquals(ga.projectConventionClass(),"com.bj58.argo.MyProjectConvention");
22 | // Assert.assertEquals(ga.controllerPattern(),"com\\.bj58\\..*\\.controllers\\..*Controller");
23 | // Assert.assertEquals(ga.viewsFolder(),"/WEB-INF/classes/views/");
24 | // Assert.assertEquals(ga.staticResourcesFolder(),"/WEB-INF/classes/static/");
25 | // }
26 | //
27 | // //采用注解自定义值
28 | // @GroupConventionAnnotation(
29 | // groupConfigFolder ="rootFolderCustom",
30 | // projectConfigFolder ="configFolderCustom",
31 | // projectLogFolder ="logPathCustom",
32 | // groupPackagesPrefix ="packagesPrefixCustom",
33 | // projectConventionClass="projectConventionClassCustom",
34 | // controllerPattern="controllerPatternCustom",
35 | // viewsFolder="viewsFolderCustom",
36 | // staticResourcesFolder="staticResourcesFolderCustom"
37 | // )
38 | // public static class AnnotationCustomClass {
39 | //
40 | // }
41 | //
42 | // @Test(description="custom")
43 | // public void testCustomAnnotation() {
44 | // GroupConventionAnnotation ga = AnnotationCustomClass.class.getAnnotation(GroupConventionAnnotation.class);
45 | // Assert.assertEquals(ga.groupConfigFolder(),"rootFolderCustom");
46 | // Assert.assertEquals(ga.projectConfigFolder(),"configFolderCustom");
47 | // Assert.assertEquals(ga.projectLogFolder(),"logPathCustom");
48 | // Assert.assertEquals(ga.groupPackagesPrefix(),"packagesPrefixCustom");
49 | // Assert.assertEquals(ga.projectConventionClass(),"projectConventionClassCustom");
50 | // Assert.assertEquals(ga.controllerPattern(),"controllerPatternCustom");
51 | // Assert.assertEquals(ga.viewsFolder(),"viewsFolderCustom");
52 | // Assert.assertEquals(ga.staticResourcesFolder(),"staticResourcesFolderCustom");
53 | // }
54 | //
55 | //
56 | //
57 | //
58 | //}
59 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/learn/ClassTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.learn;
22 |
23 | import org.testng.Assert;
24 | import org.testng.annotations.Test;
25 |
26 | import java.lang.annotation.Annotation;
27 | import java.lang.reflect.Field;
28 | import java.lang.reflect.Method;
29 |
30 | /**
31 | * @author Service Platform Architecture Team (spat@58.com)
32 | */
33 | public class ClassTest {
34 |
35 | private String s = "abc";
36 |
37 | @Test
38 | public void testAnnotation() {
39 | Annotation[] annotations = ClassTest.class.getAnnotations();
40 |
41 | Assert.assertNotNull(annotations);
42 | Assert.assertEquals(0, annotations.length);
43 |
44 | Method[] methods = ClassTest.class.getMethods();
45 |
46 | for (Method method : methods) {
47 | annotations = method.getAnnotations();
48 |
49 | Assert.assertNotNull(annotations);
50 | // Assert.assertEquals(0, annotations.length);
51 | }
52 | }
53 |
54 | @Test
55 | public void testField() throws Exception {
56 |
57 | Class clazz = ClassTest.class;
58 | for(Field field : clazz.getDeclaredFields()) {
59 | if ("s".equals(field.getName())) {
60 | field.setAccessible(true);
61 | field.set(this, "xxxxxxxxxxxxxxxxxx");
62 | }
63 | }
64 |
65 | System.out.println(this.s);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/learn/FileTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.learn;
22 |
23 | import org.testng.annotations.Test;
24 |
25 | import java.io.File;
26 |
27 | /**
28 | * @author Service Platform Architecture Team (spat@58.com)
29 | */
30 | public class FileTest {
31 |
32 | @Test
33 | public void testContain() {
34 | File src = new File("D:\\新建文件夹\\facade\\");
35 | File src1 = new File("D:\\新建文件夹\\facade");
36 | File dest = new File("D:/新建文件夹/facade/新建文件夹/aa");
37 | File dest1 = new File("D:/新建文件夹/facadeXXXX/新建文件夹/aa");
38 |
39 | System.out.println(dest.getAbsolutePath().indexOf(src.getAbsolutePath()) == 0);
40 | System.out.println(dest1.getAbsolutePath().indexOf(src.getAbsolutePath()) == 0);
41 |
42 | System.out.println(src.getAbsolutePath());
43 | System.out.println(src1.getAbsolutePath());
44 | System.out.println(dest.getAbsolutePath());
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/learn/injector/InjectTest.java:
--------------------------------------------------------------------------------
1 | ///*
2 | //* Copyright Beijing 58 Information Technology Co.,Ltd.
3 | //*
4 | //* Licensed to the Apache Software Foundation (ASF) under one
5 | //* or more contributor license agreements. See the NOTICE file
6 | //* distributed with this work for additional information
7 | //* regarding copyright ownership. The ASF licenses this file
8 | //* to you under the Apache License, Version 2.0 (the
9 | //* "License"); you may not use this file except in compliance
10 | //* with the License. You may obtain a copy of the License at
11 | //*
12 | //* http://www.apache.org/licenses/LICENSE-2.0
13 | //*
14 | //* Unless required by applicable law or agreed to in writing,
15 | //* software distributed under the License is distributed on an
16 | //* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | //* KIND, either express or implied. See the License for the
18 | //* specific language governing permissions and limitations
19 | //* under the License.
20 | //*/
21 | //package com.bj58.argo.learn.injector;
22 | //
23 | //import ArgoController;
24 | //import HadesModule;
25 | //import com.google.common.collect.Lists;
26 | //import com.google.common.collect.Sets;
27 | //import com.google.inject.Guice;
28 | //import com.google.inject.Injector;
29 | //import org.testng.annotations.Test;
30 | //
31 | ///**
32 | // * @author Service Platform Architecture Team (spat@58.com)
33 | // */
34 | //public class InjectTest {
35 | //
36 | // @Test
37 | // public void test() {
38 | // Injector injector = Guice.createInjector(new HadesModule(Sets.>newLinkedHashSet()));
39 | //
40 | // C c = injector.getInstance(C.class);
41 | // c.test();
42 | // }
43 | //
44 | // public static class C {
45 | //
46 | // public void test(){
47 | // System.out.println("aaa");
48 | // }
49 | //
50 | // }
51 | //}
52 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/learn/injector/ProvidersTest.java:
--------------------------------------------------------------------------------
1 | ///*
2 | //* Copyright Beijing 58 Information Technology Co.,Ltd.
3 | //*
4 | //* Licensed to the Apache Software Foundation (ASF) under one
5 | //* or more contributor license agreements. See the NOTICE file
6 | //* distributed with this work for additional information
7 | //* regarding copyright ownership. The ASF licenses this file
8 | //* to you under the Apache License, Version 2.0 (the
9 | //* "License"); you may not use this file except in compliance
10 | //* with the License. You may obtain a copy of the License at
11 | //*
12 | //* http://www.apache.org/licenses/LICENSE-2.0
13 | //*
14 | //* Unless required by applicable law or agreed to in writing,
15 | //* software distributed under the License is distributed on an
16 | //* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | //* KIND, either express or implied. See the License for the
18 | //* specific language governing permissions and limitations
19 | //* under the License.
20 | //*/
21 | //package com.bj58.argo.learn.injector;
22 | //
23 | //import com.google.inject.AbstractModule;
24 | //import com.google.inject.Guice;
25 | //import com.google.inject.Injector;
26 | //import com.google.inject.Provides;
27 | //import org.testng.annotations.Test;
28 | //
29 | //import javax.inject.Inject;
30 | //import javax.inject.Singleton;
31 | //
32 | ///**
33 | // * @author Service Platform Architecture Team (spat@58.com)
34 | // */
35 | //public class ProvidersTest {
36 | //
37 | // public final static Injector injector = Guice.createInjector(new MyModule());
38 | //
39 | //
40 | // @Test
41 | // public void test() {
42 | // C1 c1 = injector.getInstance(C1.class);
43 | //
44 | // System.out.println("QQQQQQQQQQQQQQQQQQQQQQ");
45 | // System.out.println(c1.c3().c2());
46 | // }
47 | //
48 | //
49 | //
50 | // public static class MyModule extends AbstractModule {
51 | //
52 | //
53 | //
54 | // @Override
55 | // protected void configure() {
56 | //
57 | //
58 | // }
59 | // }
60 | //
61 | // public static class C1 {
62 | //
63 | // public C3 c3() {
64 | // return injector.getInstance(C3.class);
65 | // }
66 | //
67 | //
68 | // @Provides
69 | // @Singleton
70 | // public C2 providerC2() {
71 | // System.out.println("provider C2 1");
72 | // C2 c2 = new C2();
73 | // System.out.println("provider C2 2");
74 | //
75 | // return c2;
76 | // }
77 | //
78 | //
79 | // }
80 | //
81 | // public static class C2 {
82 | //
83 | // public C2() {
84 | // System.out.println("XXXXXXXXXXXXXX");
85 | // }
86 | //
87 | // final long time = System.currentTimeMillis();
88 | //
89 | // public long getTime() {
90 | // return time;
91 | // }
92 | //
93 | // }
94 | //
95 | //
96 | // public static class C3 {
97 | //
98 | // private final C2 c2;
99 | //
100 | // @Inject
101 | // public C3(C2 c2) {
102 | // System.out.println("CCCCCC3");
103 | // this.c2 = c2;
104 | // }
105 | //
106 | // public C2 c2() {
107 | // return c2;
108 | // }
109 | //
110 | // }
111 | //
112 | //}
113 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/logs/LoggerTest.java:
--------------------------------------------------------------------------------
1 | ///*
2 | //* Copyright Beijing 58 Information Technology Co.,Ltd.
3 | //*
4 | //* Licensed to the Apache Software Foundation (ASF) under one
5 | //* or more contributor license agreements. See the NOTICE file
6 | //* distributed with this work for additional information
7 | //* regarding copyright ownership. The ASF licenses this file
8 | //* to you under the Apache License, Version 2.0 (the
9 | //* "License"); you may not use this file except in compliance
10 | //* with the License. You may obtain a copy of the License at
11 | //*
12 | //* http://www.apache.org/licenses/LICENSE-2.0
13 | //*
14 | //* Unless required by applicable law or agreed to in writing,
15 | //* software distributed under the License is distributed on an
16 | //* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | //* KIND, either express or implied. See the License for the
18 | //* specific language governing permissions and limitations
19 | //* under the License.
20 | //*/
21 | //package com.bj58.argo.logs;
22 | //
23 | //import com.google.inject.*;
24 | //import com.google.inject.matcher.Matchers;
25 | //import org.testng.annotations.Test;
26 | //
27 | //import javax.inject.Inject;
28 | //
29 | ///**
30 | // * @author Service Platform Architecture Team (spat@58.com)
31 | // */
32 | //public class LoggerTest {
33 | //
34 | // private Module module = new AbstractModule() {
35 | // @Override
36 | // protected void configure() {
37 | // this.bindListener(Matchers.any(), new LoggerTypeListener());
38 | // }
39 | // };
40 | //
41 | // @Test
42 | // public void test() {
43 | // Injector injector = Guice.createInjector(module);
44 | //
45 | // CI c = injector.getInstance(CI.class);
46 | //
47 | // c.test();
48 | //
49 | //
50 | // }
51 | //
52 | // @ImplementedBy(C.class)
53 | // public static interface CI {
54 | // void test();
55 | // }
56 | //
57 | // public static class C implements CI{
58 | // private Logger logger;
59 | //
60 | //
61 | //// private final Logger logger2;
62 | ////
63 | //// @Inject
64 | //// public C (Logger logger2) {
65 | //// this.logger2 = logger2;
66 | //// }
67 | //
68 | // public void test() {
69 | // System.out.println("log1");
70 | // System.out.println(logger.toString());
71 | // System.out.println("log2");
72 | //// System.out.println(logger2.toString());
73 | // }
74 | // }
75 | //
76 | //// private static class MyModule extends AbstractModule {
77 | ////
78 | //// @Override
79 | //// protected void configure() {
80 | ////
81 | //// }
82 | ////
83 | //// public
84 | //// }
85 | //}
86 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/route/PathMatcherTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.route;
22 |
23 | import com.bj58.argo.thirdparty.AntPathMatcher;
24 | import com.bj58.argo.thirdparty.PathMatcher;
25 | import org.testng.Assert;
26 | import org.testng.annotations.Test;
27 |
28 | import java.util.Map;
29 |
30 | /**
31 | * @author Service Platform Architecture Team (spat@58.com)
32 | */
33 | public class PathMatcherTest {
34 |
35 | @Test
36 | public void test() {
37 | PathMatcher pathMatcher = new AntPathMatcher();
38 |
39 | String pattern = "/{a}/{b}/{c}";
40 |
41 | String sample = "/aa/bb/cc";
42 |
43 | Assert.assertTrue(pathMatcher.match(pattern, sample));
44 |
45 | }
46 |
47 |
48 | @Test
49 | public void testPathMatch(){
50 | PathMatcher pathMatcher = new AntPathMatcher();
51 | String registeredPath = "/me/hello/{name}";
52 | String url = "/me/hello/renjun";
53 | Assert.assertTrue(pathMatcher.match(registeredPath, url));
54 |
55 | Map values = pathMatcher.extractUriTemplateVariables(registeredPath, url);
56 | Assert.assertEquals(1, values.size());
57 | Assert.assertEquals("renjun", values.get("name"));
58 |
59 | System.out.println("OK testpathMatch");
60 |
61 |
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/test/TestBeatContextProvider.java:
--------------------------------------------------------------------------------
1 | package com.bj58.argo.test;
2 |
3 | import com.google.common.collect.ImmutableMap;
4 |
5 | import java.util.Map;
6 |
7 | public class TestBeatContextProvider {
8 |
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/core/src/test/java/com/bj58/argo/utils/OnlyOnceConditionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo.utils;
22 |
23 | import com.bj58.argo.ArgoException;
24 | import org.testng.annotations.Test;
25 |
26 | /**
27 | * @author Service Platform Architecture Team (spat@58.com)
28 | */
29 | public class OnlyOnceConditionTest {
30 |
31 | @Test
32 | public void testOnlyOnce(){
33 | OnlyOnceCondition onlyOnce = OnlyOnceCondition.create("This is a test.");
34 |
35 | onlyOnce.check();
36 |
37 |
38 | }
39 |
40 | @Test(expectedExceptions = ArgoException.class)
41 | public void testMore(){
42 | OnlyOnceCondition onlyOnce = OnlyOnceCondition.create("This is a test.");
43 |
44 | onlyOnce.check();
45 |
46 | onlyOnce.check();
47 |
48 |
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/ExceptionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package learn;
22 |
23 | import org.testng.annotations.Test;
24 |
25 | /**
26 | * @author Service Platform Architecture Team (spat@58.com)
27 | */
28 | public class ExceptionTest {
29 |
30 | @Test
31 | public void test() {
32 | Exception e1 = new Exception("e1");
33 | // System.out.println("e1==================================");
34 | // e1.printStackTrace();
35 |
36 | // System.out.println("e2==================================");
37 |
38 | Exception e2 = new Exception("e2",e1);
39 | // e2.printStackTrace();
40 |
41 | // System.out.println("e3==================================");
42 |
43 | Exception e3 = new MyException(e1);
44 | e3.printStackTrace();
45 | }
46 |
47 | private static class MyException extends Exception {
48 | public MyException(Throwable e) {
49 | // super("My exception's message", e, false, false);
50 | }
51 | }
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/ExecuteLearn.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package learn;
22 |
23 | import java.util.concurrent.CompletionService;
24 | import java.util.concurrent.Future;
25 | import java.util.concurrent.FutureTask;
26 |
27 | /**
28 | * @author Service Platform Architecture Team (spat@58.com)
29 | */
30 | public class ExecuteLearn {
31 |
32 | public void testFuture() {
33 | CompletionService cs ;
34 | Future future;
35 | FutureTask ft;
36 |
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/FileTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package learn;
23 |
24 | import java.io.File;
25 | import org.junit.Test;
26 |
27 | /**
28 | * @author Service Platform Architecture Team (spat@58.com)
29 | */
30 | public class FileTest {
31 |
32 | @Test
33 | public void testFile() {
34 | String path = "/temp";
35 |
36 | String path1 = path;
37 | File file = new File(path1);
38 |
39 | System.out.println(file.isAbsolute());
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/HashmapTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package learn;
23 |
24 | import java.util.BitSet;
25 | import java.util.HashMap;
26 | import java.util.Map;
27 |
28 | import org.junit.Test;
29 |
30 | /**
31 | * @author Service Platform Architecture Team (spat@58.com)
32 | */
33 | public class HashmapTest {
34 |
35 | @Test
36 | public void testTime() {
37 | long time = System.currentTimeMillis();
38 |
39 | System.out.println(time);
40 |
41 |
42 | }
43 |
44 | @Test
45 | public void testadd() {
46 | Map m = new HashMap();
47 | m.put(100, "a");
48 | m.put(200, "b");
49 |
50 |
51 | for(Map.Entry entry : m.entrySet()) {
52 | System.out.println(entry.getValue());
53 | }
54 | }
55 |
56 | // public void testMethod() {
57 | // CA ca = null;
58 | // }
59 | //
60 | // public static class CA {
61 | // public String a() {
62 | // return null;
63 | // }
64 | // }
65 | //
66 | }
67 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/InjectorLearn.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package learn;
22 |
23 | import com.google.inject.*;
24 | import com.google.inject.spi.TypeEncounter;
25 | import com.google.inject.spi.TypeListener;
26 | import org.testng.annotations.Test;
27 |
28 | import javax.inject.Inject;
29 | import java.lang.reflect.Constructor;
30 |
31 | /**
32 | * @author Service Platform Architecture Team (spat@58.com)
33 | */
34 | public class InjectorLearn {
35 |
36 |
37 | public static class MyTypeListener implements TypeListener {
38 |
39 | @Override
40 | public void hear(TypeLiteral type, TypeEncounter encounter) {
41 |
42 |
43 | Class> clazz = type.getRawType();
44 |
45 | if (My.class != clazz)
46 | return;
47 |
48 | Constructor>[] cs = clazz.getConstructors();
49 |
50 | Constructor c = null;
51 | for(Constructor> cn : cs) {
52 | if (cn.getAnnotation(Inject.class) != null) {
53 | c = cn;
54 | break;
55 | }
56 | }
57 |
58 | if (c == null)
59 | return;
60 |
61 |
62 | Class>[] fs = c.getParameterTypes();
63 | encounter.register(new MembersInjector() {
64 | @Override
65 | public void injectMembers(I instance) {
66 | long time = System.currentTimeMillis();
67 | System.out.println("a" + time);
68 | }
69 | });
70 | }
71 | }
72 |
73 |
74 | public static class My {
75 | private String a1;
76 |
77 | @Inject
78 | public My(String a) {
79 | this.a1 = a;
80 | }
81 |
82 | public void test() {
83 | System.out.println(a1);
84 | }
85 | }
86 |
87 | @Test
88 | public void testMy1() {
89 | Injector injector = Guice.createInjector(new Module() {
90 | @Override
91 | public void configure(Binder binder) {
92 |
93 | binder.bind(String.class).toInstance("xyz");
94 |
95 | }
96 | });
97 |
98 | My1 my1 = injector.getInstance(My1.class);
99 | my1.test();
100 |
101 | My my = injector.getInstance(My.class);
102 | my.test();
103 | }
104 |
105 | public static class My1 {
106 | private String a;
107 |
108 |
109 | public My1() {
110 | long time = System.currentTimeMillis();
111 | a = String.valueOf(time);
112 | }
113 |
114 | public void test() {
115 | System.out.println(a);
116 |
117 | Thread.currentThread().getStackTrace();
118 | }
119 | }
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/LearnJssist.java:
--------------------------------------------------------------------------------
1 | package learn;
2 |
3 | import java.lang.reflect.Method;
4 | import java.lang.reflect.Modifier;
5 |
6 | import javassist.ClassClassPath;
7 | import javassist.ClassPool;
8 | import javassist.CtClass;
9 | import javassist.CtMethod;
10 | import javassist.NotFoundException;
11 | import javassist.bytecode.CodeAttribute;
12 | import javassist.bytecode.LocalVariableAttribute;
13 | import javassist.bytecode.MethodInfo;
14 |
15 | import org.junit.Test;
16 |
17 |
18 |
19 |
20 | public class LearnJssist {
21 |
22 |
23 | /**
24 | * 得到方法参数名称数组
25 | * 由于java没有提供获得参数名称的api,利用了javassist来实现
26 | * @return
27 | */
28 | public String[] getMethodParamNames(Class> clazz, String methodName) {
29 | Method method =null;
30 | try {
31 | ClassPool pool = ClassPool.getDefault();
32 |
33 |
34 | pool.insertClassPath(new ClassClassPath(clazz));
35 |
36 | CtClass cc = pool.get(clazz.getName());
37 |
38 | //DEBUG, 函数名相同的方法重载的信息读不到 2011-03-21
39 | CtMethod cm = cc.getDeclaredMethod(method.getName());
40 |
41 | //2011-03-21
42 | // String[] paramTypeNames = new String[method.getParameterTypes().length];
43 | // for (int i = 0; i < paramTypes.length; i++)
44 | // paramTypeNames[i] = paramTypes[i].getName();
45 | // CtMethod cm = cc.getDeclaredMethod(method.getName(), pool.get(new String[] {}));
46 |
47 | // 使用javaassist的反射方法获取方法的参数名
48 | MethodInfo methodInfo = cm.getMethodInfo();
49 |
50 | CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
51 | LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute
52 | .getAttribute(LocalVariableAttribute.tag);
53 | if (attr == null) {
54 | throw new RuntimeException("class:"+clazz.getName()
55 | +", have no LocalVariableTable, please use javac -g:{vars} to compile the source file");
56 | }
57 |
58 | // for(int i = 0 ; i< attr.length() ; i++){
59 | // System.out.println(i);
60 | // try {
61 | // System.out.println("===="+attr.nameIndex(i));
62 | // System.out.println("===="+attr.index(i));
63 | //// System.out.println("===="+attr.nameIndex(i));
64 | // System.out.println(clazz.getName()+"================"+i+attr.variableName(i));
65 | //
66 | //
67 | // } catch (Exception e) {
68 | // // TODO Auto-generated catch block
69 | // e.printStackTrace();
70 | // }
71 | // }
72 | //addContextVariable by lzw 用于兼容jdk 编译时 LocalVariableTable顺序问题
73 | int startIndex = getStartIndex(attr);
74 | String[] paramNames = new String[cm.getParameterTypes().length];
75 | int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
76 |
77 | for (int i = 0; i < paramNames.length; i++)
78 | paramNames[i] = attr.variableName(startIndex + i + pos);
79 | // paramNames即参数名
80 | for (int i = 0; i < paramNames.length; i++) {
81 | System.out.println(paramNames[i]);
82 | }
83 |
84 | return paramNames;
85 |
86 | } catch (NotFoundException e) {
87 | e.printStackTrace();
88 | return new String[0];
89 | }
90 | }
91 |
92 | private int getStartIndex(LocalVariableAttribute attr){
93 |
94 | // attr.st
95 |
96 | int startIndex = 0;
97 | for(int i = 0 ; i< attr.length() ; i++){
98 | if("this".equals(attr.variableName(i))){
99 | startIndex = i;
100 | break;
101 | }
102 | }
103 | return startIndex;
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/MapTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package learn;
23 |
24 | import com.google.common.collect.Maps;
25 | import org.junit.Test;
26 |
27 | import java.util.Map;
28 |
29 | /**
30 | * @author Service Platform Architecture Team (spat@58.com)
31 | */
32 | public class MapTest {
33 |
34 | @Test
35 | public void testToString() {
36 | Map map = Maps.newLinkedHashMap();
37 |
38 | map.put("a", 1);
39 | map.put("b", "x");
40 | map.put("c", true);
41 |
42 | System.out.println(map.toString());
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/MyClassLoader1.java:
--------------------------------------------------------------------------------
1 | package learn;
2 |
3 | import java.io.ByteArrayOutputStream;
4 | import java.io.FileInputStream;
5 | import java.io.FileNotFoundException;
6 | import java.io.IOException;
7 | import java.nio.ByteBuffer;
8 | import java.nio.channels.Channels;
9 | import java.nio.channels.FileChannel;
10 | import java.nio.channels.WritableByteChannel;
11 |
12 | /**
13 | * @author haojian
14 | *
15 | */
16 | public class MyClassLoader1 extends ClassLoader {
17 |
18 | public MyClassLoader1(ClassLoader parent) {
19 | super(parent);
20 | }
21 |
22 | public Class> findClass1(String name) throws Exception {
23 | byte[] bytes = loadClassBytes(name);
24 | Class theClass = defineClass(null, bytes, 0, bytes.length);
25 | if (theClass == null)
26 | throw new ClassFormatError();
27 | return theClass;
28 | }
29 |
30 | private byte[] loadClassBytes(String classFile) throws Exception {
31 | // String classFile = getClassFile();
32 | FileInputStream fis = new FileInputStream(classFile);
33 | FileChannel fileC = fis.getChannel();
34 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
35 | WritableByteChannel outC = Channels.newChannel(baos);
36 | ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
37 | while (true) {
38 | int i = fileC.read(buffer);
39 | if (i == 0 || i == -1) {
40 | break;
41 | }
42 | buffer.flip();
43 | outC.write(buffer);
44 | buffer.clear();
45 | }
46 | fis.close();
47 | return baos.toByteArray();
48 |
49 | }
50 |
51 |
52 | }
53 |
54 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/PokerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 |
22 | package learn;
23 |
24 | import junit.framework.Assert;
25 | import org.junit.Test;
26 |
27 | import java.util.ArrayList;
28 | import java.util.List;
29 | import java.util.Random;
30 |
31 | /**
32 | * @author Service Platform Architecture Team (spat@58.com)
33 | */
34 | public class PokerTest {
35 | Random rdm = new Random(System.currentTimeMillis());
36 |
37 | @Test
38 | public void test() {
39 | int count = 0;
40 | for(int i = 1; i < 100 * 100 * 100 * 100; i++) {
41 | count += occasion() ? 1 : 0;
42 | if (i % 1000000 == 0)
43 | System.out.println("i:" + i + ", count: " + count + ", " + (count * 1.0)/i);
44 |
45 | }
46 |
47 | System.out.println("i:" + 100 * 100 * 100 + ", count: " + count + ", " + (count * 1.0)/(100 * 100 * 100));
48 | }
49 |
50 | public boolean occasion() {
51 | List origin = new ArrayList(54);
52 | for(int i = 1; i <= 54; i++)
53 | origin.add(i);
54 |
55 | int[] data = new int[54];
56 |
57 | for(int i = 53; i >= 1; i--) {
58 | int index = rdm.nextInt(i);
59 | data[i] = origin.remove(index);
60 | Assert.assertTrue(data[i] > 0);
61 | }
62 |
63 | data[0] = origin.remove(0);
64 | Assert.assertTrue(data[0] > 0);
65 | Assert.assertTrue(origin.size() == 0);
66 |
67 | int first = -1;
68 | for(int i = 0; i < 51; i++) {
69 | if (first != -1 && first/17 != i/17)
70 | return false;
71 |
72 | if (data[i] < 3) {
73 | if (first != -1)
74 | return true;
75 | first = i;
76 | }
77 | }
78 |
79 | return false;
80 |
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/core/src/test/java/learn/guice/LearnKey.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package learn.guice;
22 |
23 | import com.bj58.argo.inject.ArgoSystem;
24 | import com.google.inject.*;
25 | import com.google.inject.name.Names;
26 | import org.testng.annotations.Test;
27 |
28 | /**
29 | * @author Service Platform Architecture Team (spat@58.com)
30 | */
31 | public class LearnKey {
32 |
33 | @ImplementedBy(Action1.class)
34 | public static interface Action {
35 | String process();
36 | }
37 |
38 | public static class Action1 implements Action {
39 |
40 | @Override
41 | public String process() {
42 | return "action1";
43 | }
44 | }
45 |
46 | @ArgoSystem
47 | public static class Action2 implements Action {
48 |
49 | private long time = System.nanoTime();
50 |
51 | @Override
52 | public String process() {
53 | return "action2" + time;
54 | }
55 | }
56 |
57 | @Test
58 | public void test() {
59 |
60 | Injector injector = Guice.createInjector(new ActionModule());
61 |
62 | Action action1 = injector.getInstance(Action.class);
63 |
64 | System.out.println(action1.process());
65 |
66 | Action action2 = injector.getInstance(Key.get(Action.class, Names.named("ABC")));
67 |
68 | System.out.println(action2.process());
69 |
70 | Action action3 = injector.getInstance(Key.get(Action.class, ArgoSystem.class));
71 |
72 | System.out.println(action2.process());
73 |
74 | }
75 |
76 |
77 | public static class ActionModule extends AbstractModule {
78 |
79 | @Override
80 | protected void configure() {
81 | bind(Action.class).annotatedWith(Names.named("ABC")).to(Action2.class);
82 | }
83 |
84 |
85 |
86 |
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/plugin/BJ58-SPAT-Plugins_Argo_DOC.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/58code/Argo/083e851e1ed4f29a6e228fb1506224cbca6687eb/plugin/BJ58-SPAT-Plugins_Argo_DOC.pdf
--------------------------------------------------------------------------------
/plugin/com.bj58.spat.plugins.argo_1.0.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/58code/Argo/083e851e1ed4f29a6e228fb1506224cbca6687eb/plugin/com.bj58.spat.plugins.argo_1.0.0.jar
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.bj58.spat
5 | argo-project
6 | 1.0-SNAPSHOT
7 | pom
8 |
9 | core
10 | samples/hello-world
11 | samples/company
12 |
13 |
14 | 58.com argo
15 | 58.com argo
16 | https://github.com/58code/Argo
17 |
18 |
19 | 58.com
20 | http://www.58.com/
21 |
22 |
23 |
24 |
25 | The Apache Software License, Version 2.0
26 | http://www.apache.org/licenses/LICENSE-2.0.txt
27 | repo
28 |
29 |
30 |
31 |
32 |
33 |
34 | SPAT
35 | Service Platform Architecture Team
36 | https://github.com/58code
37 | spat@58.com
38 |
39 |
40 |
41 | renjun
42 | renjun
43 | http://weibo.com/duoway
44 | rjun@outlook.com
45 |
46 |
47 |
48 | liuzw
49 | liu zhongwei
50 | liuzw@58.com
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/samples/company/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | com.bj58.spat
6 | company-sample
7 | 1.0.0
8 | 4.0.0
9 | war
10 |
11 |
12 |
13 | com.bj58.spat
14 | argo
15 | 1.0.0
16 |
17 |
18 |
19 | javax.servlet
20 | javax.servlet-api
21 | provided
22 | 3.0.1
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | org.apache.maven.plugins
33 | maven-war-plugin
34 | 2.3
35 |
36 | false
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.apache.maven.plugins
44 | maven-compiler-plugin
45 | 2.5.1
46 |
47 | 1.6
48 | 1.6
49 | UTF-8
50 |
51 |
52 |
53 |
54 | org.apache.tomcat.maven
55 | tomcat7-maven-plugin
56 | 2.0
57 |
58 | /
59 | 80
60 |
61 |
62 |
63 |
64 |
65 | org.mortbay.jetty
66 | jetty-maven-plugin
67 |
68 |
69 |
70 | 9966
71 | foo
72 | 0
73 |
74 |
75 | 80
76 | 60000
77 |
78 |
79 |
80 | /
81 |
82 |
83 |
84 |
85 | org.apache.maven.plugins
86 | maven-compiler-plugin
87 | 2.5.1
88 |
89 | 1.6
90 | 1.6
91 | UTF-8
92 |
93 |
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/samples/company/src/main/java/com/bj58/argo/GroupConventionBinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.bj58.argo;
22 |
23 | import com.bj58.argo.convention.GroupConventionAnnotation;
24 |
25 | /**
26 | *
27 | * 演示如何自定义组织策略
28 | *
29 | * @author Service Platform Architecture Team (spat@58.com)
30 | */
31 | @GroupConventionAnnotation(
32 | groupPackagesPrefix = "com.mycompany"
33 | )
34 | public class GroupConventionBinder {
35 | }
36 |
--------------------------------------------------------------------------------
/samples/company/src/main/java/com/mycompany/sample/controllers/HomeController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright Beijing 58 Information Technology Co.,Ltd.
3 | *
4 | * Licensed to the Apache Software Foundation (ASF) under one
5 | * or more contributor license agreements. See the NOTICE file
6 | * distributed with this work for additional information
7 | * regarding copyright ownership. The ASF licenses this file
8 | * to you under the Apache License, Version 2.0 (the
9 | * "License"); you may not use this file except in compliance
10 | * with the License. You may obtain a copy of the License at
11 | *
12 | * http://www.apache.org/licenses/LICENSE-2.0
13 | *
14 | * Unless required by applicable law or agreed to in writing,
15 | * software distributed under the License is distributed on an
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 | * KIND, either express or implied. See the License for the
18 | * specific language governing permissions and limitations
19 | * under the License.
20 | */
21 | package com.mycompany.sample.controllers;
22 |
23 | import com.bj58.argo.ActionResult;
24 | import com.bj58.argo.BeatContext;
25 | import com.bj58.argo.annotations.Path;
26 | import com.bj58.argo.controller.AbstractController;
27 |
28 | /**
29 | * @author Service Platform Architecture Team (spat@58.com)
30 | */
31 | @Path("hello")
32 | public class HomeController extends AbstractController{
33 |
34 | @Path("")
35 | public ActionResult hello() {
36 | return writer().write("Hello world");
37 | }
38 |
39 | @Path("argo")
40 | public ActionResult helloArgo() {
41 | return writer().write("Hello, argo");
42 | }
43 |
44 | @Path("{name}")
45 | public ActionResult helloWorld(String name) {
46 | return writer().write("Hello, %s", name);
47 | }
48 |
49 | /**
50 | * 这个是一个比较复杂的例子,
51 | * Path中的路径可以用正则表达式匹配,
52 | * @Path("{phoneNumber:\\d+}")和@Path("{name}")的匹配顺序是
53 | * 如果都匹配,先匹配模板路径长的也就是@Path("{phoneNumber:\\d+}")
54 | *
55 | * @param phoneNumber
56 | * @return
57 | */
58 | @Path("{phoneNumber:\\d+}")
59 | public ActionResult helloView(int phoneNumber) {
60 | BeatContext beatContext = beat();
61 |
62 | beatContext
63 | .getModel()
64 | .add("title", "phone")
65 | .add("phoneNumber", phoneNumber);
66 |
67 | return view("hello");
68 |
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/samples/company/src/main/resources/views/hello.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $title
5 |
6 |
7 | 这是一个demo,页面渲染采用velocity。
8 |