/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed 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.agila.impl.memory;
import org.apache.agila.services.AbstractInstanceService;
import org.apache.agila.services.InstanceInfo;
import org.apache.agila.services.InstanceServiceInfo;
import org.apache.agila.engine.Instance;
import org.apache.agila.engine.InstanceID;
import org.apache.agila.model.BusinessProcessID;
import org.apache.agila.impl.InstanceImpl;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Abstract, storage free mplementation of the InstanceService that does the
* work. Implementations just need to implement the persistence part.
* *
* @author <a href="mailto:geir@gluecode.com">Geir Magnusson Jr.</a>
* @version $Id: InstanceServiceImpl.java 38 2005-06-01 19:39:54Z chirino $
*/
public class InstanceServiceImpl extends AbstractInstanceService {
private HashMap hm = new HashMap();
private static int counter = 0;
/**
* Method to create a new instance. Implementations can just override to
* use a different kind of persistent store
* @param processID
* @param params
* @return
*/
protected Instance internalCreate(BusinessProcessID processID, Map params) {
Instance ei = new InstanceImpl();
ei.setBusinessProcessID(processID);
ei.setInstanceID(new InstanceID(++counter));
ei.setInstanceVariables(params);
ei.setStatus( Instance.STATUS_RUNNING );
hm.put(ei.getInstanceID(), ei);
return ei;
}
/**
* Method to save an instance (update/persiste). Implementations can just
* override.
*
* @param instance
*/
protected void internalSave(Instance instance) {
hm.put(instance.getInstanceID(), instance);
}
/**
* returns an instance by ID from the persistence store
*
* @param id
* @return
*/
protected Instance internalGetByID(InstanceID id) {
return (Instance) hm.get(id);
}
/**
* Returns all instances from the persistence store
* @return
*/
protected List internalListInstanceInfo() {
List data = new ArrayList();
Iterator it = hm.values().iterator();
while(it.hasNext()) {
InstanceImpl impl = (InstanceImpl) it.next();
data.add(new InstanceInfo(impl));
}
return data;
}
protected InstanceServiceInfo internalInstanceServiceInfo() {
int running = 0;
int suspended = 0;
int stopped = 0;
int completed = 0;
Iterator it = hm.values().iterator();
while(it.hasNext()) {
InstanceImpl impl = (InstanceImpl) it.next();
switch(impl.getStatus()){
case Instance.STATUS_RUNNING :
running++;
break;
case Instance.STATUS_SUSPENDED :
suspended++;
break;
case Instance.STATUS_STOPPED :
stopped++;
break;
case Instance.STATUS_COMPLETE:
completed++;
break;
}
}
return new InstanceServiceInfo( running, suspended, stopped, completed );
}
// TODO need to modify this using message queue
public void stop(InstanceID id) {
((Instance)hm.get( id )).setStatus( Instance.STATUS_STOPPED );
}
// TODO need to modify this using message queue
public void suspend(InstanceID id) {
((Instance)hm.get( id )).setStatus( Instance.STATUS_SUSPENDED );
}
// TODO need to modify this using message queue
public void resume(InstanceID id) {
((Instance)hm.get( id )).setStatus( Instance.STATUS_RUNNING );
}
}