01: /*
02: * Copyright 2004,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.bsf.util;
18:
19: import java.util.Hashtable;
20:
21: /**
22: * The <em>ObjectRegistry</em> is used to do name-to-object reference lookups.
23: * If an <em>ObjectRegistry</em> is passed as a constructor argument, then this
24: * <em>ObjectRegistry</em> will be a cascading registry: when a lookup is
25: * invoked, it will first look in its own table for a name, and if it's not
26: * there, it will cascade to the parent <em>ObjectRegistry</em>.
27: * All registration is always local. [??]
28: *
29: * @author Sanjiva Weerawarana
30: * @author Matthew J. Duftler
31: */
32: public class ObjectRegistry {
33: Hashtable reg = new Hashtable();
34: ObjectRegistry parent = null;
35:
36: public ObjectRegistry() {
37: }
38:
39: public ObjectRegistry(ObjectRegistry parent) {
40: this .parent = parent;
41: }
42:
43: // lookup an object: cascade up if needed
44: public Object lookup(String name) throws IllegalArgumentException {
45: Object obj = reg.get(name);
46:
47: if (obj == null && parent != null) {
48: obj = parent.lookup(name);
49: }
50:
51: if (obj == null) {
52: throw new IllegalArgumentException("object '" + name
53: + "' not in registry");
54: }
55:
56: return obj;
57: }
58:
59: // register an object
60: public void register(String name, Object obj) {
61: reg.put(name, obj);
62: }
63:
64: // unregister an object (silent if unknown name)
65: public void unregister(String name) {
66: reg.remove(name);
67: }
68: }
|