iuliana commented on a change in pull request #1195:
URL: https://github.com/apache/brooklyn-server/pull/1195#discussion_r667881100



##########
File path: 
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ServerResource.java
##########
@@ -528,4 +537,79 @@ protected Response exportPersistenceData(MementoCopyMode 
preferredOrigin) {
         }
     }
 
+    @Override
+    public Response importPersistenceData(String persistenceExportLocation) {
+        try {
+            File persistenceLocation = new File(persistenceExportLocation);
+            if (!persistenceLocation.isDirectory()){
+                throw WebResourceUtils.badRequest("Invalid persistence 
directory - %s does not exist or is not a directory", 
persistenceExportLocation);
+            }
+
+            // set up temporary management context using the persistence to be 
imported
+            BrooklynProperties brooklynPropertiesWithExportPersistenceDir = 
BrooklynProperties.Factory.builderDefault().build();
+            
brooklynPropertiesWithExportPersistenceDir.put("amp.persistence.dir",persistenceExportLocation);
+
+            LocalManagementContext tempMgmt = new 
LocalManagementContext(brooklynPropertiesWithExportPersistenceDir);
+            PersistenceObjectStore tempPersistenceStore = 
BrooklynPersistenceUtils.newPersistenceObjectStore(tempMgmt,null, 
persistenceExportLocation);
+            
tempPersistenceStore.prepareForSharedUse(PersistMode.REBIND,HighAvailabilityMode.AUTO);
+            BrooklynMementoPersisterToObjectStore persister = new 
BrooklynMementoPersisterToObjectStore(
+                    tempPersistenceStore, tempMgmt);
+            PersistenceExceptionHandler persistenceExceptionHandler = 
PersistenceExceptionHandlerImpl.builder().build();
+            RebindManager rebindManager = tempMgmt.getRebindManager();
+            rebindManager.setPersister(persister, persistenceExceptionHandler);
+            rebindManager.forcePersistNow(false, null);
+
+            BrooklynMementoRawData newMementoRawData = 
tempMgmt.getRebindManager().retrieveMementoRawData();
+
+            // install bundles to active management context
+            for (Map.Entry<String, ByteSource> bundleJar : 
newMementoRawData.getBundleJars().entrySet()){
+                ReferenceWithError<OsgiBundleInstallationResult> result = 
((ManagementContextInternal)mgmt()).getOsgiManager().get()
+                        .install(InputStreamSource.of("Persistence import - 
bundle install", bundleJar.getValue().read()), "", false);
+
+                if (result.hasError()) {
+                    if (log.isTraceEnabled()) {
+                        log.trace("Unable to create, format '', returning 400: 
"+result.getError().getMessage(), result.getError());
+                    }
+                    String errorMsg = "";
+                    if (result.getWithoutError()!=null) {
+                        errorMsg = result.getWithoutError().getMessage();
+                    } else {
+                        errorMsg = 
Strings.isNonBlank(result.getError().getMessage()) ? 
result.getError().getMessage() : result.getError().toString();
+                    }
+                    throw new Exception(errorMsg);
+                }
+            }
+
+            // write persisted items and rebind to load applications
+            BrooklynMementoRawData.Builder result = 
BrooklynMementoRawData.builder();
+            MementoSerializer<Object> rawSerializer = new 
XmlMementoSerializer<Object>(mgmt().getClass().getClassLoader());
+            RetryingMementoSerializer<Object> serializer = new 
RetryingMementoSerializer<Object>(rawSerializer, 1);
+
+            result.planeId(mgmt().getManagementPlaneIdMaybe().orNull());
+            result.entities(newMementoRawData.getEntities());
+            result.locations(newMementoRawData.getLocations());
+            result.policies(newMementoRawData.getPolicies());
+            result.enrichers(newMementoRawData.getEnrichers());
+            result.feeds(newMementoRawData.getFeeds());
+            result.catalogItems(newMementoRawData.getCatalogItems());
+
+            PersistenceObjectStore currentPersistenceStore = 
((BrooklynMementoPersisterToObjectStore) 
mgmt().getRebindManager().getPersister()).getObjectStore();
+            
BrooklynPersistenceUtils.writeMemento(mgmt(),result.build(),currentPersistenceStore);
+            
mgmt().getRebindManager().rebind(mgmt().getCatalogClassLoader(),null, 
mgmt().getHighAvailabilityManager().getNodeState());
+
+            // clean up the temporary management context

Review comment:
       What is the consequence if an exception is thrown and these are not 
called?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to