Package org.openengsb.core.services.internal

Source Code of org.openengsb.core.services.internal.ContextServiceImpl

/**
* Licensed to the Austrian Association for Software Tool Integration (AASTI)
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. The AASTI 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.openengsb.core.services.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.openengsb.core.api.context.Context;
import org.openengsb.core.api.context.ContextCurrentService;
import org.openengsb.core.api.context.ContextHolder;
import org.openengsb.core.api.context.ContextPath;
import org.openengsb.core.api.model.ContextConfiguration;
import org.openengsb.core.api.model.ContextId;
import org.openengsb.core.api.persistence.ConfigPersistenceService;
import org.openengsb.core.api.persistence.InvalidConfigurationException;
import org.openengsb.core.api.persistence.PersistenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ContextServiceImpl implements ContextCurrentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContextServiceImpl.class);

    private ConfigPersistenceService configPersistence;

    @Override
    public Context getContext(String path) {
        return getContext(path, false);
    }

    @Override
    public Context getContext() {
        String currentContextId = ContextHolder.get().getCurrentContextId();
        if (currentContextId == null) {
            return null;
        }
        return getContextById(currentContextId);
    }

    @Override
    public void createContext(String contextId) {
        LOGGER.debug("Creating context %s", contextId);
        ContextConfiguration contextConfiguration =
            new ContextConfiguration(new ContextId(contextId).toMetaData(), null);
        try {
            getConfigPersistenceService().persist(contextConfiguration);
        } catch (InvalidConfigurationException e) {
            LOGGER.error("Error storing context " + contextId + ": Invalid configuration", e);
        } catch (PersistenceException e) {
            LOGGER.error("Error storing context " + contextId + ": Persistence error", e);
        }
    }

    @Override
    public void deleteContext(String contextId) {
        LOGGER.debug("deleting context {}", contextId);
        try {
            getConfigPersistenceService().remove(new ContextId(contextId).toMetaData());
        } catch (PersistenceException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> getAvailableContexts() {
        List<ContextConfiguration> availableContextConfigurations =
            getContextConfigurationsOrEmptyOnError(ContextId.getContextIdWildCard());
        List<String> availableContexts = new ArrayList<String>();

        for (ContextConfiguration configuration : availableContextConfigurations) {
            availableContexts.add(ContextId.fromMetaData(configuration.getMetaData()).getId());
        }

        return availableContexts;
    }

    private Context getContextById(String currentContextId) {
        ContextId contextId = new ContextId(currentContextId);
        List<ContextConfiguration> contexts = getContextConfigurationsOrEmptyOnError(contextId.toMetaData());
        if (contexts.isEmpty()) {
            return null;
        }
        return contexts.get(0).toContext();
    }

    private Context getContext(String path, boolean create) {
        Context c = getContext();
        if (c == null) {
            return null;
        }
        Context parent = null;
        for (String pathElem : new ContextPath(path).getElements()) {
            parent = c;
            c = c.getChild(pathElem);
            if (c == null) {
                if (!create) {
                    return null;
                }
                c = parent.createChild(pathElem);
            }
        }
        return c;
    }

    private List<ContextConfiguration> getContextConfigurationsOrEmptyOnError(Map<String, String> metaData) {
        try {
            ConfigPersistenceService persistence = getConfigPersistenceService();
            return persistence.load(metaData);
        } catch (Exception e) {
            LOGGER.error("Error loading context configuration from configuration persistence", e);
            return Collections.emptyList();
        }
    }

    private ConfigPersistenceService getConfigPersistenceService() {
        return configPersistence;
    }

    public void setConfigPersistence(ConfigPersistenceService configPersistence) {
        this.configPersistence = configPersistence;
    }

}
TOP

Related Classes of org.openengsb.core.services.internal.ContextServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.