1 22 package org.objectweb.petals.jbi.registry.mock; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Map.Entry; 30 31 import javax.naming.Binding ; 32 import javax.naming.Context ; 33 import javax.naming.Name ; 34 import javax.naming.NameClassPair ; 35 import javax.naming.NameParser ; 36 import javax.naming.NamingException ; 37 38 43 public class MockContext implements Context { 44 45 private Map <String , Object > bindings = new HashMap <String , Object >(); 46 47 public Object lookup(Name name) throws NamingException { 48 return lookup(name.toString()); 49 } 50 51 public Object lookup(String name) throws NamingException { 52 return bindings.get(name); 53 } 54 55 public void bind(Name name, Object obj) throws NamingException { 56 } 57 58 public void bind(String name, Object obj) throws NamingException { 59 bindings.put(name, obj); 60 } 61 62 public void rebind(Name name, Object obj) throws NamingException { 63 bindings.put(name.toString(), obj); 64 } 65 66 public void rebind(String name, Object obj) throws NamingException { 67 bindings.put(name, obj); 68 } 69 70 public void unbind(Name name) throws NamingException { 71 unbind(name.toString()); 72 } 73 74 public void unbind(String name) throws NamingException { 75 if (name.indexOf("/") > -1) { 76 String 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 oldName, Name newName) throws NamingException { 87 } 88 89 public void rename(String oldName, String newName) throws NamingException { 90 } 91 92 public MockNamingEnumeration<NameClassPair > list(Name name) 93 throws NamingException { 94 return list(name.toString()); 95 } 96 97 public MockNamingEnumeration<NameClassPair > list(String name) 98 throws NamingException { 99 Object obj = bindings.get(name); 100 MockNamingEnumeration<NameClassPair > namingEnumeration = new MockNamingEnumeration<NameClassPair >(); 101 if (obj instanceof MockContext) { 102 List <NameClassPair > bindings = new ArrayList <NameClassPair >(); 103 MockContext mockContext = (MockContext) obj; 104 for (Entry entry : mockContext.getBindings().entrySet()) { 105 Binding binding = new Binding ((String ) entry.getKey(), entry 106 .getValue()); 107 bindings.add(binding); 108 } 109 namingEnumeration.setElems(bindings); 110 } 111 return namingEnumeration; 112 } 113 114 public MockNamingEnumeration<Binding > listBindings(Name name) 115 throws NamingException { 116 return listBindings(name.toString()); 117 } 118 119 public MockNamingEnumeration<Binding > listBindings(String name) 120 throws NamingException { 121 Object obj = bindings.get(name); 122 if (obj instanceof MockContext) { 123 MockNamingEnumeration<Binding > namingEnumeration = new MockNamingEnumeration<Binding >(); 124 List <Binding > bindings = new ArrayList <Binding >(); 125 MockContext mockContext = (MockContext) obj; 126 for (Entry entry : mockContext.getBindings().entrySet()) { 127 Binding binding = new Binding ((String ) 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 name) throws NamingException { 138 bindings.remove(name.toString()); 139 } 140 141 public void destroySubcontext(String name) throws NamingException { 142 bindings.remove(name); 143 } 144 145 public Context createSubcontext(Name name) throws NamingException { 146 return createSubcontext(name.toString()); 147 } 148 149 public Context createSubcontext(String name) throws NamingException { 150 MockContext context = new MockContext(); 151 bindings.put(name, context); 152 return context; 153 } 154 155 public Object lookupLink(Name name) throws NamingException { 156 return null; 157 } 158 159 public Object lookupLink(String name) throws NamingException { 160 return null; 161 } 162 163 public NameParser getNameParser(Name name) throws NamingException { 164 return null; 165 } 166 167 public NameParser getNameParser(String name) throws NamingException { 168 return null; 169 } 170 171 public Name composeName(Name name, Name prefix) throws NamingException { 172 return null; 173 } 174 175 public String composeName(String name, String prefix) 176 throws NamingException { 177 return null; 178 } 179 180 public Object addToEnvironment(String propName, Object propVal) 181 throws NamingException { 182 return null; 183 } 184 185 public Object removeFromEnvironment(String propName) throws NamingException { 186 return null; 187 } 188 189 public Hashtable <?, ?> getEnvironment() throws NamingException { 190 return null; 191 } 192 193 public void close() throws NamingException { 194 } 195 196 public String getNameInNamespace() throws NamingException { 197 return null; 198 } 199 200 public Map <String , Object > getBindings() { 201 return bindings; 202 } 203 204 public void setBindings(Map <String , Object > bindings) { 205 this.bindings = bindings; 206 } 207 208 } 209 | Popular Tags |