KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > registry > mock > MockContext


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: MockContext.java 10:14:53 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.registry.mock;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Map.Entry;
30
31 import javax.naming.Binding JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.Name JavaDoc;
34 import javax.naming.NameClassPair JavaDoc;
35 import javax.naming.NameParser JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 /**
39  * Mock object of the Context
40  *
41  * @author ddesjardins - eBMWebsourcing
42  */

43 public class MockContext implements Context JavaDoc {
44
45     private Map JavaDoc<String JavaDoc, Object JavaDoc> bindings = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
46
47     public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc {
48         return lookup(name.toString());
49     }
50
51     public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc {
52         return bindings.get(name);
53     }
54
55     public void bind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
56     }
57
58     public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
59         bindings.put(name, obj);
60     }
61
62     public void rebind(Name JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
63         bindings.put(name.toString(), obj);
64     }
65
66     public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc {
67         bindings.put(name, obj);
68     }
69
70     public void unbind(Name JavaDoc name) throws NamingException JavaDoc {
71         unbind(name.toString());
72     }
73
74     public void unbind(String JavaDoc name) throws NamingException JavaDoc {
75         if (name.indexOf("/") > -1) {
76             String JavaDoc sName = name.substring(0, name.indexOf("/"));
77             if (bindings.get(sName) instanceof MockContext) {
78                 ((MockContext) bindings.get(sName)).unbind(name.substring(name
79                     .indexOf("/") + 1));
80             }
81         } else {
82             bindings.remove(name);
83         }
84     }
85
86     public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc {
87     }
88
89     public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc {
90     }
91
92     public MockNamingEnumeration<NameClassPair JavaDoc> list(Name JavaDoc name)
93         throws NamingException JavaDoc {
94         return list(name.toString());
95     }
96
97     public MockNamingEnumeration<NameClassPair JavaDoc> list(String JavaDoc name)
98         throws NamingException JavaDoc {
99         Object JavaDoc obj = bindings.get(name);
100         MockNamingEnumeration<NameClassPair JavaDoc> namingEnumeration = new MockNamingEnumeration<NameClassPair JavaDoc>();
101         if (obj instanceof MockContext) {
102             List JavaDoc<NameClassPair JavaDoc> bindings = new ArrayList JavaDoc<NameClassPair JavaDoc>();
103             MockContext mockContext = (MockContext) obj;
104             for (Entry entry : mockContext.getBindings().entrySet()) {
105                 Binding JavaDoc binding = new Binding JavaDoc((String JavaDoc) entry.getKey(), entry
106                     .getValue());
107                 bindings.add(binding);
108             }
109             namingEnumeration.setElems(bindings);
110         }
111         return namingEnumeration;
112     }
113
114     public MockNamingEnumeration<Binding JavaDoc> listBindings(Name JavaDoc name)
115         throws NamingException JavaDoc {
116         return listBindings(name.toString());
117     }
118
119     public MockNamingEnumeration<Binding JavaDoc> listBindings(String JavaDoc name)
120         throws NamingException JavaDoc {
121         Object JavaDoc obj = bindings.get(name);
122         if (obj instanceof MockContext) {
123             MockNamingEnumeration<Binding JavaDoc> namingEnumeration = new MockNamingEnumeration<Binding JavaDoc>();
124             List JavaDoc<Binding JavaDoc> bindings = new ArrayList JavaDoc<Binding JavaDoc>();
125             MockContext mockContext = (MockContext) obj;
126             for (Entry entry : mockContext.getBindings().entrySet()) {
127                 Binding JavaDoc binding = new Binding JavaDoc((String JavaDoc) entry.getKey(), entry
128                     .getValue());
129                 bindings.add(binding);
130             }
131             namingEnumeration.setElems(bindings);
132             return namingEnumeration;
133         }
134         return null;
135     }
136
137     public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc {
138         bindings.remove(name.toString());
139     }
140
141     public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc {
142         bindings.remove(name);
143     }
144
145     public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc {
146         return createSubcontext(name.toString());
147     }
148
149     public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc {
150         MockContext context = new MockContext();
151         bindings.put(name, context);
152         return context;
153     }
154
155     public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc {
156         return null;
157     }
158
159     public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc {
160         return null;
161     }
162
163     public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc {
164         return null;
165     }
166
167     public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc {
168         return null;
169     }
170
171     public Name JavaDoc composeName(Name JavaDoc name, Name JavaDoc prefix) throws NamingException JavaDoc {
172         return null;
173     }
174
175     public String JavaDoc composeName(String JavaDoc name, String JavaDoc prefix)
176         throws NamingException JavaDoc {
177         return null;
178     }
179
180     public Object JavaDoc addToEnvironment(String JavaDoc propName, Object JavaDoc propVal)
181         throws NamingException JavaDoc {
182         return null;
183     }
184
185     public Object JavaDoc removeFromEnvironment(String JavaDoc propName) throws NamingException JavaDoc {
186         return null;
187     }
188
189     public Hashtable JavaDoc<?, ?> getEnvironment() throws NamingException JavaDoc {
190         return null;
191     }
192
193     public void close() throws NamingException JavaDoc {
194     }
195
196     public String JavaDoc getNameInNamespace() throws NamingException JavaDoc {
197         return null;
198     }
199
200     public Map JavaDoc<String JavaDoc, Object JavaDoc> getBindings() {
201         return bindings;
202     }
203
204     public void setBindings(Map JavaDoc<String JavaDoc, Object JavaDoc> bindings) {
205         this.bindings = bindings;
206     }
207
208 }
209
Popular Tags