Skip to content

Commit

Permalink
download2
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Nov 21, 2023
1 parent 2210325 commit c79421b
Show file tree
Hide file tree
Showing 26 changed files with 149 additions and 82 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ allprojects {
set('RedissonVersion', '3.22.1')
set('ReactorVersion', '3.4.25')
set('RxJava3Version', '3.1.6')
set('OkHttpVersion', '4.9.3')
set('XnioVersion', '3.8.6.Final')
set('VueVersion', '2.6.11')
set('VueResourceVersion', '1.5.1')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.context.DownloadContextFactory;
import com.github.linyuzai.download.core.event.DownloadCompletedEvent;
import com.github.linyuzai.download.core.event.DownloadEventPublisher;
import com.github.linyuzai.download.core.event.DownloadStartedEvent;
import com.github.linyuzai.download.core.event.*;
import com.github.linyuzai.download.core.handler.DownloadHandler;
import com.github.linyuzai.download.core.logger.DownloadLogger;
import com.github.linyuzai.download.core.options.DownloadOptions;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,19 +28,32 @@ public abstract class AbstractDownloadConcept implements DownloadConcept {

private final DownloadEventPublisher eventPublisher;

private final DownloadLogger logger;

@Override
public Object download(DownloadOptions options) {
//创建上下文
DownloadContext context = contextFactory.create();
context.set(DownloadOptions.class, options);
context.set(DownloadEventPublisher.class, eventPublisher);
eventPublisher.publish(new DownloadStartedEvent(context));
context.set(DownloadLogger.class, logger);
DownloadEventPublisher publisher = delegateDownloadEventPublisher(options);
context.set(DownloadEventPublisher.class, publisher);
publisher.publish(new DownloadStartedEvent(context));
List<DownloadHandler> filtered = handlers.stream()
.filter(it -> it.support(context))
.collect(Collectors.toList());
//处理链
return doDownload(context, filtered, () ->
eventPublisher.publish(new DownloadCompletedEvent(context)));
publisher.publish(new DownloadCompletedEvent(context)));
}

protected DownloadEventPublisher delegateDownloadEventPublisher(DownloadOptions options) {
DownloadEventListener listener = options.getEventListener();
if (listener == null) {
return eventPublisher;
} else {
return new DownloadEventPublisherDelegate(eventPublisher, listener);
}
}

protected abstract Object doDownload(DownloadContext context, List<DownloadHandler> handlers, Runnable onComplete);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Collection;
import java.util.List;

/**
* {@link DownloadEventPublisher} 的简单实现,支持 {@link DownloadEventListener} 的监听机制。
Expand All @@ -15,7 +15,7 @@ public class SimpleDownloadEventPublisher implements DownloadEventPublisher {
/**
* {@link DownloadEventListener} 集合
*/
private final Collection<DownloadEventListener> listeners;
private final List<DownloadEventListener> listeners;

@Override
public void publish(Object event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.linyuzai.download.core.log;
package com.github.linyuzai.download.core.logger;

public interface DownloadLogger {

String TAG = "Download >> ";

void info(String message);

void error(String message, Throwable e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.linyuzai.download.core.log;
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadEventListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.linyuzai.download.core.log;
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadCompletedEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.linyuzai.download.core.log;
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.compress.AbstractCompressSourceEvent;
import com.github.linyuzai.download.core.compress.CompressionCacheDeletedEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.linyuzai.download.core.log;
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadCompletedEvent;
import com.github.linyuzai.download.core.event.DownloadStartedEvent;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -28,7 +29,9 @@ public void logOnEvent(Object event) {
if (event instanceof DownloadStartedEvent) {
StopWatch watch = new StopWatch();
watch.start();
stopWatchMap.put(((DownloadStartedEvent) event).getContext().get(LOG_ID), watch);
String logId = UUID.randomUUID().toString();
((DownloadStartedEvent) event).getContext().set(LOG_ID, logId);
stopWatchMap.put(logId, watch);
} else if (event instanceof DownloadCompletedEvent) {
DownloadContext context = ((DownloadCompletedEvent) event).getContext();
String id = context.get(LOG_ID);
Expand All @@ -43,16 +46,20 @@ public void logOnEvent(Object event) {

public static class StopWatch {

public void start() {
private long start;

private long span;

public void start() {
start = System.nanoTime();
}

public void stop() {

span = System.nanoTime() - start;
}

public double getTotalTimeSeconds() {
return 0.0;
return span / 1000000000.0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ targetCompatibility = JavaVersion.VERSION_1_8
dependencies {
compileOnly 'org.springframework.boot:spring-boot-starter'
compileOnly project(':concept-download:concept-download-core')
compileOnly 'com.squareup.okhttp3:okhttp:4.9.3'
compileOnly "com.squareup.okhttp3:okhttp:${OkHttpVersion}"
}

apply from: '../../publish.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
compileOnly project(':concept-download:concept-download-core')
compileOnly project(':concept-download:concept-download-source-okhttp')
compileOnly project(':concept-download:concept-download-load-coroutines')
compileOnly "com.squareup.okhttp3:okhttp:${OkHttpVersion}"
}

apply from: '../../publish.gradle'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.download.autoconfigure;

import com.github.linyuzai.download.autoconfigure.event.ApplicationDownloadEventPublisher;
import com.github.linyuzai.download.autoconfigure.logger.CommonsDownloadLogger;
import com.github.linyuzai.download.autoconfigure.properties.DownloadProperties;
import com.github.linyuzai.download.autoconfigure.source.classpath.ClassPathPrefixSourceFactory;
import com.github.linyuzai.download.autoconfigure.source.classpath.ClassPathSourceFactory;
Expand All @@ -16,17 +17,17 @@
import com.github.linyuzai.download.core.context.DownloadContextFactory;
import com.github.linyuzai.download.core.event.DownloadEventListener;
import com.github.linyuzai.download.core.event.DownloadEventPublisher;
import com.github.linyuzai.download.core.event.DownloadEventPublisherInitializer;
import com.github.linyuzai.download.core.handler.DownloadHandler;
import com.github.linyuzai.download.core.handler.impl.CompressSourceHandler;
import com.github.linyuzai.download.core.handler.impl.CreateSourceHandler;
import com.github.linyuzai.download.core.handler.impl.LoadSourceHandler;
import com.github.linyuzai.download.core.handler.impl.WriteResponseHandler;
import com.github.linyuzai.download.core.load.DefaultSourceLoader;
import com.github.linyuzai.download.core.load.SourceLoader;
import com.github.linyuzai.download.core.log.ProgressCalculationLogger;
import com.github.linyuzai.download.core.log.StandardDownloadLogger;
import com.github.linyuzai.download.core.log.TimeSpentCalculationLogger;
import com.github.linyuzai.download.core.logger.DownloadLogger;
import com.github.linyuzai.download.core.logger.ProgressCalculationLogger;
import com.github.linyuzai.download.core.logger.StandardDownloadLogger;
import com.github.linyuzai.download.core.logger.TimeSpentCalculationLogger;
import com.github.linyuzai.download.core.source.DefaultSourceFactoryAdapter;
import com.github.linyuzai.download.core.source.SourceFactory;
import com.github.linyuzai.download.core.source.SourceFactoryAdapter;
Expand Down Expand Up @@ -59,6 +60,12 @@
@EnableConfigurationProperties(DownloadProperties.class)
public class DownloadConceptCoreAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public DownloadLogger downloadLogger() {
return new CommonsDownloadLogger();
}

@Bean
@ConditionalOnMissingBean
public StandardDownloadLogger standardDownloadLogger(DownloadProperties properties) {
Expand Down Expand Up @@ -97,11 +104,6 @@ public DownloadEventPublisher downloadEventPublisher(List<DownloadEventListener>
return new ApplicationDownloadEventPublisher(listeners);
}

@Bean
public DownloadEventPublisherInitializer downloadEventPublisherInitializer(DownloadEventPublisher publisher) {
return new DownloadEventPublisherInitializer(publisher);
}

@Bean
@ConditionalOnMissingBean
public BufferedDownloadWriter bufferedDownloadWriter() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.github.linyuzai.download.autoconfigure;

import com.github.linyuzai.download.autoconfigure.properties.DownloadProperties;
import com.github.linyuzai.download.autoconfigure.web.reactive.ReactiveDownloadAdvice;
import com.github.linyuzai.download.autoconfigure.web.reactive.ReactiveDownloadConcept;
import com.github.linyuzai.download.core.concept.DownloadConcept;
import com.github.linyuzai.download.core.context.DownloadContextFactory;
import com.github.linyuzai.download.core.event.DownloadEventPublisher;
import com.github.linyuzai.download.core.handler.DownloadHandler;
import com.github.linyuzai.download.core.logger.DownloadLogger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.util.List;

Expand All @@ -21,7 +26,15 @@ public class DownloadConceptReactiveAutoConfiguration {
@Bean
public DownloadConcept downloadConcept(DownloadContextFactory factory,
List<DownloadHandler> handlers,
DownloadEventPublisher eventPublisher) {
return new ReactiveDownloadConcept(factory, handlers, eventPublisher);
DownloadEventPublisher eventPublisher,
DownloadLogger logger) {
return new ReactiveDownloadConcept(factory, handlers, eventPublisher, logger);
}

@Bean
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public ReactiveDownloadAdvice reactiveDownloadAdvice(DownloadConcept concept,
DownloadProperties properties) {
return new ReactiveDownloadAdvice(concept, properties);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.github.linyuzai.download.autoconfigure;

import com.github.linyuzai.download.autoconfigure.properties.DownloadProperties;
import com.github.linyuzai.download.autoconfigure.web.servlet.ServletDownloadAdvice;
import com.github.linyuzai.download.autoconfigure.web.servlet.ServletDownloadConcept;
import com.github.linyuzai.download.core.concept.DownloadConcept;
import com.github.linyuzai.download.core.context.DownloadContextFactory;
import com.github.linyuzai.download.core.event.DownloadEventPublisher;
import com.github.linyuzai.download.core.handler.DownloadHandler;
import com.github.linyuzai.download.core.logger.DownloadLogger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.util.List;

Expand All @@ -21,7 +26,15 @@ public class DownloadConceptServletAutoConfiguration {
@Bean
public DownloadConcept downloadConcept(DownloadContextFactory factory,
List<DownloadHandler> handlers,
DownloadEventPublisher eventPublisher) {
return new ServletDownloadConcept(factory, handlers, eventPublisher);
DownloadEventPublisher eventPublisher,
DownloadLogger logger) {
return new ServletDownloadConcept(factory, handlers, eventPublisher, logger);
}

@Bean
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public ServletDownloadAdvice servletDownloadAdvice(DownloadConcept concept,
DownloadProperties properties) {
return new ServletDownloadAdvice(concept, properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.linyuzai.download.okhttp.OkHttpSource;
import com.github.linyuzai.download.okhttp.OkHttpSourceFactory;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -13,12 +14,11 @@
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(DownloadConceptCoreAutoConfiguration.class)
@ConditionalOnClass(OkHttpSourceFactory.class)
@ConditionalOnClass(OkHttpClient.class)
public class DownloadConceptSourceOkHttpAutoConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(OkHttpSource.class)
public OkHttpSourceFactory okHttpSourceFactory() {
return new OkHttpSourceFactory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.event.EventListener;

import java.util.Collection;
import java.util.List;

/**
* 基于 {@link ApplicationEventPublisher} 的 {@link DownloadEventPublisher},
Expand All @@ -19,7 +19,7 @@ public class ApplicationDownloadEventPublisher extends SimpleDownloadEventPublis

private ApplicationEventPublisher applicationEventPublisher;

public ApplicationDownloadEventPublisher(Collection<DownloadEventListener> listeners) {
public ApplicationDownloadEventPublisher(List<DownloadEventListener> listeners) {
super(listeners);
}

Expand Down
Loading

0 comments on commit c79421b

Please sign in to comment.