Skip to content

Commit

Permalink
Refactor apollo client, add tooling support and add helper methods for
Browse files Browse the repository at this point in the history
Date, Enum, Duration.
  • Loading branch information
nobodyiam committed Oct 8, 2016
1 parent 68bc2e1 commit 1cf492c
Show file tree
Hide file tree
Showing 24 changed files with 964 additions and 61 deletions.
2 changes: 1 addition & 1 deletion apollo-adminservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-assembly/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-biz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>apollo</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>0.1.2-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-biz</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apollo-buildtools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion apollo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo</artifactId>
<version>0.1.2-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
94 changes: 77 additions & 17 deletions apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ctrip.framework.apollo;

import java.util.Date;
import java.util.Locale;
import java.util.Set;

/**
Expand All @@ -10,7 +12,7 @@ public interface Config {
* Return the property value with the given key, or {@code defaultValue} if the key doesn't exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value
*/
public String getProperty(String key, String defaultValue);
Expand All @@ -20,9 +22,8 @@ public interface Config {
* doesn't exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as integer
* @throws NumberFormatException if the property value is invalid
*/
public Integer getIntProperty(String key, Integer defaultValue);

Expand All @@ -31,9 +32,8 @@ public interface Config {
* exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as long
* @throws NumberFormatException if the property value is invalid
*/
public Long getLongProperty(String key, Long defaultValue);

Expand All @@ -42,9 +42,8 @@ public interface Config {
* exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as short
* @throws NumberFormatException if the property value is invalid
*/
public Short getShortProperty(String key, Short defaultValue);

Expand All @@ -53,9 +52,8 @@ public interface Config {
* exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as float
* @throws NumberFormatException if the property value is invalid
*/
public Float getFloatProperty(String key, Float defaultValue);

Expand All @@ -64,9 +62,8 @@ public interface Config {
* exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as double
* @throws NumberFormatException if the property value is invalid
*/
public Double getDoubleProperty(String key, Double defaultValue);

Expand All @@ -75,9 +72,8 @@ public interface Config {
* exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as byte
* @throws NumberFormatException if the property value is invalid
*/
public Byte getByteProperty(String key, Byte defaultValue);

Expand All @@ -86,21 +82,85 @@ public interface Config {
* doesn't exist.
*
* @param key the property name
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
* @return the property value as boolean
*/
public Boolean getBooleanProperty(String key, Boolean defaultValue);

/**
* Return the array property value with the given key, or {@code defaultValue} if the key doesn't
* exist.
* Return the array property value with the given key, or {@code defaultValue} if the key doesn't exist.
*
* @param key the property name
* @param delimiter the delimiter regex
* @param defaultValue the default value when key is not found
* @param defaultValue the default value when key is not found or any error occurred
*/
public String[] getArrayProperty(String key, String delimiter, String[] defaultValue);

/**
* Return the Date property value with the given name, or {@code defaultValue} if the name doesn't exist.
* Will try to parse the date with Locale.US and formats as follows: yyyy-MM-dd HH:mm:ss.SSS,
* yyyy-MM-dd HH:mm:ss and yyyy-MM-dd
*
* @param key the property name
* @param defaultValue the default value when name is not found or any error occurred
* @return the property value
*/
public Date getDateProperty(String key, Date defaultValue);

/**
* Return the Date property value with the given name, or {@code defaultValue} if the name doesn't exist.
* Will parse the date with the format specified and Locale.US
*
* @param key the property name
* @param format the date format, see {@link java.text.SimpleDateFormat} for more
* information
* @param defaultValue the default value when name is not found or any error occurred
* @return the property value
*/
public Date getDateProperty(String key, String format, Date defaultValue);

/**
* Return the Date property value with the given name, or {@code defaultValue} if the name doesn't exist.
*
* @param key the property name
* @param format the date format, see {@link java.text.SimpleDateFormat} for more
* information
* @param locale the locale to use
* @param defaultValue the default value when name is not found or any error occurred
* @return the property value
*/
public Date getDateProperty(String key, String format, Locale locale, Date defaultValue);

/**
* Return the Enum property value with the given key, or {@code defaultValue} if the key doesn't exist.
*
* @param key the property name
* @param enumType the enum class
* @param defaultValue the default value when key is not found or any error occurred
* @param <T> the enum
* @return the property value
*/
public <T extends Enum<T>> T getEnumProperty(String key, Class<T> enumType, T defaultValue);

/**
* Return the duration property value(in milliseconds) with the given name, or {@code
* defaultValue} if the name doesn't exist. Please note the format should comply with the follow
* example (case insensitive). Examples:
* <pre>
* "123MS" -- parses as "123 milliseconds"
* "20S" -- parses as "20 seconds"
* "15M" -- parses as "15 minutes" (where a minute is 60 seconds)
* "10H" -- parses as "10 hours" (where an hour is 3600 seconds)
* "2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds)
* "2D3H4M5S123MS" -- parses as "2 days, 3 hours, 4 minutes, 5 seconds and 123 milliseconds"
* </pre>
*
* @param key the property name
* @param defaultValue the default value when name is not found or any error occurred
* @return the parsed property value(in milliseconds)
*/
public long getDurationProperty(String key, long defaultValue);

/**
* Add change listener to this config instance.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ctrip.framework.apollo.exceptions;

/**
* @author Jason Song([email protected])
*/
public class ApolloConfigStatusCodeException extends RuntimeException{
private final int m_statusCode;

public ApolloConfigStatusCodeException(int statusCode, String message) {
super(String.format("[status code: %d] %s", statusCode, message));
this.m_statusCode = statusCode;
}

public int getStatusCode() {
return m_statusCode;
}
}
Loading

0 comments on commit 1cf492c

Please sign in to comment.