1 25 package org.objectweb.carol.util.configuration; 26 27 import java.util.ArrayList ; 28 import java.util.Hashtable ; 29 import java.util.List ; 30 import java.util.Properties ; 31 import java.util.StringTokenizer ; 32 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NameClassPair ; 36 import javax.naming.NamingEnumeration ; 37 import javax.naming.NamingException ; 38 39 43 public class ProtocolConfigurationImpl implements ProtocolConfiguration, ProtocolConfigurationImplMBean { 44 45 48 private String name = null; 49 50 53 private Protocol protocol = null; 54 55 58 private Properties properties = null; 59 60 63 private Hashtable jndiEnv = null; 64 65 68 private String host = null; 69 70 73 private int port = 0; 74 75 78 private String objectName = null; 79 80 87 public ProtocolConfigurationImpl(String name, Protocol protocol, Properties properties) throws ConfigurationException { 88 this.name = name; 89 this.protocol = protocol; 90 91 configure(properties); 92 } 93 94 95 100 public void configure(Properties properties) throws ConfigurationException { 101 if (properties == null) { 102 throw new ConfigurationException("Cannot build a configuration with a null properties object"); 103 } 104 this.properties = properties; 105 106 107 extractJNDIProperties(); 109 110 parseURL(); 112 } 113 114 118 protected void extractJNDIProperties() throws ConfigurationException { 119 jndiEnv = new Hashtable (); 120 String prefixProtocol = CarolDefaultValues.CAROL_PREFIX + "." + protocol.getName() + "."; 121 122 jndiEnv.put(Context.PROVIDER_URL, getValue(prefixProtocol + CarolDefaultValues.URL_PREFIX)); 123 jndiEnv.put(Context.INITIAL_CONTEXT_FACTORY, protocol.getInitialContextFactoryClassName()); 124 } 125 126 127 131 protected void parseURL() throws ConfigurationException { 132 String url = getProviderURL(); 133 port = getPortOfUrl(url); 134 host = getHostOfUrl(url); 135 } 136 137 143 public Context getInitialContext(Hashtable env) throws NamingException { 144 Hashtable newEnv = (Hashtable ) env.clone(); 145 newEnv.putAll(jndiEnv); 146 return new InitialContext (newEnv); 147 } 148 149 152 public Protocol getProtocol() { 153 return protocol; 154 } 155 156 159 public String getName() { 160 return name; 161 } 162 163 166 public Properties getProperties() { 167 return (Properties ) properties.clone(); 168 } 169 170 173 public String getHost() { 174 return host; 175 } 176 177 180 public int getPort() { 181 return port; 182 } 183 184 187 public String getProviderURL() { 188 return (String ) jndiEnv.get(Context.PROVIDER_URL); 189 } 190 191 198 protected int getPortOfUrl(String url) throws ConfigurationException { 199 int portNumber = 0; 200 try { 201 StringTokenizer st = new StringTokenizer (url, ":"); 202 st.nextToken(); 203 st.nextToken(); 204 if (st.hasMoreTokens()) { 205 StringTokenizer lastst = new StringTokenizer (st.nextToken(), "/"); 206 String pts = lastst.nextToken().trim(); 207 int i = pts.indexOf(','); 208 if (i > 0) { 209 pts = pts.substring(0, i); 210 } 211 portNumber = new Integer (pts).intValue(); 212 } 213 return portNumber; 214 } catch (Exception e) { 215 throw new ConfigurationException("Invalid URL '" + url + "'. It should be on the format <protocol>://<hostname>:<port>"); 217 } 218 } 219 220 227 protected String getHostOfUrl(String url) throws ConfigurationException { 228 String host = null; 229 try { 231 String [] tmpSplitStr = url.split(":"); 233 234 String tmpHost = tmpSplitStr[1]; 237 238 String [] tmpSplitHost = tmpHost.split("/"); 240 241 host = tmpSplitHost[tmpSplitHost.length - 1]; 243 } catch (Exception e) { 244 throw new ConfigurationException("Invalid URL '" + url + "'. It should be on the format <protocol>://<hostname>:<port>"); 246 } 247 return host; 248 } 249 250 251 257 protected String getValue(String key) throws ConfigurationException { 258 String s = properties.getProperty(key); 260 if (s == null) { 261 throw new ConfigurationException("Property '" + key + "' was not found in the properties object of the protocol, properties are :'" + properties + "'"); 262 } 263 return s; 264 } 265 266 267 268 271 public Hashtable getJndiEnv() { 272 return jndiEnv; 273 } 274 275 279 public String getInitialContextFactoryClassName() { 280 return protocol.getInitialContextFactoryClassName(); 281 } 282 283 288 public List getNames() throws NamingException { 289 Context ctx = getInitialContext(getJndiEnv()); 290 291 List names = new ArrayList (); 292 NamingEnumeration ne = ctx.list(""); 293 String n = null; 294 while (ne.hasMore()) { 295 NameClassPair ncp = (NameClassPair ) ne.next(); 296 n = ncp.getName(); 297 names.add(n); 298 } 299 return names; 300 } 301 302 305 public String getobjectName() { 306 return objectName; 307 } 308 309 313 public void setobjectName(String name) { 314 this.objectName = name; 315 } 316 317 320 public boolean iseventProvider() { 321 return false; 322 } 323 324 328 public boolean isstateManageable() { 329 return false; 330 } 331 332 336 public boolean isstatisticsProvider() { 337 return false; 338 } 339 340 } 341 | Popular Tags |