Skip to content

Commit

Permalink
re
Browse files Browse the repository at this point in the history
  • Loading branch information
knightliao committed Sep 11, 2016
1 parent 5973d80 commit 78dcecb
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 48 deletions.
Expand Up @@ -38,10 +38,10 @@
String app() default "";

/**
* 以"/"开头则是系统的全路径,否则则是相对于classpath的路径,默认是classpath根路径
* 配置文件目标地址dir, 以"/"开头则是系统的全路径,否则则是相对于classpath的路径,默认是classpath根路径
* 注意:根路径要注意是否有权限,否则会出现找不到路径,推荐采用相对路径
*
* @return
*/
String copy2TargetDirPath() default "";
String targetDirPath() default "";
}
Expand Up @@ -41,8 +41,8 @@ public class DisconfCenterFile extends DisconfCenterBaseModel {
// 文件名
private String fileName;

// 复制到指定的路径下
private String copy2TargetDirPath;
// 配置文件 指定路径下
private String targetDirPath;

// 文件类型
private SupportFileTypeEnum supportFileTypeEnum = SupportFileTypeEnum.ANY;
Expand Down Expand Up @@ -95,18 +95,18 @@ public void setIsTaggedWithNonAnnotationFile(boolean isTaggedWithNonAnnotationFi
this.isTaggedWithNonAnnotationFile = isTaggedWithNonAnnotationFile;
}

public String getCopy2TargetDirPath() {
return copy2TargetDirPath;
public String getTargetDirPath() {
return targetDirPath;
}

public void setCopy2TargetDirPath(String copy2TargetDirPath) {
this.copy2TargetDirPath = copy2TargetDirPath;
public void setTargetDirPath(String targetDirPath) {
this.targetDirPath = targetDirPath;
}

@Override
public String toString() {
return "\n\tDisconfCenterFile [\n\tkeyMaps=" + keyMaps + "\n\tcls=" + cls + "\n\tfileName=" + fileName
+ "\n\tcopy2TargetDirPath=" + copy2TargetDirPath +
+ "\n\ttargetDirPath=" + targetDirPath +
super.toString() + "]";
}

Expand Down Expand Up @@ -141,17 +141,19 @@ public Map<String, Object> getKV() {
* 配置文件的路径
*/
public String getFilePath() {

// 不放到classpath, 则文件路径根据 userDefineDownloadDir 来设置
if (!DisClientConfig.getInstance().enableLocalDownloadDirInClassPath) {
return OsUtil.pathJoin(DisClientConfig.getInstance().userDefineDownloadDir, fileName);
}

if (copy2TargetDirPath != null) {
if (targetDirPath != null) {

if (copy2TargetDirPath.startsWith("/")) {
return OsUtil.pathJoin(copy2TargetDirPath, fileName);
if (targetDirPath.startsWith("/")) {
return OsUtil.pathJoin(targetDirPath, fileName);
}

return OsUtil.pathJoin(ClassLoaderUtil.getClassPath(), copy2TargetDirPath, fileName);
return OsUtil.pathJoin(ClassLoaderUtil.getClassPath(), targetDirPath, fileName);
}

return OsUtil.pathJoin(ClassLoaderUtil.getClassPath(), fileName);
Expand All @@ -163,13 +165,13 @@ public String getFilePath() {
public String getFileDir() {

// 获取相对于classpath的路径
if (copy2TargetDirPath != null) {
if (targetDirPath != null) {

if (copy2TargetDirPath.startsWith("/")) {
return OsUtil.pathJoin(copy2TargetDirPath);
if (targetDirPath.startsWith("/")) {
return OsUtil.pathJoin(targetDirPath);
}

return OsUtil.pathJoin(ClassLoaderUtil.getClassPath(), copy2TargetDirPath);
return OsUtil.pathJoin(ClassLoaderUtil.getClassPath(), targetDirPath);
}

return ClassLoaderUtil.getClassPath();
Expand Down
Expand Up @@ -14,11 +14,11 @@ public interface FetcherMgr {
String getValueFromServer(String url) throws Exception;

/**
* 下载配置文件, remoteUrl是目标 url, 下载到预定义的文件夹,并复制到classpath文件夹下
* 下载配置文件, remoteUrl是目标 url, 下载到预定义的文件夹,并 下载到 targetDirPath 目录下
*
* @throws Exception
*/
String downloadFileFromServer(String url, String fileName, String copy2TargetDirPath) throws Exception;
String downloadFileFromServer(String url, String fileName, String targetDirPath) throws Exception;

/**
* 释放资源
Expand Down
Expand Up @@ -96,8 +96,8 @@ private static DisconfCenterFile transformScanFile(Class<?> disconfFileClass, Se
// file name
disconfCenterFile.setFileName(disconfFileAnnotation.filename());

// copy 2 target path
disconfCenterFile.setCopy2TargetDirPath(disconfFileAnnotation.copy2TargetDirPath().trim());
// config file target dir path
disconfCenterFile.setTargetDirPath(disconfFileAnnotation.targetDirPath().trim());

// file type
disconfCenterFile.setSupportFileTypeEnum(SupportFileTypeEnum.getByFileName(disconfFileAnnotation.filename()));
Expand Down
Expand Up @@ -60,7 +60,7 @@ public <T> T getJsonData(Class<T> clazz, RemoteUrl remoteUrl, int retryTimes, in
*/
@Mock
public String downloadFromServer(RemoteUrl remoteUrl, String fileName, String localFileDir, String localFileDirTemp,
String copy2TargetDirPath, boolean download2Classpath, int retryTimes,
String targetDirPath, boolean download2Classpath, int retryTimes,
int retrySleepSeconds)
throws Exception {

Expand Down
Expand Up @@ -23,17 +23,17 @@ public interface RestfulMgr {
<T> T getJsonData(Class<T> clazz, RemoteUrl remoteUrl, int retryTimes, int retrySleepSeconds) throws Exception;

/**
* @param remoteUrl 远程地址
* @param fileName 文件名
* @param localFileDir 本地文件地址
* @param copy2TargetDirPath 下载完后,还需要复制到此文件夹下
* @param remoteUrl 远程地址
* @param fileName 文件名
* @param localFileDir 本地文件地址
* @param targetDirPath 下载完后,配置文件放到此目录下
*
* @return 如果是放到Classpath目录下,则返回相对Classpath的路径,如果不是,则返回全路径
*
* @throws Exception
*/
String downloadFromServer(RemoteUrl remoteUrl, String fileName, String localFileDir, String localFileDirTemp,
String copy2TargetDirPath,
String targetDirPath,
boolean enableLocalDownloadDirInClassPath,
int retryTimes, int retrySleepSeconds) throws Exception;

Expand Down
Expand Up @@ -76,10 +76,10 @@ public <T> T getJsonData(Class<T> clazz, RemoteUrl remoteUrl, int retryTimes, in
}

/**
* @param remoteUrl 远程地址
* @param fileName 文件名
* @param localFileDir 本地文件地址
* @param copy2TargetDirPath 下载完后,还需要复制到此文件夹下
* @param remoteUrl 远程地址
* @param fileName 文件名
* @param localFileDir 本地文件地址
* @param targetDirPath 下载完后,配置文件指定的文件目录
* @param retryTimes
* @param retrySleepSeconds
*
Expand All @@ -89,7 +89,7 @@ public <T> T getJsonData(Class<T> clazz, RemoteUrl remoteUrl, int retryTimes, in
*/
@Override
public String downloadFromServer(RemoteUrl remoteUrl, String fileName, String localFileDir, String localFileDirTemp,
String copy2TargetDirPath, boolean enableLocalDownloadDirInClassPath,
String targetDirPath, boolean enableLocalDownloadDirInClassPath,
int retryTimes, int retrySleepSeconds)
throws Exception {

Expand All @@ -110,12 +110,12 @@ public String downloadFromServer(RemoteUrl remoteUrl, String fileName, String lo
localFile = transfer2SpecifyDir(tmpFilePathUniqueFile, localFileDir, fileName, false);

// mv 到指定目录
if (copy2TargetDirPath != null) {
if (targetDirPath != null) {

//
if (enableLocalDownloadDirInClassPath || !copy2TargetDirPath.equals(ClassLoaderUtil.getClassPath
if (enableLocalDownloadDirInClassPath || !targetDirPath.equals(ClassLoaderUtil.getClassPath
())) {
localFile = transfer2SpecifyDir(tmpFilePathUniqueFile, copy2TargetDirPath, fileName, true);
localFile = transfer2SpecifyDir(tmpFilePathUniqueFile, targetDirPath, fileName, true);
}
}

Expand All @@ -129,7 +129,7 @@ public String downloadFromServer(RemoteUrl remoteUrl, String fileName, String lo
//
// 判断是否下载失败
//

if (localFile == null || !localFile.exists()) {
throw new Exception("target file cannot be found! " + fileName);
}
Expand Down Expand Up @@ -184,20 +184,20 @@ private File retryDownload(String localFileDirTemp, String fileName, RemoteUrl r
* copy/mv 到指定目录
*
* @param srcFile
* @param copy2TargetDirPath
* @param targetDirPath
* @param fileName
*
* @return
*
* @throws Exception
*/
private File transfer2SpecifyDir(File srcFile, String copy2TargetDirPath, String fileName,
private File transfer2SpecifyDir(File srcFile, String targetDirPath, String fileName,
boolean isMove) throws Exception {

// make dir
OsUtil.makeDirs(copy2TargetDirPath);
OsUtil.makeDirs(targetDirPath);

File targetPath = new File(OsUtil.pathJoin(copy2TargetDirPath, fileName));
File targetPath = new File(OsUtil.pathJoin(targetDirPath, fileName));
// 从下载文件 复制/mv 到targetPath 原子性的做转移
OsUtil.transferFileAtom(srcFile, targetPath, isMove);
return targetPath;
Expand Down
2 changes: 1 addition & 1 deletion disconf-web/profile/rd/zoo.properties
@@ -1,5 +1,5 @@

hosts=10.128.21.89:8581,10.128.21.89:8582,10.128.21.89:8583
hosts=127.0.0.1:8581,127.0.0.1:8582,127.0.0.1:8583

# zookeeper\u7684\u524D\u7F00\u8DEF\u5F84\u540D
zookeeper_url_prefix=/disconf
3 changes: 2 additions & 1 deletion docs/source/others/update.md
Expand Up @@ -8,4 +8,5 @@
- disconf-client:
- 支持https的web端 https://github.com/knightliao/disconf/issues/158
- path支持windows: https://github.com/knightliao/disconf/issues/166
- 删除类 DisconfMgrJustHostFileBean
- 删除类 DisconfMgrJustHostFileBean
- DisconfFile 的属性 copy2TargetDirPath 更改为 targetDirPath
22 changes: 16 additions & 6 deletions docs/source/tutorial-client/Tutorial11-config-download-path.md
Expand Up @@ -5,17 +5,27 @@ Tutorial 11 配置文件下载地址讨论

### 解决:按以下顺序进行判断

- 一定会下载到 disconf.user_define_download_dir 目录下(使用此方法可以方便的构造一个下载器 [Tutorial10](Tutorial10.html) )
对于注解式配置文件:

- 一定会下载到 disconf.user_define_download_dir 目录下(使用此方法可以方便的构造一个下载器. [Tutorial10](Tutorial10.html) )
- 如果 disconf.enable_local_download_dir_in_class_path 为true(默认值), 则会执行以下判断:
- 如果 @DisconfFile 的 targetDirPath 值不为空, 则会下载到 targetDirPath 这个目录下, 配置数据从该路径读取。这对于那些不想放在classpath根目录的程序, 比较有用.
- 如果 @DisconfFile 的 targetDirPath 值为空, 则会下载到classpath路径下, 配置数据从该路径读取.
- 如果 disconf.enable_local_download_dir_in_class_path 为false, 则不会下载到classpath目录下. 配置数据从 disconf.user_define_download_dir 读取

对于XML式配置文件:

- 一定会下载到 disconf.user_define_download_dir 目录下(使用此方法可以方便的构造一个下载器).
- 如果 disconf.enable_local_download_dir_in_class_path 为true(默认值), 则会执行以下判断:
- 如果 @DisconfFile 的 copy2TargetDirPath 值不为空,则会下载这个目录下。(copy2TargetDirPath值说明:以"/"开头则是系统的全路径,否则则是相对于classpath的路径,默认是classpath根路径
注意:根路径要注意是否有权限,否则会出现找不到路径,推荐采用相对路径)。这对于那些不想放在classpath根目录的程序,比较有用。
- 如果 @DisconfFile 的 copy2TargetDirPath 值为空,则会下载到根路径下。大部分程序是从根目录读取配置的,因此,默认是放到根下。
- 如果 disconf.enable_local_download_dir_in_class_path 为false, 则不会下载到classpath目录下
- 如果 @DisconfFile 的 targetDirPath 值不为空, 则会下载到 targetDirPath 这个目录下.
- 如果 @DisconfFile 的 targetDirPath 值为空, 则会下载到classpath路径下.
- 如果 disconf.enable_local_download_dir_in_class_path 为false, 则不会下载到classpath目录下.

注:

1. 如果不作任何配置的改变,默认情况下,会下载到 disconf.user_define_download_dir 目录 和 classpath 两个目录下。
2. 配置说明看这里 [config](../../config/client-config.html)
2. targetDirPath 值说明:以"/"开头则是系统的全路径,否则则是相对于classpath的路径,默认是classpath根路径。注意:根路径要注意是否有权限,否则会出现找不到路径,推荐采用相对路径。
3. 配置说明看这里 [config](../../config/client-config.html)

### 问题二 不想下载到classpath目录下

Expand Down

0 comments on commit 78dcecb

Please sign in to comment.