All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cristalise.kernel.entity.proxy.ProxyManager Maven / Gradle / Ivy

/**
 * This file is part of the CRISTAL-iSE kernel.
 * Copyright (c) 2001-2015 The CRISTAL Consortium. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * http://www.fsf.org/licensing/licenses/lgpl.html
 */
package org.cristalise.kernel.entity.proxy;

import static org.cristalise.kernel.process.Gateway.getLookup;

import org.cristalise.kernel.common.ObjectNotFoundException;
import org.cristalise.kernel.lookup.AgentPath;
import org.cristalise.kernel.lookup.DomainPath;
import org.cristalise.kernel.lookup.InvalidItemPathException;
import org.cristalise.kernel.lookup.ItemPath;
import org.cristalise.kernel.lookup.Path;
import org.cristalise.kernel.persistency.TransactionKey;

import lombok.extern.slf4j.Slf4j;

/**
 * An simple utility class to create new Proxies
 */
@Slf4j
public class ProxyManager {

    private static ItemProxy createProxy(ItemPath itemPath, TransactionKey transactionKey) throws ObjectNotFoundException {
        log.trace("createProxy() - Item:{}", itemPath);

        if( itemPath instanceof AgentPath ) return new AgentProxy((AgentPath)itemPath, transactionKey);
        else                                return new ItemProxy(itemPath, transactionKey);
    }

    public static ItemProxy getProxy(Path path) throws ObjectNotFoundException {
        return getProxy(path, null);
    }

    public static ItemProxy getProxy(Path path, TransactionKey transactionKey) throws ObjectNotFoundException {
        ItemPath itemPath = null;

        log.trace("getProxy() - path:{}", path);

        if (path instanceof ItemPath) {
            try {
                //issue #165: get ItemPath from Lookup to ensure it is a correct class
                itemPath = getLookup().getItemPath(((ItemPath)path).getName(), transactionKey);
            }
            catch (InvalidItemPathException e) {
                throw new ObjectNotFoundException(e);
            }
        }
        else if (path instanceof DomainPath) {
            //issue #165: reset target to enforce to read target from Lookup
            ((DomainPath) path).setTargetUUID(null);
            itemPath = path.getItemPath(transactionKey);
        }

        if (itemPath == null) throw new ObjectNotFoundException("Cannot use RolePath");

        return createProxy(itemPath, transactionKey);
    }

    public static AgentProxy getAgentProxy(String agentName) throws ObjectNotFoundException {
        return getAgentProxy(agentName, null);
    }

    public static AgentProxy getAgentProxy(String agentName, TransactionKey transactionKey) throws ObjectNotFoundException {
        AgentPath path = getLookup().getAgentPath(agentName, transactionKey);
        return (AgentProxy) getProxy(path, transactionKey);
    }

    public static AgentProxy getAgentProxy(AgentPath path) throws ObjectNotFoundException {
        return getAgentProxy(path, null);
    }

    public static AgentProxy getAgentProxy(AgentPath path, TransactionKey transactionKey) throws ObjectNotFoundException {
        return (AgentProxy) getProxy(path, transactionKey);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy