1 45 package org.exolab.jms.server.net; 46 47 import java.net.InetAddress ; 48 import java.net.UnknownHostException ; 49 import java.util.Map ; 50 51 import org.exolab.jms.config.ConfigHelper; 52 import org.exolab.jms.config.Configuration; 53 import org.exolab.jms.config.ConnectionFactories; 54 import org.exolab.jms.config.Connector; 55 import org.exolab.jms.config.types.SchemeType; 56 import org.exolab.jms.net.connector.AbstractConnectionFactory; 57 import org.exolab.jms.net.orb.ORB; 58 import org.exolab.jms.net.uri.InvalidURIException; 59 import org.exolab.jms.net.uri.URI; 60 import org.exolab.jms.net.uri.URIHelper; 61 import org.exolab.jms.net.util.Properties; 62 63 64 70 abstract class AbstractConnectorCfg implements ConnectorCfg { 71 72 75 private final SchemeType _scheme; 76 77 80 private final Configuration _config; 81 82 83 89 public AbstractConnectorCfg(SchemeType scheme, Configuration config) { 90 if (scheme == null) { 91 throw new IllegalArgumentException ("Argument 'scheme' is null"); 92 } 93 if (config == null) { 94 throw new IllegalArgumentException ("Argument 'config' is null"); 95 } 96 _scheme = scheme; 97 _config = config; 98 } 99 100 105 public SchemeType getScheme() { 106 return _scheme; 107 } 108 109 116 public String getConnectURI() { 117 return getExportURI(); 118 } 119 120 125 public String getExportURI() { 126 return ConfigHelper.getServerURL(_scheme, _config); 127 } 128 129 134 public String getJNDIExportURI() { 135 return ConfigHelper.getJndiURL(_scheme, _config); 136 } 137 138 146 public String getAdminExportURI() { 147 return ConfigHelper.getAdminURL(_scheme, _config); 148 } 149 150 156 public Map getConnectProperties() { 157 Properties properties = getProperties(); 158 populateConnectProperties(properties); 159 return properties.getProperties(); 160 } 161 162 168 public Map getAcceptProperties() { 169 Properties properties = getProperties(); 170 populateAcceptProperties(properties); 171 return properties.getProperties(); 172 } 173 174 179 public ConnectionFactories getConnectionFactories() { 180 ConnectionFactories result = null; 181 Connector[] connectors = _config.getConnectors().getConnector(); 182 for (int i = 0; i < connectors.length; ++i) { 183 if (connectors[i].getScheme().equals(_scheme)) { 184 result = connectors[i].getConnectionFactories(); 185 break; 186 } 187 } 188 return result; 189 } 190 191 196 protected Configuration getConfiguration() { 197 return _config; 198 } 199 200 205 protected void populateConnectProperties(Properties properties) { 206 properties.set(ORB.PROVIDER_URI, getConnectURI()); 207 } 208 209 214 protected void populateAcceptProperties(Properties properties) { 215 properties.set(ORB.PROVIDER_URI, getExportURI()); 216 } 217 218 223 protected Properties getProperties() { 224 String prefix = AbstractConnectionFactory.PROPERTY_PREFIX; 225 if (_scheme.equals(SchemeType.EMBEDDED)) { 226 prefix += "vm"; 227 } else { 228 prefix += _scheme.toString(); 229 } 230 prefix += "."; 231 return new Properties(prefix); 232 } 233 234 242 protected URI getURI(String scheme, String host, int port) { 243 URI result; 244 try { 245 result = URIHelper.create(scheme, getHost(host), port); 246 } catch (InvalidURIException exception) { 247 throw new IllegalStateException ("Failed to create URI: " 248 + exception); 249 } 250 return result; 251 } 252 253 262 protected URI getURI(String scheme, String host, int port, 263 String path) { 264 URI result; 265 try { 266 result = URIHelper.create(scheme, getHost(host), port, path); 267 } catch (InvalidURIException exception) { 268 throw new IllegalStateException ("Failed to create URI: " 269 + exception); 270 } 271 return result; 272 } 273 274 282 protected String getHost(String host) { 283 if (host.equals("localhost")) { 284 try { 285 host = InetAddress.getLocalHost().getHostAddress(); 286 } catch (UnknownHostException ignore) { 287 } 288 } 289 return host; 290 } 291 292 299 protected URI getURI(String uri) { 300 URI result; 301 try { 302 result = URIHelper.parse(uri); 303 } catch (InvalidURIException exception) { 304 throw new IllegalStateException ("Failed to parse URI: " + uri); 305 } 306 return result; 307 } 308 309 } 310 | Popular Tags |