Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / tools / resources / ResourceContext.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/resources/ResourceContext.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/tools/resources/ResourceContext.java
new file mode 100644 (file)
index 0000000..c9b7387
--- /dev/null
@@ -0,0 +1,232 @@
+// ResourceContext.java\r
+// $Id: ResourceContext.java,v 1.1 2010/06/15 12:20:25 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996, 1997.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.tools.resources;\r
+\r
+import java.util.Hashtable;\r
+\r
+/**\r
+ * The resource context.\r
+ */\r
+public class ResourceContext {\r
+\r
+    /**\r
+     * debug flag\r
+     */\r
+    public static boolean debug = false;\r
+\r
+    /**\r
+     * Our parent context, if any.\r
+     */\r
+    protected ResourceContext parent = null;\r
+    /**\r
+     * The set of registered modules.\r
+     */\r
+    public Hashtable modules = null;\r
+\r
+    /**\r
+     * Our Resource Space.\r
+     */\r
+    protected ResourceSpace space = null;\r
+\r
+    /**\r
+     * The server of that resource context.\r
+     */\r
+    protected ServerInterface server = null;\r
+\r
+    /**\r
+     * The ResourceReference of our resource.\r
+     */\r
+    protected ResourceReference reference = null;\r
+\r
+    /**\r
+     * The ResourceReference of the resource container.\r
+     */\r
+    protected ResourceReference container = null;\r
+\r
+    /**\r
+     * Get the container of the resource.\r
+     * @return A ResourceReference instance.\r
+     */\r
+    public ResourceReference getContainer() {\r
+       return container;\r
+    }\r
+\r
+    /**\r
+     * Get the ResourceReference of the resource.\r
+     * @return a ResourceReference instance.\r
+     */\r
+    public ResourceReference getResourceReference() {\r
+       return reference;\r
+    }\r
+\r
+    /**\r
+     * Set the ResourceReference of the resource.\r
+     * @param reference The ResourceReference to set.\r
+     */\r
+    public void setResourceReference(ResourceReference reference) {\r
+       this.reference = reference;\r
+       reference.updateContext(this);\r
+    }\r
+\r
+    /**\r
+     * Get our Resource Space.\r
+     * @return A ResourceSpace instance.\r
+     */\r
+    public ResourceSpace getSpace() {\r
+       return space;\r
+    }\r
+\r
+    /**\r
+     * Set the Resource Space.\r
+     * @param space Our Resource Space.\r
+     */\r
+    public void setSpace(ResourceSpace space) {\r
+       this.space = space;\r
+    }\r
+\r
+    /**\r
+     * Get the server this context is attached to.\r
+     * @return An ServerInterface instance \r
+     * (guaranteed not to be <strong>null</strong>.)\r
+     */\r
+\r
+    public ServerInterface getServer() {\r
+       return server;\r
+    }\r
+\r
+    /**\r
+     * Get that context's ancestor.\r
+     * @return A ResourceContext instance, or <strong>null</strong>.\r
+     */\r
+\r
+    public ResourceContext getParent() {\r
+       return parent;\r
+    }\r
+\r
+    /**\r
+     * Register a module within that resource context.\r
+     * @param name The module's name.\r
+     * @param impl The module's implementation.\r
+     */\r
+\r
+    public synchronized void registerModule(String name, Object impl) {\r
+       if ( modules == null )\r
+           modules = new Hashtable(7);\r
+       modules.put(name, impl);\r
+    }\r
+\r
+    /**\r
+     * Lookup a module within that resource context.\r
+     * @param name Name of the module to look for.\r
+     * @param inherited Also look within the contexts hierarchy for an \r
+     * inherited module having that name.\r
+     */\r
+\r
+    public Object getModule(String name, boolean inherited) {\r
+       Object impl = ((modules == null) ? null : modules.get(name));\r
+       if (inherited && (parent != null) && (impl == null))\r
+           impl = parent.getModule(name, true);\r
+       return impl;\r
+    }\r
+\r
+    /**\r
+     * Lookup a module within that context, and up the hierarchy of contexts.\r
+     * @param name The module's name.\r
+     * @return An object <em>implementing</em> that module.\r
+     */\r
+\r
+    public Object getModule(String name) {\r
+       return getModule(name, true);\r
+    }\r
+\r
+    public String toString() {\r
+       String tostring = "\nResourceContext : ";\r
+       if (parent == null)\r
+           tostring += "\n\tparent    : null";\r
+       tostring += "\n\tcontainer : "+container;\r
+       tostring += "\n\tspace     : "+space;\r
+       tostring += "\n\tserver    : "+server;\r
+       return tostring;\r
+    }\r
+\r
+    /**\r
+     * Create a ResourceContext from a container.\r
+     * @param container The resource container.\r
+     */\r
+    public ResourceContext(ContainerResource container)\r
+    {\r
+       this.parent    = container.getContext();\r
+       this.container = container.getResourceReference();\r
+       this.space     = (parent != null) ? parent.space : null;\r
+       this.server    = (parent != null) ? parent.server : null;\r
+       if ((this.container == null) && debug) {\r
+           System.out.println("[1] container has no Reference");\r
+           org.w3c.util.Trace.showTrace();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Create a ResourceContext from a container ResourceReference.\r
+     * @param container The resource reference of the container. \r
+     * Must be an instance of ContainerResource.\r
+     */\r
+    public ResourceContext(ResourceReference rr_container) {\r
+       try {\r
+           Resource res = rr_container.lock();\r
+           this.parent    = res.getContext();\r
+           this.container = rr_container;\r
+           this.space     = (parent != null) ? parent.space : null;\r
+           this.server    = (parent != null) ? parent.server : null;\r
+       } catch (InvalidResourceException ex) {\r
+           //should be valid\r
+           ex.printStackTrace();\r
+       } finally {\r
+           rr_container.unlock();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Create a ResourceContext from the parent context.\r
+     * @param parent The parent resource context.\r
+     */\r
+    public ResourceContext(ResourceContext parent) \r
+    {\r
+       this.parent    = parent;\r
+       this.container = parent.getResourceReference();\r
+       this.space     = parent.space;\r
+       this.server    = parent.server;\r
+       if ((this.container == null) && debug) {\r
+           System.out.println("[2] parent context has no reference");\r
+           org.w3c.util.Trace.showTrace();\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Create a ResourceContext.\r
+     * @param space The ResourceSpace.\r
+     * @param server The server.\r
+     */\r
+    public ResourceContext(ResourceSpace space, ServerInterface server)\r
+    {\r
+       this.server    = server;\r
+       this.space     = space;\r
+       this.container = null;\r
+       this.parent    = null;\r
+    }\r
+\r
+    /**\r
+     * Create a ResourceContext.\r
+     * @param server The server.\r
+     */\r
+    public ResourceContext(ServerInterface server)\r
+    {\r
+       this.server    = server;\r
+       this.space     = null;\r
+       this.container = null;\r
+       this.parent    = null;\r
+    }\r
+\r
+}\r