View Javadoc
1   /*
2    * Copyright 2009 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
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 tools.gsf.test.jndi;
18  
19  import javax.naming.Binding;
20  import javax.naming.Context;
21  import javax.naming.Name;
22  import javax.naming.NameClassPair;
23  import javax.naming.NameParser;
24  import javax.naming.NamingEnumeration;
25  import javax.naming.NamingException;
26  import java.util.HashMap;
27  import java.util.Hashtable;
28  import java.util.Map;
29  
30  /**
31   * @author Dolf Dijkstra
32   */
33  public class VerySimpleInitialContextFactory implements javax.naming.spi.InitialContextFactory {
34      private static Context initial;
35  
36      public VerySimpleInitialContextFactory() {
37          super();
38  
39      }
40  
41      @SuppressWarnings("unchecked")
42      public Context getInitialContext(final Hashtable<?, ?> environment) throws NamingException {
43          // System.out.println("getInitialContext" + environment);
44          if (initial == null) {
45              initial = new Context() {
46                  @SuppressWarnings("rawtypes")
47                  Hashtable env = new Hashtable(environment);
48                  Map<String, Object> store = new HashMap<String, Object>();
49  
50                  public Object addToEnvironment(String propName, Object propVal) throws NamingException {
51                      return env.put(propName, propVal);
52                  }
53  
54                  public void bind(Name name, Object obj) throws NamingException {
55                      store.put(name.toString(), obj);
56  
57                  }
58  
59                  public void bind(String name, Object obj) throws NamingException {
60                      store.put(name.toString(), obj);
61  
62                  }
63  
64                  public void close() throws NamingException {
65                      store.clear();
66  
67                  }
68  
69                  public Name composeName(Name name, Name prefix) throws NamingException {
70  
71                      return null;
72                  }
73  
74                  public String composeName(String name, String prefix) throws NamingException {
75  
76                      return null;
77                  }
78  
79                  public Context createSubcontext(Name name) throws NamingException {
80  
81                      return null;
82                  }
83  
84                  public Context createSubcontext(String name) throws NamingException {
85  
86                      return null;
87                  }
88  
89                  public void destroySubcontext(Name name) throws NamingException {
90  
91                  }
92  
93                  public void destroySubcontext(String name) throws NamingException {
94  
95                  }
96  
97                  public Hashtable<?, ?> getEnvironment() throws NamingException {
98                      return env;
99                  }
100 
101                 public String getNameInNamespace() throws NamingException {
102 
103                     return null;
104                 }
105 
106                 public NameParser getNameParser(Name name) throws NamingException {
107 
108                     return null;
109                 }
110 
111                 public NameParser getNameParser(String name) throws NamingException {
112 
113                     return null;
114                 }
115 
116                 public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
117 
118                     return null;
119                 }
120 
121                 public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
122 
123                     return null;
124                 }
125 
126                 public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
127 
128                     return null;
129                 }
130 
131                 public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
132 
133                     return null;
134                 }
135 
136                 public Object lookup(Name name) throws NamingException {
137                     return store.get(name.toString());
138                 }
139 
140                 public Object lookup(String name) throws NamingException {
141                     return store.get(name);
142                 }
143 
144                 public Object lookupLink(Name name) throws NamingException {
145 
146                     return null;
147                 }
148 
149                 public Object lookupLink(String name) throws NamingException {
150 
151                     return null;
152                 }
153 
154                 public void rebind(Name name, Object obj) throws NamingException {
155                     store.put(name.toString(), obj);
156 
157                 }
158 
159                 public void rebind(String name, Object obj) throws NamingException {
160                     store.put(name, obj);
161 
162                 }
163 
164                 public Object removeFromEnvironment(String propName) throws NamingException {
165 
166                     return null;
167                 }
168 
169                 public void rename(Name oldName, Name newName) throws NamingException {
170 
171                 }
172 
173                 public void rename(String oldName, String newName) throws NamingException {
174 
175                 }
176 
177                 public void unbind(Name name) throws NamingException {
178                     store.remove(name.toString());
179 
180                 }
181 
182                 public void unbind(String name) throws NamingException {
183                     store.remove(name);
184 
185                 }
186             };
187         }
188         return initial;
189     }
190 }