├── rop-war ├── .gitignore ├── src │ └── main │ │ ├── webapp │ │ ├── index.jsp │ │ └── WEB-INF │ │ │ └── web.xml │ │ ├── resources │ │ ├── properties │ │ │ ├── init.properties │ │ │ └── log4j.properties │ │ ├── mapper │ │ │ └── mybatis-config.xml │ │ ├── servlet │ │ │ └── spring-hessian-service.xml │ │ ├── struts.xml │ │ └── context │ │ │ └── applicationContext.xml │ │ └── java │ │ └── com │ │ └── prowo │ │ ├── db │ │ └── mysql │ │ │ ├── dialet │ │ │ ├── OracleDialect.java │ │ │ ├── MySQLDialect.java │ │ │ └── Dialect.java │ │ │ ├── util │ │ │ └── ReflectUtil.java │ │ │ └── interceptor │ │ │ └── Page.java │ │ ├── system │ │ └── SystemInitListener.java │ │ └── rop │ │ ├── context │ │ ├── RopContext.java │ │ ├── SpringContextHandler.java │ │ └── RopServiceHandler.java │ │ ├── web │ │ ├── BaseAction.java │ │ └── Router.java │ │ ├── filter │ │ └── BaseAccessFilter.java │ │ ├── intercept │ │ ├── LogInterceptor.java │ │ ├── CacheInterceptor.java │ │ └── RopContextIntercept.java │ │ └── memcached │ │ ├── MemcachedCalendarUtil.java │ │ └── MemcachedUtil.java └── pom.xml ├── .gitignore ├── README.md ├── rop-service ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── prowo │ │ │ └── App.java │ └── test │ │ └── java │ │ └── com │ │ └── prowo │ │ └── AppTest.java ├── pom.xml └── rop-service.iml ├── rop-common ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── prowo │ │ │ ├── rop │ │ │ ├── exception │ │ │ │ ├── RedefinedException.java │ │ │ │ └── RopException.java │ │ │ ├── utils │ │ │ │ ├── Representation.java │ │ │ │ ├── Metadata.java │ │ │ │ ├── ServiceMethodHandler.java │ │ │ │ ├── MediaType.java │ │ │ │ └── ServiceConstants.java │ │ │ ├── annotation │ │ │ │ └── ApiMethod.java │ │ │ └── log │ │ │ │ ├── LogStack.java │ │ │ │ └── LogUtils.java │ │ │ └── common │ │ │ ├── enums │ │ │ └── ResultCode.java │ │ │ ├── exception │ │ │ └── BussinessException.java │ │ │ └── utils │ │ │ ├── DateConverter.java │ │ │ ├── HttpUtils.java │ │ │ └── ZipUtils.java │ └── test │ │ └── java │ │ └── com │ │ └── prowo │ │ └── AppTest.java ├── pom.xml └── rop-common.iml ├── rop-api ├── src │ └── main │ │ └── java │ │ └── com │ │ └── prowo │ │ ├── test │ │ ├── handle │ │ │ └── IHelloWorld.java │ │ ├── response │ │ │ └── RopHelloWorldResponse.java │ │ └── request │ │ │ └── RopHelloWorldRequest.java │ │ └── common │ │ ├── annotation │ │ └── SpVersion.java │ │ ├── rop │ │ ├── RopRequestBody.java │ │ ├── RopResponse.java │ │ └── RopRequest.java │ │ └── util │ │ ├── PageElementModel.java │ │ ├── ClientMemCacheConstants.java │ │ ├── InternetProtocol.java │ │ ├── MD5.java │ │ └── FastJsonUtils.java ├── pom.xml └── rop-api.iml ├── rop-persistence ├── pom.xml ├── src │ └── test │ │ └── java │ │ └── com │ │ └── prowo │ │ └── AppTest.java └── rop-persistence.iml ├── rop-core ├── pom.xml ├── src │ ├── main │ │ └── java │ │ │ └── com │ │ │ └── prowo │ │ │ └── handler │ │ │ └── HelloWorldHandler.java │ └── test │ │ └── java │ │ └── com │ │ └── prowo │ │ └── test │ │ └── HelloWorldTest.java └── rop-core.iml ├── pom.xml └── rop.iml /rop-war/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /rop-war/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /rop-war/src/main/resources/properties/init.properties: -------------------------------------------------------------------------------- 1 | pointsPath=D:/data/froadpoints/ 2 | #pointsPath=D:/workspace/local_config/points/ -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/db/mysql/dialet/OracleDialect.java: -------------------------------------------------------------------------------- 1 | package com.prowo.db.mysql.dialet; 2 | 3 | public class OracleDialect extends Dialect { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | rop-common/target/ 3 | rop-core/target/ 4 | rop-service/target/ 5 | rop-api/target/ 6 | rop-persistence/target/ 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rop 2 | Rop(Rapid Open Platform)是借鉴淘宝开发平台(TOP:Taobal Open Platform)实现的全功能Rest Web Service 开源框架(Full-Stack)。 它高于CXF,Aixs等一般的纯技术Web Service框架,提供了请求/响应序列化、数据检验、会话管理、安全管理等高级主题的东西。最值得一提的是其参考TOP,提供了一个可扩展的错误处理模型,使开发平台级的Web Service的难度大大降低。 3 | -------------------------------------------------------------------------------- /rop-service/src/main/java/com/prowo/App.java: -------------------------------------------------------------------------------- 1 | package com.prowo; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/exception/RedefinedException.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.exception; 2 | 3 | /** 4 | * 重复定义的method 和 version 5 | * @author dengcheng 6 | * 7 | */ 8 | public class RedefinedException extends RopException{ 9 | /** 10 | * 11 | */ 12 | private static final long serialVersionUID = 8768155790007183873L; 13 | 14 | public RedefinedException(){ 15 | super("重复定义 method 以及版本"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/test/handle/IHelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.prowo.test.handle; 2 | 3 | import com.prowo.common.rop.RopRequestBody; 4 | import com.prowo.common.rop.RopResponse; 5 | import com.prowo.test.request.RopHelloWorldRequest; 6 | import com.prowo.test.response.RopHelloWorldResponse; 7 | 8 | public interface IHelloWorld { 9 | public RopResponse sayHello(RopRequestBody request); 10 | } 11 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/annotation/SpVersion.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.annotation; 2 | 3 | import java.lang.annotation.*; 4 | 5 | @Inherited 6 | @Target({ElementType.FIELD, ElementType.METHOD}) 7 | @Retention(RetentionPolicy.RUNTIME) 8 | @Documented 9 | public @interface SpVersion { 10 | 11 | /** 12 | * 用于属性过滤 json输出的时候 13 | * 14 | * @return 15 | */ 16 | int gt() default 0; 17 | 18 | int ipadGt() default 0; 19 | } 20 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/utils/Representation.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.utils; 2 | 3 | import javax.servlet.http.HttpServletResponse; 4 | 5 | public abstract class Representation { 6 | private MediaType mediaType; 7 | 8 | public MediaType getMediaType() { 9 | return mediaType; 10 | } 11 | 12 | public void setMediaType(MediaType mediaType) { 13 | this.mediaType = mediaType; 14 | } 15 | 16 | public void outPut(HttpServletResponse response) { 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/rop/RopRequestBody.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.rop; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | 5 | import java.io.Serializable; 6 | 7 | 8 | public class RopRequestBody implements Serializable { 9 | /** 10 | * 11 | */ 12 | private static final long serialVersionUID = -589833629810101948L; 13 | @JSONField(serialize = false) 14 | private T t; 15 | 16 | public T getT() { 17 | return t; 18 | } 19 | 20 | public void setT(T t) { 21 | this.t = t; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/exception/RopException.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.exception; 2 | 3 | /** 4 | * 自定义rop异常 5 | */ 6 | public class RopException extends RuntimeException { 7 | /** 8 | * 9 | */ 10 | private static final long serialVersionUID = 7115081616648590439L; 11 | 12 | public RopException() { 13 | } 14 | 15 | public RopException(String message) { 16 | super(message); 17 | } 18 | 19 | public RopException(String message, Throwable cause) { 20 | super(message, cause); 21 | } 22 | 23 | public RopException(Throwable cause) { 24 | super(cause); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/db/mysql/dialet/MySQLDialect.java: -------------------------------------------------------------------------------- 1 | package com.prowo.db.mysql.dialet; 2 | 3 | public class MySQLDialect extends Dialect { 4 | 5 | public boolean supportsLimitOffset() { 6 | return true; 7 | } 8 | 9 | public boolean supportsLimit() { 10 | return true; 11 | } 12 | 13 | public String getLimitString(String sql, int offset, 14 | String offsetPlaceholder, int limit, String limitPlaceholder) { 15 | if (offset > 0) { 16 | return sql + " limit " + offsetPlaceholder + "," + limitPlaceholder; 17 | } else { 18 | return sql + " limit " + limitPlaceholder; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/util/PageElementModel.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.util; 2 | 3 | public class PageElementModel { 4 | private String elementCode; 5 | private String elementValue; 6 | 7 | public String getElementCode() { 8 | return elementCode; 9 | } 10 | 11 | public void setElementCode(String elementCode) { 12 | this.elementCode = elementCode; 13 | } 14 | 15 | public String getElementValue() { 16 | return elementValue; 17 | } 18 | 19 | public void setElementValue(String elementValue) { 20 | this.elementValue = elementValue; 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/utils/Metadata.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.utils; 2 | 3 | public abstract class Metadata { 4 | private String name; 5 | private String description; 6 | 7 | public Metadata(String name, String description) { 8 | this.name = name; 9 | this.description = description; 10 | } 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | public String getDescription() { 21 | return description; 22 | } 23 | 24 | public void setDescription(String description) { 25 | this.description = description; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/test/response/RopHelloWorldResponse.java: -------------------------------------------------------------------------------- 1 | package com.prowo.test.response; 2 | 3 | import java.io.Serializable; 4 | 5 | public class RopHelloWorldResponse implements Serializable { 6 | 7 | private static final long serialVersionUID = 1723808150716676068L; 8 | 9 | private String content; 10 | 11 | public String getContent() { 12 | return content; 13 | } 14 | 15 | public void setContent(String content) { 16 | this.content = content; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "RopHelloWorldResponse{" + 22 | "content='" + content + '\'' + 23 | '}'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rop-war/src/main/resources/mapper/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /rop-api/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | rop-root 5 | com.prowo 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | rop-api 11 | jar 12 | 13 | rop-api 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /rop-common/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | rop-root 5 | com.prowo 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | rop-common 11 | jar 12 | 13 | rop-common 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/common/enums/ResultCode.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.enums; 2 | 3 | /** 4 | * @author linan 5 | * 响应码 6 | */ 7 | public enum ResultCode { 8 | 9 | // 系统响应码 10 | SYS_SUCCESS("0000", "成功"), 11 | SYS_FAILED("9999", "失败"), 12 | SYS_UNKNOWN_EXCEPTION("0099", "系统未知异常"), 13 | SYS_TIMEOUT("0002", "超时"), 14 | SYS_REQUEST_CONCURRENCY("0098", "并发请求"),; 15 | private String code; 16 | 17 | private String msg; 18 | 19 | private ResultCode(String code, String msg) { 20 | this.code = code; 21 | this.msg = msg; 22 | } 23 | 24 | public String getCode() { 25 | return code; 26 | } 27 | 28 | public String getMsg() { 29 | return msg; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /rop-persistence/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | rop-root 5 | com.prowo 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | rop-persistence 11 | jar 12 | 13 | rop-persistence 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /rop-common/src/test/java/com/prowo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.prowo; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /rop-service/src/test/java/com/prowo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.prowo; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /rop-persistence/src/test/java/com/prowo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.prowo; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /rop-war/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.prowo 8 | rop-root 9 | 1.0-SNAPSHOT 10 | 11 | rop-war 12 | war 13 | rop-war Maven Webapp 14 | http://maven.apache.org 15 | 16 | 17 | com.prowo 18 | rop-core 19 | 1.0-SNAPSHOT 20 | 21 | 22 | 23 | rop-war 24 | 25 | 26 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/system/SystemInitListener.java: -------------------------------------------------------------------------------- 1 | package com.prowo.system; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | 6 | import org.apache.commons.configuration.Configuration; 7 | import org.apache.commons.configuration.ConfigurationException; 8 | import org.apache.commons.configuration.PropertiesConfiguration; 9 | 10 | public class SystemInitListener implements ServletContextListener { 11 | 12 | @Override 13 | public void contextDestroyed(ServletContextEvent arg0) { 14 | System.clearProperty("pointsPath"); 15 | 16 | } 17 | 18 | @Override 19 | public void contextInitialized(ServletContextEvent arg0) { 20 | Configuration config = null; 21 | try { 22 | config = new PropertiesConfiguration("properties/init.properties"); 23 | } catch (ConfigurationException e) { 24 | 25 | } 26 | //设置配置文件根路径 27 | System.setProperty("pointsPath", config.getString("pointsPath")); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/common/exception/BussinessException.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.exception; 2 | 3 | public class BussinessException extends RuntimeException { 4 | 5 | /** 6 | * 7 | */ 8 | private static final long serialVersionUID = -5609021297595863252L; 9 | 10 | private String code; 11 | private String message; 12 | 13 | public BussinessException() { 14 | } 15 | 16 | public BussinessException(String code, String message) { 17 | super(message); 18 | this.code = code; 19 | this.message = message; 20 | } 21 | 22 | public BussinessException(String code, String message, Throwable cause) { 23 | super(message, cause); 24 | this.code = code; 25 | this.message = message; 26 | } 27 | 28 | public BussinessException(Throwable cause) { 29 | super(cause); 30 | } 31 | 32 | public String getCode() { 33 | return code; 34 | } 35 | 36 | public String getMessage() { 37 | return message; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/utils/ServiceMethodHandler.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.utils; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | /** 6 | * 处理请求的服务相关信息 7 | * 8 | */ 9 | public class ServiceMethodHandler { 10 | // 处理器对象 11 | private Object handler; 12 | 13 | // 处理器的处理方法 14 | private Method handlerMethod; 15 | 16 | private String version; 17 | /** 18 | * 是否缓存结果集 19 | */ 20 | private boolean cached; 21 | 22 | public boolean isCached() { 23 | return cached; 24 | } 25 | 26 | public void setCached(boolean cached) { 27 | this.cached = cached; 28 | } 29 | 30 | public Object getHandler() { 31 | return handler; 32 | } 33 | 34 | public void setHandler(Object handler) { 35 | this.handler = handler; 36 | } 37 | 38 | public Method getHandlerMethod() { 39 | return handlerMethod; 40 | } 41 | 42 | public void setHandlerMethod(Method handlerMethod) { 43 | this.handlerMethod = handlerMethod; 44 | } 45 | 46 | public String getVersion() { 47 | return version; 48 | } 49 | 50 | public void setVersion(String version) { 51 | this.version = version; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/test/request/RopHelloWorldRequest.java: -------------------------------------------------------------------------------- 1 | package com.prowo.test.request; 2 | 3 | import com.prowo.common.rop.RopRequest; 4 | import com.prowo.common.util.ClientMemCacheConstants; 5 | import org.apache.commons.lang3.StringUtils; 6 | 7 | public class RopHelloWorldRequest extends RopRequest { 8 | 9 | private static final long serialVersionUID = 7683393066124643022L; 10 | 11 | private String username; 12 | 13 | public String getUsername() { 14 | return username; 15 | } 16 | 17 | public void setUsername(String username) { 18 | this.username = username; 19 | } 20 | 21 | public String getCacheKey() { 22 | return StringUtils.join(ClientMemCacheConstants.ANNOTATION_CACHEKEY_CONSTANTS.HELLO_WORLD.getCacheKey(), this.username); 23 | } 24 | 25 | public int getCacheSecends() { 26 | return ClientMemCacheConstants.ANNOTATION_CACHEKEY_CONSTANTS.HELLO_WORLD.getCacheSecends(); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "RopHelloWorldRequest{" + 32 | "username='" + username + '\'' + 33 | '}'; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /rop-core/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | rop-root 5 | com.prowo 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | rop-core 11 | jar 12 | 13 | rop-core 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | com.prowo 23 | rop-api 24 | 1.0-SNAPSHOT 25 | 26 | 27 | com.prowo 28 | rop-service 29 | 1.0-SNAPSHOT 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/common/utils/DateConverter.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.utils; 2 | 3 | import java.text.ParseException; 4 | import java.util.Date; 5 | 6 | import org.apache.commons.beanutils.Converter; 7 | import org.apache.commons.lang3.StringUtils; 8 | import org.apache.commons.lang3.time.DateUtils; 9 | 10 | public class DateConverter implements Converter { 11 | 12 | public Object convert(Class type, Object value) { 13 | if (value == null) { 14 | return null; 15 | } 16 | if (value instanceof Date) { 17 | return value; 18 | } 19 | if (value instanceof Long) { 20 | Long longValue = (Long) value; 21 | return new Date(longValue.longValue()); 22 | } 23 | if (value instanceof String) { 24 | String dateStr = (String) value; 25 | if (StringUtils.isEmpty(dateStr)) { 26 | return null; 27 | } 28 | Date date = null; 29 | try { 30 | date = DateUtils.parseDate(value.toString(), new String[] { 31 | "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", 32 | "yyyy-MM-dd HH:mm", "yyyy-MM-dd" }); 33 | } catch (ParseException e) { 34 | e.printStackTrace(); 35 | } 36 | return date; 37 | } 38 | return null; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /rop-service/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | rop-root 5 | com.prowo 6 | 1.0-SNAPSHOT 7 | 8 | 4.0.0 9 | 10 | rop-service 11 | jar 12 | 13 | rop-service 14 | http://maven.apache.org 15 | 16 | 17 | UTF-8 18 | 19 | 20 | 21 | 22 | com.prowo 23 | rop-persistence 24 | 1.0-SNAPSHOT 25 | 26 | 27 | com.prowo 28 | rop-common 29 | 1.0-SNAPSHOT 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/annotation/ApiMethod.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.annotation; 2 | 3 | import java.lang.annotation.*; 4 | 5 | @Inherited 6 | @Target({ElementType.METHOD}) 7 | @Retention(RetentionPolicy.RUNTIME) 8 | @Documented 9 | public @interface ApiMethod { 10 | 11 | /** 12 | * 映射的方法 13 | * 14 | * @return 15 | */ 16 | String method() default ""; 17 | 18 | /** 19 | * 支持的api版本 20 | * 21 | * @return 22 | */ 23 | String version() default "ALL"; 24 | 25 | /** 26 | * 是否缓存结果 27 | * 缓存时间2分钟 28 | * 会根据method 为memcached 的key 来缓存。 29 | * 请谨慎使用 30 | * 31 | * @return 32 | */ 33 | boolean cached() default false; 34 | 35 | /** 36 | * aop arround 环绕处理class 37 | * 38 | * @return 39 | */ 40 | String[] InterceptorRef() default {}; 41 | 42 | /** 43 | * 每分钟访问限制 默认 44 | */ 45 | int limitCount() default 999999999; 46 | 47 | /** 48 | * 访问限制 49 | * internal 表示内网 50 | * 51 | * @return 52 | */ 53 | String accessRole() default ""; 54 | 55 | /** 56 | * @Description: 默认不忽略次图片验证拦截器 57 | */ 58 | boolean ignoreValidateInterceptor() default false; 59 | } 60 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/db/mysql/dialet/Dialect.java: -------------------------------------------------------------------------------- 1 | package com.prowo.db.mysql.dialet; 2 | 3 | /** 4 | * 类似hibernate的 Dialect 5 | * @author "linan" 6 | * 7 | */ 8 | public abstract class Dialect { 9 | 10 | public boolean supportsLimit() { 11 | return false; 12 | } 13 | 14 | public boolean supportsLimitOffset() { 15 | return supportsLimit(); 16 | } 17 | 18 | /** 19 | * 将sql变成分页sql语句,直接使用offset,limit的值作为占位符.
源代码为: 20 | * getLimitString(sql,offset 21 | * ,String.valueOf(offset),limit,String.valueOf(limit)) 22 | */ 23 | public String getLimitString(String sql, int offset, int limit) { 24 | return getLimitString(sql, offset, Integer.toString(offset), limit, 25 | Integer.toString(limit)); 26 | } 27 | 28 | /** 29 | * 将sql变成分页sql语句,提供将offset及limit使用占位符(placeholder)替换. 30 | * 31 | *
32 | 	 * 如mysql
33 | 	 * dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
34 | 	 * select * from user limit :offset,:limit
35 | 	 * 
36 | * 37 | * @return 包含占位符的分页sql 38 | */ 39 | public String getLimitString(String sql, int offset, 40 | String offsetPlaceholder, int limit, String limitPlaceholder) { 41 | throw new UnsupportedOperationException("paged queries not supported"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/context/RopContext.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.context; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.servlet.http.HttpServletResponse; 5 | 6 | import com.prowo.common.rop.RopRequestBody; 7 | import com.prowo.rop.utils.ServiceMethodHandler; 8 | 9 | public class RopContext { 10 | private RopRequestBody requestData; 11 | private ServiceMethodHandler serviceHandler; 12 | private HttpServletRequest request; 13 | private HttpServletResponse response; 14 | 15 | public HttpServletRequest getRequest() { 16 | return request; 17 | } 18 | 19 | public void setRequest(HttpServletRequest request) { 20 | this.request = request; 21 | } 22 | 23 | public HttpServletResponse getResponse() { 24 | return response; 25 | } 26 | 27 | public void setResponse(HttpServletResponse response) { 28 | this.response = response; 29 | } 30 | 31 | public ServiceMethodHandler getServiceHandler() { 32 | return serviceHandler; 33 | } 34 | 35 | public void setServiceHandler(ServiceMethodHandler serviceHandler) { 36 | this.serviceHandler = serviceHandler; 37 | } 38 | 39 | public RopRequestBody getRequestData() { 40 | return requestData; 41 | } 42 | 43 | public void setRequestData(RopRequestBody requestData) { 44 | this.requestData = requestData; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/context/SpringContextHandler.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.context; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.ApplicationContextAware; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class SpringContextHandler implements ApplicationContextAware { 9 | 10 | private static ApplicationContext applicationContext; 11 | 12 | public void setApplicationContext(ApplicationContext applicationContext) { 13 | SpringContextHandler.applicationContext = applicationContext; 14 | } 15 | 16 | public static ApplicationContext getApplicationContext() { 17 | checkApplicationContext(); 18 | return applicationContext; 19 | } 20 | 21 | public static T getBean(String name) { 22 | checkApplicationContext(); 23 | return (T) applicationContext.getBean(name); 24 | } 25 | 26 | public static T getBean(Class clazz) { 27 | checkApplicationContext(); 28 | return (T) applicationContext.getBeansOfType(clazz); 29 | } 30 | 31 | private static void checkApplicationContext() { 32 | if (applicationContext == null) 33 | throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil"); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/web/BaseAction.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.web; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | import org.apache.struts2.ServletActionContext; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | 13 | import com.opensymphony.xwork2.ActionSupport; 14 | 15 | public class BaseAction extends ActionSupport{ 16 | 17 | /** 18 | * 19 | */ 20 | private static final long serialVersionUID = 1L; 21 | 22 | @Autowired 23 | 24 | 25 | public BaseAction() { 26 | //this.meta = metaService.getMeta(this.getRequest()); 27 | } 28 | 29 | 30 | /** 31 | * 发送Ajax请求结果json 32 | * 33 | * @throws ServletException 34 | * @throws IOException 35 | */ 36 | public void sendAjaxResultByJson(String json) { 37 | this.getResponse().setContentType("application/json;charset=UTF-8"); 38 | this.getResponse().setCharacterEncoding("UTF-8"); 39 | try { 40 | PrintWriter out = this.getResponse().getWriter(); 41 | out.write(json); 42 | out.flush(); 43 | out.close(); 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | 49 | 50 | protected HttpServletResponse getResponse() { 51 | return ServletActionContext.getResponse(); 52 | } 53 | 54 | protected HttpServletRequest getRequest() { 55 | return ServletActionContext.getRequest(); 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /rop-core/src/main/java/com/prowo/handler/HelloWorldHandler.java: -------------------------------------------------------------------------------- 1 | package com.prowo.handler; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.springframework.stereotype.Component; 5 | 6 | import com.prowo.common.rop.RopRequestBody; 7 | import com.prowo.common.rop.RopResponse; 8 | import com.prowo.rop.annotation.ApiMethod; 9 | import com.prowo.rop.log.LogUtils; 10 | import com.prowo.test.handle.IHelloWorld; 11 | import com.prowo.test.request.RopHelloWorldRequest; 12 | import com.prowo.test.response.RopHelloWorldResponse; 13 | 14 | @Component 15 | public class HelloWorldHandler implements IHelloWorld { 16 | private final Logger logger = Logger.getLogger(this.getClass()); 17 | 18 | @Override 19 | @ApiMethod(method = "api.com.test.sayHello", version = "1.0.0", cached = false) 20 | public RopResponse sayHello( 21 | RopRequestBody request) { 22 | logger.info("start sayHello api, request parms: " + request.getT().toString()); 23 | 24 | RopResponse response = new RopResponse(); 25 | RopHelloWorldResponse responseData = new RopHelloWorldResponse(); 26 | RopHelloWorldRequest helloWorld = request.getT(); 27 | LogUtils.startLogWeb("测试api的debug日志功能"); 28 | LogUtils.appendLogWeb("测试debug日志"); 29 | responseData.setContent("hello ! " + helloWorld.getUsername()); 30 | response.setData(responseData); 31 | LogUtils.endLogWeb(); 32 | 33 | logger.info("end sayHello api, response parms: " + responseData.toString()); 34 | return response; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /rop-war/src/main/resources/servlet/spring-hessian-service.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/filter/BaseAccessFilter.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.Filter; 6 | import javax.servlet.FilterChain; 7 | import javax.servlet.FilterConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.ServletRequest; 10 | import javax.servlet.ServletResponse; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | 14 | import org.apache.commons.logging.Log; 15 | import org.apache.commons.logging.LogFactory; 16 | import org.springframework.stereotype.Component; 17 | 18 | /** 19 | * 访问控制Filter 20 | */ 21 | @Component 22 | public class BaseAccessFilter implements Filter { 23 | private Log log = LogFactory.getLog(BaseAccessFilter.class); 24 | 25 | public void init(FilterConfig filterConfig) throws ServletException { 26 | 27 | } 28 | 29 | public void doFilter(ServletRequest request, ServletResponse response, 30 | FilterChain chain) throws IOException, ServletException { 31 | HttpServletRequest req = (HttpServletRequest) request; 32 | HttpServletResponse res = (HttpServletResponse) response; 33 | // AccessMeta am = metaService.getMeta(req); 34 | // am.getRouter().go(am, res); 35 | // req.getRequestDispatcher("").forward(req, res); 36 | log.info("in BaseAccessFilter....."); 37 | try { 38 | chain.doFilter(request, response); 39 | } catch (Exception ex) { 40 | ex.printStackTrace(); 41 | log.error("Captured Exception Exception URL: " + req.toString()); 42 | } catch (Throwable e) { 43 | e.printStackTrace(); 44 | log.error("Captured Throwable Exception URL: " + req.toString()); 45 | log.error(this.getClass(), e); 46 | } 47 | } 48 | 49 | public void destroy() { 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/util/ClientMemCacheConstants.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.util; 2 | 3 | 4 | public class ClientMemCacheConstants { 5 | 6 | //1分钟 7 | private static final int CACHE_ONE_MINS = 60; 8 | //5分钟 9 | private static final int CACHE_FIVE_MINS = 5 * 60; 10 | //10分钟 11 | private static final int CACHE_TEN_MINS = 10 * 60; 12 | //15分钟 13 | private static final int CACHE_FIVTEEN_MINS = 15 * 60; 14 | //半小时 15 | private static final int CACHE_HALF_HOUR = 30 * 60; 16 | //一小时 17 | private static final int CACHE_ONE_HOURS = 1 * 60 * 60; 18 | //两小时 19 | private static final int CACHE_TOW_HOURS = 2 * 60 * 60; 20 | //一个星期 21 | private static final int CACHE_ONE_WEEK = 24 * 7 * 60 * 60; 22 | 23 | 24 | /** 25 | * ANNOTATION注解方式缓存key和secends 26 | * 组装格式:模块_(方法名+版本号)(cacheKey, cacheSecends�? 27 | * cacheKey只是前缀,后面可自己添加参数拼装 28 | */ 29 | public static enum ANNOTATION_CACHEKEY_CONSTANTS { 30 | 31 | HELLO_WORLD("API_ANNOTATION_HELLO_WORLD_", CACHE_ONE_MINS); 32 | 33 | private String cacheKey; 34 | private int cacheSecends; 35 | 36 | private ANNOTATION_CACHEKEY_CONSTANTS(String cacheKey, int cacheSecends) { 37 | this.cacheKey = cacheKey; 38 | this.cacheSecends = cacheSecends; 39 | } 40 | 41 | public String getCacheKey() { 42 | return cacheKey; 43 | } 44 | 45 | public void setCacheKey(String cacheKey) { 46 | this.cacheKey = cacheKey; 47 | } 48 | 49 | public int getCacheSecends() { 50 | return cacheSecends; 51 | } 52 | 53 | public void setCacheSecends(int cacheSecends) { 54 | this.cacheSecends = cacheSecends; 55 | } 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /rop-war/src/main/resources/properties/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=info,stdout,R,E,default 2 | 3 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 | log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n 6 | log4j.appender.stdout.Threshold=DEBUG 7 | log4j.appender.R=org.apache.log4j.DailyRollingFileAppender 8 | log4j.appender.R.File=/var/log/applogs/rop/infos.log 9 | log4j.appender.R.DatePattern='.'yyyy-MM-dd 10 | log4j.appender.R.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.R.layout.ConversionPattern=%-5r [%t][%d{ISO8601}]-[%5p]%x-[%m]%n 12 | log4j.appender.R.Threshold=INFO 13 | 14 | log4j.appender.E=org.apache.log4j.DailyRollingFileAppender 15 | log4j.appender.E.File=/var/log/applogs/rop/error.log 16 | log4j.appender.E.DatePattern='.'yyyy-MM-dd 17 | log4j.appender.E.layout=org.apache.log4j.PatternLayout 18 | log4j.appender.E.layout.ConversionPattern=%-5r [%t][%d{ISO8601}]-[%5p]%x-[%m]%n 19 | log4j.appender.E.Threshold=ERROR 20 | 21 | log4j.appender.default=org.apache.log4j.DailyRollingFileAppender 22 | log4j.appender.default.File=/var/log/applogs/rop/default.log 23 | log4j.appender.default.DatePattern='.'yyyy-MM-dd 24 | log4j.appender.default.layout=org.apache.log4j.PatternLayout 25 | log4j.appender.default.layout.ConversionPattern=%-5r [%t][%d{ISO8601}]-[%5p]%x-[%m]%n 26 | log4j.appender.default.Threshold=INFO 27 | 28 | ##\u663e\u793aSQL\u8bed\u53e5\u90e8\u5206 29 | log4j.logger.com.ibatis=DEBUG 30 | log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG 31 | log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG 32 | log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG 33 | log4j.logger.java.sql.Connection=DEBUG 34 | log4j.logger.java.sql.Statement=DEBUG 35 | log4j.logger.java.sql.PreparedStatement=DEBUG -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/db/mysql/util/ReflectUtil.java: -------------------------------------------------------------------------------- 1 | package com.prowo.db.mysql.util; 2 | 3 | import java.lang.reflect.Field; 4 | 5 | /** 6 | * @Description 反射工具 7 | */ 8 | 9 | public class ReflectUtil { 10 | 11 | /** 12 | * 利用反射获取指定对象的指定属性值 13 | * 14 | * @param obj 15 | * 目标对象 16 | * @param fieldName 17 | * 目标属性 18 | * @return 目标属性的值 19 | */ 20 | public static Object getFieldValue(Object obj, String fieldName) { 21 | Object result = null; 22 | Field field = ReflectUtil.getField(obj, fieldName); 23 | if (field != null) { 24 | // 取消 Java 语言访问检查 25 | field.setAccessible(true); 26 | try { 27 | result = field.get(obj); 28 | } catch (IllegalArgumentException e) { 29 | e.printStackTrace(); 30 | } catch (IllegalAccessException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | return result; 35 | } 36 | 37 | /** 38 | * 利用反射获取指定对象里面的指定属性 39 | * 40 | * @param obj 41 | * 目标对象 42 | * @param fieldName 43 | * 目标属性 44 | * @return 目标字段 45 | */ 46 | public static Field getField(Object obj, String fieldName) { 47 | Field field = null; 48 | for (Class clazz = obj.getClass(); clazz != Object.class; clazz = clazz 49 | .getSuperclass()) { 50 | try { 51 | field = clazz.getDeclaredField(fieldName); 52 | break; 53 | } catch (NoSuchFieldException e) { 54 | // 这里不用做处理,子类没有该字段可能对应的父类有,都没有就返回null 55 | } 56 | } 57 | return field; 58 | } 59 | 60 | /** 61 | * 利用反射设置指定对象的指定属性为指定的值 62 | * 63 | * @param obj 64 | * 目标对象 65 | * @param fieldName 66 | * 目标属性 67 | * @param fieldValue 68 | * 目标值 69 | */ 70 | public static void setFieldValue(Object obj, String fieldName, 71 | Object fieldValue) { 72 | Field field = ReflectUtil.getField(obj, fieldName); 73 | if (field != null) { 74 | try { 75 | field.setAccessible(true); 76 | field.set(obj, fieldValue); 77 | } catch (IllegalArgumentException e) { 78 | e.printStackTrace(); 79 | } catch (IllegalAccessException e) { 80 | e.printStackTrace(); 81 | } 82 | } 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/intercept/LogInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.intercept; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.apache.commons.lang3.StringUtils; 6 | import org.apache.struts2.ServletActionContext; 7 | 8 | import com.opensymphony.xwork2.ActionInvocation; 9 | import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 10 | import com.prowo.common.rop.RopResponse; 11 | import com.prowo.rop.log.LogStack; 12 | import com.prowo.rop.log.LogUtils; 13 | 14 | public class LogInterceptor extends AbstractInterceptor { 15 | 16 | public static final String IS_DEBUG = "IS_DEBUG"; 17 | 18 | @Override 19 | public String intercept(ActionInvocation invocation) throws Exception { 20 | HttpServletRequest request = ServletActionContext.getRequest(); 21 | if (!isDebug(request)) { 22 | return invocation.invoke(); 23 | } 24 | 25 | String method = request.getParameter("method"); 26 | String version = request.getParameter("version"); 27 | if (StringUtils.isEmpty(method) || StringUtils.isEmpty(version)) { 28 | return invocation.invoke(); 29 | } 30 | 31 | request.setAttribute(LogUtils.DEBUG_LOG, 32 | LogUtils.initLog(method + ' ' + version)); 33 | return invocation.invoke(); 34 | } 35 | 36 | /** 37 | * 是否是BUG模式 38 | * 39 | * @param request 40 | * 请求 41 | * @return 是否是BUG模式 42 | */ 43 | public static boolean isDebug(HttpServletRequest request) { 44 | String isDebug = request.getParameter(IS_DEBUG); 45 | return "1".equals(isDebug); 46 | } 47 | 48 | /** 49 | * 在response中设置debug的信息 50 | * 51 | * @param request 52 | * http请求 53 | * @param response 54 | * 结构化response 55 | */ 56 | public static void setDebugMsg(HttpServletRequest request, Object response) { 57 | if (request == null || response == null 58 | || !(response instanceof RopResponse)) { 59 | return; 60 | } 61 | if (isDebug(request)) { 62 | RopResponse ropResponse = (RopResponse) response; 63 | LogStack root = LogUtils.getRootLog(); 64 | if (root == null) 65 | return; 66 | LogUtils.endLog(root); 67 | ropResponse.setDebugMsg(root.toString()); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /rop-war/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | Archetype Created Web Application 7 | 8 | contextConfigLocation 9 | classpath:/context/*.xml 10 | 11 | 12 | log4jConfigLocation 13 | 14 | classpath:/properties/log4j.properties 15 | 16 | 17 | 18 | com.prowo.system.SystemInitListener 19 | 20 | 21 | org.springframework.web.context.ContextLoaderListener 22 | 23 | 24 | org.springframework.web.util.Log4jConfigListener 25 | 26 | 27 | encodingFilter 28 | org.springframework.web.filter.CharacterEncodingFilter 29 | 30 | encoding 31 | UTF-8 32 | 33 | 34 | forceEncoding 35 | true 36 | 37 | 38 | 39 | struts2 40 | org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 41 | 42 | 43 | struts2 44 | *.do 45 | REQUEST 46 | FORWARD 47 | 48 | 49 | baseAccessFilter 50 | org.springframework.web.filter.DelegatingFilterProxy 51 | 52 | 53 | baseAccessFilter 54 | /* 55 | 56 | 57 | 58 | remote 59 | org.springframework.web.servlet.DispatcherServlet 60 | 61 | contextConfigLocation 62 | classpath:servlet/*.xml 63 | 64 | 1 65 | 66 | 67 | remote 68 | /remote/* 69 | 70 | 71 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/utils/MediaType.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.utils; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public final class MediaType extends Metadata { 7 | 8 | public MediaType(String name, String description) { 9 | super(name, description); 10 | } 11 | 12 | private static volatile Map _types = null; 13 | 14 | public static final MediaType APPLICATION_ALL_XML = register( 15 | "application/*+xml", "All application/*+xml documents"); 16 | 17 | public static final MediaType APPLICATION_JSON = register( 18 | "application/json", "JavaScript Object Notation document"); 19 | 20 | public static final MediaType IMAGE_BMP = register("image/bmp", 21 | "Windows bitmap"); 22 | 23 | public static final MediaType IMAGE_GIF = register("image/gif", "GIF image"); 24 | 25 | public static final MediaType IMAGE_ICON = register("image/x-icon", 26 | "Windows icon (Favicon)"); 27 | 28 | public static final MediaType IMAGE_JPEG = register("image/jpeg", 29 | "JPEG image"); 30 | 31 | public static final MediaType IMAGE_PNG = register("image/png", "PNG image"); 32 | 33 | public static final MediaType TEXT_HTML = register("text/html", 34 | "HTML document"); 35 | 36 | public static final MediaType TEXT_PLAIN = register("text/plain", 37 | "Plain text"); 38 | 39 | public static final MediaType TEXT_XML = register("text/xml", "XML text"); 40 | 41 | /** 42 | * Returns the known media types map. 43 | * 44 | * @return the known media types map. 45 | */ 46 | private static Map getTypes() { 47 | if (_types == null) { 48 | _types = new HashMap(); 49 | } 50 | return _types; 51 | } 52 | 53 | public static synchronized MediaType register(String name, 54 | String description) { 55 | 56 | if (!getTypes().containsKey(name)) { 57 | final MediaType type = new MediaType(name, description); 58 | getTypes().put(name, type); 59 | } 60 | 61 | return getTypes().get(name); 62 | } 63 | 64 | public static MediaType valueOf(String name) { 65 | MediaType result = null; 66 | 67 | if ((name != null) && !name.equals("")) { 68 | result = getTypes().get(name); 69 | if (result == null) { 70 | return APPLICATION_JSON; 71 | } 72 | } 73 | 74 | return result; 75 | } 76 | 77 | /** 78 | * Returns the main type. 79 | * 80 | * @return The main type. 81 | */ 82 | public String getMainType() { 83 | String result = null; 84 | 85 | if (getName() != null) { 86 | int index = getName().indexOf('/'); 87 | 88 | // Some clients appear to use name types without subtypes 89 | if (index == -1) { 90 | index = getName().indexOf(';'); 91 | } 92 | 93 | if (index == -1) { 94 | result = getName(); 95 | } else { 96 | result = getName().substring(0, index); 97 | } 98 | } 99 | 100 | return result; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/db/mysql/interceptor/Page.java: -------------------------------------------------------------------------------- 1 | package com.prowo.db.mysql.interceptor; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import com.alibaba.fastjson.JSON; 8 | 9 | /** 10 | * @Description 分页 11 | * @author FQ 12 | * @date 2014年9月15日 下午4:44:58 13 | * @version 1.0 14 | */ 15 | 16 | public class Page implements Serializable { 17 | 18 | public static final int MAX_PAGE_SIZE = 20000;// 每页最大记录数限制 19 | 20 | private int pageNumber = 1;// 当前页码 21 | private int pageSize = 20;// 每页记录数 22 | private int totalCount = 0;// 总记录数 23 | private int pageCount = 0;// 总页数 24 | private Long begDate; // 开始时间 25 | private Long endDate; // 接收时间 26 | 27 | private List resultsContent = new ArrayList();// 对应的当前页记录 28 | 29 | 30 | public Integer getPageNumber() { 31 | return pageNumber; 32 | } 33 | 34 | public void setPageNumber(Integer pageNumber) { 35 | if (pageNumber < 1) 36 | this.pageNumber = 1; 37 | else 38 | this.pageNumber = pageNumber; 39 | } 40 | 41 | public Integer getPageSize() { 42 | return pageSize; 43 | } 44 | 45 | public void setPageSize(Integer pageSize) { 46 | if (pageSize < 1) { 47 | pageSize = 1; 48 | } else if (pageSize > MAX_PAGE_SIZE) { 49 | pageSize = MAX_PAGE_SIZE; 50 | } 51 | this.pageSize = pageSize; 52 | } 53 | 54 | public Integer getTotalCount() { 55 | return totalCount; 56 | } 57 | 58 | public void setTotalCount(Integer totalCount) { 59 | 60 | this.totalCount = totalCount; 61 | 62 | // 设置总页数的时候计算出对应的总页数,在下面的三目运算中加法拥有更高的优先级,所以最后可以不加括号。 63 | int pageCount = totalCount % pageSize == 0 ? totalCount / pageSize 64 | : totalCount / pageSize + 1; 65 | this.setPageCount(pageCount); 66 | } 67 | 68 | public Integer getPageCount() { 69 | return pageCount; 70 | } 71 | 72 | public void setPageCount(Integer pageCount) { 73 | this.pageCount = pageCount; 74 | } 75 | 76 | public List getResultsContent() { 77 | return resultsContent; 78 | } 79 | 80 | public void setResultsContent(List resultsContent) { 81 | this.resultsContent = resultsContent; 82 | } 83 | 84 | public Long getBegDate() { 85 | return begDate; 86 | } 87 | 88 | public void setBegDate(Long begDate) { 89 | this.begDate = begDate; 90 | } 91 | 92 | public Long getEndDate() { 93 | return endDate; 94 | } 95 | 96 | public void setEndDate(Long endDate) { 97 | this.endDate = endDate; 98 | } 99 | 100 | @Override 101 | public String toString() { 102 | StringBuilder builder = new StringBuilder(); 103 | builder.append("Page [pageNumber=").append(pageNumber) 104 | .append(", pageSize=").append(pageSize) 105 | .append(", resultsContent=").append(JSON.toJSONString(resultsContent)) 106 | .append(", pageCount=").append(pageCount) 107 | .append(", totalCount=").append(totalCount).append("]"); 108 | return builder.toString(); 109 | } 110 | } 111 | 112 | 113 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/rop/RopResponse.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.rop; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.prowo.common.util.Constant; 5 | import com.prowo.common.util.FastJsonUtils; 6 | import com.prowo.common.util.MD5; 7 | 8 | import java.io.Serializable; 9 | import java.security.NoSuchAlgorithmException; 10 | 11 | public class RopResponse implements Serializable { 12 | 13 | /** 14 | * 15 | */ 16 | private static final long serialVersionUID = -6939909168205094440L; 17 | 18 | String code = ""; 19 | String message = ""; 20 | String errorMessage = ""; 21 | String version; 22 | String debugMsg; 23 | 24 | private T data; 25 | 26 | public RopResponse() { 27 | 28 | } 29 | 30 | public RopResponse(String code, String message, String errorMessage) { 31 | // TODO Auto-generated constructor stub 32 | this.code = code; 33 | this.message = message; 34 | this.errorMessage = errorMessage; 35 | } 36 | 37 | 38 | // public String toJson(){ 39 | // return ""; 40 | // } 41 | public String toString() { 42 | return ""; 43 | } 44 | 45 | 46 | public T getData() { 47 | return data; 48 | } 49 | 50 | 51 | public void setData(T data) { 52 | this.data = data; 53 | } 54 | 55 | public String getCode() { 56 | return code; 57 | } 58 | 59 | 60 | public void setCode(String code) { 61 | this.code = code; 62 | } 63 | 64 | 65 | public String getMessage() { 66 | return message; 67 | } 68 | 69 | 70 | public void setMessage(String message) { 71 | this.message = message; 72 | } 73 | 74 | 75 | public String getErrorMessage() { 76 | return errorMessage; 77 | } 78 | 79 | 80 | public void setErrorMessage(String errorMessage) { 81 | this.errorMessage = errorMessage; 82 | } 83 | 84 | 85 | public String getVersion() { 86 | if (getData() != null) { 87 | 88 | Object jsonObjTargetStr = FastJsonUtils.toJson(getData()); 89 | String version = ""; 90 | try { 91 | version = MD5.encode(jsonObjTargetStr.toString()); 92 | this.version = version; 93 | } catch (NoSuchAlgorithmException e) { 94 | // TODO Auto-generated catch block 95 | e.printStackTrace(); 96 | } 97 | } 98 | return version; 99 | } 100 | 101 | @JSONField(serialize = false) 102 | public boolean isOk() { 103 | return Constant.CLIENT_ERROR_CODE.OK.getCnName().equals(code); 104 | } 105 | 106 | public void setVersion(String version) { 107 | this.version = version; 108 | } 109 | 110 | public String getDebugMsg() { 111 | return debugMsg; 112 | } 113 | 114 | public void setDebugMsg(String debugMsg) { 115 | this.debugMsg = debugMsg; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/intercept/CacheInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.intercept; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.apache.commons.lang3.StringUtils; 10 | import org.apache.commons.logging.Log; 11 | import org.apache.commons.logging.LogFactory; 12 | import org.apache.struts2.ServletActionContext; 13 | 14 | import com.opensymphony.xwork2.ActionInvocation; 15 | import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 16 | import com.prowo.common.rop.RopRequest; 17 | import com.prowo.common.utils.ZipUtils; 18 | import com.prowo.rop.context.RopContext; 19 | import com.prowo.rop.memcached.MemcachedUtil; 20 | import com.prowo.rop.utils.ServiceMethodHandler; 21 | 22 | /** 23 | * 类描述:缓存 24 | */ 25 | public class CacheInterceptor extends AbstractInterceptor { 26 | 27 | private static final long serialVersionUID = 8674873127319932933L; 28 | private static final Log logger = LogFactory.getLog(CacheInterceptor.class); 29 | 30 | public String intercept(ActionInvocation invocation) throws Exception { 31 | try { 32 | RopContext ropContext = (RopContext) invocation 33 | .getInvocationContext().getParameters().get("ropContext"); 34 | ServiceMethodHandler handler = ropContext.getServiceHandler(); 35 | RopRequest baseRequest = (RopRequest) ropContext.getRequestData() 36 | .getT(); 37 | if (handler.isCached() && baseRequest != null 38 | && !baseRequest.isCheckVersion()) { 39 | String cacheKey = baseRequest.getCacheKey(); 40 | if (StringUtils.isNotBlank(cacheKey)) { 41 | logger.info("从缓存中取�??..........."); 42 | MemcachedUtil memcachedUtil = MemcachedUtil.getInstance(); 43 | Object obj = memcachedUtil.get(cacheKey); 44 | if (obj != null) { 45 | String compressedStr = (String) obj; 46 | String dataStr = ZipUtils.gunzip(compressedStr); 47 | if (StringUtils.isNotBlank(dataStr)) { 48 | logger.info("get data from cache success........."); 49 | this.sendAjaxResultByJson(dataStr); 50 | return null; 51 | } 52 | logger.info("缓存失效,有key但没value........."); 53 | } 54 | logger.info("缓存失效............"); 55 | } 56 | } 57 | } catch (Exception e) { 58 | logger.info("从缓存中取�?�失�?..........."); 59 | return invocation.invoke(); 60 | } 61 | return invocation.invoke(); 62 | } 63 | 64 | /** 65 | * 发�?�Ajax请求结果json 66 | * 67 | * @throws ServletException 68 | * @throws IOException 69 | */ 70 | private void sendAjaxResultByJson(String json) { 71 | this.getResponse().setContentType("application/json;charset=UTF-8"); 72 | this.getResponse().setCharacterEncoding("UTF-8"); 73 | try { 74 | PrintWriter out = this.getResponse().getWriter(); 75 | out.write(json); 76 | out.flush(); 77 | out.close(); 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | } 81 | } 82 | 83 | private HttpServletResponse getResponse() { 84 | return ServletActionContext.getResponse(); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /rop-core/src/test/java/com/prowo/test/HelloWorldTest.java: -------------------------------------------------------------------------------- 1 | package com.prowo.test; 2 | 3 | import com.caucho.hessian.client.HessianProxyFactory; 4 | import com.prowo.common.rop.RopRequestBody; 5 | import com.prowo.common.rop.RopResponse; 6 | import com.prowo.common.utils.HttpUtils; 7 | import com.prowo.test.handle.IHelloWorld; 8 | import com.prowo.test.request.RopHelloWorldRequest; 9 | import com.prowo.test.response.RopHelloWorldResponse; 10 | 11 | import java.net.MalformedURLException; 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | import org.junit.Test; 16 | 17 | public class HelloWorldTest { 18 | 19 | @Test 20 | public void sayHelloTest() { 21 | String url = "http://localhost:8080/rop-war/router/rest.do"; 22 | 23 | Map requestMap = new HashMap(); 24 | requestMap.put("method", "api.com.test.sayHello"); 25 | requestMap.put("version", "1.0.0"); 26 | requestMap.put("username", "linan"); 27 | 28 | String result = HttpUtils.post(url, requestMap); 29 | System.out.println(result); 30 | } 31 | 32 | @Test 33 | public void sayHelloTest2() throws MalformedURLException { 34 | String url = "http://localhost:8080/rop-war/remote/helloWorldHandler"; 35 | 36 | HessianProxyFactory factory = new HessianProxyFactory(); 37 | IHelloWorld hello = (IHelloWorld) factory.create(IHelloWorld.class, url); 38 | 39 | RopHelloWorldRequest requestBean = new RopHelloWorldRequest(); 40 | requestBean.setUsername("linan"); 41 | requestBean.setApiVersion("1.0.0"); 42 | requestBean.setMethod("api.com.test.sayHello"); 43 | 44 | RopRequestBody request = new RopRequestBody(); 45 | request.setT(requestBean); 46 | 47 | RopResponse response = hello.sayHello(request); 48 | System.out.println(response.getData().toString()); 49 | 50 | } 51 | 52 | public static void main2() throws MalformedURLException { 53 | // String url = 54 | // "http://10.43.1.138:9990/new-points-war/remote/helloWorldImpl"; 55 | HessianProxyFactory factory = new HessianProxyFactory(); 56 | // IHelloWorld hello = 57 | // (IHelloWorld)factory.create(IHelloWorld.class,url); 58 | // RopHelloWorldRequest requestBean = new RopHelloWorldRequest(); 59 | // RopRequestBody request = new 60 | // RopRequestBody(); 61 | // requestBean.setUsername("linan"); 62 | // request.setT(requestBean); 63 | 64 | // RopResponse response =hello.sayHello(request); 65 | // System.out.println(response.getData().getContent()); 66 | 67 | // HessianProxyFactory factory = new HessianProxyFactory(); 68 | // //String url1 = 69 | // "http://10.43.1.138:9990/new-points-war/remote/queryPointsServiceImpl"; 70 | // String url1 = 71 | // "http://localhost:8080/new-points-war/remote/queryPointsServiceImpl"; 72 | // IQueryPointsService queryPointsService = 73 | // (IQueryPointsService)factory.create(IQueryPointsService.class,url1); 74 | // QueryPointsRopRequest requestBean1 = new QueryPointsRopRequest(); 75 | // RopRequestBody request1 = new 76 | // RopRequestBody(); 77 | // request1.setT(requestBean1); 78 | // RopResponse response1 = 79 | // queryPointsService.queryPoints(request1); 80 | // System.out.println(response1.getData().toString()); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/log/LogStack.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.log; 2 | 3 | import org.apache.commons.collections.CollectionUtils; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | public class LogStack { 10 | 11 | public enum LogStackType { 12 | PROCEED, END 13 | } 14 | 15 | private Long startTime; 16 | 17 | private Long endTime; 18 | 19 | private String tag; 20 | 21 | private int deep = 1; 22 | 23 | private List texts = new ArrayList(); 24 | 25 | private List subLogs; 26 | 27 | private List seq = new ArrayList(); 28 | 29 | private LogStackType type = LogStackType.PROCEED; 30 | 31 | public LogStack() { 32 | } 33 | 34 | public LogStack(String tag) { 35 | this.tag = tag; 36 | } 37 | 38 | public static LogStack create(String tag) { 39 | LogStack stack = new LogStack(tag); 40 | stack.start(); 41 | return stack; 42 | } 43 | 44 | public void start() { 45 | startTime = System.currentTimeMillis(); 46 | } 47 | 48 | public void end() { 49 | endTime = System.currentTimeMillis(); 50 | type = LogStackType.END; 51 | } 52 | 53 | public Long getStartTime() { 54 | return startTime; 55 | } 56 | 57 | public void setStartTime(Long startTime) { 58 | this.startTime = startTime; 59 | } 60 | 61 | public Long getEndTime() { 62 | return endTime; 63 | } 64 | 65 | public void setEndTime(Long endTime) { 66 | this.endTime = endTime; 67 | } 68 | 69 | public List getSubLogs() { 70 | return subLogs; 71 | } 72 | 73 | public void setSubLogs(List subLogs) { 74 | this.subLogs = subLogs; 75 | } 76 | 77 | public LogStackType getType() { 78 | return type; 79 | } 80 | 81 | public void setType(LogStackType type) { 82 | this.type = type; 83 | } 84 | 85 | public String getTag() { 86 | return tag; 87 | } 88 | 89 | public void setTag(String tag) { 90 | this.tag = tag; 91 | } 92 | 93 | public List getTexts() { 94 | return texts; 95 | } 96 | 97 | public void setTexts(List texts) { 98 | this.texts = texts; 99 | } 100 | 101 | public List getSeq() { 102 | return seq; 103 | } 104 | 105 | public void setSeq(List seq) { 106 | this.seq = seq; 107 | } 108 | 109 | public int getDeep() { 110 | return deep; 111 | } 112 | 113 | public void setDeep(int deep) { 114 | this.deep = deep; 115 | } 116 | 117 | public Long getMillis() { 118 | if (endTime == null || startTime == null) { 119 | return -1L; 120 | } 121 | return endTime - startTime; 122 | } 123 | 124 | @Override 125 | public String toString() { 126 | StringBuilder sb = new StringBuilder("[").append(tag).append(']'); 127 | sb.append(" costTime: ").append(getMillis()).append("ms"); 128 | if (CollectionUtils.isEmpty(seq)) { 129 | return sb.toString(); 130 | } 131 | 132 | sb.append('\n'); 133 | for (int i = 0; i < seq.size(); i++) { 134 | sb.append(StringUtils.repeat('\t', deep)).append(seq.get(i)); 135 | if (i != seq.size() - 1) { 136 | sb.append('\n'); 137 | } 138 | } 139 | return sb.toString(); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/common/utils/HttpUtils.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.utils; 2 | 3 | import java.io.IOException; 4 | import java.io.UnsupportedEncodingException; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import org.apache.http.HttpEntity; 10 | import org.apache.http.HttpResponse; 11 | import org.apache.http.NameValuePair; 12 | import org.apache.http.ParseException; 13 | import org.apache.http.client.ClientProtocolException; 14 | import org.apache.http.client.entity.UrlEncodedFormEntity; 15 | import org.apache.http.client.methods.HttpPost; 16 | import org.apache.http.client.methods.HttpUriRequest; 17 | import org.apache.http.impl.client.DefaultHttpClient; 18 | import org.apache.http.message.BasicNameValuePair; 19 | import org.apache.http.protocol.HTTP; 20 | import org.apache.http.util.EntityUtils; 21 | 22 | public class HttpUtils { 23 | public static String post(String url, Map params) { 24 | DefaultHttpClient httpclient = new DefaultHttpClient(); 25 | String body = null; 26 | HttpPost post = postForm(url, params); 27 | body = invoke(httpclient, post); 28 | httpclient.getConnectionManager().shutdown(); 29 | return body; 30 | } 31 | private static String invoke(DefaultHttpClient httpclient, 32 | HttpUriRequest httpost) { 33 | 34 | HttpResponse response = sendRequest(httpclient, httpost); 35 | String body = paseResponse(response); 36 | 37 | return body; 38 | } 39 | 40 | private static HttpResponse sendRequest(DefaultHttpClient httpclient, 41 | HttpUriRequest httpost) { 42 | // log.info("execute post..."); 43 | HttpResponse response = null; 44 | 45 | try { 46 | response = httpclient.execute(httpost); 47 | } catch (ClientProtocolException e) { 48 | e.printStackTrace(); 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } 52 | return response; 53 | } 54 | 55 | private static HttpPost postForm(String url, Map params){ 56 | 57 | HttpPost httpost = new HttpPost(url); 58 | List nvps = new ArrayList (); 59 | 60 | Set keySet = params.keySet(); 61 | for(String key : keySet) { 62 | nvps.add(new BasicNameValuePair(key, params.get(key))); 63 | } 64 | 65 | try { 66 | // log.info("set utf-8 form entity to httppost"); 67 | httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 68 | } catch (UnsupportedEncodingException e) { 69 | e.printStackTrace(); 70 | } 71 | 72 | return httpost; 73 | } 74 | private static String paseResponse(HttpResponse response) { 75 | // log.info("get response from http server.."); 76 | HttpEntity entity = response.getEntity(); 77 | 78 | //log.info("response status: " + response.getStatusLine()); 79 | String charset = EntityUtils.getContentCharSet(entity); 80 | // log.info(charset); 81 | 82 | String body = null; 83 | try { 84 | body = EntityUtils.toString(entity); 85 | //log.info(body); 86 | } catch (ParseException e) { 87 | e.printStackTrace(); 88 | } catch (IOException e) { 89 | e.printStackTrace(); 90 | } 91 | 92 | return body; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/web/Router.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.web; 2 | 3 | import java.security.NoSuchAlgorithmException; 4 | 5 | import org.apache.commons.lang3.StringUtils; 6 | import org.apache.log4j.Logger; 7 | import org.apache.struts2.convention.annotation.Action; 8 | import org.apache.struts2.convention.annotation.InterceptorRef; 9 | import org.apache.struts2.convention.annotation.ParentPackage; 10 | import org.apache.struts2.convention.annotation.ResultPath; 11 | 12 | import com.prowo.common.rop.RopRequest; 13 | import com.prowo.common.rop.RopResponse; 14 | import com.prowo.common.util.FastJsonUtils; 15 | import com.prowo.common.util.MD5; 16 | import com.prowo.common.utils.StringUtil; 17 | import com.prowo.common.utils.ZipUtils; 18 | import com.prowo.rop.context.RopContext; 19 | import com.prowo.rop.intercept.LogInterceptor; 20 | import com.prowo.rop.memcached.MemcachedUtil; 21 | import com.prowo.rop.utils.Representation; 22 | import com.prowo.rop.utils.ServiceMethodHandler; 23 | 24 | @ParentPackage("ropContextInterceptorPackage") 25 | @ResultPath("/ropInterceptor") 26 | public class Router extends BaseAction{ 27 | private static final long serialVersionUID = 2815915261546802210L; 28 | 29 | private static final int DEFAULT_CACHE_SECENDS = 600; 30 | 31 | private final static Logger logger = Logger 32 | .getLogger(Router.class); 33 | private RopContext ropContext; 34 | private String method; 35 | @Action(value="/router/rest", interceptorRefs = { 36 | @InterceptorRef(value = "ropInterceptor"), 37 | @InterceptorRef(value = "defaultStack"), 38 | //@InterceptorRef(value = "preventSQLInjectInterceptor"), 39 | //@InterceptorRef(value = "forceUpgradeInterceptor"), 40 | @InterceptorRef(value = "clientDefaultStack"), 41 | //@InterceptorRef(value = "cacheInterceptor") 42 | }) 43 | public void router() throws Exception{ 44 | ServiceMethodHandler handler = ropContext.getServiceHandler(); 45 | Object response = handler.getHandlerMethod().invoke(handler.getHandler(), ropContext.getRequestData()); 46 | 47 | if(response instanceof Representation){ 48 | Representation re = (Representation)response; 49 | re.outPut(ropContext.getResponse()); 50 | return; 51 | } 52 | //设置DEBUG信息 53 | LogInterceptor.setDebugMsg(ropContext.getRequest(), response); 54 | 55 | RopRequest baseRequest = (RopRequest)ropContext.getRequestData().getT(); 56 | String dataStr = ""; 57 | if(baseRequest!=null && baseRequest.isCheckVersion()){ 58 | RopResponse repResponse = (RopResponse)response; 59 | Object jsonObjTargetStr = FastJsonUtils.toJson(repResponse.getData()); 60 | String version = ""; 61 | 62 | try { 63 | version = MD5.encode((String)jsonObjTargetStr); 64 | repResponse.setData(null); 65 | repResponse.setVersion(version); 66 | 67 | 68 | } catch (NoSuchAlgorithmException e) { 69 | // TODO Auto-generated catch block 70 | e.printStackTrace(); 71 | } 72 | } 73 | // 涓轰簡璺ㄥ煙璁块棶鑰屼慨姝� 74 | String callBack = baseRequest.getRequest().getParameter("callback"); 75 | 76 | dataStr = FastJsonUtils.toJsonFilterProperties(response,baseRequest.getAppVersion(),baseRequest.isIpad()).toString(); 77 | 78 | //浠庣紦瀛樹腑鍙� 79 | if(handler.isCached() && !baseRequest.isCheckVersion()){ 80 | String cacheKey = baseRequest.getCacheKey(); 81 | int cacheSecends = baseRequest.getCacheSecends() == 0 ? DEFAULT_CACHE_SECENDS : baseRequest.getCacheSecends(); 82 | MemcachedUtil memcachedUtil = MemcachedUtil.getInstance(); 83 | boolean cacheFlag = memcachedUtil.set(cacheKey, cacheSecends, ZipUtils.gzip(dataStr)); 84 | logger.info(StringUtils.join(handler.getHandlerMethod(), (cacheFlag == true ? "GZIP娣诲姞缂撳瓨鎴愬姛" : "GZIP娣诲姞缂撳瓨澶辫触"))); 85 | } 86 | 87 | // 璇锋眰绫诲瀷涓簀sonp鐨勫満鍚� 88 | if (StringUtil.isNotEmpty(callBack)) { 89 | dataStr = "jsonpCallBack(" + dataStr + ")"; 90 | 91 | } 92 | 93 | this.sendAjaxResultByJson(dataStr); 94 | } 95 | 96 | public RopContext getRopContext() { 97 | return ropContext; 98 | } 99 | 100 | public String getMethod() { 101 | return method; 102 | } 103 | 104 | public void setMethod(String method) { 105 | this.method = method; 106 | } 107 | 108 | public void setRopContext(RopContext ropContext) { 109 | this.ropContext = ropContext; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /rop-war/src/main/resources/struts.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 50 | 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/intercept/RopContextIntercept.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.intercept; 2 | 3 | import java.lang.reflect.InvocationTargetException; 4 | import java.lang.reflect.ParameterizedType; 5 | import java.lang.reflect.Type; 6 | import java.util.Date; 7 | import java.util.Map; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | import org.apache.commons.beanutils.BeanUtils; 13 | import org.apache.commons.beanutils.ConvertUtils; 14 | import org.apache.commons.logging.Log; 15 | import org.apache.commons.logging.LogFactory; 16 | import org.apache.struts2.ServletActionContext; 17 | 18 | import com.opensymphony.xwork2.ActionContext; 19 | import com.opensymphony.xwork2.ActionInvocation; 20 | import com.opensymphony.xwork2.interceptor.Interceptor; 21 | import com.prowo.common.rop.RopRequest; 22 | import com.prowo.common.rop.RopRequestBody; 23 | import com.prowo.common.utils.DateConverter; 24 | import com.prowo.rop.context.RopContext; 25 | import com.prowo.rop.context.RopServiceHandler; 26 | import com.prowo.rop.utils.ServiceMethodHandler; 27 | 28 | public class RopContextIntercept implements Interceptor { 29 | private static final Log log = LogFactory.getLog(RopContextIntercept.class); 30 | private static final long serialVersionUID = 6249695319594379478L; 31 | private RopServiceHandler ropServiceHandler; 32 | 33 | public void destroy() { 34 | } 35 | 36 | public void init() { 37 | } 38 | 39 | public String intercept(ActionInvocation invocation) throws Exception { 40 | log.debug("init rop intercept"); 41 | String method = this.getRequest().getParameter("method"); 42 | String version = this.getRequest().getParameter("version"); 43 | ServiceMethodHandler handler = null; 44 | handler = ropServiceHandler.getHandler(method, version); 45 | 46 | Map parameterMap = invocation 47 | .getInvocationContext().getParameters(); 48 | final ActionContext context = invocation.getInvocationContext(); 49 | RopRequestBody o = this.buildParams(handler, parameterMap); 50 | log.debug("method:"+method+"|version:"+version); 51 | RopContext rc = new RopContext(); 52 | rc.setRequestData(o); 53 | rc.setServiceHandler(handler); 54 | rc.setRequest(getRequest()); 55 | rc.setResponse(getResponse()); 56 | 57 | invocation.getInvocationContext().getParameters() 58 | .put("ropContext", rc); 59 | return invocation.invoke(); 60 | 61 | 62 | } 63 | 64 | /** 65 | * 鏋勫缓鍙傛暟 66 | * 67 | * @param handler 68 | * @return 69 | * @throws InstantiationException 70 | * @throws IllegalAccessException 71 | * @throws InvocationTargetException 72 | * @throws IllegalArgumentException 73 | */ 74 | private RopRequestBody buildParams(ServiceMethodHandler handler, 75 | final Map parameterMap) 76 | throws InstantiationException, IllegalAccessException, 77 | IllegalArgumentException, InvocationTargetException { 78 | Type[] typs = handler.getHandlerMethod().getGenericParameterTypes(); 79 | Type t = typs[0]; 80 | Class obj = null; 81 | if (t instanceof ParameterizedType)/**//* 濡傛灉鏄硾鍨嬬被鍨� */{ 82 | Type[] types = ((ParameterizedType) t).getActualTypeArguments();// 娉涘瀷绫诲瀷鍒楄〃 83 | for (Type type : types) { 84 | obj = (Class) type; 85 | } 86 | } 87 | Object object = obj; 88 | object = obj.newInstance(); 89 | 90 | final RopRequest requestBody = (RopRequest) object; 91 | if(object instanceof RopRequest){ 92 | RopRequest ropRequest = (RopRequest)object; 93 | ropRequest.setRequest(getRequest()); 94 | ropRequest.setResponse(getResponse()); 95 | } 96 | 97 | DateConverter dateConverter = new DateConverter(); 98 | ConvertUtils.register(dateConverter, Date.class);// 娉ㄥ唽銏犱釜鏃ユ湡绫� 99 | //log.info(JSONObject.toJSON(parameterMap).toString()); 100 | RopRequestBody body = null; 101 | try { 102 | BeanUtils.populate(requestBody, parameterMap); 103 | body = new RopRequestBody(); 104 | body.setT(requestBody); 105 | 106 | } catch (Exception e) { 107 | log.error(e.getMessage()); 108 | } 109 | return body; 110 | } 111 | 112 | /** 113 | * 鑾峰彇HttpRequest 114 | * 115 | * @return 116 | */ 117 | private HttpServletRequest getRequest() { 118 | return ServletActionContext.getRequest(); 119 | } 120 | 121 | private HttpServletResponse getResponse() { 122 | return ServletActionContext.getResponse(); 123 | } 124 | 125 | public RopServiceHandler getRopServiceHandler() { 126 | return ropServiceHandler; 127 | } 128 | 129 | public void setRopServiceHandler(RopServiceHandler ropServiceHandler) { 130 | this.ropServiceHandler = ropServiceHandler; 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /rop-war/src/main/resources/context/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 17 | 18 | 19 | file:${pointsPath}/conf/command.properties 20 | file:${pointsPath}/conf/jdbc.properties 21 | file:${pointsPath}/conf/redis.properties 22 | file:${pointsPath}/conf/host.properties 23 | 24 | 25 | 26 | 27 | 28 | 29 | 31 | 32 | ${jdbc.driverClassName} 33 | 34 | 35 | 5 36 | 37 | 38 | 300 39 | 40 | 41 | 5 42 | 43 | 44 | 100000 45 | 46 | 47 | true 48 | 49 | 50 | SELECT COUNT(*) FROM DUAL 51 | 52 | 53 | 54 | 55 | 56 | 57 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 88 | 89 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/util/InternetProtocol.java: -------------------------------------------------------------------------------- 1 | 2 | package com.prowo.common.util; 3 | 4 | import org.apache.commons.lang3.StringUtils; 5 | import org.apache.log4j.Logger; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | 9 | public final class InternetProtocol { 10 | private static Logger log = Logger.getLogger(InternetProtocol.class); 11 | 12 | /** 13 | * 构造函数. 14 | */ 15 | private InternetProtocol() { 16 | } 17 | 18 | /** 19 | * 获取客户端IP地址 20 | * 支持多级反向代理 21 | * 22 | * @param request HttpServletRequest 23 | * @return 客户端真实IP地址 24 | */ 25 | public static String getRemoteAddr(final HttpServletRequest request) { 26 | try { 27 | String remoteAddr = request.getHeader("X-Forwarded-For"); 28 | // 如果通过多级反向代理,X-Forwarded-For的值不止一个,而是一串用逗号分隔的IP值,此时取X-Forwarded-For中第一个非unknown的有效IP字符串 29 | if (isEffective(remoteAddr) && (remoteAddr.indexOf(",") > -1)) { 30 | String[] array = remoteAddr.split(","); 31 | for (String element : array) { 32 | if (isEffective(element)) { 33 | remoteAddr = element; 34 | break; 35 | } 36 | } 37 | } 38 | if (!isEffective(remoteAddr)) { 39 | remoteAddr = request.getHeader("X-Real-IP"); 40 | } 41 | if (!isEffective(remoteAddr)) { 42 | remoteAddr = request.getRemoteAddr(); 43 | } 44 | return remoteAddr; 45 | } catch (Exception e) { 46 | log.error("get romote ip error,error message:" + e.getMessage()); 47 | return ""; 48 | } 49 | } 50 | 51 | /** 52 | * 获取客户端源端口 53 | * 54 | * @param request 55 | * @return 56 | */ 57 | public static Long getRemotePort(final HttpServletRequest request) { 58 | try { 59 | String port = request.getHeader("remote-port"); 60 | if (StringUtils.isNotBlank(port)) { 61 | try { 62 | return Long.parseLong(port); 63 | } catch (NumberFormatException ex) { 64 | log.error("convert port to long error , port: " + port); 65 | return 0l; 66 | } 67 | } else { 68 | return 0l; 69 | } 70 | } catch (Exception e) { 71 | log.error("get romote port error,error message:" + e.getMessage()); 72 | return 0l; 73 | } 74 | } 75 | 76 | /** 77 | * 远程地址是否有效. 78 | * 79 | * @param remoteAddr 远程地址 80 | * @return true代表远程地址有效,false代表远程地址无效 81 | */ 82 | private static boolean isEffective(final String remoteAddr) { 83 | boolean isEffective = false; 84 | if ((null != remoteAddr) && (!"".equals(remoteAddr.trim())) 85 | && (!"unknown".equalsIgnoreCase(remoteAddr.trim()))) { 86 | isEffective = true; 87 | } 88 | return isEffective; 89 | } 90 | 91 | // /** 92 | // * 判断是否是内网IP 93 | // * 94 | // * */ 95 | // public static boolean isInnerIP(String ipAddress) { 96 | // boolean isInnerIp = false; 97 | // long ipNum = IPMap.stringToLong(ipAddress); 98 | // /** 99 | // * 私有IP:A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 100 | // * 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址 101 | // **/ 102 | // long aBegin = IPMap.stringToLong("10.0.0.0"); 103 | // long aEnd = IPMap.stringToLong("10.255.255.255"); 104 | // long bBegin = IPMap.stringToLong("172.16.0.0"); 105 | // long bEnd = IPMap.stringToLong("172.31.255.255"); 106 | // long cBegin = IPMap.stringToLong("192.168.0.0"); 107 | // long cEnd = IPMap.stringToLong("192.168.255.255"); 108 | // isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || ipAddress.equals("127.0.0.1"); 109 | // return isInnerIp; 110 | // } 111 | 112 | // /** 113 | // * 限制公司对外IP 公司IP返回true 114 | // * @param ipAddress 115 | // * @return 116 | // */ 117 | // public static boolean isInnerIP2(String ipAddress) { 118 | // boolean isInnerIp = false; 119 | // long ipNum = IPMap.stringToLong(ipAddress); 120 | // long aBegin = IPMap.stringToLong("180.169.51.82"); 121 | // long aEnd = IPMap.stringToLong("180.169.51.94"); 122 | // long bBegin = IPMap.stringToLong("27.115.86.218"); 123 | // long bEnd = IPMap.stringToLong("27.115.86.222"); 124 | // long cBegin = IPMap.stringToLong("222.66.131.98"); 125 | // long cEnd = IPMap.stringToLong("222.66.131.98"); 126 | // long dBegin = IPMap.stringToLong("101.231.197.30"); 127 | // long dEnd = IPMap.stringToLong("101.231.197.30"); 128 | // isInnerIp = isInner(ipNum, aBegin, aEnd) || isInner(ipNum, bBegin, bEnd) || isInner(ipNum, cBegin, cEnd) || isInner(ipNum,dBegin,dEnd) || ipAddress.equals("127.0.0.1"); 129 | // return isInnerIp; 130 | // } 131 | 132 | 133 | private static boolean isInner(long userIp, long begin, long end) { 134 | return (userIp >= begin) && (userIp <= end); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/context/RopServiceHandler.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.context; 2 | 3 | import java.lang.reflect.Method; 4 | import java.text.MessageFormat; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import org.apache.log4j.Logger; 9 | import org.springframework.beans.BeansException; 10 | import org.springframework.beans.factory.InitializingBean; 11 | import org.springframework.context.ApplicationContext; 12 | import org.springframework.context.ApplicationContextAware; 13 | import org.springframework.core.annotation.AnnotationUtils; 14 | import org.springframework.stereotype.Component; 15 | import org.springframework.util.ReflectionUtils; 16 | import org.springframework.util.ReflectionUtils.MethodCallback; 17 | import org.springframework.util.ReflectionUtils.MethodFilter; 18 | 19 | import com.prowo.rop.annotation.ApiMethod; 20 | import com.prowo.rop.exception.RedefinedException; 21 | import com.prowo.rop.exception.RopException; 22 | import com.prowo.rop.utils.ServiceConstants; 23 | import com.prowo.rop.utils.ServiceMethodHandler; 24 | 25 | @Component 26 | public class RopServiceHandler implements ApplicationContextAware, 27 | InitializingBean { 28 | private static final Logger log = Logger.getLogger(RopServiceHandler.class); 29 | private ApplicationContext applicationContext; 30 | 31 | // void addServiceMethod(String method,String version,String beanName); 32 | private static Map> methodHandlerPool; 33 | 34 | public RopServiceHandler() { 35 | methodHandlerPool = new HashMap>(); 36 | } 37 | 38 | public void addHandlerMathod(String method, String version, 39 | ServiceMethodHandler handler) { 40 | 41 | Map map = methodHandlerPool.get(method); 42 | if (map == null) { 43 | map = new HashMap(); 44 | } 45 | 46 | ServiceMethodHandler targetHandler = map.get(version); 47 | 48 | if (targetHandler != null) { 49 | log.error("重复定义方法和属性:" + targetHandler.getVersion() + "|" 50 | + targetHandler.getClass().getName() + "|" 51 | + targetHandler.getHandlerMethod().getName()); 52 | throw new RedefinedException(); 53 | } 54 | map.put(version, handler); 55 | methodHandlerPool.put(method, map); 56 | } 57 | 58 | public ServiceMethodHandler getHandler(String method, String version) { 59 | ServiceMethodHandler h = null; 60 | 61 | if (version == null) { 62 | version = ServiceConstants.VERSIONS.ALL.name(); 63 | } 64 | 65 | Map map = methodHandlerPool.get(method); 66 | 67 | if (map == null) { 68 | throw new RopException(method + " 未找到"); 69 | } 70 | 71 | h = map.get(version); 72 | 73 | if (h == null) { 74 | throw new RopException(method 75 | + MessageFormat.format(" 对应{0}版本 未找到", h)); 76 | } 77 | 78 | return h; 79 | } 80 | 81 | public void setApplicationContext(ApplicationContext applicationContext) 82 | throws BeansException { 83 | this.applicationContext = applicationContext; 84 | } 85 | 86 | public void afterPropertiesSet() throws Exception { 87 | // 初始化 applicationContext6 88 | try { 89 | String[] beanNames = this.applicationContext 90 | .getBeanNamesForType(Object.class); 91 | for (final String beanName : beanNames) { 92 | Class handlerType = this.applicationContext 93 | .getType(beanName); 94 | ReflectionUtils.doWithMethods(handlerType, 95 | new MethodCallback() { 96 | public void doWith(Method method) 97 | throws IllegalArgumentException, 98 | IllegalAccessException { 99 | ApiMethod apimethod = method 100 | .getAnnotation(ApiMethod.class); 101 | if (apimethod != null) { 102 | /** 103 | * 检查aop 拦截器初始化 104 | */ 105 | if (apimethod.InterceptorRef().length > 0) { 106 | for (String interceptor : apimethod 107 | .InterceptorRef()) { 108 | Object aopInterceptor = applicationContext 109 | .getBean(interceptor); 110 | } 111 | } 112 | 113 | ServiceMethodHandler serviceMethodHandler = new ServiceMethodHandler(); 114 | serviceMethodHandler 115 | .setHandler(applicationContext 116 | .getBean(beanName)); // handler 117 | serviceMethodHandler 118 | .setHandlerMethod(method); // handler'method 119 | serviceMethodHandler.setCached(apimethod 120 | .cached()); 121 | log.info("初始化 服务方法:" + apimethod.method() 122 | + "| 版本号:" + apimethod.version()); 123 | addHandlerMathod(apimethod.method(), 124 | apimethod.version(), 125 | serviceMethodHandler); 126 | } 127 | } 128 | }, new MethodFilter() { 129 | 130 | public boolean matches(Method method) { 131 | return AnnotationUtils.findAnnotation(method, 132 | ApiMethod.class) != null; 133 | } 134 | }); 135 | } 136 | 137 | log.info("共 初始化 " + methodHandlerPool.size() + " 个服务方法"); 138 | } catch (Exception ex) { 139 | ex.printStackTrace(); 140 | } 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.prowo 7 | rop-root 8 | pom 9 | 1.0-SNAPSHOT 10 | 11 | 12 | rop-api 13 | rop-common 14 | rop-core 15 | rop-service 16 | rop-persistence 17 | rop-war 18 | 19 | 20 | 21 | 4.3.2.RELEASE 22 | UTF-8 23 | UTF-8 24 | 25 | 26 | 27 | 28 | javax.servlet 29 | servlet-api 30 | 2.5 31 | 32 | 33 | junit 34 | junit 35 | 4.11 36 | 37 | 38 | log4j 39 | log4j 40 | 1.2.17 41 | 42 | 43 | com.alibaba 44 | fastjson 45 | 1.2.7 46 | 47 | 48 | org.apache.commons 49 | commons-lang3 50 | 3.4 51 | 52 | 53 | org.apache.httpcomponents 54 | httpclient 55 | 4.2.1 56 | 57 | 58 | org.springframework 59 | spring-core 60 | ${spring-version} 61 | 62 | 63 | org.springframework 64 | spring-context 65 | ${spring-version} 66 | 67 | 68 | org.springframework 69 | spring-webmvc 70 | ${spring-version} 71 | 72 | 73 | org.springframework 74 | spring-jdbc 75 | ${spring-version} 76 | 77 | 78 | org.springframework.data 79 | spring-data-redis 80 | 1.7.2.RELEASE 81 | 82 | 83 | redis.clients 84 | jedis 85 | 2.6.2 86 | 87 | 88 | commons-collections 89 | commons-collections 90 | 3.2.1 91 | 92 | 93 | com.caucho 94 | hessian 95 | 4.0.7 96 | 97 | 98 | com.google.code.gson 99 | gson 100 | 2.2.4 101 | 102 | 103 | org.apache.struts 104 | struts2-spring-plugin 105 | 2.3.16.2 106 | 107 | 108 | org.apache.struts 109 | struts2-convention-plugin 110 | 2.3.16.2 111 | 112 | 113 | org.apache.struts 114 | struts2-json-plugin 115 | 2.3.16.2 116 | 117 | 118 | commons-beanutils 119 | commons-beanutils 120 | 1.8.3 121 | 122 | 123 | commons-dbcp 124 | commons-dbcp 125 | 1.4 126 | 127 | 128 | commons-configuration 129 | commons-configuration 130 | 1.6 131 | 132 | 133 | org.mybatis 134 | mybatis 135 | 3.2.8 136 | 137 | 138 | org.mybatis 139 | mybatis-spring 140 | 1.2.3 141 | 142 | 143 | com.whalin 144 | Memcached-Java-Client 145 | 3.0.0 146 | 147 | 148 | 149 | 150 | 151 | 152 | org.apache.maven.plugins 153 | maven-compiler-plugin 154 | 2.3.2 155 | 156 | 1.6 157 | 1.6 158 | 159 | 160 | 161 | 162 | 163 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/util/MD5.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.util; 2 | 3 | import org.apache.http.protocol.HTTP; 4 | 5 | import java.io.UnsupportedEncodingException; 6 | import java.security.MessageDigest; 7 | import java.security.NoSuchAlgorithmException; 8 | import java.util.Arrays; 9 | 10 | public class MD5 { 11 | private static final String ENCODE = "GBK"; //UTF-8 12 | public static final String KEY_EBK_USER_PASSWORD = "KEY_EBK_USER_PASSWORD"; //EBK_USER加码key 13 | 14 | public static final String KEY_TNT_USER_PASSWORD = "KEY_TNT_USER_PASSWORD"; 15 | 16 | public static String md5(String str) { 17 | try { 18 | return new MD5().code(str); 19 | } catch (Exception e) { 20 | e.printStackTrace(); 21 | } 22 | return null; 23 | } 24 | 25 | public static String transStringMD5(String str, Integer beginIndex, Integer endIndex) { 26 | try { 27 | String passMD5 = new MD5().code(str); 28 | String rst = passMD5.substring(beginIndex, endIndex); 29 | return rst; 30 | } catch (NoSuchAlgorithmException e) { 31 | e.printStackTrace(); 32 | return str; 33 | } 34 | } 35 | 36 | public String code(String str) throws NoSuchAlgorithmException { 37 | MessageDigest alga; 38 | String myinfo = str; 39 | alga = MessageDigest.getInstance("MD5"); 40 | alga.update(myinfo.getBytes()); 41 | byte[] digesta = alga.digest(); 42 | String hs = ""; 43 | String stmp = ""; 44 | for (int n = 0; n < digesta.length; n++) { 45 | stmp = (Integer.toHexString(digesta[n] & 0XFF)); 46 | if (stmp.length() == 1) 47 | hs = hs + "0" + stmp; 48 | else 49 | hs = hs + stmp; 50 | } 51 | return hs.toUpperCase(); 52 | } 53 | 54 | public static String code(String aValue, String aKey) { 55 | byte k_ipad[] = new byte[64]; 56 | byte k_opad[] = new byte[64]; 57 | byte keyb[]; 58 | byte value[]; 59 | try { 60 | keyb = aKey.getBytes(ENCODE); 61 | value = aValue.getBytes(ENCODE); 62 | } catch (UnsupportedEncodingException e) { 63 | keyb = aKey.getBytes(); 64 | value = aValue.getBytes(); 65 | } 66 | Arrays.fill(k_ipad, keyb.length, 64, (byte) 54); 67 | Arrays.fill(k_opad, keyb.length, 64, (byte) 92); 68 | for (int i = 0; i < keyb.length; i++) { 69 | k_ipad[i] = (byte) (keyb[i] ^ 0x36); 70 | k_opad[i] = (byte) (keyb[i] ^ 0x5c); 71 | } 72 | MessageDigest md = null; 73 | try { 74 | md = MessageDigest.getInstance("MD5"); 75 | } catch (NoSuchAlgorithmException e) { 76 | e.printStackTrace(); 77 | return null; 78 | } 79 | md.update(k_ipad); 80 | md.update(value); 81 | byte dg[] = md.digest(); 82 | md.reset(); 83 | md.update(k_opad); 84 | md.update(dg, 0, 16); 85 | dg = md.digest(); 86 | if (dg == null) { 87 | return null; 88 | } 89 | StringBuffer output = new StringBuffer(dg.length * 2); 90 | for (int i = 0; i < dg.length; i++) { 91 | int current = dg[i] & 0xff; 92 | if (current < 16) { 93 | output.append("0"); 94 | } 95 | output.append(Integer.toString(current, 16)); 96 | } 97 | return output.toString(); 98 | } 99 | 100 | 101 | /** 102 | * MD5的算法在RFC1321 中定义 103 | * 在RFC 1321中,给出了Test suite用来检验你的实现是否正确: 104 | * MD5 ("") = d41d8cd98f00b204e9800998ecf8427e 105 | * MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661 106 | * MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72 107 | * MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0 108 | * MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b 109 | * 110 | * @author qiuguobin 111 | */ 112 | public static String encode(String str) throws NoSuchAlgorithmException { 113 | MessageDigest alga; 114 | String myinfo = str; 115 | alga = MessageDigest.getInstance("MD5"); 116 | try { 117 | alga.update(myinfo.getBytes(HTTP.UTF_8)); 118 | } catch (UnsupportedEncodingException e) { 119 | e.printStackTrace(); 120 | } 121 | byte[] digesta = alga.digest(); 122 | String hs = ""; 123 | String stmp = ""; 124 | for (int n = 0; n < digesta.length; n++) { 125 | stmp = (Integer.toHexString(digesta[n] & 0XFF)); 126 | if (stmp.length() == 1) 127 | hs = hs + "0" + stmp; 128 | else 129 | hs = hs + stmp; 130 | } 131 | return hs; 132 | } 133 | 134 | /** 135 | * 获取byte 136 | * 137 | * @param str 138 | * @return 139 | * @throws NoSuchAlgorithmException 140 | */ 141 | public static byte[] getByte(String str) throws NoSuchAlgorithmException { 142 | MessageDigest alga; 143 | String myinfo = str; 144 | alga = MessageDigest.getInstance("MD5"); 145 | try { 146 | alga.update(myinfo.getBytes(HTTP.UTF_8)); 147 | } catch (UnsupportedEncodingException e) { 148 | e.printStackTrace(); 149 | } 150 | return alga.digest(); 151 | } 152 | 153 | 154 | public static String encode16(String str) throws NoSuchAlgorithmException { 155 | return encode(str).substring(8, 24); 156 | } 157 | 158 | public static String encode32(String str) throws NoSuchAlgorithmException { 159 | return encode(str); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/common/utils/ZipUtils.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.utils; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.util.zip.GZIPInputStream; 7 | import java.util.zip.GZIPOutputStream; 8 | import java.util.zip.ZipEntry; 9 | import java.util.zip.ZipInputStream; 10 | import java.util.zip.ZipOutputStream; 11 | 12 | public class ZipUtils { 13 | 14 | /** 15 | * 16 | * 使用gzip进行压缩 17 | */ 18 | @SuppressWarnings("restriction") 19 | public static String gzip(String primStr) { 20 | if (primStr == null || primStr.length() == 0) { 21 | return primStr; 22 | } 23 | 24 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 25 | 26 | GZIPOutputStream gzip = null; 27 | try { 28 | gzip = new GZIPOutputStream(out); 29 | gzip.write(primStr.getBytes()); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } finally { 33 | if (gzip != null) { 34 | try { 35 | gzip.close(); 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | } 41 | 42 | return new sun.misc.BASE64Encoder().encode(out.toByteArray()); 43 | } 44 | 45 | /** 46 | * 47 | *

48 | * Description:使用gzip进行解压�? 49 | *

50 | * 51 | * @param compressedStr 52 | * @return 53 | */ 54 | @SuppressWarnings("restriction") 55 | public static String gunzip(String compressedStr) { 56 | if (compressedStr == null) { 57 | return null; 58 | } 59 | 60 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 61 | ByteArrayInputStream in = null; 62 | GZIPInputStream ginzip = null; 63 | byte[] compressed = null; 64 | String decompressed = null; 65 | try { 66 | compressed = new sun.misc.BASE64Decoder() 67 | .decodeBuffer(compressedStr); 68 | in = new ByteArrayInputStream(compressed); 69 | ginzip = new GZIPInputStream(in); 70 | 71 | byte[] buffer = new byte[1024]; 72 | int offset = -1; 73 | while ((offset = ginzip.read(buffer)) != -1) { 74 | out.write(buffer, 0, offset); 75 | } 76 | decompressed = out.toString(); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } finally { 80 | if (ginzip != null) { 81 | try { 82 | ginzip.close(); 83 | } catch (IOException e) { 84 | } 85 | } 86 | if (in != null) { 87 | try { 88 | in.close(); 89 | } catch (IOException e) { 90 | } 91 | } 92 | if (out != null) { 93 | try { 94 | out.close(); 95 | } catch (IOException e) { 96 | } 97 | } 98 | } 99 | 100 | return decompressed; 101 | } 102 | 103 | /** 104 | * 使用zip进行压缩 105 | * 106 | * @param str 107 | * 压缩前的文本 108 | * @return 返回压缩后的文本 109 | */ 110 | @SuppressWarnings("restriction") 111 | public static final String zip(String str) { 112 | if (str == null) 113 | return null; 114 | byte[] compressed; 115 | ByteArrayOutputStream out = null; 116 | ZipOutputStream zout = null; 117 | String compressedStr = null; 118 | try { 119 | out = new ByteArrayOutputStream(); 120 | zout = new ZipOutputStream(out); 121 | zout.putNextEntry(new ZipEntry("0")); 122 | zout.write(str.getBytes()); 123 | zout.closeEntry(); 124 | compressed = out.toByteArray(); 125 | compressedStr = new sun.misc.BASE64Encoder() 126 | .encodeBuffer(compressed); 127 | } catch (IOException e) { 128 | compressed = null; 129 | } finally { 130 | if (zout != null) { 131 | try { 132 | zout.close(); 133 | } catch (IOException e) { 134 | } 135 | } 136 | if (out != null) { 137 | try { 138 | out.close(); 139 | } catch (IOException e) { 140 | } 141 | } 142 | } 143 | return compressedStr; 144 | } 145 | 146 | /** 147 | * 使用zip进行解压�? 148 | * 149 | * @param compressed 150 | * 压缩后的文本 151 | * @return 解压后的字符�? 152 | */ 153 | @SuppressWarnings("restriction") 154 | public static final String unzip(String compressedStr) { 155 | if (compressedStr == null) { 156 | return null; 157 | } 158 | 159 | ByteArrayOutputStream out = null; 160 | ByteArrayInputStream in = null; 161 | ZipInputStream zin = null; 162 | String decompressed = null; 163 | try { 164 | byte[] compressed = new sun.misc.BASE64Decoder() 165 | .decodeBuffer(compressedStr); 166 | out = new ByteArrayOutputStream(); 167 | in = new ByteArrayInputStream(compressed); 168 | zin = new ZipInputStream(in); 169 | zin.getNextEntry(); 170 | byte[] buffer = new byte[1024]; 171 | int offset = -1; 172 | while ((offset = zin.read(buffer)) != -1) { 173 | out.write(buffer, 0, offset); 174 | } 175 | decompressed = out.toString(); 176 | } catch (IOException e) { 177 | decompressed = null; 178 | } finally { 179 | if (zin != null) { 180 | try { 181 | zin.close(); 182 | } catch (IOException e) { 183 | } 184 | } 185 | if (in != null) { 186 | try { 187 | in.close(); 188 | } catch (IOException e) { 189 | } 190 | } 191 | if (out != null) { 192 | try { 193 | out.close(); 194 | } catch (IOException e) { 195 | } 196 | } 197 | } 198 | return decompressed; 199 | } 200 | 201 | // public static void main(String[] args) { 202 | // String str = "众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人众人"; 203 | // System.out.println(gzip(str)); 204 | // System.out.println(gunzip("H4sIAAAAAAAAAHuyZ/qTXbuejJKj5ChJBAkA+CyzgjADAAA=")); 205 | // System.out.println(zip(str)); 206 | // System.out.println(unzip(zip(str))); 207 | // 208 | // System.out.println("-----------------------------------------------"); 209 | // String jsonStr = ""; 210 | // JSONObject jobj = JSONObject.fromObject(jsonStr); 211 | // System.out.println(jobj); 212 | // System.out.println(gzip(jobj.toString())); 213 | // System.out.println(gunzip("H4sIAAAAAAAAAIuOBQApu0wNAgAAAA==")); 214 | // } 215 | } -------------------------------------------------------------------------------- /rop-api/rop-api.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /rop-common/rop-common.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /rop-persistence/rop-persistence.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /rop-service/rop-service.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /rop-core/rop-core.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/memcached/MemcachedCalendarUtil.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.memcached; 2 | 3 | import java.io.IOException; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | import java.util.Properties; 9 | 10 | import org.apache.commons.lang3.StringUtils; 11 | import org.apache.commons.logging.Log; 12 | import org.apache.commons.logging.LogFactory; 13 | import org.springframework.core.io.ClassPathResource; 14 | import org.springframework.core.io.Resource; 15 | 16 | import com.whalin.MemCached.MemCachedClient; 17 | import com.whalin.MemCached.SockIOPool; 18 | 19 | public class MemcachedCalendarUtil { 20 | private static Object CALENDAR_LOCK = new Object(); 21 | private final static Log log = LogFactory.getLog(MemcachedCalendarUtil.class); 22 | private Properties properties; 23 | public final static int ONE_HOUR=3600; 24 | public final static int TWO_HOUR=7200; 25 | public final static String memcacheMapKey="MEMACACHE_MAP_KEY"; 26 | 27 | private static MemcachedCalendarUtil instance; 28 | private static MemCachedClient calendarMemCachedClient; 29 | 30 | private void initCalendarMemCached() { 31 | try { 32 | properties = new Properties(); 33 | Resource resource=new ClassPathResource("memcached.properties"); 34 | properties.load(resource.getInputStream()); 35 | //Session缓存服务器,�?,”表示配置多个memcached服务 36 | String[] secondkillServers = properties.getProperty("calendar.cache.server").replaceAll(" ", "").split(","); 37 | SockIOPool sessionPool = SockIOPool.getInstance("calendarCacheServer"); 38 | sessionPool.setServers(secondkillServers); 39 | sessionPool.setFailover(true); 40 | sessionPool.setInitConn(10); 41 | sessionPool.setMinConn(5); 42 | sessionPool.setMaxConn(50); 43 | sessionPool.setMaintSleep(30); 44 | sessionPool.setNagle(false); 45 | sessionPool.setSocketTO(30000); 46 | sessionPool.setBufferSize(1024*1024*5); 47 | sessionPool.setAliveCheck(true); 48 | sessionPool.initialize(); /* 建立MemcachedClient实例 */ 49 | calendarMemCachedClient = new MemCachedClient("calendarCacheServer"); 50 | } catch (IOException e) { 51 | log.error(e); 52 | } 53 | catch (Exception ex) { 54 | log.error(ex, ex); 55 | } 56 | } 57 | 58 | private MemcachedCalendarUtil(){ 59 | initCalendarMemCached(); 60 | } 61 | 62 | private boolean isCacheEnabled() { 63 | boolean useCache = false; 64 | try { 65 | useCache = Boolean.valueOf(properties.getProperty("cache.enable")); 66 | } catch (Exception e) { 67 | useCache = false; 68 | log.info("Please enable memcached"); 69 | } 70 | return useCache; 71 | } 72 | 73 | /** 74 | * 改用嵌套类静态实始化 75 | * @return 76 | */ 77 | public static MemcachedCalendarUtil getInstance() { 78 | if(instance == null){ 79 | synchronized(CALENDAR_LOCK) { 80 | if (instance==null) { 81 | instance=new MemcachedCalendarUtil(); 82 | } 83 | } 84 | } 85 | return instance; 86 | } 87 | 88 | public static MemCachedClient getMemCachedCalendarClient() { 89 | if(calendarMemCachedClient == null){ 90 | synchronized(CALENDAR_LOCK) { 91 | if (calendarMemCachedClient==null) { 92 | instance = new MemcachedCalendarUtil(); 93 | } 94 | } 95 | } 96 | return calendarMemCachedClient; 97 | } 98 | 99 | /** 100 | * 替换 101 | * @param key 102 | * @param seconds 过期秒数 103 | * @param obj 104 | * @return 105 | */ 106 | public boolean replace(String key, int seconds, Object obj) { 107 | if(StringUtils.isEmpty(key)){ 108 | return false; 109 | } 110 | if(obj==null){ 111 | return true; 112 | } 113 | try{ 114 | if (isCacheEnabled()) { 115 | Date expDate = getDateAfter(seconds); 116 | boolean result = getMemCachedCalendarClient().replace(key, obj, expDate); 117 | log.debug("SET A OBJECT: KEY:" + key + ", OBJ:" + obj + ", exp:" + expDate + ", result:" + result); 118 | return result; 119 | } 120 | return true; 121 | }catch(Exception e) { 122 | log.error(e); 123 | } 124 | return false; 125 | } 126 | 127 | /** 128 | * �? 129 | * @param key 130 | * @param seconds 过期秒数 131 | * @param obj 132 | * @return 133 | */ 134 | public boolean set(String key, int seconds, Object obj) { 135 | return set(key, getDateAfter(seconds), obj); 136 | } 137 | 138 | 139 | /** 140 | * 将KEY保存到memcache�? 141 | * 142 | * @param key 143 | * @param exp 144 | * @param obj 145 | * @return 146 | */ 147 | public boolean set(String key,Date exp,Object obj){ 148 | if(StringUtils.isEmpty(key)){ 149 | return false; 150 | } 151 | if(obj==null){ 152 | return true; 153 | } 154 | try{ 155 | if (isCacheEnabled()) { 156 | boolean result = getMemCachedCalendarClient().set(key, obj, exp); 157 | log.debug("SET A OBJECT: KEY:" + key + ", OBJ:" + obj + ", exp:" + exp + ", result:" + result); 158 | return result; 159 | } 160 | return true; 161 | }catch(Exception e) { 162 | log.error(e); 163 | } 164 | return false; 165 | } 166 | 167 | /** 168 | * 把相应的Key值和描述存到此方法中�? 169 | * 170 | * @param key 171 | * @param discript 172 | * @return 173 | */ 174 | @SuppressWarnings("unchecked") 175 | public boolean putKeyDisMap(String key,String discript) { 176 | Map memMap ; 177 | Object obj = getMemCachedCalendarClient().get(memcacheMapKey); 178 | if(obj == null) { 179 | memMap = new HashMap(); 180 | } else { 181 | memMap = (HashMap) obj; 182 | } 183 | memMap.put(key, discript); 184 | getMemCachedCalendarClient().set(memcacheMapKey,memMap,getDateAfter(60*60*48)); 185 | 186 | return true; 187 | } 188 | 189 | /** 190 | * �? 191 | * @param key 192 | * @param obj 193 | * @return 194 | */ 195 | public boolean set(String key, Object obj) { 196 | return set(key,ONE_HOUR,obj); 197 | } 198 | 199 | /** 200 | * �? 201 | * @param key 202 | * @return 203 | */ 204 | public Object get(String key) { 205 | try{ 206 | if (isCacheEnabled()) { 207 | Object obj = getMemCachedCalendarClient().get(key); 208 | log.debug("GET A OBJECT: KEY:" + key + " OBJ:" + obj) ; 209 | return obj; 210 | } 211 | }catch(Exception e) { 212 | log.error(e); 213 | } 214 | return null; 215 | } 216 | 217 | /** 218 | * 清除 219 | * @param key 220 | * @return 221 | */ 222 | public boolean remove(String key) { 223 | if(StringUtils.isEmpty(key)){ 224 | return false; 225 | } 226 | try{ 227 | if (isCacheEnabled()) { 228 | log.info("delete memcached key: " + key); 229 | return getMemCachedCalendarClient().delete(key); 230 | } 231 | }catch(Exception e) { 232 | log.error(e); 233 | } 234 | return true; 235 | } 236 | 237 | /** 238 | * 获得当前�?始活参数秒的时间日期 239 | * @Title: getDateAfter 240 | * @Description: 241 | * @param 242 | * @return Date 返回类型 243 | * @throws 244 | */ 245 | public static Date getDateAfter(int second) { 246 | Calendar cal = Calendar.getInstance(); 247 | cal.add(Calendar.SECOND, second); 248 | return cal.getTime(); 249 | } 250 | } 251 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/log/LogUtils.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.log; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | 8 | import org.apache.commons.collections.CollectionUtils; 9 | import org.apache.commons.lang3.StringUtils; 10 | import org.apache.log4j.Logger; 11 | import org.apache.struts2.ServletActionContext; 12 | 13 | import com.google.gson.Gson; 14 | 15 | public class LogUtils { 16 | 17 | public static final String DEBUG_LOG = "DEBUG_LOG"; 18 | 19 | /** 20 | * 开始打印日志 21 | * @param tag 22 | * @return 23 | */ 24 | public static LogStack startLogWeb(String tag) { 25 | try { 26 | LogStack root = getRootLog(); 27 | if(root == null) { 28 | return null; 29 | } 30 | return startLog(root, tag); 31 | } catch (Exception e) { 32 | //IGNORE 不能影响主流程 33 | return null; 34 | } 35 | } 36 | 37 | /** 38 | * 添加日志 39 | * @param text 40 | * @return 41 | */ 42 | public static LogStack appendLogWeb(String text) { 43 | try { 44 | LogStack root = getRootLog(); 45 | if(root == null) { 46 | return null; 47 | } 48 | return appendLog(root, text); 49 | } catch (Exception e) { 50 | //IGNORE 不能影响主流程 51 | return null; 52 | } 53 | } 54 | 55 | /** 56 | * 结束打印本次日志 57 | * @return 58 | */ 59 | public static LogStack endLogWeb() { 60 | try { 61 | LogStack root = getRootLog(); 62 | if(root == null) { 63 | return null; 64 | } 65 | return endLog(root); 66 | } catch (Exception e) { 67 | //IGNORE 不能影响主流程 68 | return null; 69 | } 70 | } 71 | 72 | /** 73 | * 打印日志并结束 74 | * @param text 日志文本 75 | * @return 76 | * @author ltwangwei 77 | * @date 2016-1-21 下午1:38:35 78 | * @since CodingExample Ver(编码范例查看) 1.1 79 | */ 80 | public static LogStack endLogWebAndPrintText(String text){ 81 | appendLogWeb(text); 82 | return endLogWeb(); 83 | } 84 | 85 | /** 86 | * 合并打印日志 87 | * 目前只支持info级别,后期扩展 88 | * @param logger log4j 89 | * @param loggerLevel 日志级别(可扩展) 90 | * @param text 日志文本 91 | * @author ltwangwei 92 | * @date 2016-1-21 下午1:47:27 93 | * @since CodingExample Ver(编码范例查看) 1.1 94 | */ 95 | public static void printLog4jAndWeb(Logger logger, String loggerLevel, String text){ 96 | if(logger != null && StringUtils.isNotBlank(text)){ 97 | logger.info(text); 98 | appendLogWeb(text); 99 | } 100 | } 101 | 102 | public static LogStack getRootLog() { 103 | HttpServletRequest request = ServletActionContext.getRequest(); 104 | return (LogStack) request.getAttribute(DEBUG_LOG); 105 | } 106 | 107 | public static LogStack initLog(String tag) { 108 | LogStack logStack = LogStack.create(tag); 109 | List subLogs = new ArrayList(); 110 | logStack.setSubLogs(subLogs); 111 | return logStack; 112 | } 113 | 114 | public static LogStack startLog(LogStack root, String tag) { 115 | LogStack pendingLog = getPendingLog(root); 116 | if(pendingLog == null) return null; 117 | 118 | LogStack logStack = LogStack.create(tag); 119 | logStack.setDeep(pendingLog.getDeep() + 1); 120 | List subLogs; 121 | if(pendingLog.getSubLogs() == null) { 122 | subLogs = new ArrayList(); 123 | pendingLog.setSubLogs(subLogs); 124 | } else { 125 | subLogs = pendingLog.getSubLogs(); 126 | } 127 | subLogs.add(logStack); 128 | pendingLog.getSeq().add(logStack); 129 | return logStack; 130 | } 131 | 132 | public static LogStack appendLog(LogStack root, String text) { 133 | LogStack pendingLog = getPendingLog(root); 134 | if(pendingLog == null) return null; 135 | 136 | List texts = pendingLog.getTexts(); 137 | texts.add(text); 138 | pendingLog.getSeq().add(text); 139 | return pendingLog; 140 | } 141 | 142 | public static LogStack endLog(LogStack root) { 143 | LogStack pendingLog = getPendingLog(root); 144 | if(pendingLog == null) return null; 145 | 146 | pendingLog.end(); 147 | return pendingLog; 148 | } 149 | 150 | private static LogStack getPendingLog(LogStack log) { 151 | //空或者结束,直接未找到 152 | if(log == null || log.getType() == LogStack.LogStackType.END) { 153 | return null; 154 | } 155 | //进行且没有孩子直接返回本身 156 | if(log.getType() == LogStack.LogStackType.PROCEED && CollectionUtils.isEmpty(log.getSubLogs())) { 157 | return log; 158 | } 159 | 160 | //递归遍历孩子 161 | for (LogStack subLog : log.getSubLogs()) { 162 | if(subLog.getType() == LogStack.LogStackType.PROCEED) { 163 | if(CollectionUtils.isEmpty(subLog.getSubLogs())) { 164 | return subLog; 165 | }else{ 166 | return getPendingLog(subLog); 167 | } 168 | } 169 | } 170 | //孩子都已结束,直接返回本身 171 | return log; 172 | } 173 | 174 | 175 | 176 | public static void main(String[] args) { 177 | LogStack root = LogUtils.initLog("TEST"); 178 | 179 | try { 180 | Thread.sleep(1000); 181 | }catch (Exception e){ 182 | } 183 | 184 | test1(root); 185 | test2(root); 186 | 187 | root.end(); 188 | System.out.println(new Gson().toJson(root)); 189 | System.out.println(root); 190 | } 191 | 192 | private static void test1(LogStack root) { 193 | LogUtils.startLog(root, "A"); 194 | 195 | test11(root); 196 | LogUtils.appendLog(root, "1 测试"); 197 | test12(root); 198 | 199 | LogUtils.endLog(root); 200 | } 201 | 202 | private static void test11(LogStack root) { 203 | LogUtils.startLog(root, "A-1"); 204 | try { 205 | Thread.sleep(1000); 206 | }catch (Exception e){ 207 | } 208 | LogUtils.appendLog(root, "1.1 测试"); 209 | 210 | LogUtils.endLog(root); 211 | } 212 | 213 | private static void test12(LogStack root) { 214 | LogUtils.startLog(root, "A-2"); 215 | try { 216 | Thread.sleep(1000); 217 | }catch (Exception e){ 218 | } 219 | LogUtils.appendLog(root, "1.2 测试"); 220 | 221 | LogUtils.endLog(root); 222 | } 223 | 224 | private static void test2(LogStack root) { 225 | LogUtils.startLog(root, "B"); 226 | try { 227 | Thread.sleep(1000); 228 | }catch (Exception e){ 229 | } 230 | LogUtils.appendLog(root, "2 测试"); 231 | 232 | LogUtils.endLog(root); 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /rop-common/src/main/java/com/prowo/rop/utils/ServiceConstants.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.utils; 2 | 3 | import java.io.IOException; 4 | import java.util.Properties; 5 | 6 | import org.apache.commons.lang3.StringUtils; 7 | 8 | public class ServiceConstants { 9 | public final static long DISTRIBUTOR_ID = 10000; 10 | public final static long LVYUE_DISTRIBUTOR_ID = 111; 11 | public final static long DISTRIBUTION_CHANNEL = 10000; 12 | public final static String DISTRIBUTOR_CODE = "LVTU"; 13 | 14 | /** 驴途团购分销渠道ID **/ 15 | public final static long DISTRIBUTOR_GROUPBUY_CHANNEL = 10001; 16 | /** 主站团购分销渠道ID **/ 17 | public final static long DISTRIBUTOR_GROUPBUY_CHANNEL_WWW = 108; 18 | /** 驴途秒杀分销渠道ID **/ 19 | public final static long DISTRIBUTOR_SECKILL_CHANNEL = 10002; 20 | /** 主站秒杀分销渠道ID **/ 21 | public final static long DISTRIBUTOR_SECKILL_CHANNEL_WWW = 110; 22 | 23 | public static final String VALIDATE_CODE_PREFIX = "VALIDATE_CODE_PREFIX"; 24 | public static final String VALIDATE_RAND_CODE_PREFIX = "VALIDATE_RAND_CODE_PREFIX"; 25 | public static final String VALIDATE_CODE_DATE_PREFIX = "VALIDATE_CODE_DATE_PREFIX"; 26 | public static final String SHARE_CONTENT = "推荐“驴妈妈旅游”的一家超高性价比的酒店"; 27 | public static final String REFUND_NOT_ALLOW_TIPS = "该订单已超过最晚退款时间。如有需要请联系客服4001570570"; // 不能退款说明 28 | public static final String REFUND_NOT_ALLOW_TIPS_EIGHT = "该订单不退不改,抱歉无法申请退款"; 29 | public static final String REFUND_NOT_ALLOW_TIPS_NINE = "该订单已超过退款时间,抱歉无法申请退款"; 30 | public static final String REFUND_NOT_ALLOW_TIPS_TAOBAO = "该订单请联系淘宝客服申请退款"; 31 | public static final String REFUND_NOT_ALLOW_TIPS_OTHER = "该订单请联系客服4001570570申请退款"; 32 | public static final String REFUND_FROM = "无线客户端"; 33 | 34 | /** 支付限额 **/ 35 | public static final String CELLING_ALIPAY = "5000"; 36 | public static final String CELLING_TENPAY = "8000"; 37 | public static final String CELLING_UNIONPAY = "20000"; 38 | public static final String CELLING_CCBPAY = "50000"; 39 | public static final String CELLING_99BILLPAY = "50000"; 40 | 41 | /** 42 | * 缓存配置文件中的配置信息 ,是否需要图片验证 43 | */ 44 | private static final String NEW_VALIDATE_CODE = "config_client_need_validate_code_lv"; 45 | 46 | /** 47 | * 当前分钟内注册的用户数量 48 | */ 49 | public static final String CURRENT_MINUTE_REGISTER_KEY_PREFIX = "client_register_num_"; 50 | /** 当前分钟内登录的用户数量 */ 51 | public static final String CURRENT_MINUTE_LOGIN_KEY_PREFIX = "client_login_num_"; 52 | 53 | private static Properties properties; 54 | private static volatile ServiceConstants instance = null; 55 | 56 | private void init() { 57 | try { 58 | properties = new Properties(); 59 | properties 60 | .load(getClass().getResourceAsStream("/const.properties")); 61 | } catch (IOException e) { 62 | e.printStackTrace(); 63 | } 64 | // ResourceBundle.getBundle("const"); 65 | } 66 | 67 | private ServiceConstants() { 68 | init(); 69 | } 70 | 71 | public static ServiceConstants getInstance() { 72 | if (instance == null) { 73 | synchronized (ServiceConstants.class) { 74 | if (instance == null) { 75 | instance = new ServiceConstants(); 76 | // instance.init(); 77 | } 78 | } 79 | } 80 | return instance; 81 | } 82 | 83 | /** 84 | * 获取属性 85 | * 86 | * @return 87 | */ 88 | public String getValue(String key) { 89 | return properties.getProperty(key); 90 | } 91 | 92 | public String getAliPayAppUrl() { 93 | return properties.getProperty("alipayAppUrl"); 94 | } 95 | 96 | public String getAliPayWapUrl() { 97 | return properties.getProperty("alipayWapUrl"); 98 | } 99 | 100 | public String getUpompPayUrl() { 101 | return properties.getProperty("upompUrl"); 102 | } 103 | 104 | public String getTenpayWapUrl() { 105 | return properties.getProperty("tenpayWapUrl"); 106 | } 107 | 108 | public String getBaiduWapUrl() { 109 | return properties.getProperty("baiduWapUrl"); 110 | } 111 | 112 | public String getBaiduApppUrl() { 113 | return properties.getProperty("baiduAppUrl"); 114 | } 115 | 116 | public String getWeixinAndroidUrl() { 117 | return properties.getProperty("weixinAndroid"); 118 | } 119 | 120 | public String getWeixinIphoneUrl() { 121 | return properties.getProperty("weixinIphone"); 122 | } 123 | 124 | public String getWeixinUnifiedOrderAndroidUrl() { 125 | return properties.getProperty("weixinUnifiedOrderAndroid"); 126 | } 127 | 128 | public String getWeixinUnifiedOrderIOSUrl() { 129 | return properties.getProperty("weixinUnifiedOrderIOS"); 130 | } 131 | 132 | public String getCcbUrl() { 133 | return properties.getProperty("ccbUrl"); 134 | } 135 | 136 | public String getYeePayAppUrl() { 137 | return properties.getProperty("yeePayUrl"); 138 | } 139 | 140 | public String getBill99PayUrl() { 141 | return properties.getProperty("99billPayUrl"); 142 | } 143 | 144 | /** 145 | * @Description: 判断客户端注册是否需要短信验证码 146 | */ 147 | public static boolean needImageAuthCode() { 148 | // String needImageCode = "true"; 149 | // Long couter = 150 | // MemcachedUtil.getInstance().getCounter(NEW_VALIDATE_CODE); 151 | // if(couter==-1l){ 152 | // needImageCode = properties.getProperty("client_need_image_code"); 153 | // return "true".equals(needImageCode); 154 | // } else { 155 | // return couter>-1l; 156 | // } 157 | return false; 158 | } 159 | 160 | public static enum REFUND { 161 | FAIL("退款失败"), CANCEL("退款单取消"), REJECTED(" 不通过(拒绝、驳回)"), REFUNDED("退款成功"), PROCESSING( 162 | "银行处理中"), PROCESSING_PART("部分退款处理中"), REFUNDED_PART("部分退款完成"), VERIFIEDING( 163 | "退款审核中"); 164 | private String cnName; 165 | 166 | REFUND(String name) { 167 | this.cnName = name; 168 | } 169 | 170 | public String getCode() { 171 | return this.name(); 172 | } 173 | 174 | public String getCnName() { 175 | return this.cnName; 176 | } 177 | 178 | public static String getCnName(String code) { 179 | for (REFUND item : REFUND.values()) { 180 | if (item.getCode().equals(code)) { 181 | return item.getCnName(); 182 | } 183 | } 184 | return code; 185 | } 186 | } 187 | 188 | public static enum VERSIONS { 189 | ALL("0", "所有版本"); 190 | private String cnName; 191 | private int versionNumber; 192 | 193 | VERSIONS(String number, String cnName) { 194 | this.cnName = cnName; 195 | this.versionNumber = Integer.parseInt(number); 196 | } 197 | 198 | public String getCode() { 199 | return this.name(); 200 | } 201 | 202 | public int getVersionNumber() { 203 | return this.versionNumber; 204 | } 205 | 206 | public String getCnName() { 207 | return this.cnName; 208 | } 209 | } 210 | 211 | public static enum ACCESS_ROLE { 212 | internal("内网"); 213 | private String code; 214 | private String cnName; 215 | 216 | ACCESS_ROLE(String cnName) { 217 | this.cnName = cnName; 218 | } 219 | 220 | public String getCode() { 221 | return this.name(); 222 | } 223 | 224 | public String getCnName() { 225 | return this.cnName; 226 | } 227 | 228 | public static String getCnName(String code) { 229 | for (ACCESS_ROLE item : ACCESS_ROLE.values()) { 230 | if (item.name().equals(code)) { 231 | return item.getCnName(); 232 | } 233 | } 234 | return code; 235 | } 236 | } 237 | 238 | public static enum SALE_CHANNEL { 239 | 240 | groupbuy, // 销售渠道-团购 241 | seckill; // 销售渠道-秒杀 242 | } 243 | 244 | // 压缩图片的接口地址 245 | public String getCompressImageUrl() { 246 | String value = getValue("compressImageUrl"); 247 | if (StringUtils.isNotBlank(value)) { 248 | return value; 249 | } 250 | return ""; 251 | } 252 | 253 | public String getPicServerUrl() { 254 | 255 | String value = getValue("picServerUrl"); 256 | if (StringUtils.isNotBlank(value)) { 257 | return value; 258 | } 259 | return ""; 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /rop.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | file://$MODULE_DIR$/rop-war/src/main/resources/context/applicationContext.xml 8 | file://$MODULE_DIR$/rop-war/src/main/resources/servlet/spring-hessian-service.xml 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/rop/RopRequest.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.rop; 2 | 3 | import com.prowo.common.util.InternetProtocol; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.Serializable; 9 | 10 | /** 11 | * 客户端请求参数基类 12 | */ 13 | public class RopRequest implements Serializable { 14 | 15 | private static final long serialVersionUID = -1519300204899324513L; 16 | private String method; 17 | private String version; 18 | private String apiVersion; 19 | private String token; 20 | private String lvsessionid; 21 | private String udid; 22 | private String osVersion; 23 | private String deviceName; 24 | private String firstChannel; 25 | private String secondChannel; 26 | private String lvversion; 27 | private boolean queryCount; 28 | private HttpServletRequest request; 29 | private HttpServletResponse response; 30 | private String ip; 31 | private String customerId; 32 | 33 | /** 34 | * 缓存动态key 35 | */ 36 | private String cacheKey; 37 | 38 | /** 39 | * 缓存动态时间 40 | */ 41 | private int cacheSecends; 42 | 43 | /** 44 | * 验证码 45 | */ 46 | private String validateCode; 47 | 48 | /** 49 | * 用户ID 50 | **/ 51 | private Long userId; 52 | 53 | /** 54 | * 用户编码 55 | **/ 56 | private String userNo; 57 | 58 | /** 59 | * 是否是检查版本 60 | */ 61 | private boolean checkVersion; 62 | 63 | /** 64 | * 纬度 65 | */ 66 | private String latitude; 67 | 68 | /** 69 | * 经度 70 | */ 71 | private String longitude; 72 | 73 | // @NotNull(message="方法名不能为空!") 74 | public String getMethod() { 75 | return method; 76 | } 77 | 78 | public void setMethod(String method) { 79 | this.method = method; 80 | } 81 | 82 | // @NotNull(message="版本号不能为空!") 83 | public String getVersion() { 84 | return version; 85 | } 86 | 87 | public void setVersion(String version) { 88 | this.version = version; 89 | } 90 | 91 | public String getApiVersion() { 92 | return apiVersion; 93 | } 94 | 95 | public void setApiVersion(String apiVersion) { 96 | this.apiVersion = apiVersion; 97 | } 98 | 99 | public String getToken() { 100 | return token; 101 | } 102 | 103 | public void setToken(String token) { 104 | this.token = token; 105 | } 106 | 107 | public String getUdid() { 108 | return udid; 109 | } 110 | 111 | public void setUdid(String udid) { 112 | this.udid = udid; 113 | } 114 | 115 | public String getOsVersion() { 116 | return osVersion; 117 | } 118 | 119 | public void setOsVersion(String osVersion) { 120 | this.osVersion = osVersion; 121 | } 122 | 123 | public String getDeviceName() { 124 | return deviceName; 125 | } 126 | 127 | public void setDeviceName(String deviceName) { 128 | this.deviceName = deviceName; 129 | } 130 | 131 | public String getFirstChannel() { 132 | return firstChannel; 133 | } 134 | 135 | public void setFirstChannel(String firstChannel) { 136 | this.firstChannel = firstChannel; 137 | } 138 | 139 | public String getSecondChannel() { 140 | return secondChannel; 141 | } 142 | 143 | public void setSecondChannel(String secondChannel) { 144 | this.secondChannel = secondChannel; 145 | } 146 | 147 | public String getLvsessionid() { 148 | return lvsessionid; 149 | } 150 | 151 | public void setLvsessionid(String lvsessionid) { 152 | this.lvsessionid = lvsessionid; 153 | } 154 | 155 | public Long getUserId() { 156 | return userId; 157 | } 158 | 159 | public void setUserId(Long userId) { 160 | this.userId = userId; 161 | } 162 | 163 | public String getUserNo() { 164 | return userNo; 165 | } 166 | 167 | public void setUserNo(String userNo) { 168 | this.userNo = userNo; 169 | } 170 | 171 | public boolean isCheckVersion() { 172 | return checkVersion; 173 | } 174 | 175 | public void setCheckVersion(boolean checkVersion) { 176 | this.checkVersion = checkVersion; 177 | } 178 | 179 | public boolean isAnroid() { 180 | return "ANDROID".equals(firstChannel); 181 | } 182 | 183 | public boolean isIOS() { 184 | return "IPHONE".equals(firstChannel); 185 | } 186 | 187 | public boolean isIpad() { 188 | return "IPAD".equals(firstChannel); 189 | } 190 | 191 | public HttpServletRequest getRequest() { 192 | return request; 193 | } 194 | 195 | public void setRequest(HttpServletRequest request) { 196 | this.request = request; 197 | } 198 | 199 | public HttpServletResponse getResponse() { 200 | return response; 201 | } 202 | 203 | public void setResponse(HttpServletResponse response) { 204 | this.response = response; 205 | } 206 | 207 | public String getIp() { 208 | if (!StringUtils.isEmpty(ip)) { 209 | return ip; 210 | } else { 211 | this.ip = InternetProtocol.getRemoteAddr(getRequest()); 212 | return ip; 213 | } 214 | } 215 | 216 | public String getCachKey() { 217 | return "1"; 218 | } 219 | 220 | public void setIp(String ip) { 221 | this.ip = ip; 222 | } 223 | 224 | public String getValidateCode() { 225 | return validateCode; 226 | } 227 | 228 | public void setValidateCode(String validateCode) { 229 | this.validateCode = validateCode; 230 | } 231 | 232 | public String getCacheKey() { 233 | return cacheKey; 234 | } 235 | 236 | public void setCacheKey(String cacheKey) { 237 | this.cacheKey = cacheKey; 238 | } 239 | 240 | public int getCacheSecends() { 241 | return cacheSecends; 242 | } 243 | 244 | public void setCacheSecends(int cacheSecends) { 245 | this.cacheSecends = cacheSecends; 246 | } 247 | 248 | public String getLvversion() { 249 | return lvversion; 250 | } 251 | 252 | public void setLvversion(String lvversion) { 253 | this.lvversion = lvversion; 254 | } 255 | 256 | public String getLatitude() { 257 | return latitude; 258 | } 259 | 260 | public void setLatitude(String latitude) { 261 | this.latitude = latitude; 262 | } 263 | 264 | public String getLongitude() { 265 | return longitude; 266 | } 267 | 268 | public void setLongitude(String longitude) { 269 | this.longitude = longitude; 270 | } 271 | 272 | public int getAppVersion() { 273 | int appVersion = 0; 274 | if (StringUtils.isNotBlank(this.lvversion)) { 275 | try { 276 | appVersion = Integer.parseInt(this.lvversion.replace(".", "")); 277 | } catch (Exception e) { 278 | e.printStackTrace(); 279 | } 280 | } 281 | return appVersion; 282 | } 283 | 284 | public String getCustomerId() { 285 | return customerId; 286 | } 287 | 288 | public void setCustomerId(String customerId) { 289 | this.customerId = customerId; 290 | } 291 | 292 | public boolean isQueryCount() { 293 | return queryCount; 294 | } 295 | 296 | public void setQueryCount(boolean queryCount) { 297 | this.queryCount = queryCount; 298 | } 299 | 300 | public String getValidateCodeCacheKey() { 301 | return "VALIDATE_RAND_CODE_PREFIX" + this.lvsessionid; 302 | } 303 | 304 | @Override 305 | public String toString() { 306 | return "RopRequest [method=" + method + ", version=" + version 307 | + ", apiVersion=" + apiVersion + ", token=" + token 308 | + ", lvsessionid=" + lvsessionid + ", udid=" + udid 309 | + ", osVersion=" + osVersion + ", deviceName=" + deviceName 310 | + ", firstChannel=" + firstChannel + ", secondChannel=" 311 | + secondChannel + ", lvversion=" + lvversion + ", queryCount=" 312 | + queryCount + ", request=" + request + ", response=" 313 | + response + ", ip=" + ip + ", customerId=" + customerId 314 | + ", cacheKey=" + cacheKey + ", validateCode=" + validateCode 315 | + ", userId=" + userId + ", userNo=" + userNo 316 | + ", checkVersion=" + checkVersion + ", latitude=" + latitude 317 | + ", longitude=" + longitude + "]"; 318 | } 319 | 320 | } 321 | -------------------------------------------------------------------------------- /rop-api/src/main/java/com/prowo/common/util/FastJsonUtils.java: -------------------------------------------------------------------------------- 1 | package com.prowo.common.util; 2 | 3 | import com.alibaba.fastjson.serializer.*; 4 | import com.prowo.common.annotation.SpVersion; 5 | import org.springframework.util.ReflectionUtils; 6 | 7 | import java.lang.reflect.Field; 8 | import java.lang.reflect.Method; 9 | import java.util.*; 10 | 11 | public class FastJsonUtils { 12 | 13 | private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue,// 输出空置字段 14 | SerializerFeature.WriteNullListAsEmpty,// list字段如果为null,输出为[],而不是null 15 | SerializerFeature.WriteNullNumberAsZero,// 数值字段如果为null,输出为0,而不是null 16 | SerializerFeature.WriteNullBooleanAsFalse,// Boolean字段如果为null,输出为false,而不是null 17 | SerializerFeature.WriteNullStringAsEmpty, 18 | //SerializerFeature.WriteJavaBeanNullFieldBeatyValue,// 字符类型字段如果为null,输出为"",而不是null 19 | SerializerFeature.DisableCircularReferenceDetect 20 | }; 21 | 22 | public static Object toJson(Object object) { 23 | return FastJsonUtils.paser(object, null, false); 24 | } 25 | 26 | private static Object paser(Object object, final Long appVersion, 27 | final boolean isIpad) { 28 | ValueFilter filter = new ValueFilter() { 29 | 30 | public Object process(Object object, String name, Object value) { 31 | Field field = ReflectionUtils 32 | .findField(object.getClass(), name); 33 | 34 | if (field == null) { 35 | String method = name.replaceFirst(name.substring(0, 1), 36 | name.substring(0, 1).toUpperCase()); 37 | Method m = ReflectionUtils.findMethod(object.getClass(), 38 | "get" + method); 39 | if (object != null && m != null 40 | && m.getGenericReturnType().equals(Date.class)) { 41 | if (value == null) { 42 | return ""; 43 | } 44 | } 45 | } 46 | 47 | if (object != null && field != null 48 | && field.getGenericType() != null 49 | && field.getGenericType().toString().equals("T")) { 50 | if (value == null) { 51 | return new HashMap(); 52 | } 53 | } 54 | if (object != null && field != null 55 | && field.getType().equals(Date.class)) { 56 | if (value == null) { 57 | return ""; 58 | } 59 | } 60 | if (object != null && field != null 61 | && field.getType().equals(List.class)) { 62 | if (value == null) { 63 | return new ArrayList(); 64 | } 65 | } 66 | if (object != null && field != null 67 | && field.getType().equals(Map.class)) { 68 | if (value == null) { 69 | return new HashMap(); 70 | } 71 | } 72 | return value; 73 | 74 | } 75 | }; 76 | List filters = new ArrayList(); 77 | 78 | if (appVersion != null) { 79 | PropertyPreFilter properterFilter = new PropertyPreFilter() { 80 | 81 | public boolean apply(JSONSerializer serializer, Object object, 82 | String name) { 83 | Field field = ReflectionUtils.findField(object.getClass(), 84 | name); 85 | if (field == null) { 86 | String method = name.replaceFirst(name.substring(0, 1), 87 | name.substring(0, 1).toUpperCase()); 88 | Method m = ReflectionUtils.findMethod( 89 | object.getClass(), "get" + method); 90 | if (m != null) { 91 | 92 | /** 93 | * 当客户端版本号 小于设定的版本号就过滤属性 appVersion 客户端版本号。 94 | * spversion 支持的最低版本号 95 | * 96 | */ 97 | SpVersion spversion = m 98 | .getAnnotation(SpVersion.class); 99 | if (isIpad) { 100 | if (spversion != null 101 | && spversion.ipadGt() > appVersion 102 | .intValue()) { 103 | return false; 104 | } 105 | } else { 106 | if (spversion != null 107 | && spversion.gt() > appVersion 108 | .intValue()) { 109 | return false; 110 | } 111 | } 112 | } 113 | } else { 114 | SpVersion spversion = field 115 | .getAnnotation(SpVersion.class); 116 | 117 | if (isIpad) { 118 | if (spversion != null 119 | && spversion.ipadGt() > appVersion 120 | .intValue()) { 121 | return false; 122 | } 123 | } else { 124 | if (spversion != null 125 | && spversion.gt() > appVersion.intValue()) { 126 | return false; 127 | } 128 | } 129 | } 130 | return true; 131 | } 132 | }; 133 | 134 | filters.add(properterFilter); 135 | } 136 | filters.add(filter); 137 | return toJson(object, filters, features); 138 | } 139 | 140 | public static boolean isNotNumber(Object object, Field field) { 141 | if (object != null 142 | && field != null 143 | && !(field.getType().equals(Long.class) 144 | || field.getType().equals(Integer.class) 145 | || field.getType().equals(int.class) 146 | || field.getType().equals(long.class) 147 | || field.getType().equals(float.class) 148 | || field.getType().equals(Float.class) || field 149 | .getType().equals(Number.class))) { 150 | return true; 151 | } 152 | return false; 153 | } 154 | 155 | public static Object toJsonFilterProperties(Object object, long appVersion, 156 | boolean isIpad) { 157 | return FastJsonUtils.paser(object, appVersion, isIpad); 158 | } 159 | 160 | private static Object toJson(Object object, List filters, 161 | SerializerFeature... features) { 162 | SerializeWriter out = new SerializeWriter(); 163 | 164 | try { 165 | JSONSerializer serializer = new JSONSerializer(out); 166 | for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { 167 | serializer.config(feature, true); 168 | } 169 | 170 | serializer.config(SerializerFeature.WriteDateUseDateFormat, true); 171 | for (SerializeFilter filter : filters) { 172 | if (filter != null) { 173 | if (filter instanceof PropertyPreFilter) { 174 | serializer.getPropertyPreFilters().add( 175 | (PropertyPreFilter) filter); 176 | } 177 | 178 | if (filter instanceof NameFilter) { 179 | serializer.getNameFilters().add((NameFilter) filter); 180 | } 181 | 182 | if (filter instanceof ValueFilter) { 183 | serializer.getValueFilters().add((ValueFilter) filter); 184 | } 185 | 186 | if (filter instanceof PropertyFilter) { 187 | serializer.getPropertyFilters().add( 188 | (PropertyFilter) filter); 189 | } 190 | 191 | if (filter instanceof BeforeFilter) { 192 | serializer.getBeforeFilters() 193 | .add((BeforeFilter) filter); 194 | } 195 | 196 | if (filter instanceof AfterFilter) { 197 | serializer.getAfterFilters().add((AfterFilter) filter); 198 | } 199 | } 200 | } 201 | serializer.write(object); 202 | 203 | return out.toString(); 204 | } finally { 205 | out.close(); 206 | } 207 | } 208 | 209 | /** 210 | * 返回格式化JSON字符串。 211 | * 212 | * @param json 未格式化的JSON字符串。 213 | * @return 格式化的JSON字符串。 214 | */ 215 | public static String formatJson(String json) { 216 | StringBuffer result = new StringBuffer(); 217 | 218 | int length = json.length(); 219 | int number = 0; 220 | char key = 0; 221 | 222 | // 遍历输入字符串。 223 | for (int i = 0; i < length; i++) { 224 | // 1、获取当前字符。 225 | key = json.charAt(i); 226 | 227 | // 2、如果当前字符是前方括号、前花括号做如下处理: 228 | if ((key == '[') || (key == '{')) { 229 | // (1)如果前面还有字符,并且字符为“:”,打印:换行和缩进字符字符串。 230 | if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) { 231 | result.append('\n'); 232 | result.append(indent(number)); 233 | } 234 | 235 | // (2)打印:当前字符。 236 | result.append(key); 237 | 238 | // (3)前方括号、前花括号,的后面必须换行。打印:换行。 239 | result.append('\n'); 240 | 241 | // (4)每出现一次前方括号、前花括号;缩进次数增加一次。打印:新行缩进。 242 | number++; 243 | result.append(indent(number)); 244 | 245 | // (5)进行下一次循环。 246 | continue; 247 | } 248 | 249 | // 3、如果当前字符是后方括号、后花括号做如下处理: 250 | if ((key == ']') || (key == '}')) { 251 | // (1)后方括号、后花括号,的前面必须换行。打印:换行。 252 | result.append('\n'); 253 | 254 | // (2)每出现一次后方括号、后花括号;缩进次数减少一次。打印:缩进。 255 | number--; 256 | result.append(indent(number)); 257 | 258 | // (3)打印:当前字符。 259 | result.append(key); 260 | 261 | // (4)如果当前字符后面还有字符,并且字符不为“,”,打印:换行。 262 | if (((i + 1) < length) && (json.charAt(i + 1) != ',')) { 263 | result.append('\n'); 264 | } 265 | 266 | // (5)继续下一次循环。 267 | continue; 268 | } 269 | 270 | // 4、如果当前字符是逗号。逗号后面换行,并缩进,不改变缩进次数。 271 | if ((key == ',')) { 272 | result.append(key); 273 | result.append('\n'); 274 | result.append(indent(number)); 275 | continue; 276 | } 277 | 278 | // 5、打印:当前字符。 279 | result.append(key); 280 | } 281 | 282 | return result.toString(); 283 | } 284 | 285 | /** 286 | * 返回指定次数的缩进字符串。每一次缩进三个空格。 287 | * 288 | * @param number 缩进次数。 289 | * @return 指定缩进次数的字符串。 290 | */ 291 | private static String indent(int number) { 292 | StringBuffer result = new StringBuffer(); 293 | for (int i = 0; i < number; i++) { 294 | result.append(" "); 295 | } 296 | return result.toString(); 297 | } 298 | 299 | public static void main(String[] args) { 300 | // RopResponseContent rc = new RopResponseContent(); 301 | // List list = new ArrayList(); 302 | // RopPassInfoResponse response = new RopPassInfoResponse(); 303 | // // response.setInValidTime(new Date()); 304 | // 305 | // // list.add(response); 306 | // rc.setList(list); 307 | 308 | String result = FastJsonUtils.formatJson(""); 309 | // Object o = FastJsonUtils.toJsonFilterProperties(rc,530); 310 | // System.out.println(o); 311 | // RopResponseContent rc = new RopResponseContent(); 312 | // List list = new 313 | // ArrayList(); 314 | // RopPassInfoResponse response =new RopPassInfoResponse(); 315 | // response.setInValidTime(new Date()); 316 | // 317 | // list.add(response); 318 | // rc.setList(list); 319 | // 320 | // Object o = FastJsonUtils.toJsonFilterProperties(rc,530); 321 | // System.out.println(o); 322 | } 323 | } 324 | -------------------------------------------------------------------------------- /rop-war/src/main/java/com/prowo/rop/memcached/MemcachedUtil.java: -------------------------------------------------------------------------------- 1 | package com.prowo.rop.memcached; 2 | 3 | import java.io.IOException; 4 | import java.util.Calendar; 5 | import java.util.Date; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | import java.util.Properties; 9 | import java.util.Random; 10 | 11 | import org.apache.commons.lang3.StringUtils; 12 | import org.apache.commons.logging.Log; 13 | import org.apache.commons.logging.LogFactory; 14 | import org.springframework.core.io.ClassPathResource; 15 | import org.springframework.core.io.Resource; 16 | 17 | import com.whalin.MemCached.MemCachedClient; 18 | import com.whalin.MemCached.SockIOPool; 19 | 20 | 21 | public class MemcachedUtil { 22 | private static Object LOCK = new Object(); 23 | private static Object SESSION_LOCK = new Object(); 24 | private final static Log log = LogFactory.getLog(MemcachedUtil.class); 25 | private Properties properties; 26 | private MemCachedClient memCachedClient; 27 | private MemCachedClient sessionMemCachedClient; 28 | public final static int ONE_HOUR=3600; 29 | public final static int TWO_HOUR=7200; 30 | public final static String memcacheMapKey="MEMACACHE_MAP_KEY"; 31 | 32 | private static MemcachedUtil instance; 33 | 34 | //distribution lock 35 | private static final int DEFAULT_LOCK_SECCONDS=5; 36 | private static final int DEFAULT_TRYLOCK_TIMEOUT_SECONDS=5; 37 | 38 | private void init() { 39 | try { 40 | properties = new Properties(); 41 | Resource resource=new ClassPathResource("memcached.properties"); 42 | properties.load(resource.getInputStream()); 43 | //数据缓存服务器,�?,”表示配置多个memcached服务 44 | String[] servers = properties.getProperty("cache.server").replaceAll(" ", "").split(","); 45 | SockIOPool pool = SockIOPool.getInstance("dataServer"); 46 | pool.setServers(servers); 47 | pool.setFailover(true); 48 | pool.setInitConn(10); 49 | pool.setMinConn(5); 50 | pool.setMaxConn(50); 51 | pool.setMaintSleep(30); 52 | pool.setNagle(false); 53 | pool.setSocketTO(30000); 54 | pool.setBufferSize(1024*1024*5); 55 | pool.setAliveCheck(true); 56 | pool.initialize(); /* 建立MemcachedClient实例 */ 57 | memCachedClient = new MemCachedClient("dataServer"); 58 | } catch (IOException e) { 59 | log.error(e); 60 | } 61 | catch (Exception ex) { 62 | log.error(ex, ex); 63 | } 64 | } 65 | 66 | private void initSessionMemCached() { 67 | try { 68 | properties = new Properties(); 69 | Resource resource=new ClassPathResource("memcached.properties"); 70 | properties.load(resource.getInputStream()); 71 | //Session缓存服务器,�?,”表示配置多个memcached服务 72 | String[] sessionServers = properties.getProperty("session.cache.server").replaceAll(" ", "").split(","); 73 | SockIOPool sessionPool = SockIOPool.getInstance("sessionServer"); 74 | sessionPool.setServers(sessionServers); 75 | sessionPool.setFailover(true); 76 | sessionPool.setInitConn(10); 77 | sessionPool.setMinConn(5); 78 | sessionPool.setMaxConn(50); 79 | sessionPool.setMaintSleep(30); 80 | sessionPool.setNagle(false); 81 | sessionPool.setSocketTO(30000); 82 | sessionPool.setBufferSize(1024*1024*5); 83 | sessionPool.setAliveCheck(true); 84 | sessionPool.initialize(); /* 建立MemcachedClient实例 */ 85 | sessionMemCachedClient = new MemCachedClient("sessionServer"); 86 | } catch (IOException e) { 87 | log.error(e); 88 | } 89 | catch (Exception ex) { 90 | log.error(ex, ex); 91 | } 92 | } 93 | 94 | private MemcachedUtil(){ 95 | init(); 96 | } 97 | 98 | private boolean isCacheEnabled() { 99 | boolean useCache = false; 100 | try { 101 | useCache = Boolean.valueOf(properties.getProperty("cache.enable")); 102 | } catch (Exception e) { 103 | useCache = false; 104 | log.info("Please enable memcached"); 105 | } 106 | return useCache; 107 | } 108 | 109 | /** 110 | * 改用嵌套类静态实始化 111 | * @return 112 | */ 113 | public static MemcachedUtil getInstance() { 114 | if(instance == null){ 115 | synchronized(LOCK) { 116 | if (instance==null) { 117 | instance=new MemcachedUtil(); 118 | } 119 | } 120 | } 121 | return instance; 122 | } 123 | 124 | private MemCachedClient getMemCachedClient(boolean isForSession) { 125 | if(isForSession){ 126 | if(sessionMemCachedClient == null){ 127 | synchronized(SESSION_LOCK) { 128 | if (sessionMemCachedClient==null) { 129 | initSessionMemCached(); 130 | } 131 | } 132 | } 133 | return sessionMemCachedClient; 134 | } 135 | else 136 | return memCachedClient; 137 | } 138 | 139 | public boolean replace(String key, int seconds, Object obj) { 140 | return replace(key, seconds, obj,false); 141 | } 142 | /** 143 | * 替换 144 | * @param key 145 | * @param seconds 过期秒数 146 | * @param obj 147 | * @return 148 | */ 149 | public boolean replace(String key, int seconds, Object obj,boolean isForSession) { 150 | if(StringUtils.isEmpty(key)){ 151 | return false; 152 | } 153 | if(obj==null){ 154 | return true; 155 | } 156 | try{ 157 | if (isCacheEnabled()) { 158 | Date expDate = getDateAfter(seconds); 159 | boolean result = getMemCachedClient(isForSession).replace(key, obj, expDate); 160 | if(log.isDebugEnabled()){ 161 | log.debug("SET A OBJECT: KEY:" + key + ", OBJ:" + obj + ", exp:" + expDate + ", result:" + result); 162 | } 163 | return result; 164 | } 165 | return true; 166 | }catch(Exception e) { 167 | log.error(e); 168 | } 169 | return false; 170 | } 171 | 172 | /** 173 | * �? 174 | * @param key 175 | * @param seconds 过期秒数 176 | * @param obj 177 | * @return 178 | */ 179 | public boolean set(String key, int seconds, Object obj,boolean isForSession) { 180 | return set(key, getDateAfter(seconds), obj,isForSession); 181 | } 182 | 183 | public boolean set(String key, int seconds, Object obj) { 184 | return set(key, getDateAfter(seconds), obj,false); 185 | } 186 | 187 | 188 | /** 189 | * 将KEY保存到memcache�? 190 | * 191 | * @param key 192 | * @param exp 193 | * @param obj 194 | * @return 195 | */ 196 | public boolean set(String key,Date exp,Object obj,boolean isForSession){ 197 | if(StringUtils.isEmpty(key)){ 198 | return false; 199 | } 200 | if(obj==null){ 201 | return true; 202 | } 203 | try{ 204 | if (isCacheEnabled()) { 205 | boolean result = getMemCachedClient(isForSession).set(key, obj, exp); 206 | if(log.isDebugEnabled()){ 207 | log.debug("SET A OBJECT: KEY:" + key + ", OBJ:" + obj + ", exp:" + exp + ", result:" + result); 208 | } 209 | return result; 210 | } 211 | return true; 212 | }catch(Exception e) { 213 | log.error(e); 214 | } 215 | return false; 216 | } 217 | 218 | public boolean set(String key,Date exp,Object obj){ 219 | return set(key,exp,obj,false); 220 | } 221 | 222 | /** 223 | * @deprecated 此方法严重影响流量,应该被废�? 224 | * @author Brian 225 | * 226 | * 保存key和描述信息; 227 | * 228 | * @param key 229 | * @param second (单位:秒) 230 | * @param obj 231 | * @return 232 | */ 233 | public boolean setWithDis(String key,int seconds,Object obj,String discript){ 234 | if(StringUtils.isEmpty(key)){ 235 | return false; 236 | } 237 | if(obj==null){ 238 | return true; 239 | } 240 | try{ 241 | if (isCacheEnabled()) { 242 | Date expDate = getDateAfter(seconds); 243 | //this.putKeyDisMap(key,discript); 244 | boolean result = memCachedClient.set(key, obj, expDate); 245 | if(log.isDebugEnabled()){ 246 | log.debug("SET A OBJECT: KEY:" + key + ", OBJ:" + obj + ", expDate:" + expDate) ; 247 | } 248 | return result; 249 | } 250 | return true; 251 | }catch(Exception e) { 252 | log.error(e); 253 | } 254 | return false; 255 | } 256 | 257 | /** 258 | * 把相应的Key值和描述存到此方法中�? 259 | * 260 | * @param key 261 | * @param discript 262 | * @return 263 | */ 264 | @SuppressWarnings("unchecked") 265 | public boolean putKeyDisMap(String key,String discript) { 266 | Map memMap ; 267 | Object obj = memCachedClient.get(memcacheMapKey); 268 | if(obj == null) { 269 | memMap = new HashMap(); 270 | } else { 271 | memMap = (HashMap) obj; 272 | } 273 | memMap.put(key, discript); 274 | memCachedClient.set(memcacheMapKey,memMap,getDateAfter(60*60*48)); 275 | 276 | return true; 277 | } 278 | 279 | /** 280 | * �? 281 | * @param key 282 | * @param obj 283 | * @return 284 | */ 285 | public boolean set(String key, Object obj) { 286 | return set(key,ONE_HOUR,obj); 287 | } 288 | 289 | /** 290 | * �? 291 | * @param key 292 | * @return 293 | */ 294 | public Object get(String key,boolean isForSession) { 295 | try{ 296 | if (isCacheEnabled()) { 297 | Object obj = getMemCachedClient(isForSession).get(key); 298 | if(log.isDebugEnabled()){ 299 | log.debug("GET A OBJECT: KEY:" + key + " OBJ:" + obj) ; 300 | } 301 | return obj; 302 | } 303 | }catch(Exception e) { 304 | log.error(e); 305 | } 306 | return null; 307 | } 308 | 309 | public Object get(String key) { 310 | return get(key,false); 311 | } 312 | 313 | /** 314 | * �? 315 | * @param key 316 | * @return 317 | */ 318 | public boolean remove(String key,boolean isForSession) { 319 | if(StringUtils.isEmpty(key)){ 320 | return false; 321 | } 322 | try{ 323 | if (isCacheEnabled()) { 324 | log.info("delete memcached key: " + key); 325 | return getMemCachedClient(isForSession).delete(key); 326 | } 327 | }catch(Exception e) { 328 | log.error(e); 329 | } 330 | return true; 331 | } 332 | public boolean remove(String key) { 333 | return remove(key,false); 334 | } 335 | 336 | /** 337 | * 获得当前�?始活参数秒的时间日期 338 | * @Title: getDateAfter 339 | * @Description: 340 | * @param 341 | * @return Date 返回类型 342 | * @throws 343 | */ 344 | public static Date getDateAfter(int second) { 345 | Calendar cal = Calendar.getInstance(); 346 | cal.add(Calendar.SECOND, second); 347 | return cal.getTime(); 348 | } 349 | 350 | 351 | // memcached incr/decr 原子操作 352 | /** 353 | * 计数�? 354 | * incr命令语法为incr key integer 即将指定主键key的value值加上给定的integer,默认为1 355 | * @param key 356 | * @param isForSession 357 | * @return obj 358 | */ 359 | public Object incr(String key) { 360 | try{ 361 | if (isCacheEnabled()) { 362 | Object obj = getMemCachedClient(false).incr(key); 363 | if(log.isDebugEnabled()){ 364 | log.debug("incr A OBJECT: KEY:" + key + " OBJ:" + obj) ; 365 | } 366 | return obj; 367 | } 368 | }catch(Exception e) { 369 | log.error(e); 370 | } 371 | return -1; 372 | } 373 | 374 | /** 375 | * 计数�? 376 | * decr命令语法为decr key interger,即将指定主键key的value值减去给定的interger�? 377 | * @param key 378 | * @param isForSession 379 | * @return 380 | */ 381 | public Object decr(String key) { 382 | try{ 383 | if (isCacheEnabled()) { 384 | Object obj = getMemCachedClient(false).decr(key); 385 | if(log.isDebugEnabled()){ 386 | log.debug("incr A OBJECT: KEY:" + key + " OBJ:" + obj) ; 387 | } 388 | return obj; 389 | } 390 | }catch(Exception e) { 391 | log.error(e); 392 | } 393 | return -1; 394 | } 395 | 396 | /** 397 | * 存储key的计数器,�?�为count 398 | * @param key 399 | * @param count 400 | * @return 401 | */ 402 | public long addOrIncr(String key,long count) { 403 | try{ 404 | return getMemCachedClient(false).addOrIncr(key,count); 405 | } catch (Exception e) { 406 | log.error(e); 407 | } 408 | return 0l; 409 | } 410 | 411 | /** 412 | * 初始化计数器�? 413 | * @param key 414 | * @param count 415 | * @return 416 | */ 417 | public long addOrIncrAndInit(String key,long count) { 418 | try{ 419 | getMemCachedClient(false).delete(key); 420 | return getMemCachedClient(false).addOrIncr(key,count); 421 | }catch(Exception e) { 422 | log.error(e); 423 | } 424 | return 0l; 425 | } 426 | 427 | /** 428 | * 获取计数器�?? 429 | * 获取key的计数器,如果不存在返回-1�? 430 | * @param key 431 | * @param count 432 | * @return 433 | */ 434 | public long getCounter(String key) { 435 | try{ 436 | return getMemCachedClient(false).getCounter(key); 437 | }catch(Exception e) { 438 | log.error(e); 439 | } 440 | return -1; 441 | } 442 | 443 | public boolean tryLock(String lockKey){ 444 | return tryLock(lockKey,DEFAULT_LOCK_SECCONDS,DEFAULT_TRYLOCK_TIMEOUT_SECONDS); 445 | } 446 | 447 | public boolean tryLock(String lockKey, int lockSec, int timeOutSec){ 448 | MemCachedClient client=getMemCachedClient(false); 449 | 450 | long start=System.currentTimeMillis(); 451 | while(true){ 452 | boolean locked=client.add(lockKey, "", MemcachedCalendarUtil.getDateAfter(lockSec)); 453 | if(locked){ 454 | return true; 455 | }else{ 456 | long now=System.currentTimeMillis(); 457 | long costed = now-start; 458 | if(costed>=timeOutSec*1000){ 459 | return false; 460 | } 461 | } 462 | } 463 | } 464 | 465 | /** 466 | * 该锁是否已被持有 false:未被持有,true:已被持�? 467 | * @param lockKey 468 | * @param lockSec 469 | * @return 470 | */ 471 | public boolean isHoldLock(String lockKey, int lockSec){ 472 | boolean isHoldLock = false; 473 | MemCachedClient client = getMemCachedClient(false); 474 | boolean locked = client.add(lockKey, "holded", MemcachedCalendarUtil.getDateAfter(lockSec)); 475 | if(!locked){ 476 | isHoldLock = true; 477 | } 478 | return isHoldLock; 479 | } 480 | 481 | 482 | public boolean releaseLock(String lockKey){ 483 | MemCachedClient client=getMemCachedClient(false); 484 | return client.delete(lockKey); 485 | } 486 | 487 | @SuppressWarnings("unchecked") 488 | public static void main(String[] args) throws Exception { 489 | Map map = (Map) MemcachedUtil.getInstance().get("MEM_TEST_KEY_LL_6"); 490 | if(map == null){ 491 | map = new HashMap(); 492 | } 493 | 494 | for(int i=0;i<1;i++) { 495 | Random r = new Random(System.currentTimeMillis()); 496 | String key = "KEY_" + r.nextLong(); 497 | System.out.println("keys: " +key); 498 | 499 | byte[] bt = new byte[0]; 500 | String value= new String(bt,"UTF-8"); 501 | for(int j=0;j<10000;j++) { 502 | value= value + "qweqweqweqweqweqwewequyrqwieurpasjdflkasdfasdrwqioeurpqwerqweqwertyuiopqwertyuiopqwertyuiopqwertqqqq"; 503 | } 504 | //System.out.println(key + " : " + MemcachedUtil.getInstance().get(key)); 505 | map.put(key, value); 506 | } 507 | 508 | boolean a =MemcachedUtil.getInstance().set("MEM_TEST_KEY_LL_6", map); 509 | 510 | boolean b = MemcachedUtil.getInstance().set("MEM_TEST_KEY_LL_2", "2"); 511 | 512 | 513 | System.out.println("keys: " + map.size()); 514 | 515 | System.out.println(a + ", " + b); 516 | 517 | } 518 | } 519 | --------------------------------------------------------------------------------