001: // ResourceContext.java
002: // $Id: ResourceContext.java,v 1.9 2000/08/16 21:37:53 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.resources;
007:
008: import java.util.Hashtable;
009:
010: /**
011: * The resource context.
012: */
013: public class ResourceContext {
014:
015: /**
016: * debug flag
017: */
018: public static boolean debug = false;
019:
020: /**
021: * Our parent context, if any.
022: */
023: protected ResourceContext parent = null;
024: /**
025: * The set of registered modules.
026: */
027: public Hashtable modules = null;
028:
029: /**
030: * Our Resource Space.
031: */
032: protected ResourceSpace space = null;
033:
034: /**
035: * The server of that resource context.
036: */
037: protected ServerInterface server = null;
038:
039: /**
040: * The ResourceReference of our resource.
041: */
042: protected ResourceReference reference = null;
043:
044: /**
045: * The ResourceReference of the resource container.
046: */
047: protected ResourceReference container = null;
048:
049: /**
050: * Get the container of the resource.
051: * @return A ResourceReference instance.
052: */
053: public ResourceReference getContainer() {
054: return container;
055: }
056:
057: /**
058: * Get the ResourceReference of the resource.
059: * @return a ResourceReference instance.
060: */
061: public ResourceReference getResourceReference() {
062: return reference;
063: }
064:
065: /**
066: * Set the ResourceReference of the resource.
067: * @param reference The ResourceReference to set.
068: */
069: public void setResourceReference(ResourceReference reference) {
070: this .reference = reference;
071: reference.updateContext(this );
072: }
073:
074: /**
075: * Get our Resource Space.
076: * @return A ResourceSpace instance.
077: */
078: public ResourceSpace getSpace() {
079: return space;
080: }
081:
082: /**
083: * Set the Resource Space.
084: * @param space Our Resource Space.
085: */
086: public void setSpace(ResourceSpace space) {
087: this .space = space;
088: }
089:
090: /**
091: * Get the server this context is attached to.
092: * @return An ServerInterface instance
093: * (guaranteed not to be <strong>null</strong>.)
094: */
095:
096: public ServerInterface getServer() {
097: return server;
098: }
099:
100: /**
101: * Get that context's ancestor.
102: * @return A ResourceContext instance, or <strong>null</strong>.
103: */
104:
105: public ResourceContext getParent() {
106: return parent;
107: }
108:
109: /**
110: * Register a module within that resource context.
111: * @param name The module's name.
112: * @param impl The module's implementation.
113: */
114:
115: public synchronized void registerModule(String name, Object impl) {
116: if (modules == null)
117: modules = new Hashtable(7);
118: modules.put(name, impl);
119: }
120:
121: /**
122: * Lookup a module within that resource context.
123: * @param name Name of the module to look for.
124: * @param inherited Also look within the contexts hierarchy for an
125: * inherited module having that name.
126: */
127:
128: public Object getModule(String name, boolean inherited) {
129: Object impl = ((modules == null) ? null : modules.get(name));
130: if (inherited && (parent != null) && (impl == null))
131: impl = parent.getModule(name, true);
132: return impl;
133: }
134:
135: /**
136: * Lookup a module within that context, and up the hierarchy of contexts.
137: * @param name The module's name.
138: * @return An object <em>implementing</em> that module.
139: */
140:
141: public Object getModule(String name) {
142: return getModule(name, true);
143: }
144:
145: public String toString() {
146: String tostring = "\nResourceContext : ";
147: if (parent == null)
148: tostring += "\n\tparent : null";
149: tostring += "\n\tcontainer : " + container;
150: tostring += "\n\tspace : " + space;
151: tostring += "\n\tserver : " + server;
152: return tostring;
153: }
154:
155: /**
156: * Create a ResourceContext from a container.
157: * @param container The resource container.
158: */
159: public ResourceContext(ContainerResource container) {
160: this .parent = container.getContext();
161: this .container = container.getResourceReference();
162: this .space = (parent != null) ? parent.space : null;
163: this .server = (parent != null) ? parent.server : null;
164: if ((this .container == null) && debug) {
165: System.out.println("[1] container has no Reference");
166: org.w3c.util.Trace.showTrace();
167: }
168: }
169:
170: /**
171: * Create a ResourceContext from a container ResourceReference.
172: * @param container The resource reference of the container.
173: * Must be an instance of ContainerResource.
174: */
175: public ResourceContext(ResourceReference rr_container) {
176: try {
177: Resource res = rr_container.lock();
178: this .parent = res.getContext();
179: this .container = rr_container;
180: this .space = (parent != null) ? parent.space : null;
181: this .server = (parent != null) ? parent.server : null;
182: } catch (InvalidResourceException ex) {
183: //should be valid
184: ex.printStackTrace();
185: } finally {
186: rr_container.unlock();
187: }
188: }
189:
190: /**
191: * Create a ResourceContext from the parent context.
192: * @param parent The parent resource context.
193: */
194: public ResourceContext(ResourceContext parent) {
195: this .parent = parent;
196: this .container = parent.getResourceReference();
197: this .space = parent.space;
198: this .server = parent.server;
199: if ((this .container == null) && debug) {
200: System.out.println("[2] parent context has no reference");
201: org.w3c.util.Trace.showTrace();
202: }
203: }
204:
205: /**
206: * Create a ResourceContext.
207: * @param space The ResourceSpace.
208: * @param server The server.
209: */
210: public ResourceContext(ResourceSpace space, ServerInterface server) {
211: this .server = server;
212: this .space = space;
213: this .container = null;
214: this .parent = null;
215: }
216:
217: /**
218: * Create a ResourceContext.
219: * @param server The server.
220: */
221: public ResourceContext(ServerInterface server) {
222: this.server = server;
223: this.space = null;
224: this.container = null;
225: this.parent = null;
226: }
227:
228: }
|