/Users/lyon/j4p/src/javagroup/process/ProcessNamespace.java
|
1 /*
2 * Copyright (C) 1997 Luke Gorrie
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17 * MA 02139, USA.
18 */
19
20 package javagroup.process;
21
22 import javagroup.util.Namespace;
23
24 import java.util.Hashtable;
25
26 /**
27 * Namespace used for mapping per-process namespaces.
28 *
29 * @author Luke Gorrie
30 */
31 public class ProcessNamespace extends Namespace {
32
33 /**
34 * Reference to the ProcessManager the object is managing a namespace
35 * for.
36 */
37 protected ProcessManager _processManager;
38
39 /**
40 * Hashtable mapping processes to Namespaces *
41 */
42 protected Hashtable _processToNamespaceMap;
43
44 public ProcessNamespace(ProcessManager processManager) {
45 _processManager = processManager;
46 _processToNamespaceMap = new Hashtable();
47 }
48
49 public synchronized void registerNamespaceToProcess(
50 Namespace namespace,
51 JProcess process) {
52
53 // choose namespace for process
54 if (process != null) {
55 Namespace process_namespace = getNamespaceForProcess(process);
56 if (process_namespace != null) {
57 process_namespace.registerNamespace(namespace);
58 }
59 }
60
61 // if no process, use default namespace
62 else
63 super.registerNamespace(namespace);
64
65 }
66
67 public synchronized void registerNamespace(Namespace namespace) {
68 registerNamespaceToProcess(namespace,
69 _processManager.getCurrentProcess());
70 }
71
72 public synchronized void registerNamespaceForProcess(
73 Namespace namespace,
74 JProcess process) {
75 _processToNamespaceMap.put(process, namespace);
76 }
77
78 public synchronized Namespace getNamespaceForProcess(JProcess process) {
79
80 if (process == null)
81 return null;
82
83 // get namespace from map
84 Namespace process_namespace =
85 (Namespace) _processToNamespaceMap.get(process);
86
87 if (process_namespace == null) {
88 //try { Thread.sleep(100); } catch (Throwable t) {}
89 process_namespace = new Namespace();
90 _processToNamespaceMap.put(process, process_namespace);
91 //_processToNamespaceMap.put(System.out, process_namespace);
92 }
93
94 // null if no match
95 return process_namespace;
96 }
97
98 public synchronized void removeNamespaceForProcess(JProcess process) {
99
100 _processToNamespaceMap.remove(process);
101
102 }
103
104 public synchronized
105 void registerInstanceForClass(Class klass, Object instance) {
106 Namespace namespace =
107 getNamespaceForProcess(
108 _processManager.getCurrentProcess());
109
110 if (namespace != null)
111 namespace.registerInstanceForClass(klass, instance);
112
113 else
114 super.registerInstanceForClass(klass, instance);
115
116 }
117
118 public synchronized Object getInstanceForClass(Class klass) {
119
120 Namespace namespace =
121 getNamespaceForProcess(
122 _processManager.getCurrentProcess());
123
124 Object instance = null;
125
126 if (namespace != null)
127 instance = namespace.getInstanceForClass(klass);
128
129 if (instance == null)
130 instance = super.getInstanceForClass(klass);
131
132 return instance;
133
134 }
135
136 }
137
138