001: /*
002: * $Id: AnyNamespace.java,v 1.25 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.runtime;
011:
012: import java.io.IOException;
013: import java.io.Writer;
014: import anvil.java.util.BindingEnumeration;
015: import anvil.core.Any;
016: import anvil.core.AnyAbstractClass;
017: import anvil.script.Context;
018: import anvil.script.Function;
019: import anvil.script.Namespace;
020: import anvil.script.Type;
021: import anvil.core.Serializer;
022: import anvil.core.Unserializer;
023: import anvil.core.UnserializationException;
024: import anvil.server.PersistentNamespace;
025:
026: /// @class Namespace
027: /// Namespace is a wrapper for namespaces.
028: /// There are two categories of namespaces,
029: /// server namespace, declared in server's configuration
030: /// and <code>global</code> namespace, always present thread
031: /// specific namespace.
032:
033: /**
034: * class AnyNamespace
035: *
036: * @author: Jani Lehtimäki
037: */
038: public class AnyNamespace extends AnyAbstractClass {
039:
040: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
041: "Namespace",
042: AnyNamespace.class,
043: //DOC{{
044: ""
045: + " @class Namespace\n"
046: + " Namespace is a wrapper for namespaces.\n"
047: + " There are two categories of namespaces,\n"
048: + " server namespace, declared in server's configuration\n"
049: + " and <code>global</code> namespace, always present thread\n"
050: + " specific namespace.\n"
051: + " @method commit\n"
052: + " Commits changes made to this namespace, if applicable.\n"
053: + " @synopsis Namespace commit()\n"
054: //}}DOC
055: );
056: static {
057: RuntimeModule.class.getName();
058: }
059:
060: private Namespace _namespace;
061: private String _name;
062: private boolean _global;
063:
064: public AnyNamespace(Namespace namespace) {
065: _namespace = namespace;
066: _name = namespace.getName();
067: _global = namespace instanceof Context;
068: }
069:
070: public final anvil.script.ClassType classOf() {
071: return __class__;
072: }
073:
074: public String toString() {
075: return "Namespace(" + _namespace.toString() + ")";
076: }
077:
078: public Writer toAnvil(Writer writer) throws IOException {
079: writer.write(_namespace.getName());
080: return writer;
081: }
082:
083: public Object toObject() {
084: return _namespace;
085: }
086:
087: public Any getAttribute(Context context, String attribute) {
088: return _namespace.getVariable(attribute);
089: }
090:
091: public Any checkAttribute(Context context, String attribute) {
092: return getAttribute(context, attribute);
093: }
094:
095: public Any setAttribute(Context context, String attribute, Any value) {
096: if (!_global) {
097: context.checkNamespace(_name, true);
098: }
099: return _namespace.setVariable(attribute, value);
100: }
101:
102: public boolean deleteAttribute(Context context, String attribute) {
103: if (!_global) {
104: context.checkNamespace(_name, true);
105: }
106: return _namespace.deleteVariable(attribute);
107: }
108:
109: public Any getReference(Context context, Any index) {
110: return getAttribute(context, index.toString());
111: }
112:
113: public Any checkReference(Context context, Any index) {
114: return getAttribute(context, index.toString());
115: }
116:
117: public Any setReference(Context context, Any index, Any value) {
118: return setAttribute(context, index.toString(), value);
119: }
120:
121: public Any setReference(Context context, Any value) {
122: return this ;
123: }
124:
125: public boolean deleteReference(Context context, Any index) {
126: return deleteAttribute(context, index.toString());
127: }
128:
129: public BindingEnumeration enumeration() {
130: return _namespace.getVariables();
131: }
132:
133: public final void serialize(Serializer serializer)
134: throws IOException {
135: if (serializer.register(this )) {
136: return;
137: }
138: serializer.write('N');
139: serializer.write(_namespace.getName());
140: }
141:
142: public static final Any unserialize(Unserializer unserializer)
143: throws UnserializationException {
144: unserializer.consume('s');
145: String name = unserializer.getUTF16String();
146: Any ns = unserializer.getContext().getNS(name);
147: unserializer.register(ns);
148: return ns;
149: }
150:
151: /// @method commit
152: /// Commits changes made to this namespace, if applicable.
153: /// @synopsis Namespace commit()
154: public Any m_commit(Context context) {
155: if (!_global) {
156: context.checkNamespace(_name, true);
157: if (_namespace instanceof PersistentNamespace) {
158: ((PersistentNamespace) _namespace).commit();
159: }
160: }
161: return this;
162: }
163:
164: }
|