|
|
@ -15,79 +15,194 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package com.arialyy.aria.core.manager; |
|
|
|
package com.arialyy.aria.core.manager; |
|
|
|
|
|
|
|
|
|
|
|
import com.arialyy.aria.core.download.DownloadEntity; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.download.DownloadGroupEntity; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.inf.AbsEntity; |
|
|
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
|
|
|
import com.arialyy.aria.core.inf.AbsTaskEntity; |
|
|
|
import com.arialyy.aria.core.upload.UploadEntity; |
|
|
|
|
|
|
|
import com.arialyy.aria.core.upload.UploadTaskEntity; |
|
|
|
import com.arialyy.aria.core.upload.UploadTaskEntity; |
|
|
|
|
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.CommonUtil; |
|
|
|
import com.arialyy.aria.util.CommonUtil; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
|
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Created by Aria.Lao on 2017/11/1. |
|
|
|
* Created by Aria.Lao on 2017/11/1. |
|
|
|
* 任务实体管理器,负责 |
|
|
|
* 任务实体管理器,负责 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class TaskEntityManager { |
|
|
|
public class TEManager { |
|
|
|
private static final String TAG = "TaskManager"; |
|
|
|
private static final String TAG = "TaskManager"; |
|
|
|
private static volatile TaskEntityManager INSTANCE = null; |
|
|
|
private static volatile TEManager INSTANCE = null; |
|
|
|
private Map<String, AbsTaskEntity> map = new ConcurrentHashMap<>(); |
|
|
|
private Map<String, AbsTaskEntity> map = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
private Lock lock; |
|
|
|
|
|
|
|
|
|
|
|
public static TaskEntityManager getInstance() { |
|
|
|
public static TEManager getInstance() { |
|
|
|
if (INSTANCE == null) { |
|
|
|
if (INSTANCE == null) { |
|
|
|
synchronized (TaskEntityManager.class) { |
|
|
|
synchronized (TEManager.class) { |
|
|
|
INSTANCE = new TaskEntityManager(); |
|
|
|
INSTANCE = new TEManager(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return INSTANCE; |
|
|
|
return INSTANCE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TEManager() { |
|
|
|
|
|
|
|
lock = new ReentrantLock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 通过key创建任务,只适应于单任务,不能用于HTTP任务组,可用于Ftp文件夹 |
|
|
|
|
|
|
|
* 如果是任务组,请使用{@link #createGTEntity(Class, List)} |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return 如果任务实体创建失败,返回null |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public <TE extends AbsTaskEntity> TE createTEntity(Class<TE> clazz, String key) { |
|
|
|
|
|
|
|
final Lock lock = this.lock; |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(key)); |
|
|
|
|
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
|
|
|
|
ITEntityFactory factory = chooseFactory(clazz); |
|
|
|
|
|
|
|
if (factory == null) { |
|
|
|
|
|
|
|
ALog.e(TAG, "任务实体创建失败"); |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tEntity = factory.create(key); |
|
|
|
|
|
|
|
map.put(convertKey(key), tEntity); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return (TE) tEntity; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 创建任务组实体 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return 如果任务实体创建失败,返回null |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public <TE extends AbsTaskEntity> TE createGTEntity(Class<TE> clazz, List<String> urls) { |
|
|
|
|
|
|
|
final Lock lock = this.lock; |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
String groupName = CommonUtil.getMd5Code(urls); |
|
|
|
|
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(groupName)); |
|
|
|
|
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
|
|
|
|
IGTEntityFactory factory = chooseGroupFactory(clazz); |
|
|
|
|
|
|
|
if (factory == null) { |
|
|
|
|
|
|
|
ALog.e(TAG, "任务实体创建失败"); |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tEntity = factory.create(groupName, urls); |
|
|
|
|
|
|
|
map.put(convertKey(groupName), tEntity); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return (TE) tEntity; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 通过下载实体获取下载任务实体,如果查找不到任务实体,则重新创建任务实体 |
|
|
|
* 通过实体创建任务 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return 如果任务实体创建失败,返回null |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public DownloadTaskEntity getDTEntity(DownloadEntity entity) { |
|
|
|
public <TE extends AbsTaskEntity> TE createTEntity(Class<TE> clazz, AbsEntity absEntity) { |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); |
|
|
|
final Lock lock = this.lock; |
|
|
|
if (tEntity == null || !(tEntity instanceof DownloadTaskEntity)) { |
|
|
|
lock.lock(); |
|
|
|
tEntity = DTEntityFactory.getInstance().create(entity); |
|
|
|
try { |
|
|
|
map.put(convertKey(entity.getKey()), tEntity); |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(absEntity.getKey())); |
|
|
|
|
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
|
|
|
|
ITEntityFactory factory = chooseFactory(clazz); |
|
|
|
|
|
|
|
if (factory == null) { |
|
|
|
|
|
|
|
ALog.e(TAG, "任务实体创建失败"); |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tEntity = factory.create(absEntity); |
|
|
|
|
|
|
|
map.put(convertKey(absEntity.getKey()), tEntity); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return (TE) tEntity; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IGTEntityFactory chooseGroupFactory(Class clazz) { |
|
|
|
|
|
|
|
if (clazz == DownloadGroupTaskEntity.class) { |
|
|
|
|
|
|
|
return DGTEntityFactory.getInstance(); |
|
|
|
} |
|
|
|
} |
|
|
|
return (DownloadTaskEntity) tEntity; |
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ITEntityFactory chooseFactory(Class clazz) { |
|
|
|
|
|
|
|
if (clazz == DownloadTaskEntity.class) { |
|
|
|
|
|
|
|
return DTEntityFactory.getInstance(); |
|
|
|
|
|
|
|
} else if (clazz == UploadTaskEntity.class) { |
|
|
|
|
|
|
|
return UTEntityFactory.getInstance(); |
|
|
|
|
|
|
|
} else if (clazz == DownloadGroupTaskEntity.class) { |
|
|
|
|
|
|
|
return DGTEntityFactory.getInstance(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 通过下载实体获取下载任务组实体,如果查找不到任务组实体,则重新创建任务组实体 |
|
|
|
* 从任务实体管理器中获取任务实体 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public DownloadGroupTaskEntity getDGTEntity(DownloadGroupEntity entity) { |
|
|
|
public <TE extends AbsTaskEntity> TE getTEntity(Class<TE> clazz, String key) { |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); |
|
|
|
final Lock lock = this.lock; |
|
|
|
if (tEntity == null || !(tEntity instanceof DownloadGroupTaskEntity)) { |
|
|
|
lock.lock(); |
|
|
|
tEntity = DGTEntityFactory.getInstance().create(entity); |
|
|
|
try { |
|
|
|
map.put(convertKey(entity.getKey()), tEntity); |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(key)); |
|
|
|
|
|
|
|
if (tEntity == null) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return (TE) tEntity; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
return (DownloadGroupTaskEntity) tEntity; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 通过下载实体获取下载任务组实体,如果查找不到任务组实体,则重新创建任务组实体 |
|
|
|
* 向管理器中增加任务实体 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return {@code false} 实体为null,添加失败 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public UploadTaskEntity getUTEntity(UploadEntity entity) { |
|
|
|
public boolean addTEntity(AbsTaskEntity te) { |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(entity.getKey())); |
|
|
|
if (te == null) { |
|
|
|
if (tEntity == null || !(tEntity instanceof UploadTaskEntity)) { |
|
|
|
ALog.e(TAG, "任务实体添加失败"); |
|
|
|
tEntity = UTEntityFactory.getInstance().create(entity); |
|
|
|
return false; |
|
|
|
map.put(convertKey(entity.getKey()), tEntity); |
|
|
|
} |
|
|
|
|
|
|
|
final Lock lock = this.lock; |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return map.put(convertKey(te.key), te) != null; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
return (UploadTaskEntity) tEntity; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 通过key删除任务实体 |
|
|
|
* 通过key删除任务实体 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public AbsTaskEntity removeTEntity(String key) { |
|
|
|
public AbsTaskEntity removeTEntity(String key) { |
|
|
|
|
|
|
|
final Lock lock = this.lock; |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
return map.remove(convertKey(key)); |
|
|
|
return map.remove(convertKey(key)); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String convertKey(String key) { |
|
|
|
private String convertKey(String key) { |
|
|
|
return CommonUtil.encryptBASE64(key); |
|
|
|
final Lock lock = this.lock; |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return CommonUtil.keyToHashKey(key); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|