Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.unomi.shell.migration.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.karaf.shell.api.console.Session;
import org.jline.reader.LineReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import static org.apache.unomi.shell.migration.service.MigrationConfig.*;
import static org.apache.unomi.shell.migration.service.MigrationServiceImpl.MIGRATION_FS_ROOT_FOLDER;
/**
* This class is instantiated for each migration process, it contains useful methods to handle the current migration lifecycle.
*
* This class allow for keeping track of the migration steps by persisting the steps and there state on the FileSystem,
* allowing for a migration to be able to restart from a failure in case it happens.
*
* This class allow also for logging the migration informations depending on the current context:
* - if executed in karaf shell using a karaf shell session, then the logging will be done in the shell console
* - if executed outside karaf shell using OSGI service direct, then the logging will be done using classical logger systems
*
* This class allow also to do a best effort on missing configuration information, by prompting questions in the karaf shell
* (not supported in case direct OSGI service usage)
*/
public class MigrationContext {
private static final Logger LOGGER = LoggerFactory.getLogger(MigrationContext.class);
private static final Path MIGRATION_FS_HISTORY_FILE = Paths.get(System.getProperty( "karaf.data" ), MIGRATION_FS_ROOT_FOLDER, "history.json");
private enum MigrationStepState {
STARTED,
COMPLETED
}
protected MigrationContext(Session session, MigrationConfig migrationConfig) {
this.session = session;
this.migrationConfig = migrationConfig;
this.objectMapper = new ObjectMapper();
}
private final Session session;
private final MigrationConfig migrationConfig;
private final ObjectMapper objectMapper;
private CloseableHttpClient httpClient;
private Map history = new HashMap<>();
private Map userConfig = new HashMap<>();
/**
* Try to recover from a previous run
* I case we found an existing history we will ask if we want to recover or if we want to restart from the beginning
* (it is also configurable using the conf: recoverFromHistory)
*/
protected void tryRecoverFromHistory() throws IOException {
if (Files.exists(MIGRATION_FS_HISTORY_FILE)) {
if (getConfigBoolean(MIGRATION_HISTORY_RECOVER)) {
history = objectMapper.readValue(MIGRATION_FS_HISTORY_FILE.toFile(), new TypeReference