diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/FileStorageServiceDTOUtils.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/FileStorageServiceDTOUtils.java index a56cefd7077..de31f2bdb5d 100644 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/FileStorageServiceDTOUtils.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/FileStorageServiceDTOUtils.java @@ -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 msgs = new HashMap<>(); - for (Entry entry : e.getPerPropertyMessage().entrySet()) { + for (Entry 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 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])); diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java index bfdec1c4134..b0cf7baf27c 100755 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java @@ -5446,7 +5446,7 @@ public class SailingServiceImpl extends ProxiedRemoteServiceServlet implements S for (Entry 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 } diff --git a/java/com.sap.sse.filestorage/META-INF/MANIFEST.MF b/java/com.sap.sse.filestorage/META-INF/MANIFEST.MF index 91c199054dc..2826c1b8bf3 100755 --- a/java/com.sap.sse.filestorage/META-INF/MANIFEST.MF +++ b/java/com.sap.sse.filestorage/META-INF/MANIFEST.MF @@ -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 diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageManagementService.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageManagementService.java index 8242b18a0a7..bafce474f22 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageManagementService.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageManagementService.java @@ -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> { 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; /** diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageService.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageService.java index 4dcc510a315..056dcbea789 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageService.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageService.java @@ -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 { /** * @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} diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/Property.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServiceProperty.java similarity index 86% rename from java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/Property.java rename to java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServiceProperty.java index f61f8a073cf..b5e322e0633 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/Property.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServiceProperty.java @@ -5,7 +5,7 @@ package com.sap.sse.filestorage; * @author Fredrik Teschke * */ -public interface Property { +public interface FileStorageServiceProperty { boolean isRequired(); String getName(); diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServicePropertyStore.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServicePropertyStore.java index f551ef518d6..9b40ff0852a 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServicePropertyStore.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServicePropertyStore.java @@ -12,4 +12,8 @@ public interface FileStorageServicePropertyStore { Map readAllProperties(String serviceName); void writeProperty(String serviceName, String propertyName, String propertyValue); + + String readActiveServiceName(); + + void writeActiveService(String serviceName); } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServiceResolver.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServiceResolver.java new file mode 100644 index 00000000000..d5ecaa02b84 --- /dev/null +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/FileStorageServiceResolver.java @@ -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); +} diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/InvalidPropertiesException.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/InvalidPropertiesException.java index 6c2077e7eab..d1436d4b335 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/InvalidPropertiesException.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/InvalidPropertiesException.java @@ -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 perPropertyMessages = new HashMap<>(); + private final Map perPropertyMessages = new HashMap<>(); public InvalidPropertiesException(String message) { super(message); @@ -19,15 +19,15 @@ public class InvalidPropertiesException extends Exception { @SafeVarargs public InvalidPropertiesException(String message, - Pair... perPropertyMessages) { + Pair... perPropertyMessages) { this(message, null, perPropertyMessages); } @SafeVarargs public InvalidPropertiesException(String message, Throwable cause, - Pair... perPropertyMessages) { + Pair... perPropertyMessages) { super(message, cause); - for (Pair pair : perPropertyMessages) { + for (Pair pair : perPropertyMessages) { this.perPropertyMessages.put(pair.getA(), pair.getB()); } } @@ -40,7 +40,7 @@ public class InvalidPropertiesException extends Exception { return super.getMessage(); } - public Map getPerPropertyMessage() { + public Map getPerPropertyMessage() { return perPropertyMessages; } } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/Activator.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/Activator.java index 0616e999142..de86806886e 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/Activator.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/Activator.java @@ -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 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 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 diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/AmazonS3FileStorageServiceImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/AmazonS3FileStorageServiceImpl.java index 65fb4db99b6..d2b690119d0 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/AmazonS3FileStorageServiceImpl.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/AmazonS3FileStorageServiceImpl.java @@ -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 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(accessId, "seems to be invalid"), new Pair(accessKey, + new Pair(accessId, "seems to be invalid"), new Pair(accessKey, "seems to be invalid")); } // test if bucket exists if (!s3.doesBucketExist(bucketName.getValue())) { - throw new InvalidPropertiesException("invalid bucket", new Pair(bucketName, + throw new InvalidPropertiesException("invalid bucket", new Pair(bucketName, "bucket does not exist")); } } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/BaseFileStorageServiceImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/BaseFileStorageServiceImpl.java new file mode 100644 index 00000000000..d4ddf9e84ed --- /dev/null +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/BaseFileStorageServiceImpl.java @@ -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 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 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(); + } +} diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/EmptyFileStorageServicePropertyStoreImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/EmptyFileStorageServicePropertyStoreImpl.java new file mode 100644 index 00000000000..3534760008e --- /dev/null +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/EmptyFileStorageServicePropertyStoreImpl.java @@ -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 readAllProperties(String serviceName) { + return new HashMap(); + } + + @Override + public void writeProperty(String serviceName, String propertyName, String propertyValue) { + + } + + @Override + public String readActiveServiceName() { + return null; + } + + @Override + public void writeActiveService(String serviceName) { + } + +} diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageManagementServiceImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageManagementServiceImpl.java index 1f1afe25113..8241c69c6e4 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageManagementServiceImpl.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageManagementServiceImpl.java @@ -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 { +public class FileStorageManagementServiceImpl implements ReplicableFileStorageManagementService, + ServiceTrackerCustomizer { private final Logger logger = Logger.getLogger(FileStorageManagementServiceImpl.class.getName()); - - private FileStorageService active; + private FileStorageService active; private final TypeBasedServiceFinder serviceFinder; - private final FileStorageServicePropertyStore propertyStore; - private final BundleContext context; + private final Map, OperationExecutionListener> operationExecutionListeners = new ConcurrentHashMap<>(); + private final Set> operationsSentToMasterForReplication = new HashSet<>(); + private ReplicationMasterDescriptor replicationMasterDescriptor; + private ThreadLocal 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 reference, FileStorageService service) { - //ignore + // file storage service has been modified: ignore } @Override public void removedService(ServiceReference 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 properties = (Map) 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 properties = new HashMap<>(); + for (FileStorageService service : getAvailableFileStorageServices()) { + properties.put(service, service.getProperties()); + } + os.writeObject(properties); + } + + @Override + public void addOperationExecutionListener( + OperationExecutionListener listener) { + operationExecutionListeners.put(listener, listener); + } + + @Override + public void removeOperationExecutionListener( + OperationExecutionListener listener) { + operationExecutionListeners.remove(listener); + } + + @Override + public Iterable> getOperationExecutionListeners() { + return operationExecutionListeners.keySet(); + } + + @Override + public void addOperationSentToMasterForReplication( + OperationWithResultWithIdWrapper operationWithResultWithIdWrapper) { + operationsSentToMasterForReplication.add(operationWithResultWithIdWrapper); + } + + @Override + public boolean hasSentOperationToMaster(OperationWithResult 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(); } } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/PropertyImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageServicePropertyImpl.java similarity index 63% rename from java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/PropertyImpl.java rename to java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageServicePropertyImpl.java index 427439bd2d3..8e433d0b973 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/PropertyImpl.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageServicePropertyImpl.java @@ -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; + } } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageServiceResolverAgainstOsgiRegistryImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageServiceResolverAgainstOsgiRegistryImpl.java new file mode 100644 index 00000000000..8fac614b0c0 --- /dev/null +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/FileStorageServiceResolverAgainstOsgiRegistryImpl.java @@ -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 serviceFinder; + + public FileStorageServiceResolverAgainstOsgiRegistryImpl(TypeBasedServiceFinder serviceFinder) { + this.serviceFinder = serviceFinder; + } + + @Override + public FileStorageService getFileStorageService(String serviceName) { + try { + return serviceFinder.findService(serviceName); + } catch (NoCorrespondingServiceRegisteredException e) { + return null; + } + } + +} diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/LocalFileStorageServiceImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/LocalFileStorageServiceImpl.java index 007f061fc20..78f27eba5a6 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/LocalFileStorageServiceImpl.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/LocalFileStorageServiceImpl.java @@ -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 diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/MongoFileStorageServicePropertyStoreImpl.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/MongoFileStorageServicePropertyStoreImpl.java index 6896894880d..6bc3695cf05 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/MongoFileStorageServicePropertyStoreImpl.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/MongoFileStorageServicePropertyStoreImpl.java @@ -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 readAllProperties(String serviceName) { DBObject query = new BasicDBObject(FieldNames.SERVICE_NAME.name(), serviceName); - DBCursor cursor = collection.find(query); + DBCursor cursor = propertiesCollection.find(query); Map 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); } } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ObjectInputStreamResolvingAgainstFileStorageServiceResolver.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ObjectInputStreamResolvingAgainstFileStorageServiceResolver.java new file mode 100644 index 00000000000..7b311f6bdd5 --- /dev/null +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ObjectInputStreamResolvingAgainstFileStorageServiceResolver.java @@ -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 { + + protected ObjectInputStreamResolvingAgainstFileStorageServiceResolver(InputStream in, + FileStorageServiceResolver cache) throws IOException { + super(in, cache); + } + +} diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ReplicableFileStorageManagementService.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ReplicableFileStorageManagementService.java index 9e7aec1e973..36a5df5b112 100644 --- a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ReplicableFileStorageManagementService.java +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/impl/ReplicableFileStorageManagementService.java @@ -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); } diff --git a/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/operations/FileStorageServiceOperation.java b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/operations/FileStorageServiceOperation.java new file mode 100644 index 00000000000..695cdc0e8f1 --- /dev/null +++ b/java/com.sap.sse.filestorage/src/com/sap/sse/filestorage/operations/FileStorageServiceOperation.java @@ -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 extends + OperationWithResult { + +} diff --git a/java/target/env.sh b/java/target/env.sh index 20988048fbf..8ef97faa381 100644 --- a/java/target/env.sh +++ b/java/target/env.sh @@ -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