Search in sources :

Example 1 with AgentPath

use of org.cristalise.kernel.lookup.AgentPath in project kernel by cristal-ise.

the class ItemImplementation method delegatedAction.

@Override
public String delegatedAction(SystemKey agentId, SystemKey delegateId, String stepPath, int transitionID, String requestData) throws AccessRightsException, InvalidTransitionException, ObjectNotFoundException, InvalidDataException, PersistencyException, ObjectAlreadyExistsException, InvalidCollectionModification {
    Workflow lifeCycle = null;
    try {
        AgentPath agent = new AgentPath(agentId);
        AgentPath delegate = delegateId == null ? null : new AgentPath(delegateId);
        Logger.msg(1, "ItemImplementation::request(" + mItemPath + ") - Transition " + transitionID + " on " + stepPath + " by " + (delegate == null ? "" : delegate + " on behalf of ") + agent);
        // TODO: check if delegate is allowed valid for agent
        lifeCycle = (Workflow) mStorage.get(mItemPath, ClusterType.LIFECYCLE + "/workflow", null);
        String finalOutcome = lifeCycle.requestAction(agent, delegate, stepPath, mItemPath, transitionID, requestData);
        // store the workflow if we've changed the state of the domain wf
        if (!(stepPath.startsWith("workflow/predefined")))
            mStorage.put(mItemPath, lifeCycle, lifeCycle);
        // remove entity path if transaction was successful
        if (stepPath.equals("workflow/predefined/Erase")) {
            Logger.msg("Erasing item path " + mItemPath.toString());
            Gateway.getLookupManager().delete(mItemPath);
        }
        mStorage.commit(lifeCycle);
        return finalOutcome;
    } catch (AccessRightsException | InvalidTransitionException | ObjectNotFoundException | PersistencyException | InvalidDataException | ObjectAlreadyExistsException | InvalidCollectionModification ex) {
        if (Logger.doLog(8))
            Logger.error(ex);
        String errorOutcome = handleError(agentId, delegateId, stepPath, lifeCycle, ex);
        if (StringUtils.isBlank(errorOutcome)) {
            mStorage.abort(lifeCycle);
            throw ex;
        } else {
            mStorage.commit(lifeCycle);
            return errorOutcome;
        }
    } catch (InvalidAgentPathException | ObjectCannotBeUpdated | CannotManageException ex) {
        if (Logger.doLog(8))
            Logger.error(ex);
        String errorOutcome = handleError(agentId, delegateId, stepPath, lifeCycle, ex);
        if (StringUtils.isBlank(errorOutcome)) {
            mStorage.abort(lifeCycle);
            throw new InvalidDataException(ex.getClass().getName() + " - " + ex.getMessage());
        } else {
            mStorage.commit(lifeCycle);
            return errorOutcome;
        }
    } catch (Exception ex) {
        // non-CORBA exception hasn't been caught!
        Logger.error("Unknown Error: requestAction on " + mItemPath + " by " + agentId + " executing " + stepPath);
        Logger.error(ex);
        String errorOutcome = handleError(agentId, delegateId, stepPath, lifeCycle, ex);
        if (StringUtils.isBlank(errorOutcome)) {
            mStorage.abort(lifeCycle);
            throw new InvalidDataException("Extraordinary Exception during execution:" + ex.getClass().getName() + " - " + ex.getMessage());
        } else {
            mStorage.commit(lifeCycle);
            return errorOutcome;
        }
    }
}
Also used : InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) CannotManageException(org.cristalise.kernel.common.CannotManageException) AgentPath(org.cristalise.kernel.lookup.AgentPath) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) InvalidCollectionModification(org.cristalise.kernel.common.InvalidCollectionModification) MappingException(org.exolab.castor.mapping.MappingException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) ValidationException(org.exolab.castor.xml.ValidationException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) MarshalException(org.exolab.castor.xml.MarshalException) InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) IOException(java.io.IOException) PersistencyException(org.cristalise.kernel.common.PersistencyException) CannotManageException(org.cristalise.kernel.common.CannotManageException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) ObjectCannotBeUpdated(org.cristalise.kernel.common.ObjectCannotBeUpdated) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException)

Example 2 with AgentPath

use of org.cristalise.kernel.lookup.AgentPath in project kernel by cristal-ise.

the class ItemImplementation method initialise.

@Override
public void initialise(SystemKey agentId, String propString, String initWfString, String initCollsString) throws AccessRightsException, InvalidDataException, PersistencyException {
    Logger.msg(5, "Item::initialise(" + mItemPath + ") - agent:" + agentId);
    Object locker = new Object();
    AgentPath agentPath;
    try {
        agentPath = new AgentPath(agentId);
    } catch (InvalidItemPathException e) {
        throw new AccessRightsException("Invalid Agent Id:" + agentId);
    }
    // must supply properties
    if (propString == null || propString.length() == 0 || propString.equals("<NULL/>")) {
        throw new InvalidDataException("No properties supplied");
    }
    // store properties
    try {
        PropertyArrayList props = (PropertyArrayList) Gateway.getMarshaller().unmarshall(propString);
        for (Property thisProp : props.list) mStorage.put(mItemPath, thisProp, locker);
    } catch (Throwable ex) {
        Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Properties were invalid: " + propString);
        Logger.error(ex);
        mStorage.abort(locker);
        throw new InvalidDataException("Properties were invalid");
    }
    // Store an event and the initial properties
    try {
        Schema initSchema = LocalObjectLoader.getSchema("ItemInitialization", 0);
        Outcome initOutcome = new Outcome(0, propString, initSchema);
        History hist = new History(mItemPath, locker);
        Event newEvent = hist.addEvent(new AgentPath(agentId), null, "", "Initialize", "", "", initSchema, Bootstrap.getPredefSM(), PredefinedStep.DONE, "last");
        initOutcome.setID(newEvent.getID());
        Viewpoint newLastView = new Viewpoint(mItemPath, initSchema, "last", newEvent.getID());
        mStorage.put(mItemPath, initOutcome, locker);
        mStorage.put(mItemPath, newLastView, locker);
    } catch (Throwable ex) {
        Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Could not store event and outcome.");
        Logger.error(ex);
        mStorage.abort(locker);
        throw new PersistencyException("Error storing event and outcome");
    }
    // init collections
    if (initCollsString != null && initCollsString.length() > 0 && !initCollsString.equals("<NULL/>")) {
        try {
            CollectionArrayList colls = (CollectionArrayList) Gateway.getMarshaller().unmarshall(initCollsString);
            for (Collection<?> thisColl : colls.list) {
                mStorage.put(mItemPath, thisColl, locker);
            }
        } catch (Throwable ex) {
            Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Collections were invalid: " + initCollsString);
            Logger.error(ex);
            mStorage.abort(locker);
            throw new InvalidDataException("Collections were invalid");
        }
    }
    // create wf
    Workflow lc = null;
    try {
        if (initWfString == null || initWfString.length() == 0 || initWfString.equals("<NULL/>")) {
            lc = new Workflow(new CompositeActivity(), getNewPredefStepContainer());
        } else {
            lc = new Workflow((CompositeActivity) Gateway.getMarshaller().unmarshall(initWfString), getNewPredefStepContainer());
        }
        mStorage.put(mItemPath, lc, locker);
    } catch (Throwable ex) {
        Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Workflow was invalid: " + initWfString);
        Logger.error(ex);
        mStorage.abort(locker);
        throw new InvalidDataException("Workflow was invalid");
    }
    // All objects are in place, initialize the workflow to get the Item running
    lc.initialise(mItemPath, agentPath, locker);
    mStorage.put(mItemPath, lc, locker);
    mStorage.commit(locker);
    Logger.msg(3, "Initialisation of item " + mItemPath + " was successful");
}
Also used : InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) AgentPath(org.cristalise.kernel.lookup.AgentPath) Schema(org.cristalise.kernel.persistency.outcome.Schema) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) History(org.cristalise.kernel.events.History) CollectionArrayList(org.cristalise.kernel.collection.CollectionArrayList) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) CompositeActivity(org.cristalise.kernel.lifecycle.instance.CompositeActivity) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PropertyArrayList(org.cristalise.kernel.property.PropertyArrayList) Event(org.cristalise.kernel.events.Event) PersistencyException(org.cristalise.kernel.common.PersistencyException) Property(org.cristalise.kernel.property.Property)

Example 3 with AgentPath

use of org.cristalise.kernel.lookup.AgentPath in project kernel by cristal-ise.

the class ItemImplementation method handleError.

/**
 * @param agentId
 * @param delegateId
 * @param stepPath
 * @param ex
 * @return
 * @throws PersistencyException
 * @throws ObjectNotFoundException
 * @throws AccessRightsException
 * @throws InvalidTransitionException
 * @throws InvalidDataException
 * @throws ObjectAlreadyExistsException
 * @throws InvalidCollectionModification
 */
private String handleError(SystemKey agentId, SystemKey delegateId, String stepPath, Workflow lifeCycle, Exception ex) throws PersistencyException, ObjectNotFoundException, AccessRightsException, InvalidTransitionException, InvalidDataException, ObjectAlreadyExistsException, InvalidCollectionModification {
    if (!Gateway.getProperties().getBoolean("StateMachine.enableErrorHandling", false))
        return null;
    int errorTransId = ((Activity) lifeCycle.search(stepPath)).getErrorTransitionId();
    if (errorTransId == -1)
        return null;
    try {
        AgentPath agent = new AgentPath(agentId);
        AgentPath delegate = delegateId == null ? null : new AgentPath(delegateId);
        String errorOutcome = Gateway.getMarshaller().marshall(new ErrorInfo(ex));
        lifeCycle.requestAction(agent, delegate, stepPath, mItemPath, errorTransId, errorOutcome);
        if (!(stepPath.startsWith("workflow/predefined")))
            mStorage.put(mItemPath, lifeCycle, lifeCycle);
        return errorOutcome;
    } catch (InvalidAgentPathException | ObjectCannotBeUpdated | CannotManageException | MarshalException | ValidationException | IOException | MappingException e) {
        Logger.error(e);
        return "";
    }
}
Also used : MarshalException(org.exolab.castor.xml.MarshalException) ValidationException(org.exolab.castor.xml.ValidationException) CannotManageException(org.cristalise.kernel.common.CannotManageException) AgentPath(org.cristalise.kernel.lookup.AgentPath) ErrorInfo(org.cristalise.kernel.scripting.ErrorInfo) Activity(org.cristalise.kernel.lifecycle.instance.Activity) CompositeActivity(org.cristalise.kernel.lifecycle.instance.CompositeActivity) IOException(java.io.IOException) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) MappingException(org.exolab.castor.mapping.MappingException) ObjectCannotBeUpdated(org.cristalise.kernel.common.ObjectCannotBeUpdated) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException)

Example 4 with AgentPath

use of org.cristalise.kernel.lookup.AgentPath in project kernel by cristal-ise.

the class Gateway method connect.

/**
 * Log in with the given username and password, and initialises the {@link Lookup}, {@link TransactionManager} and {@link ProxyManager}.
 * It shall be uses in client processes only.
 *
 * @param agentName - username
 * @param agentPassword - password
 * @return an AgentProxy on the requested user
 *
 * @throws InvalidDataException - bad params
 * @throws PersistencyException - error starting storages
 * @throws ObjectNotFoundException - object not found
 */
public static AgentProxy connect(String agentName, String agentPassword, String resource) throws InvalidDataException, ObjectNotFoundException, PersistencyException {
    Authenticator auth = getAuthenticator();
    if (!auth.authenticate(agentName, agentPassword, resource))
        throw new InvalidDataException("Login failed");
    try {
        if (mLookup != null)
            mLookup.close();
        mLookup = (Lookup) mC2KProps.getInstance("Lookup");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        Logger.error(ex);
        throw new InvalidDataException("Lookup " + mC2KProps.getString("Lookup") + " could not be instantiated");
    }
    mLookup.open(auth);
    mStorage = new TransactionManager(auth);
    mProxyManager = new ProxyManager();
    // find agent proxy
    AgentPath agentPath = mLookup.getAgentPath(agentName);
    AgentProxy agent = (AgentProxy) mProxyManager.getProxy(agentPath);
    agent.setAuthObj(auth);
    ScriptConsole.setUser(agent);
    // Run module startup scripts. Server does this during bootstrap
    mModules.setUser(agent);
    mModules.runScripts("startup");
    Logger.msg("Gateway.connect(agent) - DONE.");
    return agent;
}
Also used : TransactionManager(org.cristalise.kernel.persistency.TransactionManager) AgentPath(org.cristalise.kernel.lookup.AgentPath) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) ProxyManager(org.cristalise.kernel.entity.proxy.ProxyManager) AgentProxy(org.cristalise.kernel.entity.proxy.AgentProxy) Authenticator(org.cristalise.kernel.process.auth.Authenticator)

Example 5 with AgentPath

use of org.cristalise.kernel.lookup.AgentPath in project kernel by cristal-ise.

the class ClusterStorageManager method get.

/**
 * Internal get method. Retrieves clusters from ClusterStorages & maintains the memory cache.
 * <br>
 * There is a special case for Viewpoint. When path ends with /data it returns referenced Outcome instead of Viewpoint.
 *
 * @param itemPath current Iten
 * @param path the cluster path
 * @return the C2KObject located by path
 */
public C2KLocalObject get(ItemPath itemPath, String path) throws PersistencyException, ObjectNotFoundException {
    // check cache first
    Map<String, C2KLocalObject> sysKeyMemCache = memoryCache.get(itemPath);
    if (sysKeyMemCache != null) {
        synchronized (sysKeyMemCache) {
            C2KLocalObject obj = sysKeyMemCache.get(path);
            if (obj != null) {
                Logger.msg(7, "ClusterStorageManager.get() - found " + itemPath + "/" + path + " in memcache");
                return obj;
            }
        }
    }
    // special case for Viewpoint- When path ends with /data it returns referenced Outcome instead of Viewpoint
    if (path.startsWith(VIEWPOINT.getName()) && path.endsWith("/data")) {
        StringTokenizer tok = new StringTokenizer(path, "/");
        if (tok.countTokens() == 4) {
            // to not catch viewpoints called 'data'
            Viewpoint view = (Viewpoint) get(itemPath, path.substring(0, path.lastIndexOf("/")));
            if (view != null)
                return view.getOutcome();
            else
                return null;
        }
    }
    C2KLocalObject result = null;
    // deal out top level remote maps
    if (path.indexOf('/') == -1) {
        if (path.equals(HISTORY.getName())) {
            result = new History(itemPath, null);
        } else if (path.equals(JOB.getName())) {
            if (itemPath instanceof AgentPath)
                result = new JobList((AgentPath) itemPath, null);
            else
                throw new ObjectNotFoundException("Items do not have job lists");
        }
    }
    if (result == null) {
        // else try each reader in turn until we find it
        ArrayList<ClusterStorage> readers = findStorages(ClusterStorage.getClusterType(path), false);
        for (ClusterStorage thisReader : readers) {
            try {
                result = thisReader.get(itemPath, path);
                Logger.msg(7, "ClusterStorageManager.get() - reading " + path + " from " + thisReader.getName() + " for item " + itemPath);
                // got it!
                if (result != null)
                    break;
            } catch (PersistencyException e) {
                Logger.msg(7, "ClusterStorageManager.get() - reader " + thisReader.getName() + " could not retrieve " + itemPath + "/" + path + ": " + e.getMessage());
            }
        }
    }
    // No result was found after reading the list of ClusterStorages
    if (result == null) {
        throw new ObjectNotFoundException("ClusterStorageManager.get() - Path " + path + " not found in " + itemPath);
    }
    putInMemoryCache(itemPath, path, result);
    return result;
}
Also used : StringTokenizer(java.util.StringTokenizer) C2KLocalObject(org.cristalise.kernel.entity.C2KLocalObject) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) AgentPath(org.cristalise.kernel.lookup.AgentPath) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) PersistencyException(org.cristalise.kernel.common.PersistencyException) JobList(org.cristalise.kernel.entity.agent.JobList) History(org.cristalise.kernel.events.History)

Aggregations

AgentPath (org.cristalise.kernel.lookup.AgentPath)23 InvalidDataException (org.cristalise.kernel.common.InvalidDataException)10 ObjectNotFoundException (org.cristalise.kernel.common.ObjectNotFoundException)10 InvalidItemPathException (org.cristalise.kernel.lookup.InvalidItemPathException)8 CannotManageException (org.cristalise.kernel.common.CannotManageException)6 PersistencyException (org.cristalise.kernel.common.PersistencyException)6 ObjectAlreadyExistsException (org.cristalise.kernel.common.ObjectAlreadyExistsException)5 ObjectCannotBeUpdated (org.cristalise.kernel.common.ObjectCannotBeUpdated)5 RolePath (org.cristalise.kernel.lookup.RolePath)5 InvalidAgentPathException (org.cristalise.kernel.lookup.InvalidAgentPathException)4 IOException (java.io.IOException)3 AccessRightsException (org.cristalise.kernel.common.AccessRightsException)3 AgentProxy (org.cristalise.kernel.entity.proxy.AgentProxy)3 CompositeActivity (org.cristalise.kernel.lifecycle.instance.CompositeActivity)3 Workflow (org.cristalise.kernel.lifecycle.instance.Workflow)3 DomainPath (org.cristalise.kernel.lookup.DomainPath)3 ItemPath (org.cristalise.kernel.lookup.ItemPath)3 Viewpoint (org.cristalise.kernel.persistency.outcome.Viewpoint)3 Property (org.cristalise.kernel.property.Property)3 PropertyArrayList (org.cristalise.kernel.property.PropertyArrayList)3