PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/openjms-0.7.7-beta-1/modules/jms/src/main/java/org/exolab/jms/config/ConfigHelper.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 358 lines | 163 code | 25 blank | 170 comment | 48 complexity | 01cec3482ed11559461c97a5a2f56bfa MD5 | raw file
  1. /**
  2. * Redistribution and use of this software and associated documentation
  3. * ("Software"), with or without modification, are permitted provided
  4. * that the following conditions are met:
  5. *
  6. * 1. Redistributions of source code must retain copyright
  7. * statements and notices. Redistributions must also contain a
  8. * copy of this document.
  9. *
  10. * 2. Redistributions in binary form must reproduce the
  11. * above copyright notice, this list of conditions and the
  12. * following disclaimer in the documentation and/or other
  13. * materials provided with the distribution.
  14. *
  15. * 3. The name "Exolab" must not be used to endorse or promote
  16. * products derived from this Software without prior written
  17. * permission of Exoffice Technologies. For written permission,
  18. * please contact info@exolab.org.
  19. *
  20. * 4. Products derived from this Software may not be called "Exolab"
  21. * nor may "Exolab" appear in their names without prior written
  22. * permission of Exoffice Technologies. Exolab is a registered
  23. * trademark of Exoffice Technologies.
  24. *
  25. * 5. Due credit should be given to the Exolab Project
  26. * (http://www.exolab.org/).
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
  29. * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
  30. * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  31. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  32. * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  33. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  39. * OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * Copyright 2003-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
  42. *
  43. * $Id: ConfigHelper.java,v 1.8 2005/12/01 13:53:23 tanderson Exp $
  44. */
  45. package org.exolab.jms.config;
  46. import java.net.InetAddress;
  47. import java.net.UnknownHostException;
  48. import org.exolab.jms.config.types.SchemeType;
  49. /**
  50. * Helper class for interrogating the configuration.
  51. *
  52. * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
  53. * @version $Revision: 1.8 $ $Date: 2005/12/01 13:53:23 $
  54. */
  55. public class ConfigHelper {
  56. /**
  57. * Returns the server URL for the specified scheme.
  58. *
  59. * @param scheme the connector scheme
  60. * @param config the configuration to use
  61. * @return the server URL for the specified scheme
  62. */
  63. public static String getServerURL(SchemeType scheme,
  64. Configuration config) {
  65. String url = null;
  66. ServerConfiguration server = config.getServerConfiguration();
  67. if (scheme.equals(SchemeType.TCP)) {
  68. url = getServerURL(scheme, server.getHost(),
  69. config.getTcpConfiguration());
  70. } else if (scheme.equals(SchemeType.TCPS)) {
  71. url = getServerURL(scheme, server.getHost(),
  72. config.getTcpsConfiguration());
  73. } else if (scheme.equals(SchemeType.RMI)) {
  74. RmiConfiguration rmi = config.getRmiConfiguration();
  75. if (rmi.getEmbeddedRegistry()) {
  76. // if the registry is embedded within the OpenJMS server,
  77. // use the server host
  78. url = getServerURL(scheme, server.getHost(), rmi);
  79. } else {
  80. url = getServerURL(scheme, rmi.getRegistryHost(), rmi);
  81. }
  82. } else if (scheme.equals(SchemeType.HTTP)) {
  83. url = getServerURL(scheme, config.getHttpConfiguration());
  84. } else if (scheme.equals(SchemeType.HTTPS)) {
  85. url = getServerURL(scheme, config.getHttpsConfiguration());
  86. } else if (scheme.equals(SchemeType.EMBEDDED)) {
  87. url = "vm:openjms";
  88. }
  89. return url;
  90. }
  91. /**
  92. * Returns the embedded JNDI URL for the specified scheme.
  93. *
  94. * @param scheme the connector scheme
  95. * @param config the configuration to use
  96. * @return embedded JNDI URL for the specified scheme
  97. */
  98. public static String getJndiURL(SchemeType scheme, Configuration config) {
  99. String url = null;
  100. ServerConfiguration server = config.getServerConfiguration();
  101. if (scheme.equals(SchemeType.TCP)) {
  102. url = getJndiURL(scheme, server.getHost(),
  103. config.getTcpConfiguration());
  104. } else if (scheme.equals(SchemeType.TCPS)) {
  105. url = getJndiURL(scheme, server.getHost(),
  106. config.getTcpsConfiguration());
  107. } else if (scheme.equals(SchemeType.HTTP)) {
  108. url = getJndiURL(scheme, config.getHttpConfiguration());
  109. } else if (scheme.equals(SchemeType.HTTPS)) {
  110. url = getJndiURL(scheme, config.getHttpsConfiguration());
  111. } else if (scheme.equals(SchemeType.RMI)) {
  112. RmiConfiguration rmi = config.getRmiConfiguration();
  113. if (rmi.getEmbeddedRegistry()) {
  114. // if the registry is embedded within the OpenJMS server,
  115. // use the server host
  116. url = getJndiURL(scheme, server.getHost(), rmi);
  117. } else {
  118. url = getJndiURL(scheme, rmi.getRegistryHost(), rmi);
  119. }
  120. } else if (scheme.equals(SchemeType.EMBEDDED)) {
  121. url = "vm:openjms";
  122. }
  123. return url;
  124. }
  125. /**
  126. * Returns the server administration URL for the specified scheme.
  127. *
  128. * @param scheme the connector scheme
  129. * @param config the configuration to use
  130. * @return the server administration URL for the specified scheme
  131. */
  132. public static String getAdminURL(SchemeType scheme, Configuration config) {
  133. String url = null;
  134. ServerConfiguration server = config.getServerConfiguration();
  135. if (scheme.equals(SchemeType.TCP)) {
  136. url = getAdminURL(scheme, server.getHost(),
  137. config.getTcpConfiguration());
  138. } else if (scheme.equals(SchemeType.TCPS)) {
  139. url = getAdminURL(scheme, server.getHost(),
  140. config.getTcpsConfiguration());
  141. } else if (scheme.equals(SchemeType.RMI)) {
  142. RmiConfiguration rmi = config.getRmiConfiguration();
  143. if (rmi.getEmbeddedRegistry()) {
  144. // if the registry is embedded within the OpenJMS server,
  145. // use the server host
  146. url = getAdminURL(scheme, server.getHost(), rmi);
  147. } else {
  148. url = getAdminURL(scheme, rmi.getRegistryHost(), rmi);
  149. }
  150. } else if (scheme.equals(SchemeType.HTTP)) {
  151. url = getAdminURL(scheme, config.getHttpConfiguration());
  152. } else if (scheme.equals(SchemeType.HTTPS)) {
  153. url = getAdminURL(scheme, config.getHttpsConfiguration());
  154. } else if (scheme.equals(SchemeType.EMBEDDED)) {
  155. url = "vm:openjms";
  156. }
  157. return url;
  158. }
  159. /**
  160. * Returns the server URL for the TCP/TCPS connector.
  161. *
  162. * @param scheme the connector scheme
  163. * @param host the server host
  164. * @param config the TCP/TCPS configuration
  165. * @return the server URL for the TCP/TCPS connector
  166. */
  167. private static String getServerURL(SchemeType scheme, String host,
  168. TcpConfigurationType config) {
  169. return getURL(scheme, host, config.getInternalHost(), config.getPort());
  170. }
  171. /**
  172. * Returns the server URL for the RMI connector.
  173. *
  174. * @param scheme the connector scheme
  175. * @param host the server host
  176. * @param config the RMI configuration
  177. * @return the server URL for the RMI connector
  178. */
  179. private static String getServerURL(SchemeType scheme, String host,
  180. RmiConfiguration config) {
  181. return getURL(scheme, host, config.getRegistryPort());
  182. }
  183. /**
  184. * Returns the server URL for the HTTP/HTTPS connector.
  185. *
  186. * @param scheme the connector scheme
  187. * @param config the HTTP/HTTPS configuration
  188. * @return the server URL for the HTTP/HTTPS connector
  189. */
  190. private static String getServerURL(SchemeType scheme,
  191. HttpConfigurationType config) {
  192. return getURL(scheme, config.getWebServerHost(),
  193. config.getWebServerPort(), config.getServlet());
  194. }
  195. /**
  196. * Returns the embedded JNDI URL for the TCP/TCPS connector.
  197. *
  198. * @return the embedded JNDI URL for the TCP/TCPS connector
  199. */
  200. private static String getJndiURL(SchemeType scheme, String host,
  201. TcpConfigurationType config) {
  202. int port = config.getJndiPort();
  203. if (port == 0) {
  204. port = config.getPort();
  205. }
  206. return getURL(scheme, host, config.getInternalHost(), port);
  207. }
  208. /**
  209. * Returns the embedded JNDI URL for the RMI connector.
  210. *
  211. * @return the embedded JNDI URL for the RMI connector
  212. */
  213. private static String getJndiURL(SchemeType scheme, String host,
  214. RmiConfiguration config) {
  215. return getURL(scheme, host, config.getRegistryPort());
  216. }
  217. /**
  218. * Returns the embedded JNDI URL for the HTTP/HTTPS connector.
  219. *
  220. * @return the embedded JNDI URL for the HTTP/HTTPS connector
  221. */
  222. private static String getJndiURL(SchemeType scheme,
  223. HttpConfigurationType config) {
  224. return getURL(scheme, config.getWebServerHost(),
  225. config.getWebServerPort(), config.getServlet());
  226. }
  227. /**
  228. * Returns the admin URL for the TCP/TCPS connector.
  229. *
  230. * @return the admin URL for the TCP/TCPS connector
  231. */
  232. private static String getAdminURL(SchemeType scheme, String host,
  233. TcpConfigurationType config) {
  234. int port = config.getAdminPort();
  235. if (port == 0) {
  236. port = config.getPort();
  237. }
  238. return getURL(scheme, host, config.getInternalHost(), port);
  239. }
  240. /**
  241. * Returns the admin URL for the RMI connector.
  242. *
  243. * @return the admin URL for the RMI connector
  244. */
  245. private static String getAdminURL(SchemeType scheme, String host,
  246. RmiConfiguration config) {
  247. return getURL(scheme, host, config.getRegistryPort());
  248. }
  249. /**
  250. * Returns the admin URL for the HTTP/HTTPS connector.
  251. *
  252. * @return the admin URL for the HTTP/HTTPS connector
  253. */
  254. private static String getAdminURL(SchemeType scheme,
  255. HttpConfigurationType config) {
  256. return getURL(scheme, config.getWebServerHost(),
  257. config.getWebServerPort(), config.getServlet());
  258. }
  259. /**
  260. * Constructs a URL with no path.
  261. *
  262. * @param scheme the connector scheme
  263. * @param host the host
  264. * @param port the port
  265. * @return a URL formed from the concatenation of the arguments
  266. */
  267. private static String getURL(SchemeType scheme, String host, int port) {
  268. return getURL(scheme, host, port, "");
  269. }
  270. /**
  271. * Constructs a URL with a path.
  272. *
  273. * @param scheme the connector scheme
  274. * @param host the host
  275. * @param port the port
  276. * @param path the path
  277. * @return a URL formed from the concatenation of the arguments
  278. */
  279. private static String getURL(SchemeType scheme, String host, int port,
  280. String path) {
  281. return getURL(scheme.toString(), host, port, path);
  282. }
  283. /**
  284. * Constructs a URL with a path.
  285. *
  286. * @param scheme the connector scheme
  287. * @param host the host
  288. * @param port the port
  289. * @param path the path
  290. * @return a URL formed from the concatenation of the arguments
  291. */
  292. private static String getURL(String scheme, String host, int port,
  293. String path) {
  294. String result = scheme + "://" + getHost(host) + ":" + port;
  295. if (!path.startsWith("/")) {
  296. result += "/" + path;
  297. } else {
  298. result += path;
  299. }
  300. return result;
  301. }
  302. /**
  303. * Returns a URL with an alternative host encoded.
  304. *
  305. * @param scheme the connector scheme
  306. * @param host the server host
  307. * @return the server URL for the TCP/TCPS connector
  308. * @param altHost the alternative host. May be <code>null</code>
  309. * @param port the port
  310. * @return the URL
  311. */
  312. private static String getURL(SchemeType scheme, String host,
  313. String altHost, int port) {
  314. String url = getURL(scheme, host, port);
  315. if (altHost != null) {
  316. url += "?alt=" + altHost;
  317. }
  318. return url;
  319. }
  320. /**
  321. * Returns the host address, if the supplied host is localhost, else returns
  322. * it, unchanged.
  323. */
  324. private static String getHost(String host) {
  325. if (host.equals("localhost")) {
  326. try {
  327. host = InetAddress.getLocalHost().getHostAddress();
  328. } catch (UnknownHostException ignore) {
  329. }
  330. }
  331. return host;
  332. }
  333. }