01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.naming.factory;
19:
20: import java.util.Hashtable;
21:
22: import javax.naming.Context;
23: import javax.naming.Name;
24: import javax.naming.NamingException;
25: import javax.naming.RefAddr;
26: import javax.naming.Reference;
27: import javax.naming.spi.ObjectFactory;
28:
29: import org.apache.naming.ResourceLinkRef;
30:
31: /**
32: * <p>Object factory for resource links.</p>
33: *
34: * @author Remy Maucherat
35: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
36: */
37:
38: public class ResourceLinkFactory implements ObjectFactory {
39:
40: // ----------------------------------------------------------- Constructors
41:
42: // ------------------------------------------------------- Static Variables
43:
44: /**
45: * Global naming context.
46: */
47: private static Context globalContext = null;
48:
49: // --------------------------------------------------------- Public Methods
50:
51: /**
52: * Set the global context (note: can only be used once).
53: *
54: * @param newGlobalContext new global context value
55: */
56: public static void setGlobalContext(Context newGlobalContext) {
57: if (globalContext != null)
58: return;
59: globalContext = newGlobalContext;
60: }
61:
62: // -------------------------------------------------- ObjectFactory Methods
63:
64: /**
65: * Create a new DataSource instance.
66: *
67: * @param obj The reference object describing the DataSource
68: */
69: public Object getObjectInstance(Object obj, Name name,
70: Context nameCtx, Hashtable environment)
71: throws NamingException {
72:
73: if (!(obj instanceof ResourceLinkRef))
74: return null;
75:
76: // Can we process this request?
77: Reference ref = (Reference) obj;
78:
79: String type = ref.getClassName();
80:
81: // Read the global ref addr
82: String globalName = null;
83: RefAddr refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
84: if (refAddr != null) {
85: globalName = refAddr.getContent().toString();
86: Object result = null;
87: result = globalContext.lookup(globalName);
88: // FIXME: Check type
89: return result;
90: }
91:
92: return (null);
93:
94: }
95:
96: }
|