//Source file: H:\\TEMP\\generated\\ispuml\\mdaTransformation\\rules\\CopyPrimitiveTypesAction.java
package modTransf.rules.xml;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import modTransf.rules.core.Guard;
import modTransf.rules.core.Action;
import modTransf.engine.RuleContext;
import modTransf.engine.TransformationException;
import javax.script.CompiledScript;
import modTransf.script.CompiledScriptFactory;
import javax.script.ScriptException;
import javax.script.ScriptContext;
import modTransf.rules.core.RuleContextWrapperForScriptContext;
import javax.script.ScriptEngine;
import modTransf.rules.core.GuardClause;
import modTransf.rules.core.ActionClause;
/**
* An action for outputting messages in a log mechanism.
* If no log mechanism is specified, the messages is output in the System.out.
* The method return the bean provided as parameter.
* Possible levels are in order:
* "trace", "debug", "info", "warn", "error", or "fatal".
* @todo Create a LogAction in core, and extends it in xml (like the other tags).
*/
public class LogTag implements GuardClause, ActionClause
{
/** The log level, as defined in commons-logging
* "trace", "debug", "info", "warn", "error", or "fatal"
*/
protected String level;
/** The log name to use */
protected String logName;
/** The log message */
protected String msg;
/** The language used in the msg */
protected String exprLanguage;
/** The compiled version of the message */
protected CompiledScript compiledScript;
/** The internal log used if any */
protected Log log;
/** The internal log used */
protected InternalLog internalLog;
/**
* @param bean
* @param request
* @return Object
* @throws TransformationException
*/
public boolean isAllowed(Object bean, RuleContext request)
throws TransformationException
{
execute(bean, request);
return true;
}
/**
* @param bean
* @param request
* @return Object
* @throws TransformationException
*/
public Object execute(Object bean, RuleContext request)
throws TransformationException
{
//System.out.println("******* LOG call ********");
if(internalLog==null)
{
internalLog = initInternalLog();
}
internalLog.log(bean, request);
return null;
}
protected InternalLog initInternalLog()
throws TransformationException
{
try
{
if( exprLanguage != null )
compiledScript = CompiledScriptFactory.compileScript(msg, exprLanguage);
else
compiledScript = new StringCompiledExpression(msg);
}
catch(ScriptException ex)
{
throw new TransformationException(ex);
}
if(logName==null)
{ // Output in the system.out
// Compute the message
if(level==null)
{
return new SystemOutLog();
}
else
{
return new SystemOutLevelLog();
}
}
else
{
if(level==null)
{
level = "fatal";
// use the specified log
}
log = LogFactory.getLog(logName);
if("trace".equalsIgnoreCase(level))
{
return new TraceLog();
}
else if("debug".equalsIgnoreCase(level))
{
return new DebugLog();
}
else if("info".equalsIgnoreCase(level))
{
return new InfoLog();
}
else if("warn".equalsIgnoreCase(level))
{
return new WarnLog();
}
else if("error".equalsIgnoreCase(level))
{
return new ErrorLog();
}
else if("fatal".equalsIgnoreCase(level))
{
return new FatalLog();
}
else
{
return new SystemOutLevelLog();
} // end if
}
}
/**
* Compute the message. The message can have runtime expressions.
* @return
*/
protected Object computeMsg(Object bean, RuleContext request)
throws TransformationException
{
try
{
ScriptContext scriptContext = new RuleContextWrapperForScriptContext(request);
return compiledScript.eval(scriptContext);
}
catch(ScriptException ex)
{
throw new TransformationException(ex);
}
}
/**
* Access method for the name property.
*
* @return the current value of the level property
*/
public String getLevel()
{
return level;
}
/**
* Sets the value of the level property.
*
* @param level the new value of the name property
*/
public void setLevel(String level)
{
this.level = level;
}
/**
* Access method for the msg property.
*
* @return the current value of the value property
*/
public String getMsg()
{
return msg;
}
/**
* Sets the value of the msg property.
*
* @param aValue the new value of the value property
*/
public void setMsg(String msg)
{
this.msg = msg;
}
/**
* Access method for the logName property.
*
* @return the current value of the scope property
*/
public String getLogName()
{
return logName;
}
/**
* Sets the value of the logName property.
*
* @param aScope the new value of the scope property
*/
public void setLogName(String logName)
{
this.logName = logName;
}
/**
* Inner class
*/
abstract class InternalLog
{
abstract void log(Object bean, RuleContext request)
throws TransformationException;
} // end inner class
/**
* Inner class
*/
class SystemOutLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
System.out.println(computeMsg(bean, request));
}
} // end inner class
/**
* Inner class
*/
class SystemOutLevelLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
System.out.println("["+level+"] "+computeMsg(bean, request));
}
} // end inner class
/**
* Inner class
*/
class TraceLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
if(log.isTraceEnabled())
{
log.trace(computeMsg(bean, request));
}
}
} // end inner class
/**
* Inner class
*/
class DebugLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
if(log.isDebugEnabled())
{
log.debug(computeMsg(bean, request));
}
}
} // end inner class
/**
* Inner class
*/
class InfoLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
if(log.isInfoEnabled())
{
log.info(computeMsg(bean, request));
}
}
} // end inner class
/**
* Inner class
*/
class WarnLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
if(log.isWarnEnabled())
{
log.warn(computeMsg(bean, request));
}
}
} // end inner class
/**
* Inner class
*/
class ErrorLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
if(log.isErrorEnabled())
{
log.error(computeMsg(bean, request));
}
}
} // end inner class
/**
* Inner class
*/
class FatalLog extends InternalLog
{
void log(Object bean, RuleContext request)
throws TransformationException
{
if(log.isFatalEnabled())
{
log.fatal(computeMsg(bean, request));
}
}
} // end inner class
public void setExprLanguage(String exprLanguage)
{
this.exprLanguage = exprLanguage;
}
/**
* engineFinish
*
* @param context RuleContext
*/
public void engineFinish(RuleContext context)
{
}
/**
* engineStart
*
* @param context RuleContext
*/
public void engineStart(RuleContext context)
{
}
/**
* setUpLocalVariables
*
* @param object Object
* @param context RuleContext
*/
public void setUpLocalVariables(Object object, RuleContext context)
{
}
/**
* A simple implementation returning the provided String as is.
*/
public class StringCompiledExpression extends CompiledScript
{
String msg;
StringCompiledExpression( String msg )
{
this.msg = msg;
}
/**
* eval
*
* @param scriptContext ScriptContext
* @return Object
*/
public Object eval(ScriptContext scriptContext)
{
return msg;
}
/**
* getEngine
*
* @return ScriptEngine
*/
public ScriptEngine getEngine()
{
throw new UnsupportedOperationException();
}
}
}