使用spi机制优化组件的加载

pull/716/head
laoyuyu 4 years ago
parent fed6ae55e1
commit 288dcf633e
  1. 4
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoader.java
  2. 12
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDGLoaderUtil.java
  3. 13
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpDLoaderUtil.java
  4. 5
      FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java
  5. 14
      FtpComponent/src/main/java/com/arialyy/aria/ftp/upload/FtpULoaderUtil.java
  6. 3
      FtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil
  7. 4
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoader.java
  8. 14
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGLoaderUtil.java
  9. 11
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java
  10. 5
      HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java
  11. 13
      HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java
  12. 3
      HttpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil
  13. 15
      M3U8Component/src/main/java/com/arialyy/aria/m3u8/M3U8Listener.java
  14. 12
      M3U8Component/src/main/java/com/arialyy/aria/m3u8/live/M3U8LiveUtil.java
  15. 11
      M3U8Component/src/main/java/com/arialyy/aria/m3u8/vod/M3U8VodUtil.java
  16. 2
      M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil
  17. 1
      M3U8Component/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener
  18. 7
      PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java
  19. 16
      PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsSubDLoadUtil.java
  20. 4
      PublicComponent/src/main/java/com/arialyy/aria/core/inf/IUtil.java
  21. 16
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseDListener.java
  22. 23
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseListener.java
  23. 14
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/BaseUListener.java
  24. 17
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/DownloadGroupListener.java
  25. 4
      PublicComponent/src/main/java/com/arialyy/aria/core/listener/IEventListener.java
  26. 9
      PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java
  27. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java
  28. 3
      PublicComponent/src/main/java/com/arialyy/aria/core/wrapper/AbsTaskWrapper.java
  29. 26
      PublicComponent/src/main/java/com/arialyy/aria/exception/AriaComponentException.java
  30. 235
      PublicComponent/src/main/java/com/arialyy/aria/util/AriaServiceLoader.java
  31. 67
      PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java
  32. 3
      PublicComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.listener.IEventListener
  33. 14
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/download/SFtpDLoaderUtil.java
  34. 14
      SFtpComponent/src/main/java/com/arialyy/aria/sftp/upload/SFtpULoaderUtil.java
  35. 2
      SFtpComponent/src/main/resources/META-INF/services/com.arialyy.aria.core.inf.IUtil

@ -45,7 +45,9 @@ final class FtpDGLoader extends AbsGroupLoader {
@Override @Override
protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) {
return new FtpSubDLoaderUtil(wrapper, getScheduler(), needGetFileInfo, getKey()); FtpSubDLoaderUtil subUtil = new FtpSubDLoaderUtil(getScheduler(), needGetFileInfo, getKey());
subUtil.setParams(wrapper, null);
return subUtil;
} }
/** /**

@ -18,7 +18,6 @@ package com.arialyy.aria.ftp.download;
import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.group.AbsGroupLoader; import com.arialyy.aria.core.group.AbsGroupLoader;
import com.arialyy.aria.core.group.AbsGroupLoaderUtil; import com.arialyy.aria.core.group.AbsGroupLoaderUtil;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption; import com.arialyy.aria.ftp.FtpTaskOption;
@ -29,13 +28,12 @@ import com.arialyy.aria.ftp.FtpTaskOption;
*/ */
public final class FtpDGLoaderUtil extends AbsGroupLoaderUtil { public final class FtpDGLoaderUtil extends AbsGroupLoaderUtil {
public FtpDGLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(FtpTaskOption.class);
}
@Override protected AbsGroupLoader getLoader() { @Override protected AbsGroupLoader getLoader() {
return mLoader == null ? new FtpDGLoader(getTaskWrapper(), getListener()) : mLoader; if (mLoader == null) {
((AbsTaskWrapper) getTaskWrapper()).generateTaskOption(FtpTaskOption.class);
mLoader = new FtpDGLoader((AbsTaskWrapper) getTaskWrapper(), getListener());
}
return mLoader;
} }
@Override protected LoaderStructure buildLoaderStructure() { @Override protected LoaderStructure buildLoaderStructure() {

@ -16,14 +16,12 @@
package com.arialyy.aria.ftp.download; package com.arialyy.aria.ftp.download;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption; import com.arialyy.aria.ftp.FtpTaskOption;
/** /**
@ -32,13 +30,12 @@ import com.arialyy.aria.ftp.FtpTaskOption;
*/ */
public final class FtpDLoaderUtil extends AbsNormalLoaderUtil { public final class FtpDLoaderUtil extends AbsNormalLoaderUtil {
public FtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(FtpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
return mLoader == null ? new NormalLoader(getTaskWrapper(), getListener()) : mLoader; if (mLoader == null){
getTaskWrapper().generateTaskOption(FtpTaskOption.class);
mLoader = new NormalLoader(getTaskWrapper(), getListener());
}
return mLoader ;
} }
public LoaderStructure BuildLoaderStructure() { public LoaderStructure BuildLoaderStructure() {

@ -32,9 +32,8 @@ final class FtpSubDLoaderUtil extends AbsSubDLoadUtil {
* @param schedulers 调度器 * @param schedulers 调度器
* @param needGetInfo {@code true} 需要获取文件信息{@code false} 不需要获取文件信息 * @param needGetInfo {@code true} 需要获取文件信息{@code false} 不需要获取文件信息
*/ */
FtpSubDLoaderUtil(DTaskWrapper taskWrapper, Handler schedulers, boolean needGetInfo, FtpSubDLoaderUtil(Handler schedulers, boolean needGetInfo, String parentKey) {
String parentKey) { super(schedulers, needGetInfo, parentKey);
super(taskWrapper, schedulers, needGetInfo, parentKey);
} }
@Override protected SubLoader getLoader() { @Override protected SubLoader getLoader() {

@ -17,7 +17,6 @@ package com.arialyy.aria.ftp.upload;
import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter; import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
@ -26,7 +25,6 @@ import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.task.IThreadTaskAdapter; import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.ftp.FtpTaskOption; import com.arialyy.aria.ftp.FtpTaskOption;
/** /**
@ -35,14 +33,12 @@ import com.arialyy.aria.ftp.FtpTaskOption;
*/ */
public final class FtpULoaderUtil extends AbsNormalLoaderUtil { public final class FtpULoaderUtil extends AbsNormalLoaderUtil {
public FtpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(FtpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
return mLoader == null ? new FtpULoader((UTaskWrapper) getTaskWrapper(), getListener()) if (mLoader == null) {
: mLoader; getTaskWrapper().generateTaskOption(FtpTaskOption.class);
mLoader = new FtpULoader((UTaskWrapper) getTaskWrapper(), getListener());
}
return mLoader;
} }
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {

@ -0,0 +1,3 @@
com.arialyy.aria.ftp.download.FtpDLoaderUtil
com.arialyy.aria.ftp.download.FtpDGLoaderUtil
com.arialyy.aria.ftp.upload.FtpULoaderUtil

@ -45,7 +45,9 @@ final class HttpDGLoader extends AbsGroupLoader {
@Override @Override
protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) { protected AbsSubDLoadUtil createSubLoader(DTaskWrapper wrapper, boolean needGetFileInfo) {
return new HttpSubDLoaderUtil(wrapper, getScheduler(), needGetFileInfo, getKey()); HttpSubDLoaderUtil subUtil = new HttpSubDLoaderUtil(getScheduler(), needGetFileInfo, getKey());
subUtil.setParams(wrapper, null);
return subUtil;
} }
private void startSub() { private void startSub() {

@ -19,9 +19,7 @@ import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.group.AbsGroupLoader; import com.arialyy.aria.core.group.AbsGroupLoader;
import com.arialyy.aria.core.group.AbsGroupLoaderUtil; import com.arialyy.aria.core.group.AbsGroupLoaderUtil;
import com.arialyy.aria.core.listener.DownloadGroupListener; import com.arialyy.aria.core.listener.DownloadGroupListener;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.http.HttpTaskOption;
/** /**
@ -30,14 +28,12 @@ import com.arialyy.aria.http.HttpTaskOption;
*/ */
public final class HttpDGLoaderUtil extends AbsGroupLoaderUtil { public final class HttpDGLoaderUtil extends AbsGroupLoaderUtil {
public HttpDGLoaderUtil(AbsTaskWrapper taskWrapper, IEventListener listener) {
super(taskWrapper, listener);
taskWrapper.generateTaskOption(HttpTaskOption.class);
}
@Override protected AbsGroupLoader getLoader() { @Override protected AbsGroupLoader getLoader() {
return mLoader == null ? new HttpDGLoader(getTaskWrapper(), if (mLoader == null) {
(DownloadGroupListener) getListener()) : mLoader; getTaskWrapper().generateTaskOption(HttpTaskOption.class);
mLoader = new HttpDGLoader(getTaskWrapper(), (DownloadGroupListener) getListener());
}
return mLoader;
} }
@Override protected LoaderStructure buildLoaderStructure() { @Override protected LoaderStructure buildLoaderStructure() {

@ -24,6 +24,7 @@ import com.arialyy.aria.core.loader.NormalLoader;
import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper;
import com.arialyy.aria.http.HttpRecordHandler; import com.arialyy.aria.http.HttpRecordHandler;
import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.http.HttpTaskOption;
@ -32,13 +33,13 @@ import com.arialyy.aria.http.HttpTaskOption;
* @Date 2019-09-21 * @Date 2019-09-21
*/ */
public final class HttpDLoaderUtil extends AbsNormalLoaderUtil { public final class HttpDLoaderUtil extends AbsNormalLoaderUtil {
public HttpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(HttpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
return mLoader == null ? new NormalLoader(getTaskWrapper(), getListener()) : mLoader; if (mLoader == null){
getTaskWrapper().generateTaskOption(HttpTaskOption.class);
mLoader = new NormalLoader(getTaskWrapper(), getListener());
}
return mLoader;
} }
public LoaderStructure BuildLoaderStructure() { public LoaderStructure BuildLoaderStructure() {

@ -33,9 +33,8 @@ final class HttpSubDLoaderUtil extends AbsSubDLoadUtil {
* @param schedulers 调度器 * @param schedulers 调度器
* @param needGetInfo {@code true} 需要获取文件信息{@code false} 不需要获取文件信息 * @param needGetInfo {@code true} 需要获取文件信息{@code false} 不需要获取文件信息
*/ */
HttpSubDLoaderUtil(DTaskWrapper taskWrapper, Handler schedulers, boolean needGetInfo, HttpSubDLoaderUtil( Handler schedulers, boolean needGetInfo, String parentKey) {
String parentKey) { super(schedulers, needGetInfo, parentKey);
super(taskWrapper, schedulers, needGetInfo, parentKey);
} }
@Override protected SubLoader getLoader() { @Override protected SubLoader getLoader() {

@ -17,7 +17,6 @@ package com.arialyy.aria.http.upload;
import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.common.SubThreadConfig;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter; import com.arialyy.aria.core.loader.AbsNormalTTBuilderAdapter;
@ -26,7 +25,6 @@ import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.task.IThreadTaskAdapter; import com.arialyy.aria.core.task.IThreadTaskAdapter;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpRecordHandler; import com.arialyy.aria.http.HttpRecordHandler;
import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.http.HttpTaskOption;
@ -35,14 +33,13 @@ import com.arialyy.aria.http.HttpTaskOption;
* @Date 2019-09-19 * @Date 2019-09-19
*/ */
public final class HttpULoaderUtil extends AbsNormalLoaderUtil { public final class HttpULoaderUtil extends AbsNormalLoaderUtil {
public HttpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(HttpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
return mLoader == null ? new HttpULoader((UTaskWrapper) getTaskWrapper(), getListener()) if (mLoader == null) {
: mLoader; getTaskWrapper().generateTaskOption(HttpTaskOption.class);
mLoader = new HttpULoader((UTaskWrapper) getTaskWrapper(), getListener());
}
return mLoader;
} }
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {

@ -0,0 +1,3 @@
com.arialyy.aria.http.download.HttpDLoaderUtil
com.arialyy.aria.http.download.HttpDGLoaderUtil
com.arialyy.aria.http.upload.HttpULoaderUtil

@ -16,29 +16,20 @@
package com.arialyy.aria.m3u8; package com.arialyy.aria.m3u8;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Message; import android.os.Message;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.listener.BaseListener; import com.arialyy.aria.core.listener.BaseListener;
import com.arialyy.aria.core.listener.IDLoadListener; import com.arialyy.aria.core.listener.IDLoadListener;
import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.listener.ISchedulers;
import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.core.task.DownloadTask;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.DeleteM3u8Record; import com.arialyy.aria.util.DeleteM3u8Record;
/** /**
* 下载监听类 * 下载监听类
*/ */
public final class M3U8Listener public final class M3U8Listener extends BaseListener implements IDLoadListener {
extends BaseListener<DownloadEntity, DTaskWrapper, AbsTask<DTaskWrapper>>
implements IDLoadListener {
public M3U8Listener(AbsTask<DTaskWrapper> task, Handler outHandler) {
super(task, outHandler);
}
@Override @Override
public void onPostPre(long fileSize) { public void onPostPre(long fileSize) {
@ -88,7 +79,7 @@ public final class M3U8Listener
} }
@Override protected void handleCancel() { @Override protected void handleCancel() {
int sType = getTask().getSchedulerType(); int sType = getTask(DownloadTask.class).getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false); mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT); mEntity.setState(IEntity.STATE_WAIT);

@ -16,10 +16,8 @@
package com.arialyy.aria.m3u8.live; package com.arialyy.aria.m3u8.live;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.http.HttpTaskOption;
import com.arialyy.aria.m3u8.M3U8InfoTask; import com.arialyy.aria.m3u8.M3U8InfoTask;
import com.arialyy.aria.m3u8.M3U8Listener; import com.arialyy.aria.m3u8.M3U8Listener;
@ -36,8 +34,7 @@ import com.arialyy.aria.m3u8.M3U8TaskOption;
*/ */
public class M3U8LiveUtil extends AbsNormalLoaderUtil { public class M3U8LiveUtil extends AbsNormalLoaderUtil {
public M3U8LiveUtil(AbsTaskWrapper wrapper, IEventListener listener) { public M3U8LiveUtil() {
super(wrapper, listener);
} }
@Override public DTaskWrapper getTaskWrapper() { @Override public DTaskWrapper getTaskWrapper() {
@ -45,11 +42,12 @@ public class M3U8LiveUtil extends AbsNormalLoaderUtil {
} }
@Override public M3U8LiveLoader getLoader() { @Override public M3U8LiveLoader getLoader() {
if (mLoader == null) {
getTaskWrapper().generateM3u8Option(M3U8TaskOption.class); getTaskWrapper().generateM3u8Option(M3U8TaskOption.class);
getTaskWrapper().generateTaskOption(HttpTaskOption.class); getTaskWrapper().generateTaskOption(HttpTaskOption.class);
return mLoader = new M3U8LiveLoader(getTaskWrapper(), (M3U8Listener) getListener());
mLoader == null ? new M3U8LiveLoader(getTaskWrapper(), (M3U8Listener) getListener()) }
: (M3U8LiveLoader) mLoader; return (M3U8LiveLoader) mLoader;
} }
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {

@ -16,11 +16,9 @@
package com.arialyy.aria.m3u8.vod; package com.arialyy.aria.m3u8.vod;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.http.HttpTaskOption;
import com.arialyy.aria.m3u8.M3U8InfoTask; import com.arialyy.aria.m3u8.M3U8InfoTask;
import com.arialyy.aria.m3u8.M3U8Listener; import com.arialyy.aria.m3u8.M3U8Listener;
@ -36,8 +34,7 @@ import com.arialyy.aria.m3u8.M3U8TaskOption;
*/ */
public final class M3U8VodUtil extends AbsNormalLoaderUtil { public final class M3U8VodUtil extends AbsNormalLoaderUtil {
public M3U8VodUtil(AbsTaskWrapper wrapper, IEventListener listener) { public M3U8VodUtil() {
super(wrapper, listener);
} }
@Override public DTaskWrapper getTaskWrapper() { @Override public DTaskWrapper getTaskWrapper() {
@ -45,10 +42,12 @@ public final class M3U8VodUtil extends AbsNormalLoaderUtil {
} }
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
if (mLoader == null) {
getTaskWrapper().generateM3u8Option(M3U8TaskOption.class); getTaskWrapper().generateM3u8Option(M3U8TaskOption.class);
getTaskWrapper().generateTaskOption(HttpTaskOption.class); getTaskWrapper().generateTaskOption(HttpTaskOption.class);
return mLoader == null ? new M3U8VodLoader(getTaskWrapper(), (M3U8Listener) getListener()) mLoader = new M3U8VodLoader(getTaskWrapper(), (M3U8Listener) getListener());
: mLoader; }
return mLoader;
} }
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {

@ -0,0 +1,2 @@
com.arialyy.aria.m3u8.live.M3U8LiveUtil
com.arialyy.aria.m3u8.vod.M3U8VodUtil

@ -19,6 +19,7 @@ import com.arialyy.aria.core.inf.IUtil;
import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -34,10 +35,12 @@ public abstract class AbsGroupLoaderUtil implements IUtil {
private AbsTaskWrapper mTaskWrapper; private AbsTaskWrapper mTaskWrapper;
private boolean isStop = false, isCancel = false; private boolean isStop = false, isCancel = false;
protected AbsGroupLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
mTaskWrapper = wrapper; @Override public IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) {
mTaskWrapper = taskWrapper;
mListener = listener; mListener = listener;
mLoader = getLoader(); mLoader = getLoader();
return this;
} }
protected abstract AbsGroupLoader getLoader(); protected abstract AbsGroupLoader getLoader();

@ -15,20 +15,16 @@
*/ */
package com.arialyy.aria.core.group; package com.arialyy.aria.core.group;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message;
import com.arialyy.aria.core.TaskRecord; import com.arialyy.aria.core.TaskRecord;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IThreadStateManager;
import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.inf.IUtil;
import com.arialyy.aria.core.listener.IDLoadListener; import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.listener.ISchedulers;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.SubLoader; import com.arialyy.aria.core.loader.SubLoader;
import com.arialyy.aria.exception.AriaException; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -49,12 +45,16 @@ public abstract class AbsSubDLoadUtil implements IUtil, Runnable {
* @param schedulers 调度器 * @param schedulers 调度器
* @param needGetInfo {@code true} 需要获取文件信息{@code false} 不需要获取文件信息 * @param needGetInfo {@code true} 需要获取文件信息{@code false} 不需要获取文件信息
*/ */
protected AbsSubDLoadUtil(DTaskWrapper taskWrapper, Handler schedulers, boolean needGetInfo, String parentKey) { protected AbsSubDLoadUtil(Handler schedulers, boolean needGetInfo, String parentKey) {
mWrapper = taskWrapper;
mSchedulers = schedulers; mSchedulers = schedulers;
this.parentKey = parentKey; this.parentKey = parentKey;
this.needGetInfo = needGetInfo; this.needGetInfo = needGetInfo;
}
@Override public IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) {
mWrapper = (DTaskWrapper) taskWrapper;
mDLoader = getLoader(); mDLoader = getLoader();
return this;
} }
/** /**

@ -18,7 +18,9 @@ package com.arialyy.aria.core.inf;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
/** /**
* Created by lyy on 2016/10/31. * Created by lyy on 2016/10/31.
@ -26,6 +28,8 @@ import com.arialyy.aria.core.upload.UploadEntity;
*/ */
public interface IUtil { public interface IUtil {
IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener);
/** /**
* 获取任务标志 * 获取任务标志
* *

@ -15,26 +15,16 @@
*/ */
package com.arialyy.aria.core.listener; package com.arialyy.aria.core.listener;
import android.os.Handler;
import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.loader.IRecordHandler;
import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.core.task.DownloadTask;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.DeleteDRecord; import com.arialyy.aria.util.DeleteDRecord;
import com.arialyy.aria.util.RecordUtil;
/** /**
* 下载监听类 * 下载监听类
*/ */
public class BaseDListener extends BaseListener<DownloadEntity, DTaskWrapper, AbsTask<DTaskWrapper>> public class BaseDListener extends BaseListener implements IDLoadListener {
implements IDLoadListener {
public BaseDListener(AbsTask<DTaskWrapper> task, Handler outHandler) {
super(task, outHandler);
}
@Override @Override
public void onPostPre(long fileSize) { public void onPostPre(long fileSize) {
@ -52,7 +42,7 @@ public class BaseDListener extends BaseListener<DownloadEntity, DTaskWrapper, Ab
} }
@Override protected void handleCancel() { @Override protected void handleCancel() {
int sType = getTask().getSchedulerType(); int sType = getTask(DownloadTask.class).getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false); mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT); mEntity.setState(IEntity.STATE_WAIT);

@ -29,22 +29,20 @@ import com.arialyy.aria.util.ErrorHelp;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
public abstract class BaseListener<ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>, public abstract class BaseListener implements IEventListener {
TASK extends AbsTask<TASK_WRAPPER>> protected String TAG = getClass().getSimpleName();
implements IEventListener { static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
protected static String TAG;
protected static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
protected SoftReference<Handler> outHandler; protected SoftReference<Handler> outHandler;
private long mLastLen; //上一次发送长度 private long mLastLen; //上一次发送长度
private boolean isFirst = true; private boolean isFirst = true;
private TASK mTask; private AbsTask mTask;
protected long mLastSaveTime; long mLastSaveTime;
protected ENTITY mEntity; protected AbsEntity mEntity;
protected TASK_WRAPPER mTaskWrapper; protected AbsTaskWrapper mTaskWrapper;
private boolean isConvertSpeed; private boolean isConvertSpeed;
private long mUpdateInterval; private long mUpdateInterval;
protected BaseListener(TASK task, Handler outHandler) { @Override public IEventListener setParams(AbsTask task, Handler outHandler) {
this.outHandler = new SoftReference<>(outHandler); this.outHandler = new SoftReference<>(outHandler);
mTask = new WeakReference<>(task).get(); mTask = new WeakReference<>(task).get();
mEntity = mTask.getTaskWrapper().getEntity(); mEntity = mTask.getTaskWrapper().getEntity();
@ -54,10 +52,11 @@ public abstract class BaseListener<ENTITY extends AbsEntity, TASK_WRAPPER extend
mLastLen = mEntity.getCurrentProgress(); mLastLen = mEntity.getCurrentProgress();
mLastSaveTime = System.currentTimeMillis(); mLastSaveTime = System.currentTimeMillis();
TAG = CommonUtil.getClassName(getClass()); TAG = CommonUtil.getClassName(getClass());
return this;
} }
protected TASK getTask() { protected <TASK extends AbsTask> TASK getTask(Class<TASK> clazz) {
return mTask; return (TASK) mTask;
} }
@Override public void onPre() { @Override public void onPre() {

@ -15,26 +15,18 @@
*/ */
package com.arialyy.aria.core.listener; package com.arialyy.aria.core.listener;
import android.os.Handler;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.inf.TaskSchedulerType;
import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.core.task.UploadTask;
import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.util.DeleteURecord; import com.arialyy.aria.util.DeleteURecord;
/** /**
* 下载监听类 * 下载监听类
*/ */
public class BaseUListener extends BaseListener<UploadEntity, UTaskWrapper, AbsTask<UTaskWrapper>> public class BaseUListener extends BaseListener implements IUploadListener {
implements IUploadListener {
public BaseUListener(AbsTask<UTaskWrapper> task, Handler outHandler) {
super(task, outHandler);
}
@Override protected void handleCancel() { @Override protected void handleCancel() {
int sType = getTask().getSchedulerType(); int sType = getTask(UploadTask.class).getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false); mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT); mEntity.setState(IEntity.STATE_WAIT);

@ -16,7 +16,6 @@
package com.arialyy.aria.core.listener; package com.arialyy.aria.core.listener;
import android.os.Handler; import android.os.Handler;
import com.arialyy.aria.core.download.DGTaskWrapper;
import com.arialyy.aria.core.download.DownloadEntity; import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity; import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.group.GroupSendParams; import com.arialyy.aria.core.group.GroupSendParams;
@ -35,15 +34,14 @@ import static com.arialyy.aria.core.task.AbsTask.ERROR_INFO_KEY;
/** /**
* Created by Aria.Lao on 2017/7/20. 任务组下载事件 * Created by Aria.Lao on 2017/7/20. 任务组下载事件
*/ */
public class DownloadGroupListener public class DownloadGroupListener extends BaseListener implements IDGroupListener {
extends BaseListener<DownloadGroupEntity, DGTaskWrapper, AbsTask<DGTaskWrapper>>
implements IDGroupListener {
private GroupSendParams<DownloadGroupTask, DownloadEntity> mSeedEntity; private GroupSendParams<DownloadGroupTask, DownloadEntity> mSeedEntity;
public DownloadGroupListener(AbsTask<DGTaskWrapper> task, Handler outHandler) { @Override public IEventListener setParams(AbsTask task, Handler outHandler) {
super(task, outHandler); IEventListener listener = super.setParams(task, outHandler);
mSeedEntity = new GroupSendParams<>(); mSeedEntity = new GroupSendParams<>();
mSeedEntity.groupTask = (DownloadGroupTask) task; mSeedEntity.groupTask = (DownloadGroupTask) task;
return listener;
} }
@Override @Override
@ -164,12 +162,13 @@ public class DownloadGroupListener
} }
private void saveCurrentLocation() { private void saveCurrentLocation() {
if (mEntity.getSubEntities() == null || mEntity.getSubEntities().isEmpty()) { DownloadGroupEntity dgEntity = (DownloadGroupEntity) mEntity;
if (dgEntity.getSubEntities() == null || dgEntity.getSubEntities().isEmpty()) {
ALog.w(TAG, "保存进度失败,子任务为null"); ALog.w(TAG, "保存进度失败,子任务为null");
return; return;
} }
long location = 0; long location = 0;
for (DownloadEntity e : mEntity.getSubEntities()) { for (DownloadEntity e : dgEntity.getSubEntities()) {
location += e.getCurrentProgress(); location += e.getCurrentProgress();
} }
if (location > mEntity.getFileSize()) { if (location > mEntity.getFileSize()) {
@ -193,7 +192,7 @@ public class DownloadGroupListener
} }
@Override protected void handleCancel() { @Override protected void handleCancel() {
int sType = getTask().getSchedulerType(); int sType = getTask(DownloadGroupTask.class).getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) { if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false); mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT); mEntity.setState(IEntity.STATE_WAIT);

@ -15,6 +15,8 @@
*/ */
package com.arialyy.aria.core.listener; package com.arialyy.aria.core.listener;
import android.os.Handler;
import com.arialyy.aria.core.task.AbsTask;
import com.arialyy.aria.exception.AriaException; import com.arialyy.aria.exception.AriaException;
/** /**
@ -23,6 +25,8 @@ import com.arialyy.aria.exception.AriaException;
*/ */
public interface IEventListener { public interface IEventListener {
IEventListener setParams(AbsTask task, Handler outHandler);
/** /**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态 * 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态
*/ */

@ -19,6 +19,7 @@ package com.arialyy.aria.core.loader;
import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.inf.IUtil;
import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper;
import com.arialyy.aria.exception.AriaException; import com.arialyy.aria.exception.AriaException;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -34,10 +35,14 @@ public abstract class AbsNormalLoaderUtil implements IUtil {
private AbsTaskWrapper mTaskWrapper; private AbsTaskWrapper mTaskWrapper;
private boolean isStop = false, isCancel = false; private boolean isStop = false, isCancel = false;
protected AbsNormalLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) { protected AbsNormalLoaderUtil() {
mTaskWrapper = wrapper; }
@Override public IUtil setParams(AbsTaskWrapper taskWrapper, IEventListener listener) {
mTaskWrapper = taskWrapper;
mListener = listener; mListener = listener;
mLoader = getLoader(); mLoader = getLoader();
return this;
} }
/** /**

@ -64,7 +64,7 @@ public abstract class AbsTask<TASK_WRAPPER extends AbsTaskWrapper>
return mOutHandler; return mOutHandler;
} }
protected synchronized IUtil getUtil() { synchronized IUtil getUtil() {
if (mUtil == null) { if (mUtil == null) {
mUtil = ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); mUtil = ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener);
} }

@ -29,8 +29,7 @@ import com.arialyy.aria.util.ComponentUtil;
/** /**
* Created by lyy on 2017/2/23. 所有任务实体的父类 * Created by lyy on 2017/2/23. 所有任务实体的父类
*/ */
public abstract class AbsTaskWrapper<ENTITY extends AbsEntity> public abstract class AbsTaskWrapper<ENTITY extends AbsEntity> implements ITaskWrapper {
implements ITaskWrapper {
/** /**
* 刷新信息 {@code true} 重新刷新下载信息 * 刷新信息 {@code true} 重新刷新下载信息

@ -0,0 +1,26 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.exception;
public class AriaComponentException extends RuntimeException {
public AriaComponentException(String message, Exception e) {
super(message, e);
}
public AriaComponentException(String s) {
super(s);
}
}

@ -0,0 +1,235 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
/**
* aria 服务加载器
* 参考{@link ServiceLoader}
*/
public class AriaServiceLoader<S> {
private static final String PREFIX = "META-INF/services/";
// The class or interface representing the service being loaded
private final Class<S> service;
// The class loader used to locate, load, and instantiate providers
private final ClassLoader loader;
// The access control context taken when the ServiceLoader is created
// Android-changed: do not use legacy security code.
// private final AccessControlContext acc;
private LazyLoader lazyLoader;
public void reload() {
lazyLoader = new LazyLoader(service, loader);
}
private AriaServiceLoader(Class<S> svc, ClassLoader cl) {
service = svc;
loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl;
// Android-changed: Do not use legacy security code.
// On Android, System.getSecurityManager() is always null.
// acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null;
reload();
}
public static <S> AriaServiceLoader<S> load(Class<S> service, ClassLoader loader) {
return new AriaServiceLoader<>(service, loader);
}
public static <S> AriaServiceLoader<S> load(Class<S> service) {
Thread.currentThread().setContextClassLoader(AriaServiceLoader.class.getClassLoader());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
return AriaServiceLoader.load(service, cl);
}
public S getService(String serviceName) {
return lazyLoader.loadService(serviceName);
}
private class LazyLoader {
private Class<S> service;
private ClassLoader loader;
private Enumeration<URL> configs = null;
private Iterator<String> pending = null;
private LazyLoader(Class<S> service, ClassLoader loader) {
this.service = service;
this.loader = loader;
parseConfig();
}
// Parse the content of the given URL as a provider-configuration file.
//
// @param service
// The service type for which providers are being sought;
// used to construct error detail strings
//
// @param u
// The URL naming the configuration file to be parsed
//
// @return A (possibly empty) iterator that will yield the provider-class
// names in the given configuration file that are not yet members
// of the returned set
//
// @throws ServiceConfigurationError
// If an I/O error occurs while reading from the given URL, or
// if a configuration-file format error is detected
//
private Iterator<String> parse(Class<?> service, URL u) throws ServiceConfigurationError {
InputStream in = null;
BufferedReader r = null;
ArrayList<String> names = new ArrayList<>();
try {
in = u.openStream();
r = new BufferedReader(new InputStreamReader(in, "utf-8"));
int lc = 1;
while ((lc = parseLine(service, u, r, lc, names)) >= 0) ;
} catch (IOException x) {
fail(service, "Error reading configuration file", x);
} finally {
try {
if (r != null) r.close();
if (in != null) in.close();
} catch (IOException y) {
fail(service, "Error closing configuration file", y);
}
}
return names.iterator();
}
private void fail(Class<?> service, String msg, Throwable cause)
throws ServiceConfigurationError {
throw new ServiceConfigurationError(service.getName() + ": " + msg,
cause);
}
private void fail(Class<?> service, String msg)
throws ServiceConfigurationError {
throw new ServiceConfigurationError(service.getName() + ": " + msg);
}
private void fail(Class<?> service, URL u, int line, String msg)
throws ServiceConfigurationError {
fail(service, u + ":" + line + ": " + msg);
}
// Parse a single line from the given configuration file, adding the name
// on the line to the names list.
//
private int parseLine(Class<?> service, URL u, BufferedReader r, int lc,
List<String> names)
throws IOException, ServiceConfigurationError {
String ln = r.readLine();
if (ln == null) {
return -1;
}
int ci = ln.indexOf('#');
if (ci >= 0) ln = ln.substring(0, ci);
ln = ln.trim();
int n = ln.length();
if (n != 0) {
if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0)) {
fail(service, u, lc, "Illegal configuration-file syntax");
}
int cp = ln.codePointAt(0);
if (!Character.isJavaIdentifierStart(cp)) {
fail(service, u, lc, "Illegal provider-class name: " + ln);
}
for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
cp = ln.codePointAt(i);
if (!Character.isJavaIdentifierPart(cp) && (cp != '.')) {
fail(service, u, lc, "Illegal provider-class name: " + ln);
}
}
if (!names.contains(ln)) {
names.add(ln);
}
}
return lc + 1;
}
private S loadService(String serviceName) {
while (pending.hasNext()) {
if (pending.next().equals(serviceName)) {
Class<?> c = null;
try {
c = Class.forName(serviceName, false, loader);
} catch (ClassNotFoundException x) {
fail(service,
// Android-changed: Let the ServiceConfigurationError have a cause.
"Provider " + serviceName + " not found", x);
// "Provider " + cn + " not found");
}
if (!service.isAssignableFrom(c)) {
// Android-changed: Let the ServiceConfigurationError have a cause.
ClassCastException cce = new ClassCastException(
service.getCanonicalName() + " is not assignable from " + c.getCanonicalName());
fail(service,
"Provider " + serviceName + " not a subtype", cce);
// fail(service,
// "Provider " + cn + " not a subtype");
}
try {
return service.cast(c.newInstance());
} catch (Throwable x) {
fail(service, "Provider " + serviceName + " could not be instantiated", x);
}
}
}
throw new Error(); // This cannot happen
}
/**
* 解析配置文件
*/
private void parseConfig() {
if (configs == null) {
try {
String fullName = PREFIX + service.getName();
if (loader == null) {
configs = ClassLoader.getSystemResources(fullName);
} else {
configs = loader.getResources(fullName);
}
} catch (IOException x) {
fail(service, "Error locating configuration files", x);
}
}
while ((pending == null) || !pending.hasNext()) {
if (!configs.hasMoreElements()) {
return;
}
pending = parse(service, configs.nextElement());
}
}
}
}

@ -24,10 +24,9 @@ import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.task.AbsTask; import com.arialyy.aria.core.task.AbsTask;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.core.wrapper.ITaskWrapper; import com.arialyy.aria.core.wrapper.ITaskWrapper;
import com.arialyy.aria.exception.AriaComponentException;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
/** /**
@ -44,9 +43,12 @@ public class ComponentUtil {
private String TAG = CommonUtil.getClassName(getClass()); private String TAG = CommonUtil.getClassName(getClass());
private static volatile ComponentUtil INSTANCE = null; private static volatile ComponentUtil INSTANCE = null;
private AriaServiceLoader<IUtil> utilLoader;
private AriaServiceLoader<IEventListener> listenerLoader;
private ComponentUtil() { private ComponentUtil() {
utilLoader = AriaServiceLoader.load(IUtil.class);
listenerLoader = AriaServiceLoader.load(IEventListener.class);
} }
public static ComponentUtil getInstance() { public static ComponentUtil getInstance() {
@ -99,12 +101,12 @@ public class ComponentUtil {
} }
/** /**
* 创建下载工具 * 创建任务工具
* *
* @return 返回下载工具创建失败返回null * @return 返回任务工具
*/ */
public synchronized <T extends IUtil> T buildUtil(AbsTaskWrapper wrapper, public synchronized IUtil buildUtil(AbsTaskWrapper wrapper, IEventListener listener) {
IEventListener listener) { utilLoader.reload();
int requestType = wrapper.getRequestType(); int requestType = wrapper.getRequestType();
String className = null; String className = null;
switch (requestType) { switch (requestType) {
@ -139,29 +141,11 @@ public class ComponentUtil {
className = "com.arialyy.aria.sftp.upload.SFtpULoaderUtil"; className = "com.arialyy.aria.sftp.upload.SFtpULoaderUtil";
break; break;
} }
if (className == null) { IUtil util = utilLoader.getService(className);
ALog.e(TAG, "不识别的类名:" + className); if (util == null) {
return null; throw new AriaComponentException("加载工具异常,请求类型:" + wrapper.getRequestType());
} }
T util = null; return util.setParams(wrapper, listener);
try {
Class<T> clazz = (Class<T>) getClass().getClassLoader().loadClass(className);
Class[] paramTypes = { AbsTaskWrapper.class, IEventListener.class };
Object[] params = { wrapper, listener };
Constructor<T> con = clazz.getConstructor(paramTypes);
util = con.newInstance(params);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return util;
} }
/** /**
@ -170,7 +154,7 @@ public class ComponentUtil {
* @param wrapperType 任务类型{@link ITaskWrapper} * @param wrapperType 任务类型{@link ITaskWrapper}
* @return 返回事件监听如果创建失败返回null * @return 返回事件监听如果创建失败返回null
*/ */
public synchronized <T extends IEventListener> T buildListener(int wrapperType, AbsTask task, public synchronized IEventListener buildListener(int wrapperType, AbsTask task,
Handler outHandler) { Handler outHandler) {
String className = null, errorStr = "请添加FTP插件"; String className = null, errorStr = "请添加FTP插件";
switch (wrapperType) { switch (wrapperType) {
@ -197,25 +181,12 @@ public class ComponentUtil {
if (className == null) { if (className == null) {
return null; return null;
} }
T listener = null;
try { IEventListener listener = listenerLoader.getService(className);
Class<T> clazz = (Class<T>) getClass().getClassLoader().loadClass(className); if (listener == null) {
Class[] paramTypes = { AbsTask.class, Handler.class }; throw new AriaComponentException(errorStr);
Object[] params = { task, outHandler };
Constructor<T> con = clazz.getConstructor(paramTypes);
listener = con.newInstance(params);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(errorStr);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} }
return listener; return listener.setParams(task, outHandler);
} }
/** /**

@ -0,0 +1,3 @@
com.arialyy.aria.core.listener.BaseDListener
com.arialyy.aria.core.listener.BaseUListener
com.arialyy.aria.core.listener.DownloadGroupListener

@ -16,13 +16,11 @@
package com.arialyy.aria.sftp.download; package com.arialyy.aria.sftp.download;
import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DTaskWrapper;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.sftp.SFtpTaskOption; import com.arialyy.aria.sftp.SFtpTaskOption;
/** /**
@ -32,14 +30,12 @@ import com.arialyy.aria.sftp.SFtpTaskOption;
*/ */
public class SFtpDLoaderUtil extends AbsNormalLoaderUtil { public class SFtpDLoaderUtil extends AbsNormalLoaderUtil {
public SFtpDLoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(SFtpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
return mLoader == null ? new SFtpDLoader((DTaskWrapper) getTaskWrapper(), getListener()) if (mLoader == null) {
: mLoader; getTaskWrapper().generateTaskOption(SFtpTaskOption.class);
mLoader = new SFtpDLoader((DTaskWrapper) getTaskWrapper(), getListener());
}
return mLoader;
} }
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {

@ -15,14 +15,12 @@
*/ */
package com.arialyy.aria.sftp.upload; package com.arialyy.aria.sftp.upload;
import com.arialyy.aria.core.listener.IEventListener;
import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoader;
import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil;
import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.LoaderStructure;
import com.arialyy.aria.core.loader.NormalTTBuilder; import com.arialyy.aria.core.loader.NormalTTBuilder;
import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.loader.NormalThreadStateManager;
import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
import com.arialyy.aria.sftp.SFtpTaskOption; import com.arialyy.aria.sftp.SFtpTaskOption;
/** /**
@ -32,14 +30,12 @@ import com.arialyy.aria.sftp.SFtpTaskOption;
*/ */
public class SFtpULoaderUtil extends AbsNormalLoaderUtil { public class SFtpULoaderUtil extends AbsNormalLoaderUtil {
public SFtpULoaderUtil(AbsTaskWrapper wrapper, IEventListener listener) {
super(wrapper, listener);
wrapper.generateTaskOption(SFtpTaskOption.class);
}
@Override public AbsNormalLoader getLoader() { @Override public AbsNormalLoader getLoader() {
return mLoader == null ? new SFtpULoader((UTaskWrapper) getTaskWrapper(), getListener()) if (mLoader == null) {
: mLoader; getTaskWrapper().generateTaskOption(SFtpTaskOption.class);
mLoader = new SFtpULoader((UTaskWrapper) getTaskWrapper(), getListener());
}
return mLoader;
} }
@Override public LoaderStructure BuildLoaderStructure() { @Override public LoaderStructure BuildLoaderStructure() {

@ -0,0 +1,2 @@
com.arialyy.aria.sftp.download.SFtpDLoaderUtil
com.arialyy.aria.sftp.upload.SFtpULoaderUtil
Loading…
Cancel
Save