/*
* 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;
import org.apache.agila.engine.Instance;
import org.apache.agila.engine.Token;
import org.apache.agila.model.Binding;
import org.apache.agila.model.Node;
import org.apache.agila.model.NodeContext;
import org.apache.agila.services.InstanceService;
import org.apache.agila.services.notification.NotificationService;
import org.apache.agila.services.task.TaskService;
import org.apache.agila.services.TimerService;
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* @author <a href="mailto:geir@gluecode.com">Geir Magnusson Jr.</a>
* @version $Id: NodeContextImpl.java 38 2005-06-01 19:39:54Z chirino $
*/
public class NodeContextImpl implements NodeContext {
protected Token currentToken = null;
protected Token nextToken = null;
private TimerService ts = null;
private TaskService taskService = null;
private NotificationService notificationService = null;
private Node node = null;
private final Map instanceData;
private Map appData = new HashMap();
public NodeContextImpl(Node node, Instance instance,
TimerService timer, TaskService task,
NotificationService notificationService) {
this.node = node;
this.instanceData = instance.getInstanceVariables();
this.ts = timer;
this.taskService = task;
this.notificationService = notificationService;
}
/**
* Retrieves a binding value
*
* @param name
* @return
*/
public Object getBoundValue(String name) {
Map bm = node.getBindings();
Binding binding = (Binding) bm.get(name);
if (binding == null) {
return null;
}
switch(binding.getType()) {
case Binding.STATIC :
return binding.getValue();
case Binding.EL :
try {
return jexlResolver(binding.getValue(), this.getInstanceData());
}
catch(Exception e) {
// TODO somethig useful
e.printStackTrace();
return "";
}
default :
return binding.getValue();
}
}
public void setBoundValue(String name, Object value) {
Map bm = node.getBindings();
Binding binding = (Binding) bm.get(name);
if (binding == null) {
// TODO tell someone
return;
}
switch(binding.getType()) {
case Binding.EL :
try {
/*
* Awful hack - need to fix so exprs can be used
* for now, assume a scalar reference and just set it
*/
this.getInstanceData().put(binding.getName(), value);
}
catch(Exception e) {
// TODO somethig useful
e.printStackTrace();
}
default :
// TODO urg...
}
return;
}
public void setCurrentExecutionToken(Token token) {
this.currentToken = token;
}
public Token getCurrentExecutionToken() {
return this.currentToken;
}
public void setNextExecutionToken(Token token) {
this.nextToken = token;
}
public Token getNextExecutionToken() {
return this.nextToken;
}
public void setTimerService(TimerService svc) {
this.ts = svc;
}
public TimerService getTimerService() {
return this.ts;
}
public void setTaskService(TaskService svc) {
this.taskService = svc;
}
public TaskService getTaskService() {
return this.taskService;
}
public NotificationService getNotificationService() {
return this.notificationService;
}
/**
* Retrieves the data sent by an outside process
*
* @return
*/
public Map getAppData() {
return this.appData;
}
public void setAppData(Map m) {
this.appData = m;
}
public Map getInstanceData() {
return instanceData;
}
protected Object jexlResolver(String expression, Map vars)
throws Exception {
if (expression == null) {
return null;
}
if (vars == null) {
return null;
}
Expression e = ExpressionFactory.createExpression(expression);
JexlContext jc = JexlHelper.createContext();
jc.setVars(vars);
return e.evaluate(jc);
}
}