add file storage management service

This commit is contained in:
Fredrik Teschke
2015-01-23 17:52:21 +01:00
parent 3edbbb04bb
commit 944614c1ac
19 changed files with 283 additions and 90 deletions
@@ -8,7 +8,8 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: com.sap.sse.filestorage,
com.sap.sse.filestorage.impl,
com.sap.sse.filestorage.testsupport
Import-Package: org.osgi.framework;version="1.8.0"
Import-Package: com.sap.sse.osgi,
org.osgi.framework;version="1.8.0"
Bundle-Activator: com.sap.sse.filestorage.impl.Activator
Require-Bundle: org.apache.servicemix.bundles.aws-java-sdk;bundle-version="1.9.8",
com.fasterxml.jackson.core.jackson-databind,
@@ -0,0 +1,36 @@
package com.sap.sse.filestorage;
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
/**
* OSGi service for managing {@link FileStorageService FileStorageServices}.
*
* @author Fredrik Teschke
*
*/
public interface FileStorageManagementService {
FileStorageService[] getAvailableFileStorageServices();
FileStorageService getFileStorageService(String name);
/**
* Sets the property of the service, and also stores the property value so that it can be restored
* after a server restart.
* @throws NoCorrespondingServiceRegisteredException service may have disappeared from registry in the meantime
* @throws IllegalArgumentException if the property with name {@code propertyName} doesn't exist for the service
*/
void setFileStorageServiceProperty(String serviceName, String propertyName, String propertyValue)
throws NoCorrespondingServiceRegisteredException, IllegalArgumentException;
/**
* @return may be {@code null} if no service has been selected so far.
*/
FileStorageService getActiveFileStorageService();
/**
* @throws InvalidPropertiesException
* if the service properties are {@link FileStorageService#testProperties() invalid}. Then the old
* active service (if any) remains active.
*/
void setActiveFileStorageService(FileStorageService service) throws InvalidPropertiesException;
}
@@ -4,11 +4,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import com.sap.sse.common.TypeBasedServiceFinder;
/**
* Service for storing files. The interface is intentionally agnostic of the underlying implementation, which may be
* provided e.g. by using Amazon's S3 or simply saving the files to a statically reachable directory on one of our
* servers. Implementations of this service are announced via the OSGi service registry. They should provide their name
* as the OSGi property "name";
* as the OSGi property {@link TypeBasedServiceFinder#TYPE type};
*
* TODO The storage service to use should be configured via the AdminConsole. Upon auto-discovering the available
* services, the AdminConsole should allow to edit properties for each service (e.g. access credentials for AWS S3). We
@@ -34,6 +36,8 @@ public interface FileStorageService {
Property[] getProperties();
/**
* Should not be called directly, but through {@link FileStorageManagementService#setFileStorageServiceProperty}
* as this deals with storing the new values.
* @throws IllegalArgumentException
* if {@code name} is not a valid property name
*/
@@ -1,15 +1,30 @@
package com.sap.sse.filestorage.impl;
import java.util.Dictionary;
import java.util.Hashtable;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import com.sap.sse.common.TypeBasedServiceFinder;
import com.sap.sse.filestorage.FileStorageManagementService;
import com.sap.sse.filestorage.FileStorageService;
public class Activator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
context.registerService(FileStorageService.class, new AmazonS3FileStorageServiceImpl(), null);
Dictionary<String, String> dict = new Hashtable<>();
dict.put(TypeBasedServiceFinder.TYPE, AmazonS3FileStorageServiceImpl.NAME);
context.registerService(FileStorageService.class, new AmazonS3FileStorageServiceImpl(), dict);
TransientFileStorageManagementServiceImpl mgmtService = new TransientFileStorageManagementServiceImpl();
context.registerService(FileStorageManagementService.class, mgmtService,
null);
mgmtService.setContext(context);
//TODO load service properties from DB on startup
}
@Override
@@ -35,6 +35,7 @@ import com.sap.sse.filestorage.Property;
*
*/
public class AmazonS3FileStorageServiceImpl implements FileStorageService {
public static final String NAME = "Amazon S3";
private static final Logger logger = Logger.getLogger(AmazonS3FileStorageServiceImpl.class.getName());
private static final String baseUrl = "s3.amazonaws.com";
@@ -144,7 +145,7 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
@Override
public String getName() {
return "Amazon S3";
return NAME;
}
@Override
@@ -0,0 +1,48 @@
package com.sap.sse.filestorage.impl;
import org.osgi.framework.BundleContext;
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
import com.sap.sse.common.TypeBasedServiceFinder;
import com.sap.sse.filestorage.FileStorageManagementService;
import com.sap.sse.filestorage.FileStorageService;
import com.sap.sse.filestorage.InvalidPropertiesException;
import com.sap.sse.osgi.CachedOsgiTypeBasedServiceFinderFactory;
public class TransientFileStorageManagementServiceImpl implements FileStorageManagementService {
private FileStorageService active;
private TypeBasedServiceFinder<FileStorageService> serviceFinder;
public void setContext(BundleContext context) {
serviceFinder = new CachedOsgiTypeBasedServiceFinderFactory(context)
.createServiceFinder(FileStorageService.class);
}
@Override
public FileStorageService getActiveFileStorageService() {
return active;
}
@Override
public void setActiveFileStorageService(FileStorageService service) throws InvalidPropertiesException {
active = service;
}
@Override
public FileStorageService[] getAvailableFileStorageServices() {
return serviceFinder.findAllServices().toArray(new FileStorageService[0]);
}
@Override
public FileStorageService getFileStorageService(String name) {
return serviceFinder.findService(name);
}
@Override
public void setFileStorageServiceProperty(String serviceName, String propertyName, String propertyValue)
throws NoCorrespondingServiceRegisteredException, IllegalArgumentException {
//TODO mgmt service that persists properties on master
serviceFinder.findService(serviceName).setProperty(propertyName, propertyValue);
}
}