/*
* $Id: Product.java,v 1.14 2002/09/16 08:05:02 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil;
import java.io.OutputStream;
import java.util.Locale;
import java.util.TimeZone;
import anvil.core.Any;
import anvil.core.AnyDouble;
import anvil.core.AnyString;
import anvil.script.Context;
import anvil.script.Function;
import anvil.script.Namespace;
import anvil.script.NamespaceImpl;
import anvil.script.Module;
import anvil.server.Address;
import anvil.server.Citizen;
/**
* Product is a product of data and logic. Data
* is given by the user (for instance setting global
* variables) and logic is taken from the script
* the document is using.
*
* @author: Jani Lehtim�ki
*/
public class Product
{
private Module _script;
private Context _context;
private NamespaceImpl _globals = new NamespaceImpl();
/**
* Constructs new Product.
*
* @param script Module
*/
public Product(Address address, OutputStream output, Citizen citizen, Module script)
{
_script = script;
_context = new Context(Thread.currentThread(), address.getZone(), citizen, _globals);
_context.setOutputStream(output);
}
/**
* Constructs new Product.
*
* @param script Module
*/
public Product(Address address, OutputStream output, Module script)
{
_script = script;
_context = new Context(Thread.currentThread(), address.getZone(), null, _globals);
_context.setOutputStream(output);
}
/**
* Constructs new Product.
*
* @param script Module
*/
public Product(Address address, Module script)
{
_script = script;
_context = new Context(Thread.currentThread(), address.getZone(), null, _globals);
}
/**
* Destroys this product and context associated with it.
*/
public void destroy()
{
_script = null;
_context.destroy();
_context = null;
_globals.destroy();
_globals = null;
}
/**
* Sets the output stream to write to.
*
* @param output Output stream
*/
public void setOutputStream(OutputStream output)
{
_context.setOutputStream(output);
}
/**
* Returns the script being used by this
* document.
*
* @return Module
*/
public Module getModule()
{
return _script;
}
/**
* Returns the global namespace used.
*
* @return Namespace
*/
public Namespace getGlobalNamespace()
{
return _globals;
}
/**
* Gets a variable from global namespace.
*
* @param name Name of variable
* @return Any, or <code>null</code> if it wasn't found
*/
public Any getGlobal(String name)
{
return _globals.getVariable(name);
}
/**
* Sets a variable to global namespace.
*
* @param name Name of variable
* @param value Value
* @return Instance of Any to where the value was wrapped.
*/
public Any setGlobal(String name, boolean value)
{
return _globals.setVariable(name, value ? Any.TRUE : Any.FALSE);
}
/**
* Sets a variable to global namespace.
*
* @param name Name of variable
* @param value Value
* @return Instance of Any to where the value was wrapped.
*/
public Any setGlobal(String name, int value)
{
return _globals.setVariable(name, Any.create(value));
}
/**
* Sets a variable to global namespace.
*
* @param name Name of variable
* @param value Value
* @return Instance of Any to where the value was wrapped.
*/
public Any setGlobal(String name, double value)
{
return _globals.setVariable(name, new AnyDouble(value));
}
/**
* Sets a variable to global namespace.
*
* @param name Name of variable
* @param value Value
* @return Instance of Any to where the value was wrapped.
*/
public Any setGlobal(String name, String value)
{
return _globals.setVariable(name, new AnyString(value));
}
/**
* Sets a variable to global namespace.
*
* @param name Name of variable
* @param value Value
* @return Instance of Any to where the value was wrapped.
*/
public Any setGlobal(String name, Any value)
{
return _globals.setVariable(name, value);
}
/**
* Sets a variable to global namespace.
*
* @param name Name of variable
* @param value Value
* @return Instance of Any to where the value was wrapped.
*/
public Any setGlobal(String name, Object value)
{
return _globals.setVariable(name, Any.create(value));
}
/**
* Sets the default language. Each keyword have entry
* with all possible languages used (for instance "en",
* "fi", "se")
*
* @param language Default language
*/
public void setLanguage(String language)
{
_context.setLanguage(language);
}
/**
* Sets the locale.
*
* @param locale Locale instance
*/
public final void setLocale(Locale locale)
{
_context.setLocale(locale);
}
/**
* Sets the timezone.
*
* @param timezone TimeZone instance
*/
public final void setTimeZone(TimeZone timezone)
{
_context.setTimeZone(timezone);
}
/**
* Calls given function.
*
* @param function Name of function to call
*/
public Any forge(String function) throws ForgingException
{
return _context.execute(_script, function, Any.ARRAY0);
}
public Any forge(String function, Object p0) throws ForgingException
{
Any[] p = new Any[]{
Any.create(p0)
};
return _context.execute(_script, function, p);
}
public Any forge(String function, Object p0, Object p1) throws ForgingException
{
Any[] p = new Any[]{
Any.create(p0),
Any.create(p1)
};
return _context.execute(_script, function, p);
}
public Any forge(String function, Object p0, Object p1, Object p2) throws ForgingException
{
Any[] p = new Any[]{
Any.create(p0),
Any.create(p1),
Any.create(p2)
};
return _context.execute(_script, function, p);
}
public Any forge(String function, Object p0, Object p1, Object p2, Object p3) throws ForgingException
{
Any[] p = new Any[]{
Any.create(p0),
Any.create(p1),
Any.create(p2),
Any.create(p3)
};
return _context.execute(_script, function, p);
}
public Any forge(String function, Object p0, Object p1, Object p2, Object p3, Object p4) throws ForgingException
{
Any[] p = new Any[]{
Any.create(p0),
Any.create(p1),
Any.create(p2),
Any.create(p3),
Any.create(p4)
};
return _context.execute(_script, function, p);
}
public Any forge(String function, Any[] parameters) throws ForgingException
{
return _context.execute(_script, function, parameters);
}
}