1
0
Эх сурвалжийг харах

Merge pull request #92 from yiming187/common

Extract common module
lepdou 9 жил өмнө
parent
commit
0ac426a6a4
26 өөрчлөгдсөн 70 нэмэгдсэн , 351 устгасан
  1. 1 1
      apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/AppController.java
  2. 1 1
      apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ClusterController.java
  3. 0 71
      apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/GlobalDefaultExceptionHandler.java
  4. 1 1
      apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java
  5. 1 1
      apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java
  6. 1 1
      apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java
  7. 1 1
      apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java
  8. 1 1
      apollo-biz/pom.xml
  9. 0 1
      apollo-biz/src/main/java/com/ctrip/apollo/biz/repository/ReleaseRepository.java
  10. 1 1
      apollo-biz/src/main/java/com/ctrip/apollo/biz/service/AppService.java
  11. 1 1
      apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java
  12. 1 1
      apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java
  13. 1 1
      apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemSetService.java
  14. 1 1
      apollo-biz/src/main/java/com/ctrip/apollo/biz/service/NamespaceService.java
  15. 27 0
      apollo-common/pom.xml
  16. 13 8
      apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java
  17. 1 1
      apollo-common/src/main/java/com/ctrip/apollo/common/controller/WebMvcConfig.java
  18. 1 1
      apollo-common/src/main/java/com/ctrip/apollo/common/utils/BeanUtils.java
  19. 0 29
      apollo-configservice/src/main/java/com/ctrip/apollo/configservice/controller/WebMvcConfig.java
  20. 4 4
      apollo-core/src/main/java/com/ctrip/apollo/core/ServiceNameConsts.java
  21. 3 3
      apollo-core/src/main/java/com/ctrip/apollo/core/utils/StringUtils.java
  22. 1 5
      apollo-portal/pom.xml
  23. 1 2
      apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/AppController.java
  24. 1 1
      apollo-portal/src/main/java/com/ctrip/apollo/portal/service/txtresolver/PropertyResolver.java
  25. 0 213
      apollo-portal/src/main/java/com/ctrip/apollo/portal/util/BeanUtils.java
  26. 6 0
      pom.xml

+ 1 - 1
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/AppController.java

@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
 import com.ctrip.apollo.biz.entity.App;
 import com.ctrip.apollo.biz.service.AdminService;
 import com.ctrip.apollo.biz.service.AppService;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.AppDTO;
 import com.ctrip.apollo.core.exception.NotFoundException;
 import com.ctrip.apollo.core.utils.StringUtils;

+ 1 - 1
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ClusterController.java

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
 import com.ctrip.apollo.biz.entity.Cluster;
 import com.ctrip.apollo.biz.service.ClusterService;
 import com.ctrip.apollo.biz.service.ViewService;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.ClusterDTO;
 import com.ctrip.apollo.core.exception.NotFoundException;
 

+ 0 - 71
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/GlobalDefaultExceptionHandler.java

@@ -1,71 +0,0 @@
-package com.ctrip.apollo.adminservice.controller;
-
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.HttpMediaTypeException;
-import org.springframework.web.HttpRequestMethodNotSupportedException;
-import org.springframework.web.bind.annotation.ControllerAdvice;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-import com.ctrip.apollo.core.exception.NotFoundException;
-
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-
-import static org.springframework.http.HttpStatus.BAD_REQUEST;
-import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
-import static org.springframework.http.HttpStatus.NOT_FOUND;
-import static org.springframework.http.MediaType.APPLICATION_JSON;
-
-@ControllerAdvice
-public class GlobalDefaultExceptionHandler {
-  @ExceptionHandler(Exception.class)
-  public ResponseEntity<Map<String, Object>> exception(HttpServletRequest request, Exception ex) {
-    return handleError(request, INTERNAL_SERVER_ERROR, ex);
-  }
-
-  private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
-                                                          HttpStatus status, Throwable ex) {
-    return handleError(request, status, ex, ex.getMessage());
-  }
-
-  private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
-                                                          HttpStatus status, Throwable ex,
-                                                          String message) {
-    ex = resolveError(ex);
-    Map<String, Object> errorAttributes = new LinkedHashMap<>();
-    errorAttributes.put("status", status.value());
-    errorAttributes.put("message", message);
-    errorAttributes.put("timestamp",
-        LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
-    errorAttributes.put("exception", resolveError(ex).getClass().getName());
-    HttpHeaders headers = new HttpHeaders();
-    headers.setContentType(APPLICATION_JSON);
-    return new ResponseEntity<>(errorAttributes, headers, status);
-  }
-
-  @ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
-  public ResponseEntity<Map<String, Object>> methodNotSupportedException(HttpServletRequest request,
-                                                                         ServletException ex) {
-    return handleError(request, BAD_REQUEST, ex);
-  }
-
-  @ExceptionHandler(NotFoundException.class)
-  @ResponseStatus(value = NOT_FOUND)
-  public void notFound(HttpServletRequest req, NotFoundException ex) {
-  }
-
-  private Throwable resolveError(Throwable ex) {
-    while (ex instanceof ServletException && ex.getCause() != null) {
-      ex = ((ServletException) ex).getCause();
-    }
-    return ex;
-  }
-}

+ 1 - 1
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
 import com.ctrip.apollo.biz.entity.Item;
 import com.ctrip.apollo.biz.service.ItemService;
 import com.ctrip.apollo.biz.service.ViewService;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.ItemDTO;
 import com.ctrip.apollo.core.exception.NotFoundException;
 

+ 1 - 1
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
 import com.ctrip.apollo.biz.entity.Namespace;
 import com.ctrip.apollo.biz.service.NamespaceService;
 import com.ctrip.apollo.biz.service.ViewService;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.NamespaceDTO;
 import com.ctrip.apollo.core.exception.NotFoundException;
 

+ 1 - 1
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java

@@ -13,7 +13,7 @@ import com.ctrip.apollo.biz.entity.Release;
 import com.ctrip.apollo.biz.service.ConfigService;
 import com.ctrip.apollo.biz.service.ReleaseService;
 import com.ctrip.apollo.biz.service.ViewService;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.ReleaseDTO;
 import com.ctrip.apollo.core.exception.NotFoundException;
 

+ 1 - 1
apollo-adminservice/src/test/java/com/ctrip/apollo/adminservice/controller/AppControllerTest.java

@@ -10,7 +10,7 @@ import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
 
 import com.ctrip.apollo.biz.entity.App;
 import com.ctrip.apollo.biz.repository.AppRepository;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.AppDTO;
 
 public class AppControllerTest extends AbstractControllerTest{

+ 1 - 1
apollo-biz/pom.xml

@@ -14,7 +14,7 @@
 	<dependencies>
 		<dependency>
 			<groupId>com.ctrip.apollo</groupId>
-			<artifactId>apollo-core</artifactId>
+			<artifactId>apollo-common</artifactId>
 		</dependency>
 		<dependency>
 			<groupId>org.springframework.boot</groupId>

+ 0 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/repository/ReleaseRepository.java

@@ -2,7 +2,6 @@ package com.ctrip.apollo.biz.repository;
 
 import java.util.List;
 
-import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.repository.PagingAndSortingRepository;
 import org.springframework.data.repository.query.Param;
 

+ 1 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/AppService.java

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.biz.entity.App;
 import com.ctrip.apollo.biz.repository.AppRepository;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 
 @Service
 public class AppService {

+ 1 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java

@@ -5,7 +5,7 @@ import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.biz.entity.Cluster;
 import com.ctrip.apollo.biz.repository.ClusterRepository;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 
 @Service
 public class ClusterService {

+ 1 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java

@@ -5,7 +5,7 @@ import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.biz.entity.Item;
 import com.ctrip.apollo.biz.repository.ItemRepository;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 
 @Service
 public class ItemService {

+ 1 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemSetService.java

@@ -5,7 +5,7 @@ import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.biz.entity.Item;
 import com.ctrip.apollo.biz.repository.ItemRepository;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 import com.ctrip.apollo.core.dto.ItemChangeSets;
 import com.ctrip.apollo.core.dto.ItemDTO;
 

+ 1 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/NamespaceService.java

@@ -5,7 +5,7 @@ import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.biz.entity.Namespace;
 import com.ctrip.apollo.biz.repository.NamespaceRepository;
-import com.ctrip.apollo.biz.utils.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 
 @Service
 public class NamespaceService {

+ 27 - 0
apollo-common/pom.xml

@@ -0,0 +1,27 @@
+<?xml version="1.0"  encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<parent>
+		<groupId>com.ctrip.apollo</groupId>
+		<artifactId>apollo</artifactId>
+		<version>0.0.1-SNAPSHOT</version>
+		<relativePath>../pom.xml</relativePath>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>apollo-common</artifactId>
+	<name>Apollo Common</name>
+	<dependencies>
+		<dependency>
+			<groupId>com.ctrip.apollo</groupId>
+			<artifactId>apollo-core</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.data</groupId>
+			<artifactId>spring-data-commons</artifactId>
+		</dependency>
+	</dependencies>
+</project>

+ 13 - 8
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/GlobalDefaultExceptionHandler.java → apollo-common/src/main/java/com/ctrip/apollo/common/controller/GlobalDefaultExceptionHandler.java

@@ -1,4 +1,4 @@
-package com.ctrip.apollo.portal.controller;
+package com.ctrip.apollo.common.controller;
 
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
@@ -8,6 +8,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 
+import com.ctrip.apollo.core.exception.AbstractBaseException;
 import com.ctrip.apollo.core.exception.BadRequestException;
 import com.ctrip.apollo.core.exception.NotFoundException;
 
@@ -32,13 +33,12 @@ public class GlobalDefaultExceptionHandler {
   }
 
   private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
-                                                          HttpStatus status, Throwable ex) {
+      HttpStatus status, Throwable ex) {
     return handleError(request, status, ex, ex.getMessage());
   }
 
   private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request,
-                                                          HttpStatus status, Throwable ex,
-                                                          String message) {
+      HttpStatus status, Throwable ex, String message) {
     ex = resolveError(ex);
     Map<String, Object> errorAttributes = new LinkedHashMap<>();
     errorAttributes.put("status", status.value());
@@ -46,6 +46,9 @@ public class GlobalDefaultExceptionHandler {
     errorAttributes.put("timestamp",
         LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
     errorAttributes.put("exception", resolveError(ex).getClass().getName());
+    if (ex instanceof AbstractBaseException) {
+      errorAttributes.put("errorCode", ((AbstractBaseException) ex).getErrorCode());
+    }
     HttpHeaders headers = new HttpHeaders();
     headers.setContentType(APPLICATION_JSON);
     return new ResponseEntity<>(errorAttributes, headers, status);
@@ -53,17 +56,19 @@ public class GlobalDefaultExceptionHandler {
 
   @ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
   public ResponseEntity<Map<String, Object>> methodNotSupportedException(HttpServletRequest request,
-                                                                         ServletException ex) {
+      ServletException ex) {
     return handleError(request, BAD_REQUEST, ex);
   }
 
   @ExceptionHandler(NotFoundException.class)
-  public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request, NotFoundException ex) {
+  public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request,
+      NotFoundException ex) {
     return handleError(request, NOT_FOUND, ex);
   }
-  
+
   @ExceptionHandler(BadRequestException.class)
-  public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, BadRequestException ex){
+  public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
+      BadRequestException ex) {
     return handleError(request, BAD_REQUEST, ex);
   }
 

+ 1 - 1
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/WebMvcConfig.java → apollo-common/src/main/java/com/ctrip/apollo/common/controller/WebMvcConfig.java

@@ -1,4 +1,4 @@
-package com.ctrip.apollo.adminservice.controller;
+package com.ctrip.apollo.common.controller;
 
 import java.util.List;
 

+ 1 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/utils/BeanUtils.java → apollo-common/src/main/java/com/ctrip/apollo/common/utils/BeanUtils.java

@@ -1,4 +1,4 @@
-package com.ctrip.apollo.biz.utils;
+package com.ctrip.apollo.common.utils;
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;

+ 0 - 29
apollo-configservice/src/main/java/com/ctrip/apollo/configservice/controller/WebMvcConfig.java

@@ -1,29 +0,0 @@
-package com.ctrip.apollo.configservice.controller;
-
-import java.util.List;
-
-import org.springframework.context.annotation.Configuration;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
-import org.springframework.http.MediaType;
-import org.springframework.web.method.support.HandlerMethodArgumentResolver;
-import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
-
-@Configuration
-public class WebMvcConfig extends WebMvcConfigurerAdapter {
-
-  @Override
-  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
-
-    PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
-    resolver.setFallbackPageable(new PageRequest(0, 10));
-
-    argumentResolvers.add(resolver);
-  }
-
-  @Override
-  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
-    configurer.ignoreAcceptHeader(true).defaultContentType(MediaType.APPLICATION_JSON);
-  }
-}

+ 4 - 4
apollo-core/src/main/java/com/ctrip/apollo/core/ServiceNameConsts.java

@@ -2,11 +2,11 @@ package com.ctrip.apollo.core;
 
 public interface ServiceNameConsts {
 
-  final String APOLLO_METASERVICE = "apollo-metaservice";
+  String APOLLO_METASERVICE = "apollo-metaservice";
 
-  final String APOLLO_CONFIGSERVICE = "apollo-configservice";
+  String APOLLO_CONFIGSERVICE = "apollo-configservice";
 
-  final String APOLLO_ADMINSERVICE = "apollo-adminservice";
+  String APOLLO_ADMINSERVICE = "apollo-adminservice";
 
-  final String APOLLO_PORTAL = "apollo-portal";
+  String APOLLO_PORTAL = "apollo-portal";
 }

+ 3 - 3
apollo-core/src/main/java/com/ctrip/apollo/core/utils/StringUtils.java

@@ -67,7 +67,7 @@ public class StringUtils {
       return true;
     }
     for (int i = 0; i < strLen; i++) {
-      if ((Character.isWhitespace(str.charAt(i)) == false)) {
+      if (Character.isWhitespace(str.charAt(i)) == false) {
         return false;
       }
     }
@@ -252,7 +252,7 @@ public class StringUtils {
    */
   private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
     if (str == null || prefix == null) {
-      return (str == null && prefix == null);
+      return str == null && prefix == null;
     }
     if (prefix.length() > str.length()) {
       return false;
@@ -325,7 +325,7 @@ public class StringUtils {
   }
 
   public static interface StringFormatter<T> {
-    public String format(T obj);
+    String format(T obj);
   }
 
   public static <T> String join(Collection<T> collection, String separator) {

+ 1 - 5
apollo-portal/pom.xml

@@ -13,11 +13,7 @@
 	<dependencies>
 		<dependency>
 			<groupId>com.ctrip.apollo</groupId>
-			<artifactId>apollo-core</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>org.springframework.boot</groupId>
-			<artifactId>spring-boot-starter-web</artifactId>
+			<artifactId>apollo-common</artifactId>
 		</dependency>
 	</dependencies>
 	<build>

+ 1 - 2
apollo-portal/src/main/java/com/ctrip/apollo/portal/controller/AppController.java

@@ -12,7 +12,6 @@ import com.ctrip.apollo.core.exception.BadRequestException;
 import com.ctrip.apollo.core.utils.StringUtils;
 import com.ctrip.apollo.portal.entity.ClusterNavTree;
 import com.ctrip.apollo.portal.service.AppService;
-import com.google.common.base.Strings;
 
 @RestController
 @RequestMapping("/apps")
@@ -23,7 +22,7 @@ public class AppController {
 
   @RequestMapping("/{appId}/navtree")
   public ClusterNavTree nav(@PathVariable String appId) {
-    if (Strings.isNullOrEmpty(appId)) {
+    if (StringUtils.isEmpty(appId)) {
       throw new BadRequestException("app id can not be empty.");
     }
 

+ 1 - 1
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/txtresolver/PropertyResolver.java

@@ -3,7 +3,7 @@ package com.ctrip.apollo.portal.service.txtresolver;
 import com.ctrip.apollo.core.dto.ItemChangeSets;
 import com.ctrip.apollo.core.dto.ItemDTO;
 import com.ctrip.apollo.core.utils.StringUtils;
-import com.ctrip.apollo.portal.util.BeanUtils;
+import com.ctrip.apollo.common.utils.BeanUtils;
 
 import org.springframework.stereotype.Component;
 

+ 0 - 213
apollo-portal/src/main/java/com/ctrip/apollo/portal/util/BeanUtils.java

@@ -1,213 +0,0 @@
-package com.ctrip.apollo.portal.util;
-
-import org.springframework.beans.BeanWrapper;
-import org.springframework.beans.BeanWrapperImpl;
-import org.springframework.util.CollectionUtils;
-
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-
-public class BeanUtils {
-
-  /**
-   * <pre>
-   *     List<UserBean> userBeans = userDao.queryUsers();
-   *     List<UserDTO> userDTOs = BeanUtil.batchTransform(UserDTO.class, userBeans);
-   * </pre>
-   */
-  public static <T> List<T> batchTransform(final Class<T> clazz, List srcList) {
-
-    if (CollectionUtils.isEmpty(srcList)) {
-      return Collections.EMPTY_LIST;
-    }
-
-    List<T> result = new ArrayList<>(srcList.size());
-    for (Object srcObject : srcList) {
-      result.add(transfrom(clazz, srcObject));
-    }
-    return result;
-  }
-
-  /**
-   * 封装{@link org.springframework.beans.BeanUtils#copyProperties},惯用与直接将转换结果返回
-   *
-   * <pre>
-   *      UserBean userBean = new UserBean("username");
-   *      return BeanUtil.transform(UserDTO.class, userBean);
-   * </pre>
-   */
-  public static <T> T transfrom(Class<T> clazz, Object src) {
-    if (src == null) {
-      return null;
-    }
-    T instance = null;
-    try {
-      instance = clazz.newInstance();
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
-    org.springframework.beans.BeanUtils.copyProperties(src, instance, getNullPropertyNames(src));
-    return instance;
-  }
-
-  static String[] getNullPropertyNames(Object source) {
-    final BeanWrapper src = new BeanWrapperImpl(source);
-    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
-
-    Set<String> emptyNames = new HashSet<String>();
-    for (java.beans.PropertyDescriptor pd : pds) {
-      Object srcValue = src.getPropertyValue(pd.getName());
-      if (srcValue == null) {
-        emptyNames.add(pd.getName());
-      }
-    }
-    String[] result = new String[emptyNames.size()];
-    return emptyNames.toArray(result);
-  }
-
-  /**
-   * 用于将一个列表转换为列表中的对象的某个属性映射到列表中的对象
-   *
-   * <pre>
-   *      List<UserDTO> userList = userService.queryUsers();
-   *      Map<Integer, userDTO> userIdToUser = BeanUtil.mapByKey("userId", Integer.class, userList,
-   * UserDTO.class);
-   * </pre>
-   *
-   * @param key 属性名
-   */
-  public static <K, V> Map<K, V> mapByKey(String key, List list) {
-    Map<K, V> map = new HashMap<K, V>();
-    if (CollectionUtils.isEmpty(list)) {
-      return map;
-    }
-    try {
-      Class clazz = list.get(0).getClass();
-      Field field = deepFindField(clazz, key);
-      field.setAccessible(true);
-      for (Object o : list) {
-        map.put((K) field.get(o), (V) o);
-      }
-    } catch (Exception e) {
-      throw new RuntimeException();
-    }
-    return map;
-  }
-
-  /**
-   * 根据列表里面的属性聚合
-   *
-   * <pre>
-   *       List<ShopDTO> shopList = shopService.queryShops();
-   *       Map<Integer, List<ShopDTO>> city2Shops = BeanUtil.aggByKeyToList("cityId", shopList);
-   * </pre>
-   */
-  public static <K, V> Map<K, List<V>> aggByKeyToList(String key, List list) {
-    Map<K, List<V>> map = new HashMap<K, List<V>>();
-    if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list
-      return map;
-    }
-    try {
-      Class clazz = list.get(0).getClass();
-      Field field = deepFindField(clazz, key);
-      field.setAccessible(true);
-      for (Object o : list) {
-        K k = (K) field.get(o);
-        if (map.get(k) == null) {
-          map.put(k, new ArrayList<V>());
-        }
-        map.get(k).add((V) o);
-      }
-    } catch (Exception e) {
-      throw new RuntimeException();
-    }
-    return map;
-  }
-
-  /**
-   * 用于将一个对象的列表转换为列表中对象的属性集合
-   *
-   * <pre>
-   *     List<UserDTO> userList = userService.queryUsers();
-   *     Set<Integer> userIds = BeanUtil.toPropertySet("userId", userList);
-   * </pre>
-   */
-  public static Set toPropertySet(String key, List list) {
-    Set set = new HashSet();
-    if (CollectionUtils.isEmpty(list)) {// 防止外面传入空list
-      return set;
-    }
-    try {
-      Class clazz = list.get(0).getClass();
-      Field field = deepFindField(clazz, key);
-      if (field == null) {
-        return set;
-      }
-      field.setAccessible(true);
-      for (Object o : list) {
-        set.add(field.get(o));
-      }
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
-    return set;
-  }
-
-
-  private static Field deepFindField(Class clazz, String key) {
-    Field field = null;
-    while (!clazz.getName().equals(Object.class.getName())) {
-      try {
-        field = clazz.getDeclaredField(key);
-        if (field != null) {
-          break;
-        }
-      } catch (Exception e) {
-        clazz = clazz.getSuperclass();
-      }
-    }
-    return field;
-  }
-
-  /**
-   * 获取某个对象的某个属性
-   */
-  public static Object getProperty(Object obj, String fieldName) {
-    try {
-      Field field = deepFindField(obj.getClass(), fieldName);
-      if (field != null) {
-        field.setAccessible(true);
-        return field.get(obj);
-      }
-    } catch (Exception e) {
-      // ig
-    }
-    return null;
-  }
-
-  /**
-   * 设置某个对象的某个属性
-   */
-  public static void setProperty(Object obj, String fieldName, Object value) {
-    try {
-      Field field = deepFindField(obj.getClass(), fieldName);
-      if (field != null) {
-        field.setAccessible(true);
-        field.set(obj, value);
-      }
-    } catch (Exception e) {
-      // ig
-    }
-  }
-
-  public static List toPropertyList(String key, List list) {
-    return new ArrayList(toPropertySet(key, list));
-  }
-}

+ 6 - 0
pom.xml

@@ -89,6 +89,7 @@
 		<module>apollo-buildtools</module>
 		<module>apollo-core</module>
 		<module>apollo-client</module>
+		<module>apollo-common</module>
 		<module>apollo-biz</module>
 		<module>apollo-configservice</module>
 		<module>apollo-adminservice</module>
@@ -103,6 +104,11 @@
 				<artifactId>apollo-core</artifactId>
 				<version>${project.version}</version>
 			</dependency>
+			<dependency>
+				<groupId>com.ctrip.apollo</groupId>
+				<artifactId>apollo-common</artifactId>
+				<version>${project.version}</version>
+			</dependency>
 			<dependency>
 				<groupId>com.ctrip.apollo</groupId>
 				<artifactId>apollo-biz</artifactId>