1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

format code with maven plugins

This commit is contained in:
fangjian0423
2019-09-26 17:15:41 +08:00
parent ed6942d9df
commit 2435167e1e
529 changed files with 6304 additions and 5164 deletions

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -18,16 +18,16 @@ package com.alibaba.alicloud.oss;
import java.util.Map;
import com.aliyun.oss.OSS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import com.aliyun.oss.OSS;
/**
* Shutdown All OSS Clients when {@code ApplicationContext} gets closed
* {@link ApplicationListener}
* {@link ApplicationListener}.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@@ -46,4 +46,5 @@ public class OssApplicationListener implements ApplicationListener<ContextClosed
ossClientMap.get(beanName).shutdown();
});
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -16,29 +16,31 @@
package com.alibaba.alicloud.oss;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.alibaba.alicloud.oss.resource.OssStorageProtocolResolver;
import com.aliyun.oss.OSS;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.alicloud.oss.resource.OssStorageProtocolResolver;
import com.aliyun.oss.OSS;
import org.springframework.context.annotation.Lazy;
import java.util.concurrent.*;
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
/**
* OSS Auto {@link Configuration}
* OSS Auto {@link Configuration}.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@Configuration
@ConditionalOnClass(OSS.class)
@ConditionalOnProperty(name = OssConstants.ENABLED, havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(name = OssConstants.ENABLED, havingValue = "true",
matchIfMissing = true)
public class OssAutoConfiguration {
@Bean

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -17,15 +17,29 @@
package com.alibaba.alicloud.oss;
/**
* OSS constants
* OSS constants.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public interface OssConstants {
public final class OssConstants {
String PREFIX = "spring.cloud.alibaba.oss";
String ENABLED = PREFIX + ".enabled";
/**
* Prefix of OSSConfigurationProperties.
*/
public static final String PREFIX = "spring.cloud.alibaba.oss";
String OSS_TASK_EXECUTOR_BEAN_NAME = "ossTaskExecutor";
/**
* Enable OSS.
*/
public static final String ENABLED = PREFIX + ".enabled";
/**
* OSS ThreadPool bean name.
*/
public static final String OSS_TASK_EXECUTOR_BEAN_NAME = "ossTaskExecutor";
private OssConstants() {
throw new AssertionError("Must not instantiate constant utility class");
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -21,15 +21,15 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.aliyun.oss.OSSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.ApplicationContext;
import com.aliyun.oss.OSSClient;
/**
* Actuator {@link Endpoint} to expose OSS Meta Data
* Actuator {@link Endpoint} to expose OSS Meta Data.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -24,7 +24,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* OSS {@link Endpoint} Auto-{@link Configuration}
* OSS {@link Endpoint} Auto-{@link Configuration}.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -16,8 +16,10 @@
package com.alibaba.alicloud.oss.resource;
import com.aliyun.oss.OSS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -27,8 +29,6 @@ import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import com.aliyun.oss.OSS;
/**
* A {@link ProtocolResolver} implementation for the {@code oss://} protocol.
*
@@ -37,6 +37,9 @@ import com.aliyun.oss.OSS;
public class OssStorageProtocolResolver
implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware {
/**
* protocol of oss resource.
*/
public static final String PROTOCOL = "oss://";
private static final Logger log = LoggerFactory
@@ -82,4 +85,5 @@ public class OssStorageProtocolResolver
throws BeansException {
this.beanFactory = beanFactory;
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -16,6 +16,18 @@
package com.alibaba.alicloud.oss.resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
@@ -23,17 +35,12 @@ import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;
import org.springframework.util.Assert;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
/**
@@ -48,25 +55,32 @@ import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
*/
public class OssStorageResource implements WritableResource {
private static final Logger logger = LoggerFactory.getLogger(OssStorageResource.class);
private static final Logger logger = LoggerFactory
.getLogger(OssStorageResource.class);
private static final String MESSAGE_KEY_NOT_EXIST = "The specified key does not exist.";
private final OSS oss;
private final String bucketName;
private final String objectKey;
private final URI location;
private final boolean autoCreateFiles;
private final ExecutorService ossTaskExecutor;
private final ConfigurableListableBeanFactory beanFactory;
public OssStorageResource(OSS oss, String location, ConfigurableListableBeanFactory beanFactory) {
this(oss, location, beanFactory,false);
}
public OssStorageResource(OSS oss, String location,
ConfigurableListableBeanFactory beanFactory) {
this(oss, location, beanFactory, false);
}
public OssStorageResource(OSS oss, String location,ConfigurableListableBeanFactory beanFactory, boolean autoCreateFiles) {
public OssStorageResource(OSS oss, String location,
ConfigurableListableBeanFactory beanFactory, boolean autoCreateFiles) {
Assert.notNull(oss, "Object Storage Service can not be null");
Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL),
"Location must start with " + OssStorageProtocolResolver.PROTOCOL);
@@ -89,7 +103,8 @@ public class OssStorageResource implements WritableResource {
throw new IllegalArgumentException("Invalid location: " + location, e);
}
this.ossTaskExecutor = this.beanFactory.getBean(OSS_TASK_EXECUTOR_BEAN_NAME, ExecutorService.class);
this.ossTaskExecutor = this.beanFactory.getBean(OSS_TASK_EXECUTOR_BEAN_NAME,
ExecutorService.class);
}
public boolean isAutoCreateFiles() {
@@ -219,7 +234,7 @@ public class OssStorageResource implements WritableResource {
/**
* create a bucket.
* @return
* @return OSS Bucket
*/
public Bucket createBucket() {
return this.oss.createBucket(this.bucketName);
@@ -231,10 +246,10 @@ public class OssStorageResource implements WritableResource {
}
/**
* acquire an OutputStream for write.
* Note: please close the stream after writing is done
* @return
* @throws IOException
* acquire an OutputStream for write. Note: please close the stream after writing is
* done
* @return OutputStream of OSS resource
* @throws IOException throw by oss operation
*/
@Override
public OutputStream getOutputStream() throws IOException {

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -16,17 +16,22 @@
package com.alibaba.alicloud.oss.resource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.util.StreamUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.util.StreamUtils;
/**
* @author lich
@@ -95,4 +100,5 @@ public class DummyOssClient {
public List<Bucket> bucketList() {
return new ArrayList<>(bucketSet.values());
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* 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
* https://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,
@@ -16,16 +16,26 @@
package com.alibaba.alicloud.oss.resource;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.aliyun.oss.OSS;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.test.context.SpringBootTest;
@@ -37,15 +47,8 @@ import org.springframework.core.io.WritableResource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StreamUtils;
import java.io.*;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
@@ -82,45 +85,46 @@ public class OssStorageResourceTest {
@Test
public void testResourceType() {
assertEquals(OssStorageResource.class, remoteResource.getClass());
assertThat(remoteResource.getClass()).isEqualTo(OssStorageResource.class);
OssStorageResource ossStorageResource = (OssStorageResource) remoteResource;
assertEquals("myfilekey", ossStorageResource.getFilename());
assertFalse(ossStorageResource.isBucket());
assertThat(ossStorageResource.getFilename()).isEqualTo("myfilekey");
assertThat(ossStorageResource.isBucket()).isEqualTo(false);
}
@Test
public void testValidObject() throws Exception {
assertTrue(remoteResource.exists());
assertThat(remoteResource.exists()).isEqualTo(true);
OssStorageResource ossStorageResource = (OssStorageResource) remoteResource;
assertTrue(ossStorageResource.bucketExists());
assertEquals(4096L, remoteResource.contentLength());
assertEquals("oss://aliyun-test-bucket/myfilekey",
remoteResource.getURI().toString());
assertEquals("myfilekey", remoteResource.getFilename());
assertThat(ossStorageResource.bucketExists()).isEqualTo(true);
assertThat(remoteResource.contentLength()).isEqualTo(4096L);
assertThat(remoteResource.getURI().toString())
.isEqualTo("oss://aliyun-test-bucket/myfilekey");
assertThat(remoteResource.getFilename()).isEqualTo("myfilekey");
}
@Test
public void testBucketResource() throws Exception {
assertTrue(bucketResource.exists());
assertTrue(((OssStorageResource) this.bucketResource).isBucket());
assertTrue(((OssStorageResource) this.bucketResource).bucketExists());
assertEquals("oss://aliyun-test-bucket/", bucketResource.getURI().toString());
assertEquals("aliyun-test-bucket", this.bucketResource.getFilename());
assertThat(bucketResource.exists()).isEqualTo(true);
assertThat(((OssStorageResource) this.bucketResource).isBucket()).isEqualTo(true);
assertThat(((OssStorageResource) this.bucketResource).bucketExists())
.isEqualTo(true);
assertThat(bucketResource.getURI().toString())
.isEqualTo("oss://aliyun-test-bucket/");
assertThat(this.bucketResource.getFilename()).isEqualTo("aliyun-test-bucket");
}
@Test
public void testBucketNotEndingInSlash() {
assertTrue(
assertThat(
new OssStorageResource(this.oss, "oss://aliyun-test-bucket", beanFactory)
.isBucket());
.isBucket()).isEqualTo(true);
}
@Test
public void testSpecifyPathCorrect() {
OssStorageResource ossStorageResource = new OssStorageResource(this.oss,
"oss://aliyun-test-bucket/myfilekey", beanFactory, false);
assertTrue(ossStorageResource.exists());
assertThat(ossStorageResource.exists()).isEqualTo(true);
}
@Test
@@ -128,9 +132,10 @@ public class OssStorageResourceTest {
OssStorageResource ossStorageResource = new OssStorageResource(this.oss,
"oss://aliyun-test-bucket", beanFactory, false);
assertTrue(ossStorageResource.isBucket());
assertEquals("aliyun-test-bucket", ossStorageResource.getBucket().getName());
assertTrue(ossStorageResource.exists());
assertThat(ossStorageResource.isBucket()).isEqualTo(true);
assertThat(ossStorageResource.getBucket().getName())
.isEqualTo("aliyun-test-bucket");
assertThat(ossStorageResource.exists()).isEqualTo(true);
}
@Test
@@ -173,25 +178,27 @@ public class OssStorageResourceTest {
@Test
public void testBucketResourceStatuses() {
assertFalse(this.bucketResource.isOpen());
assertFalse(((WritableResource) this.bucketResource).isWritable());
assertTrue(this.bucketResource.exists());
assertThat(this.bucketResource.isOpen()).isEqualTo(false);
assertThat(((WritableResource) this.bucketResource).isWritable())
.isEqualTo(false);
assertThat(this.bucketResource.exists()).isEqualTo(true);
}
@Test
public void testWritable() throws Exception {
assertTrue(this.remoteResource instanceof WritableResource);
assertThat(this.remoteResource instanceof WritableResource).isEqualTo(true);
WritableResource writableResource = (WritableResource) this.remoteResource;
assertTrue(writableResource.isWritable());
assertThat(writableResource.isWritable()).isEqualTo(true);
writableResource.getOutputStream();
}
@Test
public void testWritableOutputStream() throws Exception {
String location = "oss://aliyun-test-bucket/test";
OssStorageResource resource = new OssStorageResource(this.oss, location, beanFactory,true);
OssStorageResource resource = new OssStorageResource(this.oss, location,
beanFactory, true);
OutputStream os = resource.getOutputStream();
assertNotNull(os);
assertThat(os).isNotNull();
byte[] randomBytes = generateRandomBytes(1203);
String expectedString = new String(randomBytes);
@@ -204,18 +211,18 @@ public class OssStorageResourceTest {
byte[] result = StreamUtils.copyToByteArray(in);
String actualString = new String(result);
assertEquals(expectedString, actualString);
assertThat(actualString).isEqualTo(expectedString);
}
@Test
public void testCreateBucket() {
String location = "oss://my-new-test-bucket/";
OssStorageResource resource = new OssStorageResource(this.oss, location, beanFactory, true);
OssStorageResource resource = new OssStorageResource(this.oss, location,
beanFactory, true);
resource.createBucket();
assertTrue(resource.bucketExists());
assertThat(resource.bucketExists()).isEqualTo(true);
}
/**
@@ -228,8 +235,8 @@ public class OssStorageResourceTest {
@Bean(name = OSS_TASK_EXECUTOR_BEAN_NAME)
@ConditionalOnMissingBean
public ExecutorService ossTaskExecutor() {
return new ThreadPoolExecutor(8, 128,
60, TimeUnit.SECONDS, new SynchronousQueue<>());
return new ThreadPoolExecutor(8, 128, 60, TimeUnit.SECONDS,
new SynchronousQueue<>());
}
@Bean