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

#734 -> update maven coordinates

This commit is contained in:
fangjian0423
2019-07-08 11:38:27 +08:00
parent 3998ea23f7
commit aa03ddbbe2
498 changed files with 1208 additions and 1203 deletions

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss;
import java.util.Map;
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}
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class OssApplicationListener implements ApplicationListener<ContextClosedEvent> {
private static final Logger log = LoggerFactory
.getLogger(OssApplicationListener.class);
@Override
public void onApplicationEvent(ContextClosedEvent event) {
Map<String, OSS> ossClientMap = event.getApplicationContext()
.getBeansOfType(OSS.class);
log.info("{} OSSClients will be shutdown soon", ossClientMap.size());
ossClientMap.keySet().forEach(beanName -> {
log.info("shutdown ossClient: {}", beanName);
ossClientMap.get(beanName).shutdown();
});
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss;
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.cloud.alicloud.oss.resource.OssStorageProtocolResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.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)
public class OssAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public OssStorageProtocolResolver ossStorageProtocolResolver() {
return new OssStorageProtocolResolver();
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss;
/**
* OSS constants
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public interface OssConstants {
String PREFIX = "spring.cloud.alibaba.oss";
String ENABLED = PREFIX + ".enabled";
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss.endpoint;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Actuator {@link Endpoint} to expose OSS Meta Data
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@Endpoint(id = "oss")
public class OssEndpoint {
@Autowired
private ApplicationContext applicationContext;
@ReadOperation
public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<>();
Map<String, OSSClient> ossClientMap = applicationContext
.getBeansOfType(OSSClient.class);
int size = ossClientMap.size();
List<Object> ossClientList = new ArrayList<>();
ossClientMap.keySet().forEach(beanName -> {
Map<String, Object> ossProperties = new HashMap<>();
OSSClient client = ossClientMap.get(beanName);
ossProperties.put("beanName", beanName);
ossProperties.put("endpoint", client.getEndpoint().toString());
ossProperties.put("clientConfiguration", client.getClientConfiguration());
ossProperties.put("credentials",
client.getCredentialsProvider().getCredentials());
ossProperties.put("bucketList", client.listBuckets().stream()
.map(bucket -> bucket.getName()).toArray());
ossClientList.add(ossProperties);
});
result.put("size", size);
result.put("info", ossClientList);
return result;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss.endpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* OSS {@link Endpoint} Auto-{@link Configuration}
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@ConditionalOnClass(Endpoint.class)
public class OssEndpointAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public OssEndpoint ossEndpoint() {
return new OssEndpoint();
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss.resource;
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;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.DefaultResourceLoader;
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.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class OssStorageProtocolResolver
implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware {
public static final String PROTOCOL = "oss://";
private static final Logger log = LoggerFactory
.getLogger(OssStorageProtocolResolver.class);
private ConfigurableListableBeanFactory beanFactory;
private OSS oss;
private OSS getOSS() {
if (this.oss == null) {
if (this.beanFactory.getBeansOfType(OSS.class).size() > 1) {
log.warn(
"There are multiple OSS instances, consider marking one of them as @Primary to resolve oss "
+ "protocol.");
}
this.oss = this.beanFactory.getBean(OSS.class);
}
return this.oss;
}
@Override
public Resource resolve(String location, ResourceLoader resourceLoader) {
if (!location.startsWith(PROTOCOL)) {
return null;
}
return new OssStorageResource(getOSS(), location);
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
if (DefaultResourceLoader.class.isAssignableFrom(resourceLoader.getClass())) {
((DefaultResourceLoader) resourceLoader).addProtocolResolver(this);
}
else {
log.warn("The provided delegate resource loader is not an implementation "
+ "of DefaultResourceLoader. Custom Protocol using oss:// prefix will not be enabled.");
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
this.beanFactory = beanFactory;
}
}

View File

@@ -0,0 +1,195 @@
/*
* Copyright (C) 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
*
* 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 org.springframework.cloud.alicloud.oss.resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
/**
* Implements {@link Resource} for reading and writing objects in Aliyun Object Storage
* Service (OSS). An instance of this class represents a handle to a bucket or an OSSObject.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @see OSS
* @see Bucket
* @see OSSObject
*/
public class OssStorageResource implements Resource {
private final OSS oss;
private final String bucketName;
private final String objectKey;
private final URI location;
public OssStorageResource(OSS oss, String location) {
Assert.notNull(oss, "Object Storage Service can not be null");
Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL),
"Location must start with " + OssStorageProtocolResolver.PROTOCOL);
this.oss = oss;
try {
URI locationUri = new URI(location);
this.bucketName = locationUri.getAuthority();
if (locationUri.getPath() != null && locationUri.getPath().length() > 1) {
this.objectKey = locationUri.getPath().substring(1);
}
else {
this.objectKey = null;
}
this.location = locationUri;
}
catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid location: " + location, e);
}
}
@Override
public boolean exists() {
try {
return isBucket() ? getBucket() != null : getOSSObject() != null;
}
catch (Exception e) {
return false;
}
}
/**
* Since the oss: protocol will normally not have a URL stream handler registered,
* this method will always throw a {@link java.net.MalformedURLException}.
* @return The URL for the OSS resource, if a URL stream handler is registered for the
* oss protocol.
*/
@Override
public URL getURL() throws IOException {
return this.location.toURL();
}
@Override
public URI getURI() throws IOException {
return this.location;
}
@Override
public File getFile() throws IOException {
throw new UnsupportedOperationException(
getDescription() + " cannot be resolved to absolute file path");
}
@Override
public long contentLength() throws IOException {
assertExisted();
if (isBucket()) {
throw new FileNotFoundException("OSSObject not existed.");
}
return getOSSObject().getObjectMetadata().getContentLength();
}
@Override
public long lastModified() throws IOException {
assertExisted();
if (isBucket()) {
throw new FileNotFoundException("OSSObject not existed.");
}
return getOSSObject().getObjectMetadata().getLastModified().getTime();
}
@Override
public Resource createRelative(String relativePath) throws IOException {
return new OssStorageResource(this.oss,
this.location.resolve(relativePath).toString());
}
@Override
public String getFilename() {
return isBucket() ? this.bucketName : this.objectKey;
}
@Override
public String getDescription() {
return this.location.toString();
}
@Override
public InputStream getInputStream() throws IOException {
assertExisted();
if (isBucket()) {
throw new IllegalStateException(
"Cannot open an input stream to a bucket: '" + this.location + "'");
}
else {
return getOSSObject().getObjectContent();
}
}
/**
* Returns the {@link Bucket} associated with the resource.
* @return the bucket if it exists, or null otherwise
*/
public Bucket getBucket() {
return this.oss.listBuckets().stream()
.filter(bucket -> bucket.getName().equals(this.bucketName)).findFirst()
.get();
}
/**
* Checks for the existence of the {@link Bucket} associated with the resource.
* @return true if the bucket exists
*/
public boolean bucketExists() {
return getBucket() != null;
}
/**
* Gets the underlying resource object in Aliyun Object Storage Service.
* @return The resource object, will be null if it does not exist in Aliyun Object
* Storage Service.
* @throws OSSException it is thrown upon error when accessing OSS
* @throws ClientException it is the one thrown by the client side when accessing OSS
*/
public OSSObject getOSSObject() {
return this.oss.getObject(this.bucketName, this.objectKey);
}
/**
* Check if this resource references a bucket and not a blob.
* @return if the resource is bucket
*/
public boolean isBucket() {
return this.objectKey == null;
}
private void assertExisted() throws FileNotFoundException {
if (!exists()) {
throw new FileNotFoundException("Bucket or OSSObject not existed.");
}
}
}

View File

@@ -0,0 +1,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alicloud.oss.OssAutoConfiguration,\
org.springframework.cloud.alicloud.oss.endpoint.OssEndpointAutoConfiguration
org.springframework.context.ApplicationListener=\
org.springframework.cloud.alicloud.oss.OssApplicationListener