mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-16 07:10:48 +00:00
add replication to file storage service
This commit is contained in:
1 parent
180c1dce36
commit
1a0b2ffa25
22 files changed
+419
-128
No files matched your search
+4
-4
@@ -11,16 +11,16 @@ import com.sap.sailing.gwt.ui.shared.FileStorageServicePropertyErrors;
|
||||
import com.sap.sailing.gwt.ui.shared.FileStorageServiceDTO;
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.InvalidPropertiesException;
|
||||
import com.sap.sse.filestorage.Property;
|
||||
import com.sap.sse.filestorage.FileStorageServiceProperty;
|
||||
|
||||
public class FileStorageServiceDTOUtils {
|
||||
public static FileStorageServicePropertyDTO convert(Property p) {
|
||||
public static FileStorageServicePropertyDTO convert(FileStorageServiceProperty p) {
|
||||
return new FileStorageServicePropertyDTO(p.isRequired(), p.getName(), p.getValue(), p.getDescription());
|
||||
}
|
||||
|
||||
public static FileStorageServicePropertyErrors convert(InvalidPropertiesException e) {
|
||||
Map<FileStorageServicePropertyDTO, String> msgs = new HashMap<>();
|
||||
for (Entry<Property, String> entry : e.getPerPropertyMessage().entrySet()) {
|
||||
for (Entry<FileStorageServiceProperty, String> entry : e.getPerPropertyMessage().entrySet()) {
|
||||
msgs.put(convert(entry.getKey()), entry.getValue());
|
||||
}
|
||||
return new FileStorageServicePropertyErrors(e.getMessage(), msgs);
|
||||
@@ -28,7 +28,7 @@ public class FileStorageServiceDTOUtils {
|
||||
|
||||
public static FileStorageServiceDTO convert(FileStorageService s) {
|
||||
List<FileStorageServicePropertyDTO> pDtos = new ArrayList<>();
|
||||
for (Property p : s.getProperties()) {
|
||||
for (FileStorageServiceProperty p : s.getProperties()) {
|
||||
pDtos.add(convert(p));
|
||||
}
|
||||
return new FileStorageServiceDTO(s.getName(), s.getDescription(), pDtos.toArray(new FileStorageServicePropertyDTO[0]));
|
||||
|
||||
+1
-1
@@ -5446,7 +5446,7 @@ public class SailingServiceImpl extends ProxiedRemoteServiceServlet implements S
|
||||
for (Entry<String, String> p : properties.entrySet()) {
|
||||
try {
|
||||
getService().getFileStorageManagementService()
|
||||
.setFileStorageServiceProperty(serviceName, p.getKey(), p.getValue());
|
||||
.setFileStorageServiceProperty(getFileStorageService(serviceName), p.getKey(), p.getValue());
|
||||
} catch (NoCorrespondingServiceRegisteredException | IllegalArgumentException e) {
|
||||
//ignore, doing refresh afterwards anyways
|
||||
}
|
||||
|
||||
@@ -18,5 +18,8 @@ Require-Bundle: org.apache.servicemix.bundles.aws-java-sdk;bundle-version="1.9.8
|
||||
com.fasterxml.jackson.core.jackson-annotations,
|
||||
org.apache.commons.logging;bundle-version="1.1.3",
|
||||
com.sap.sse.common,
|
||||
com.sap.sse.mongodb;bundle-version="1.0.0",
|
||||
com.mongodb.driver;bundle-version="2.6.2"
|
||||
com.sap.sse.mongodb,
|
||||
com.mongodb.driver;bundle-version="2.6.2",
|
||||
com.sap.sse.replication,
|
||||
com.sap.sse.operationaltransformation,
|
||||
com.sap.sse
|
||||
+9
-5
@@ -1,6 +1,9 @@
|
||||
package com.sap.sse.filestorage;
|
||||
|
||||
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
|
||||
import com.sap.sse.filestorage.impl.ReplicableFileStorageManagementService;
|
||||
import com.sap.sse.filestorage.operations.FileStorageServiceOperation;
|
||||
import com.sap.sse.replication.impl.ReplicableWithObjectInputStream;
|
||||
|
||||
/**
|
||||
* OSGi service for managing {@link FileStorageService FileStorageServices}.
|
||||
@@ -8,21 +11,22 @@ import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public interface FileStorageManagementService {
|
||||
public interface FileStorageManagementService extends
|
||||
ReplicableWithObjectInputStream<ReplicableFileStorageManagementService, FileStorageServiceOperation<?>> {
|
||||
FileStorageService[] getAvailableFileStorageServices();
|
||||
|
||||
/**
|
||||
* @return {@code null} if no service for that name is found
|
||||
*/
|
||||
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)
|
||||
void setFileStorageServiceProperty(FileStorageService service, String propertyName, String propertyValue)
|
||||
throws NoCorrespondingServiceRegisteredException, IllegalArgumentException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
|
||||
import com.sap.sse.common.IsManagedByCache;
|
||||
import com.sap.sse.common.TypeBasedServiceFinder;
|
||||
|
||||
/**
|
||||
@@ -20,7 +21,7 @@ import com.sap.sse.common.TypeBasedServiceFinder;
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public interface FileStorageService {
|
||||
public interface FileStorageService extends IsManagedByCache<FileStorageServiceResolver> {
|
||||
/**
|
||||
* @param originalFileExtension
|
||||
* may be {@code null}
|
||||
@@ -33,7 +34,7 @@ public interface FileStorageService {
|
||||
*/
|
||||
void removeFile(URI uri) throws OperationFailedException, InvalidPropertiesException;
|
||||
|
||||
Property[] getProperties();
|
||||
FileStorageServiceProperty[] getProperties();
|
||||
|
||||
/**
|
||||
* Should not be called directly, but through {@link FileStorageManagementService#setFileStorageServiceProperty}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ package com.sap.sse.filestorage;
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public interface Property {
|
||||
public interface FileStorageServiceProperty {
|
||||
boolean isRequired();
|
||||
String getName();
|
||||
|
||||
+4
@@ -12,4 +12,8 @@ public interface FileStorageServicePropertyStore {
|
||||
Map<String, String> readAllProperties(String serviceName);
|
||||
|
||||
void writeProperty(String serviceName, String propertyName, String propertyValue);
|
||||
|
||||
String readActiveServiceName();
|
||||
|
||||
void writeActiveService(String serviceName);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.sap.sse.filestorage;
|
||||
|
||||
|
||||
public interface FileStorageServiceResolver {
|
||||
/**
|
||||
* @return {@code null} if this service is not known
|
||||
*/
|
||||
FileStorageService getFileStorageService(String serviceName);
|
||||
}
|
||||
+5
-5
@@ -7,7 +7,7 @@ import com.sap.sse.common.Util.Pair;
|
||||
|
||||
public class InvalidPropertiesException extends Exception {
|
||||
private static final long serialVersionUID = -7328897153875728802L;
|
||||
private final Map<Property, String> perPropertyMessages = new HashMap<>();
|
||||
private final Map<FileStorageServiceProperty, String> perPropertyMessages = new HashMap<>();
|
||||
|
||||
public InvalidPropertiesException(String message) {
|
||||
super(message);
|
||||
@@ -19,15 +19,15 @@ public class InvalidPropertiesException extends Exception {
|
||||
|
||||
@SafeVarargs
|
||||
public InvalidPropertiesException(String message,
|
||||
Pair<Property, String>... perPropertyMessages) {
|
||||
Pair<FileStorageServiceProperty, String>... perPropertyMessages) {
|
||||
this(message, null, perPropertyMessages);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public InvalidPropertiesException(String message, Throwable cause,
|
||||
Pair<Property, String>... perPropertyMessages) {
|
||||
Pair<FileStorageServiceProperty, String>... perPropertyMessages) {
|
||||
super(message, cause);
|
||||
for (Pair<Property, String> pair : perPropertyMessages) {
|
||||
for (Pair<FileStorageServiceProperty, String> pair : perPropertyMessages) {
|
||||
this.perPropertyMessages.put(pair.getA(), pair.getB());
|
||||
}
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class InvalidPropertiesException extends Exception {
|
||||
return super.getMessage();
|
||||
}
|
||||
|
||||
public Map<Property, String> getPerPropertyMessage() {
|
||||
public Map<FileStorageServiceProperty, String> getPerPropertyMessage() {
|
||||
return perPropertyMessages;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import com.sap.sse.filestorage.FileStorageManagementService;
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.FileStorageServicePropertyStore;
|
||||
import com.sap.sse.mongodb.MongoDBConfiguration;
|
||||
import com.sap.sse.replication.Replicable;
|
||||
|
||||
public class Activator implements BundleActivator {
|
||||
private ServiceTracker<FileStorageService, FileStorageService> tracker;
|
||||
@@ -20,7 +21,9 @@ public class Activator implements BundleActivator {
|
||||
public void start(BundleContext context) throws Exception {
|
||||
MongoDBConfiguration dbConfig = MongoDBConfiguration.getDefaultConfiguration();
|
||||
dbConfig.getService().registerExclusively(MongoFileStorageServicePropertyStoreImpl.class,
|
||||
MongoFileStorageServicePropertyStoreImpl.COLLECTION_NAME);
|
||||
MongoFileStorageServicePropertyStoreImpl.PROPERTIES_COLLECTION_NAME);
|
||||
dbConfig.getService().registerExclusively(MongoFileStorageServicePropertyStoreImpl.class,
|
||||
MongoFileStorageServicePropertyStoreImpl.ACTIVE_SERVICE_COLLECTION_NAME);
|
||||
|
||||
FileStorageServicePropertyStore propertyStore = new MongoFileStorageServicePropertyStoreImpl(
|
||||
dbConfig.getService());
|
||||
@@ -29,8 +32,14 @@ public class Activator implements BundleActivator {
|
||||
dict.put(TypeBasedServiceFinder.TYPE, AmazonS3FileStorageServiceImpl.NAME);
|
||||
context.registerService(FileStorageService.class, new AmazonS3FileStorageServiceImpl(), dict);
|
||||
|
||||
//register mgmt service
|
||||
FileStorageManagementServiceImpl mgmtService = new FileStorageManagementServiceImpl(context, propertyStore);
|
||||
context.registerService(FileStorageManagementService.class, mgmtService, null);
|
||||
|
||||
//register mgmt service as replicable
|
||||
Dictionary<String, String> replicableServiceProperties = new Hashtable<>();
|
||||
replicableServiceProperties.put(Replicable.OSGi_Service_Registry_ID_Property_Name, mgmtService.getId().toString());
|
||||
context.registerService(Replicable.class.getName(), mgmtService, replicableServiceProperties);
|
||||
|
||||
// track all FileStorageServices, so that their properties can be set from the database when added to the OSGi
|
||||
// registry
|
||||
|
||||
+10
-39
@@ -3,8 +3,6 @@ package com.sap.sse.filestorage.impl;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -20,9 +18,9 @@ import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import com.sap.sse.common.Util.Pair;
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.FileStorageServiceProperty;
|
||||
import com.sap.sse.filestorage.InvalidPropertiesException;
|
||||
import com.sap.sse.filestorage.OperationFailedException;
|
||||
import com.sap.sse.filestorage.Property;
|
||||
|
||||
/**
|
||||
* For testing purposes configure the access credentials as follows: To link this service to an AWS account, create the
|
||||
@@ -34,32 +32,28 @@ import com.sap.sse.filestorage.Property;
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public class AmazonS3FileStorageServiceImpl implements FileStorageService {
|
||||
public class AmazonS3FileStorageServiceImpl extends BaseFileStorageServiceImpl implements FileStorageService {
|
||||
public static final String NAME = "Amazon S3";
|
||||
public static final String DESCRIPTION = "Store files on Amazon S3. The resulting file name is a random UUID plus the original file ending.";
|
||||
|
||||
private static final Logger logger = Logger.getLogger(AmazonS3FileStorageServiceImpl.class.getName());
|
||||
|
||||
private static final String baseUrl = "s3.amazonaws.com";
|
||||
private static final String retrievalProtocol = "http";
|
||||
// private static final String bucketName = "ftes-sap-sailing";
|
||||
|
||||
private final PropertyImpl accessId = new PropertyImpl("accessId", false,
|
||||
private final FileStorageServicePropertyImpl accessId = new FileStorageServicePropertyImpl("accessId", false,
|
||||
"Access ID (leave blank to use ~/.aws/credentials instead)");
|
||||
private final PropertyImpl accessKey = new PropertyImpl("accessKey", false,
|
||||
private final FileStorageServicePropertyImpl accessKey = new FileStorageServicePropertyImpl("accessKey", false,
|
||||
"Secret Access Key (leave blank to use ~/.aws/credentials instead)");
|
||||
private final PropertyImpl bucketName = new PropertyImpl("bucketName", true,
|
||||
private final FileStorageServicePropertyImpl bucketName = new FileStorageServicePropertyImpl("bucketName", true,
|
||||
"Name of Bucket to use (has to already exist, and user needs sufficient permissions)");
|
||||
private final Map<String, PropertyImpl> properties = new HashMap<>();
|
||||
|
||||
public AmazonS3FileStorageServiceImpl() {
|
||||
super(NAME, DESCRIPTION);
|
||||
addProperties(accessId, accessKey, bucketName);
|
||||
}
|
||||
|
||||
private void addProperties(PropertyImpl... properties) {
|
||||
for (PropertyImpl p : properties) {
|
||||
this.properties.put(p.getName(), p);
|
||||
}
|
||||
}
|
||||
|
||||
private AmazonS3Client createS3Client() throws InvalidPropertiesException {
|
||||
AWSCredentials creds;
|
||||
|
||||
@@ -133,29 +127,6 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
|
||||
logger.info("Removed file " + uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Property[] getProperties() {
|
||||
return new Property[] { accessId, accessKey, bucketName };
|
||||
}
|
||||
|
||||
@Override
|
||||
public void internalSetProperty(String name, String value) {
|
||||
if (!properties.containsKey(name)) {
|
||||
throw new IllegalArgumentException("Property " + name + " does not exist");
|
||||
}
|
||||
properties.get(name).setValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Store files on Amazon S3. The resulting file name is a random UUID plus the original file ending.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testProperties() throws InvalidPropertiesException {
|
||||
AmazonS3Client s3 = createS3Client();
|
||||
@@ -166,13 +137,13 @@ public class AmazonS3FileStorageServiceImpl implements FileStorageService {
|
||||
s3.doesBucketExist(bucketName.getValue());
|
||||
} catch (Exception e) {
|
||||
throw new InvalidPropertiesException("invalid credentials or not enough access rights for the bucket", e,
|
||||
new Pair<Property, String>(accessId, "seems to be invalid"), new Pair<Property, String>(accessKey,
|
||||
new Pair<FileStorageServiceProperty, String>(accessId, "seems to be invalid"), new Pair<FileStorageServiceProperty, String>(accessKey,
|
||||
"seems to be invalid"));
|
||||
}
|
||||
|
||||
// test if bucket exists
|
||||
if (!s3.doesBucketExist(bucketName.getValue())) {
|
||||
throw new InvalidPropertiesException("invalid bucket", new Pair<Property, String>(bucketName,
|
||||
throw new InvalidPropertiesException("invalid bucket", new Pair<FileStorageServiceProperty, String>(bucketName,
|
||||
"bucket does not exist"));
|
||||
}
|
||||
}
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.sap.sse.filestorage.impl;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sap.sse.common.IsManagedByCache;
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.FileStorageServiceProperty;
|
||||
import com.sap.sse.filestorage.FileStorageServiceResolver;
|
||||
|
||||
public abstract class BaseFileStorageServiceImpl implements FileStorageService {
|
||||
private final String name;
|
||||
private final String description;
|
||||
protected final Map<String, FileStorageServicePropertyImpl> propertiesByNameInInsertionOrder = new LinkedHashMap<>();
|
||||
|
||||
protected BaseFileStorageServiceImpl(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
protected void addProperties(FileStorageServicePropertyImpl... properties) {
|
||||
for (FileStorageServicePropertyImpl p : properties) {
|
||||
this.propertiesByNameInInsertionOrder.put(p.getName(), p);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IsManagedByCache<FileStorageServiceResolver> resolve(FileStorageServiceResolver domainFactory) {
|
||||
return domainFactory.getFileStorageService(getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileStorageServiceProperty[] getProperties() {
|
||||
return propertiesByNameInInsertionOrder.values().toArray(new FileStorageServicePropertyImpl[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void internalSetProperty(String name, String value) {
|
||||
if (!propertiesByNameInInsertionOrder.containsKey(name)) {
|
||||
throw new IllegalArgumentException("Property " + name + " does not exist");
|
||||
}
|
||||
propertiesByNameInInsertionOrder.get(name).setValue(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName();
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.sap.sse.filestorage.impl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sap.sse.filestorage.FileStorageServicePropertyStore;
|
||||
|
||||
public enum EmptyFileStorageServicePropertyStoreImpl implements FileStorageServicePropertyStore {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Map<String, String> readAllProperties(String serviceName) {
|
||||
return new HashMap<String, String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeProperty(String serviceName, String propertyName, String propertyValue) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readActiveServiceName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeActiveService(String serviceName) {
|
||||
}
|
||||
|
||||
}
|
||||
+164
-33
@@ -1,57 +1,68 @@
|
||||
package com.sap.sse.filestorage.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceEvent;
|
||||
import org.osgi.framework.ServiceListener;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
import org.osgi.util.tracker.ServiceTrackerCustomizer;
|
||||
|
||||
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.FileStorageServiceProperty;
|
||||
import com.sap.sse.filestorage.FileStorageServicePropertyStore;
|
||||
import com.sap.sse.filestorage.FileStorageServiceResolver;
|
||||
import com.sap.sse.osgi.CachedOsgiTypeBasedServiceFinderFactory;
|
||||
import com.sap.sse.replication.OperationExecutionListener;
|
||||
import com.sap.sse.replication.OperationWithResult;
|
||||
import com.sap.sse.replication.ReplicationMasterDescriptor;
|
||||
import com.sap.sse.replication.impl.OperationWithResultWithIdWrapper;
|
||||
|
||||
/**
|
||||
* Implements {@link ServiceTrackerCustomizer} so that all {@link FileStorageServices} announced in the
|
||||
* registry can receive their stored properties.
|
||||
*
|
||||
* TODO implements Replicable, build Operations
|
||||
* add fully qualified classname of Replicable to java/target/env.sh
|
||||
* register as Replicable OSGi service (com.sap.sse.security.impl.Activator#83)
|
||||
* TODO store active selection
|
||||
* Implements {@link ServiceTrackerCustomizer} so that all {@link FileStorageServices} announced in the registry can
|
||||
* receive their stored properties.
|
||||
*
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public class FileStorageManagementServiceImpl implements FileStorageManagementService,
|
||||
ServiceTrackerCustomizer<FileStorageService, FileStorageService> {
|
||||
public class FileStorageManagementServiceImpl implements ReplicableFileStorageManagementService,
|
||||
ServiceTrackerCustomizer<FileStorageService, FileStorageService> {
|
||||
private final Logger logger = Logger.getLogger(FileStorageManagementServiceImpl.class.getName());
|
||||
|
||||
private FileStorageService active;
|
||||
|
||||
private FileStorageService active;
|
||||
private final TypeBasedServiceFinder<FileStorageService> serviceFinder;
|
||||
private final FileStorageServicePropertyStore propertyStore;
|
||||
private final BundleContext context;
|
||||
|
||||
private final Map<OperationExecutionListener<ReplicableFileStorageManagementService>, OperationExecutionListener<ReplicableFileStorageManagementService>> operationExecutionListeners = new ConcurrentHashMap<>();
|
||||
private final Set<OperationWithResultWithIdWrapper<?, ?>> operationsSentToMasterForReplication = new HashSet<>();
|
||||
private ReplicationMasterDescriptor replicationMasterDescriptor;
|
||||
private ThreadLocal<Boolean> currentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster = ThreadLocal.withInitial(() -> false);
|
||||
|
||||
/**
|
||||
* Is set to an {@link EmptyFileStorageServicePropertyStoreImpl} on replicas.
|
||||
*/
|
||||
private FileStorageServicePropertyStore propertyStore;
|
||||
private final BundleContext context;
|
||||
private final FileStorageServiceResolver serviceResolver;
|
||||
|
||||
public FileStorageManagementServiceImpl(BundleContext context, FileStorageServicePropertyStore propertyStore) {
|
||||
this.context = context;
|
||||
serviceFinder = new CachedOsgiTypeBasedServiceFinderFactory(context)
|
||||
.createServiceFinder(FileStorageService.class);
|
||||
this.propertyStore = propertyStore;
|
||||
|
||||
context.addServiceListener(new ServiceListener() {
|
||||
|
||||
@Override
|
||||
public void serviceChanged(ServiceEvent event) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
});
|
||||
serviceResolver = new FileStorageServiceResolverAgainstOsgiRegistryImpl(serviceFinder);
|
||||
active = getFileStorageService(propertyStore.readActiveServiceName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,7 +75,7 @@ public class FileStorageManagementServiceImpl implements FileStorageManagementSe
|
||||
|
||||
@Override
|
||||
public void setActiveFileStorageService(FileStorageService service) {
|
||||
active = service;
|
||||
apply(s -> s.internalSetActiveFileStorageService(service));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,15 +85,13 @@ public class FileStorageManagementServiceImpl implements FileStorageManagementSe
|
||||
|
||||
@Override
|
||||
public FileStorageService getFileStorageService(String name) {
|
||||
return serviceFinder.findService(name);
|
||||
return serviceResolver.getFileStorageService(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFileStorageServiceProperty(String serviceName, String propertyName, String propertyValue)
|
||||
public void setFileStorageServiceProperty(FileStorageService service, String propertyName, String propertyValue)
|
||||
throws NoCorrespondingServiceRegisteredException, IllegalArgumentException {
|
||||
//TODO replicate
|
||||
propertyStore.writeProperty(serviceName, propertyName, propertyValue);
|
||||
serviceFinder.findService(serviceName).internalSetProperty(propertyName, propertyValue);
|
||||
apply(s -> s.internalSetFileStorageServiceProperty(service, propertyName, propertyValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,11 +106,133 @@ public class FileStorageManagementServiceImpl implements FileStorageManagementSe
|
||||
|
||||
@Override
|
||||
public void modifiedService(ServiceReference<FileStorageService> reference, FileStorageService service) {
|
||||
//ignore
|
||||
// file storage service has been modified: ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removedService(ServiceReference<FileStorageService> reference, FileStorageService service) {
|
||||
//ignore
|
||||
// file storage service has been removed: ignore
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void internalSetFileStorageServiceProperty(FileStorageService service, String propertyName,
|
||||
String propertyValue) throws NoCorrespondingServiceRegisteredException, IllegalArgumentException {
|
||||
propertyStore.writeProperty(service.getName(), propertyName, propertyValue);
|
||||
service.internalSetProperty(propertyName, propertyValue);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void internalSetActiveFileStorageService(FileStorageService service) {
|
||||
propertyStore.writeActiveService(service == null ? null : service.getName());
|
||||
active = service;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ObjectInputStream createObjectInputStreamResolvingAgainstCache(InputStream is) throws IOException {
|
||||
return new ObjectInputStreamResolvingAgainstFileStorageServiceResolver(is, serviceResolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initiallyFillFromInternal(ObjectInputStream is) throws IOException, ClassNotFoundException,
|
||||
InterruptedException {
|
||||
logger.info("Initializing file storage mgmt service state from initial load");
|
||||
|
||||
// use empty property store (does not connect to MongoDB) on replicas
|
||||
propertyStore = EmptyFileStorageServicePropertyStoreImpl.INSTANCE;
|
||||
|
||||
// FileStorageServices are resolved against the OSGi registry by their name so that we
|
||||
// get the correct instances from the object stream
|
||||
FileStorageService activeService = (FileStorageService) is.readObject();
|
||||
logger.info("Setting active file storage service: " + activeService);
|
||||
internalSetActiveFileStorageService(activeService);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<FileStorageService, FileStorageServiceProperty[]> properties = (Map<FileStorageService, FileStorageServiceProperty[]>) is
|
||||
.readObject();
|
||||
for (FileStorageService service : properties.keySet()) {
|
||||
for (FileStorageServiceProperty property : properties.get(service)) {
|
||||
logger.info("Setting file storage service property for " + service + ": " + property);
|
||||
internalSetFileStorageServiceProperty(service, property.getName(), property.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serializeForInitialReplicationInternal(ObjectOutputStream os) throws IOException {
|
||||
os.writeObject(active);
|
||||
|
||||
// The FileStorageService keys of the map are resolved on the replica by the stream resolving
|
||||
// against the OSGi registry. This means the property values would be lost, which is why they
|
||||
// are added separately in this map.
|
||||
Map<FileStorageService, FileStorageServiceProperty[]> properties = new HashMap<>();
|
||||
for (FileStorageService service : getAvailableFileStorageServices()) {
|
||||
properties.put(service, service.getProperties());
|
||||
}
|
||||
os.writeObject(properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOperationExecutionListener(
|
||||
OperationExecutionListener<ReplicableFileStorageManagementService> listener) {
|
||||
operationExecutionListeners.put(listener, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeOperationExecutionListener(
|
||||
OperationExecutionListener<ReplicableFileStorageManagementService> listener) {
|
||||
operationExecutionListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<OperationExecutionListener<ReplicableFileStorageManagementService>> getOperationExecutionListeners() {
|
||||
return operationExecutionListeners.keySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOperationSentToMasterForReplication(
|
||||
OperationWithResultWithIdWrapper<ReplicableFileStorageManagementService, ?> operationWithResultWithIdWrapper) {
|
||||
operationsSentToMasterForReplication.add(operationWithResultWithIdWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSentOperationToMaster(OperationWithResult<ReplicableFileStorageManagementService, ?> operation) {
|
||||
return operationsSentToMasterForReplication.remove(operation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearReplicaState() throws MalformedURLException, IOException, InterruptedException {
|
||||
//don't need to clear anything - only know about file storage services, and these will live on anyway
|
||||
}
|
||||
|
||||
@Override
|
||||
public Serializable getId() {
|
||||
return getClass().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReplicationMasterDescriptor getMasterDescriptor() {
|
||||
return replicationMasterDescriptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startedReplicatingFrom(ReplicationMasterDescriptor master) {
|
||||
replicationMasterDescriptor = master;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stoppedReplicatingFrom(ReplicationMasterDescriptor master) {
|
||||
replicationMasterDescriptor = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster(boolean b) {
|
||||
currentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster.set(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCurrentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster() {
|
||||
return currentlyFillingFromInitialLoadOrApplyingOperationReceivedFromMaster.get();
|
||||
}
|
||||
}
|
||||
+9
-4
@@ -1,18 +1,18 @@
|
||||
package com.sap.sse.filestorage.impl;
|
||||
|
||||
import com.sap.sse.filestorage.Property;
|
||||
import com.sap.sse.filestorage.FileStorageServiceProperty;
|
||||
|
||||
public class PropertyImpl implements Property {
|
||||
public class FileStorageServicePropertyImpl implements FileStorageServiceProperty {
|
||||
private final boolean isRequired;
|
||||
private final String name;
|
||||
private final String description;
|
||||
private String value;
|
||||
|
||||
public PropertyImpl(String name, boolean isRequired, String description) {
|
||||
public FileStorageServicePropertyImpl(String name, boolean isRequired, String description) {
|
||||
this(name, isRequired, description, null);
|
||||
}
|
||||
|
||||
public PropertyImpl(String name, boolean isRequired, String description, String value) {
|
||||
public FileStorageServicePropertyImpl(String name, boolean isRequired, String description, String value) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.value = value;
|
||||
@@ -42,4 +42,9 @@ public class PropertyImpl implements Property {
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + "=" + value;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.sap.sse.filestorage.impl;
|
||||
|
||||
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
|
||||
import com.sap.sse.common.TypeBasedServiceFinder;
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.FileStorageServiceResolver;
|
||||
|
||||
public class FileStorageServiceResolverAgainstOsgiRegistryImpl implements FileStorageServiceResolver {
|
||||
private final TypeBasedServiceFinder<FileStorageService> serviceFinder;
|
||||
|
||||
public FileStorageServiceResolverAgainstOsgiRegistryImpl(TypeBasedServiceFinder<FileStorageService> serviceFinder) {
|
||||
this.serviceFinder = serviceFinder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileStorageService getFileStorageService(String serviceName) {
|
||||
try {
|
||||
return serviceFinder.findService(serviceName);
|
||||
} catch (NoCorrespondingServiceRegisteredException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+9
-20
@@ -13,9 +13,16 @@ import java.util.logging.Logger;
|
||||
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
import com.sap.sse.filestorage.InvalidPropertiesException;
|
||||
import com.sap.sse.filestorage.Property;
|
||||
|
||||
public class LocalFileStorageServiceImpl implements FileStorageService {
|
||||
public class LocalFileStorageServiceImpl extends BaseFileStorageServiceImpl implements FileStorageService {
|
||||
private static final String NAME = "Local Storage";
|
||||
private static final String DESCRIPTION = "";
|
||||
|
||||
protected LocalFileStorageServiceImpl() {
|
||||
super(NAME, DESCRIPTION);
|
||||
//addProperties(...);
|
||||
}
|
||||
|
||||
private static final Logger logger = Logger.getLogger(LocalFileStorageServiceImpl.class.getName());
|
||||
|
||||
private static final String host = "media.sapsailing.com";
|
||||
@@ -75,24 +82,6 @@ public class LocalFileStorageServiceImpl implements FileStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Property[] getProperties() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void internalSetProperty(String name, String value) throws IllegalArgumentException {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
+27
-6
@@ -12,24 +12,30 @@ import com.sap.sse.filestorage.FileStorageServicePropertyStore;
|
||||
import com.sap.sse.mongodb.MongoDBService;
|
||||
|
||||
public class MongoFileStorageServicePropertyStoreImpl implements FileStorageServicePropertyStore {
|
||||
public static final String COLLECTION_NAME = "FileStorageServiceProperties";
|
||||
private final DBCollection collection;
|
||||
public static final String PROPERTIES_COLLECTION_NAME = "FileStorageServiceProperties";
|
||||
public static final String ACTIVE_SERVICE_COLLECTION_NAME = "ActiveFileStorageService";
|
||||
private final DBCollection propertiesCollection;
|
||||
private final DBCollection activeServiceCollection;
|
||||
|
||||
private static enum FieldNames {
|
||||
SERVICE_NAME, PROPERTY_NAME, PROPERTY_VALUE;
|
||||
}
|
||||
|
||||
public MongoFileStorageServicePropertyStoreImpl(MongoDBService dbService) {
|
||||
collection = dbService.getDB().getCollection(COLLECTION_NAME);
|
||||
propertiesCollection = dbService.getDB().getCollection(PROPERTIES_COLLECTION_NAME);
|
||||
DBObject index = new BasicDBObjectBuilder().add(FieldNames.SERVICE_NAME.name(), true)
|
||||
.add(FieldNames.PROPERTY_NAME.name(), true).get();
|
||||
collection.ensureIndex(index);
|
||||
propertiesCollection.ensureIndex(index, "unique service name/property name combination", true);
|
||||
|
||||
activeServiceCollection = dbService.getDB().getCollection(ACTIVE_SERVICE_COLLECTION_NAME);
|
||||
index = new BasicDBObjectBuilder().add(FieldNames.SERVICE_NAME.name(), true).get();
|
||||
propertiesCollection.ensureIndex(index, "unique service name", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> readAllProperties(String serviceName) {
|
||||
DBObject query = new BasicDBObject(FieldNames.SERVICE_NAME.name(), serviceName);
|
||||
DBCursor cursor = collection.find(query);
|
||||
DBCursor cursor = propertiesCollection.find(query);
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
|
||||
for (DBObject property : cursor) {
|
||||
@@ -45,7 +51,22 @@ public class MongoFileStorageServicePropertyStoreImpl implements FileStorageServ
|
||||
DBObject obj = new BasicDBObjectBuilder().add(FieldNames.SERVICE_NAME.name(), serviceName)
|
||||
.add(FieldNames.PROPERTY_NAME.name(), propertyName)
|
||||
.add(FieldNames.PROPERTY_VALUE.name(), propertyValue).get();
|
||||
collection.insert(obj);
|
||||
propertiesCollection.insert(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readActiveServiceName() {
|
||||
DBObject obj = activeServiceCollection.findOne();
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
return (String) obj.get(FieldNames.SERVICE_NAME.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeActiveService(String serviceName) {
|
||||
DBObject obj = new BasicDBObject(FieldNames.SERVICE_NAME.name(), serviceName);
|
||||
activeServiceCollection.insert(obj);
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.sap.sse.filestorage.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.sap.sse.filestorage.FileStorageServiceResolver;
|
||||
import com.sap.sse.util.ObjectInputStreamResolvingAgainstCache;
|
||||
|
||||
/**
|
||||
* @see ObjectInputStreamResolvingAgainstCache for reason why subclass is necessary
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public class ObjectInputStreamResolvingAgainstFileStorageServiceResolver extends
|
||||
ObjectInputStreamResolvingAgainstCache<FileStorageServiceResolver> {
|
||||
|
||||
protected ObjectInputStreamResolvingAgainstFileStorageServiceResolver(InputStream in,
|
||||
FileStorageServiceResolver cache) throws IOException {
|
||||
super(in, cache);
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -2,7 +2,7 @@ package com.sap.sse.filestorage.impl;
|
||||
|
||||
import com.sap.sse.common.NoCorrespondingServiceRegisteredException;
|
||||
import com.sap.sse.filestorage.FileStorageManagementService;
|
||||
import com.sap.sse.filestorage.common.FileStorageService;
|
||||
import com.sap.sse.filestorage.FileStorageService;
|
||||
|
||||
/**
|
||||
* Publishes those methods of {@link FileStorageManagementService} that are required by operations implemented as lambda
|
||||
@@ -13,9 +13,9 @@ import com.sap.sse.filestorage.common.FileStorageService;
|
||||
* @author Fredrik Teschke
|
||||
*
|
||||
*/
|
||||
public interface ReplicableFileStorageManagementService {
|
||||
void internalSetFileStorageServiceProperty(String serviceName, String propertyName, String propertyValue)
|
||||
public interface ReplicableFileStorageManagementService extends FileStorageManagementService {
|
||||
Void internalSetFileStorageServiceProperty(FileStorageService service, String propertyName, String propertyValue)
|
||||
throws NoCorrespondingServiceRegisteredException, IllegalArgumentException;
|
||||
|
||||
void internalSetActiveFileStorageService(FileStorageService service);
|
||||
Void internalSetActiveFileStorageService(FileStorageService service);
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.sap.sse.filestorage.operations;
|
||||
|
||||
import com.sap.sse.filestorage.impl.ReplicableFileStorageManagementService;
|
||||
import com.sap.sse.replication.OperationWithResult;
|
||||
|
||||
public interface FileStorageServiceOperation<ResultT> extends
|
||||
OperationWithResult<ReplicableFileStorageManagementService, ResultT> {
|
||||
|
||||
}
|
||||
+1
-1
@@ -41,7 +41,7 @@ EXPEDITION_PORT=2010
|
||||
# set the REPLICATE_MASTER_EXCHANGE_NAME variable to the
|
||||
# same channel the master is using in its REPLICATION_CHANNEL variable
|
||||
|
||||
# REPLICATE_ON_START=com.sap.sailing.server.impl.RacingEventServiceImpl,com.sap.sse.security.impl.SecurityServiceImpl
|
||||
# REPLICATE_ON_START=com.sap.sailing.server.impl.RacingEventServiceImpl,com.sap.sse.security.impl.SecurityServiceImpl,com.sap.sse.filestorage.impl.FileStorageManagementServiceImpl
|
||||
|
||||
# Host where the master Java instance is running
|
||||
# Make sure firewall configurations allow access
|
||||
|
||||
Reference in new issue
Block a user