KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > factory > ResourceLinkFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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
19 package org.apache.naming.factory;
20
21 import java.util.Hashtable JavaDoc;
22
23 import javax.naming.Context JavaDoc;
24 import javax.naming.Name JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.naming.RefAddr JavaDoc;
27 import javax.naming.Reference JavaDoc;
28 import javax.naming.spi.ObjectFactory JavaDoc;
29
30 import org.apache.naming.ResourceLinkRef;
31
32
33 /**
34  * <p>Object factory for resource links.</p>
35  *
36  * @author Remy Maucherat
37  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
38  */

39
40 public class ResourceLinkFactory
41     implements ObjectFactory JavaDoc {
42
43
44     // ----------------------------------------------------------- Constructors
45

46
47     // ------------------------------------------------------- Static Variables
48

49
50     /**
51      * Global naming context.
52      */

53     private static Context JavaDoc globalContext = null;
54
55
56     // --------------------------------------------------------- Public Methods
57

58
59     /**
60      * Set the global context (note: can only be used once).
61      *
62      * @param newGlobalContext new global context value
63      */

64     public static void setGlobalContext(Context JavaDoc newGlobalContext) {
65         if (globalContext != null)
66             return;
67         globalContext = newGlobalContext;
68     }
69
70
71     // -------------------------------------------------- ObjectFactory Methods
72

73
74     /**
75      * Create a new DataSource instance.
76      *
77      * @param obj The reference object describing the DataSource
78      */

79     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx,
80                                     Hashtable JavaDoc environment)
81         throws NamingException JavaDoc {
82         
83         if (!(obj instanceof ResourceLinkRef))
84             return null;
85
86         // Can we process this request?
87
Reference JavaDoc ref = (Reference JavaDoc) obj;
88
89         String JavaDoc type = ref.getClassName();
90
91         // Read the global ref addr
92
String JavaDoc globalName = null;
93         RefAddr JavaDoc refAddr = ref.get(ResourceLinkRef.GLOBALNAME);
94         if (refAddr != null) {
95             globalName = refAddr.getContent().toString();
96             Object JavaDoc result = null;
97             result = globalContext.lookup(globalName);
98             // FIXME: Check type
99
return result;
100         }
101
102         return (null);
103
104         
105     }
106
107
108 }
109
Popular Tags