PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jboss-5.1.0/varia/src/main/org/jboss/services/binding/ServiceBindingManager.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 1052 lines | 333 code | 50 blank | 669 comment | 22 complexity | a9a3eed48f0be62f9633b34d32068c89 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.services.binding;
  23. import java.net.InetAddress;
  24. import java.net.URL;
  25. import java.net.UnknownHostException;
  26. import java.util.Set;
  27. import org.jboss.services.binding.impl.SimpleServiceBindingValueSourceImpl;
  28. import org.jboss.services.binding.impl.StringReplacementServiceBindingValueSourceImpl;
  29. import org.jboss.services.binding.impl.Util;
  30. import org.jboss.services.binding.impl.XSLTServiceBindingValueSourceConfig;
  31. import org.jboss.services.binding.impl.XSLTServiceBindingValueSourceImpl;
  32. import org.w3c.dom.Element;
  33. /**
  34. * The services configuration binding manager implementation.
  35. *
  36. * <p>The ServiceBindingManager enables the centralized management
  37. * of ports, by service. The port configuration store is abstracted out
  38. * using the ServiceBindingStore interface.
  39. *
  40. * @version $Revision: 88905 $
  41. * @author <a href="mailto:bitpushr@rochester.rr.com">Mike Finn</a>
  42. * @author Scott.Stark@jboss.org
  43. * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
  44. * @author Brian Stansberry
  45. *
  46. * @jmx:mbean
  47. */
  48. public class ServiceBindingManager
  49. implements ServiceBindingManagerMBean
  50. {
  51. // ----------------------------------------------------------------- Static
  52. /** Enumeration of types of binding requests */
  53. public enum BindingType { INT, INETADDRESS, STRING, ELEMENT, URL, RESOURCE, GENERIC };
  54. /**
  55. * Algorithm for obtaining a {@link ServiceBindingValueSource} given a particular
  56. * binding and binding type.
  57. *
  58. * @param binding the binding
  59. * @param bindingType the binding type
  60. * @return the appropriate {@link ServiceBindingValueSource}. Will not return <code>null</code>.
  61. *
  62. * @throws ClassNotFoundException if any {@link ServiceBinding#getServiceBindingValueSourceClassName()} cannot be found
  63. * @throws InstantiationException if any {@link ServiceBinding#getServiceBindingValueSourceClassName()} cannot be instantiated
  64. * @throws IllegalAccessException if any {@link ServiceBinding#getServiceBindingValueSourceClassName()} is not public
  65. * @throws IllegalStateException if no appropriate ServiceBindingValueSource can be identified
  66. */
  67. public static ServiceBindingValueSource getServiceBindingValueSource(ServiceBinding binding, BindingType bindingType)
  68. {
  69. ServiceBindingValueSource source = binding.getServiceBindingValueSource();
  70. if (source == null)
  71. {
  72. switch (bindingType)
  73. {
  74. case INT:
  75. case INETADDRESS:
  76. source = new SimpleServiceBindingValueSourceImpl();
  77. break;
  78. case STRING:
  79. source = new StringReplacementServiceBindingValueSourceImpl();
  80. break;
  81. case ELEMENT:
  82. case URL:
  83. case RESOURCE:
  84. Object config = binding.getServiceBindingValueSourceConfig();
  85. if (config instanceof XSLTServiceBindingValueSourceConfig)
  86. {
  87. source = new XSLTServiceBindingValueSourceImpl();
  88. }
  89. else
  90. {
  91. source = new StringReplacementServiceBindingValueSourceImpl();
  92. }
  93. break;
  94. default:
  95. throw new IllegalStateException("No ServiceBindingValueSource configured for " +
  96. binding + " and no default source available for binding of type " +
  97. bindingType);
  98. }
  99. }
  100. return source;
  101. }
  102. // ---------------------------------------------------------------- Fields
  103. /**
  104. * The name of the config set this manager is associated with. This is a
  105. * logical name used to lookup ServiceBindings from the ServiceBindingStore.
  106. */
  107. private String serverName;
  108. /** The ServiceBindingStore instance */
  109. private final ServiceBindingStore store;
  110. // ----------------------------------------------------------- Constructors
  111. public ServiceBindingManager(String serverName, ServiceBindingStore store)
  112. {
  113. if (serverName == null)
  114. throw new IllegalArgumentException("serverName is null");
  115. if (store == null)
  116. throw new IllegalArgumentException("store is null");
  117. this.serverName = serverName;
  118. this.store = store;
  119. }
  120. // ------------------------------------------------------------- Properties
  121. /**
  122. * Gets the value of the <code>serverName</code> param this instance should pass
  123. * to <code>ServiceBindingStore</code> when
  124. * {@link ServiceBindingStore#getServiceBinding(String, String, String) requesting bindings}.
  125. *
  126. * @return name of the set of bindings this server uses
  127. *
  128. * @jmx:attribute
  129. */
  130. public String getServerName()
  131. {
  132. return this.serverName;
  133. }
  134. /**
  135. * Sets the value of the <code>serverName</code> param this instance should pass
  136. * to <code>ServiceBindingStore</code> when
  137. * {@link ServiceBindingStore#getServiceBinding(String, String, String) requesting bindings}.
  138. *
  139. * @return name of the set of bindings this server uses. Cannot be <code>null</code>
  140. *
  141. * @throws IllegalArgumentException if <code>serverName</code> is <code>null</code>
  142. */
  143. public void setServerName(String serverName)
  144. {
  145. if (serverName == null)
  146. {
  147. throw new IllegalArgumentException("serverName is null");
  148. }
  149. this.serverName = serverName;
  150. }
  151. public Set<ServiceBinding> getServiceBindings()
  152. {
  153. return this.store.getServiceBindings(this.serverName);
  154. }
  155. // ----------------------------------------------------------------- Public
  156. /**
  157. * Gets the <code>int</code> binding value for the
  158. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  159. * and no binding name qualifier.
  160. * <p>
  161. * This is typically the {@link ServiceBinding#getPort() port}.
  162. * </p>
  163. *
  164. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  165. * to identify the appropriate binding. Cannot be <code>null</code>.
  166. *
  167. * @return the binding value as an <code>int</code>
  168. *
  169. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  170. *
  171. * @see IntServiceBindingValueSource
  172. */
  173. public int getIntBinding(String serviceName) throws NoSuchBindingException
  174. {
  175. return getIntBinding(serviceName, null);
  176. }
  177. /**
  178. * Gets the <code>int</code> binding value for the
  179. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  180. * and <code>bindingName</code> qualifier.
  181. * <p>
  182. * This is typically the {@link ServiceBinding#getPort() port}.
  183. * </p>
  184. *
  185. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  186. * to identify the appropriate binding. Cannot be <code>null</code>.
  187. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  188. * to identify the appropriate binding. May be <code>null</code>.
  189. *
  190. * @return the binding value as an <code>int</code>
  191. *
  192. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  193. *
  194. * @see IntServiceBindingValueSource
  195. */
  196. public int getIntBinding(String serviceName, String bindingName) throws NoSuchBindingException
  197. {
  198. ServiceBinding binding = store.getServiceBinding(serverName, serviceName, bindingName);
  199. ServiceBindingValueSource source = getServiceBindingValueSource(binding, BindingType.INT);
  200. if (source instanceof IntServiceBindingValueSource)
  201. {
  202. return ((IntServiceBindingValueSource) source).getIntServiceBindingValue(binding);
  203. }
  204. else
  205. {
  206. return Util.getBindingValue(source, binding, Number.class).intValue();
  207. }
  208. }
  209. /**
  210. * Same as {@link #getIntBinding(String, String)} but, if no matching
  211. * service binding is found, creates a new one using the given
  212. * <code>hostName</code> and <code>basePort</code>.
  213. *
  214. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  215. * to identify the appropriate binding. Cannot be <code>null</code>.
  216. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  217. * to identify the appropriate binding. May be <code>null</code>.
  218. * @param hostName Host name to use for new service binding if one is
  219. * created.
  220. * @param basePort base port to use for the binding; ServiceBindingStore
  221. * may adjust this.
  222. *
  223. * @return the binding value as an <code>int</code>
  224. *
  225. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  226. * to create same binding with different
  227. * binding values
  228. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  229. */
  230. public int getIntBinding(String serviceName, String bindingName,
  231. String hostName, int basePort) throws UnknownHostException, DuplicateServiceException
  232. {
  233. return getIntBinding(serviceName, bindingName, hostName, basePort, false, hostName != null);
  234. }
  235. /**
  236. * Same as {@link #getIntBinding(String, String)} but, if no matching
  237. * service binding is found, creates a new one using the given
  238. * <code>hostName</code> and <code>basePort</code>.
  239. *
  240. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  241. * to identify the appropriate binding. Cannot be <code>null</code>.
  242. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  243. * to identify the appropriate binding. May be <code>null</code>.
  244. * @param hostName Host name to use for new service binding if one is
  245. * created.
  246. * @param basePort base port to use for the binding; ServiceBindingStore
  247. * may adjust this.
  248. * @param fixedPort whether runtime @{link ServiceBinding}s created from this
  249. * metadata can alter the port value based on the server
  250. * on which the binding is running.
  251. * @param fixedHostName whether runtime @{link ServiceBinding}s created from
  252. * this metadata can alter the hostName value based on
  253. * the server on which the binding is running.
  254. *
  255. * @return the binding value as an <code>int</code>
  256. *
  257. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  258. * to create same binding with different
  259. * binding values
  260. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  261. */
  262. public int getIntBinding(String serviceName, String bindingName,
  263. String hostName, int basePort, boolean fixedPort, boolean fixedHostName) throws UnknownHostException, DuplicateServiceException
  264. {
  265. try
  266. {
  267. return getIntBinding(serviceName, bindingName);
  268. }
  269. catch (NoSuchBindingException e)
  270. {
  271. createBindingFromDefaults(serviceName, bindingName, hostName, basePort, fixedPort, fixedHostName);
  272. try
  273. {
  274. return getIntBinding(serviceName, bindingName);
  275. }
  276. catch (NoSuchBindingException e1)
  277. {
  278. // Shouldn't be possible
  279. throw new IllegalStateException("Newly created binding not found", e1);
  280. }
  281. }
  282. }
  283. /**
  284. * Gets the <code>InetAddress</code> binding value for the
  285. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  286. * and no binding name qualifier.
  287. * <p>
  288. * This is typically the {@link ServiceBinding#getBindAddress() bind address}.
  289. * </p>
  290. *
  291. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  292. * to identify the appropriate binding. Cannot be <code>null</code>.
  293. *
  294. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  295. *
  296. * @see InetAddressServiceBindingValueSource
  297. */
  298. public InetAddress getInetAddressBinding(String serviceName) throws NoSuchBindingException
  299. {
  300. return getInetAddressBinding(serviceName, null);
  301. }
  302. /**
  303. * Gets the <code>InetAddress</code> binding value for the
  304. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  305. * and <code>bindingName</code> qualifier.
  306. * <p>
  307. * This is typically the {@link ServiceBinding#getBindAddress() bind address}.
  308. * </p>
  309. *
  310. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  311. * to identify the appropriate binding. Cannot be <code>null</code>.
  312. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  313. * to identify the appropriate binding. May be <code>null</code>.
  314. *
  315. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  316. *
  317. * @see InetAddressServiceBindingValueSource
  318. */
  319. public InetAddress getInetAddressBinding(String serviceName, String bindingName) throws NoSuchBindingException
  320. {
  321. ServiceBinding binding = store.getServiceBinding(serverName, serviceName, bindingName);
  322. ServiceBindingValueSource source = getServiceBindingValueSource(binding, BindingType.INETADDRESS);
  323. if (source instanceof InetAddressServiceBindingValueSource)
  324. {
  325. return ((InetAddressServiceBindingValueSource) source).getInetAddressServiceBindingValue(binding);
  326. }
  327. else
  328. {
  329. return Util.getBindingValue(source, binding, InetAddress.class);
  330. }
  331. }
  332. /**
  333. * Same as {@link #getInetAddressBinding(String, String)} but, if no matching
  334. * service binding is found, creates a new one using the given
  335. * <code>hostName</code> and <code>basePort</code>.
  336. *
  337. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  338. * to identify the appropriate binding. Cannot be <code>null</code>.
  339. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  340. * to identify the appropriate binding. May be <code>null</code>.
  341. * @param hostName Host name to use for new service binding if one is
  342. * created.
  343. * @param basePort base port to use for the binding; ServiceBindingStore
  344. * may adjust this.
  345. *
  346. * @return the binding value as an <code>InetAddress</code>
  347. *
  348. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  349. * to create same binding with different
  350. * binding values
  351. *
  352. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  353. */
  354. public InetAddress getInetAddressBinding(String serviceName, String bindingName,
  355. String hostName, int basePort) throws UnknownHostException, DuplicateServiceException
  356. {
  357. return getInetAddressBinding(serviceName, bindingName, hostName, basePort, false, hostName != null);
  358. }
  359. /**
  360. * Same as {@link #getInetAddressBinding(String, String)} but, if no matching
  361. * service binding is found, creates a new one using the given
  362. * <code>hostName</code> and <code>basePort</code>.
  363. *
  364. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  365. * to identify the appropriate binding. Cannot be <code>null</code>.
  366. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  367. * to identify the appropriate binding. May be <code>null</code>.
  368. * @param hostName Host name to use for new service binding if one is
  369. * created.
  370. * @param basePort base port to use for the binding; ServiceBindingStore
  371. * may adjust this.
  372. * @param fixedPort whether runtime @{link ServiceBinding}s created from this
  373. * metadata can alter the port value based on the server
  374. * on which the binding is running.
  375. * @param fixedHostName whether runtime @{link ServiceBinding}s created from
  376. * this metadata can alter the hostName value based on
  377. * the server on which the binding is running.
  378. *
  379. * @return the binding value as an <code>InetAddress</code>
  380. *
  381. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  382. * to create same binding with different
  383. * binding values
  384. *
  385. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  386. */
  387. public InetAddress getInetAddressBinding(String serviceName, String bindingName,
  388. String hostName, int basePort, boolean fixedPort, boolean fixedHostName) throws UnknownHostException, DuplicateServiceException
  389. {
  390. try
  391. {
  392. return getInetAddressBinding(serviceName, bindingName);
  393. }
  394. catch (NoSuchBindingException e)
  395. {
  396. createBindingFromDefaults(serviceName, bindingName, hostName, basePort, fixedPort, fixedHostName);
  397. try
  398. {
  399. return getInetAddressBinding(serviceName, bindingName);
  400. }
  401. catch (NoSuchBindingException e1)
  402. {
  403. // Shouldn't be possible
  404. throw new IllegalStateException("Newly created binding not found", e1);
  405. }
  406. }
  407. }
  408. /**
  409. * Gets the <code>String</code> binding value for the
  410. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  411. * and no binding name qualifier.
  412. * <p>
  413. * This is typically the {@link ServiceBinding#getHostName() host name}.
  414. * </p>
  415. *
  416. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  417. * to identify the appropriate binding. Cannot be <code>null</code>.
  418. *
  419. * @return the raw binding value.
  420. *
  421. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  422. *
  423. * @see StringServiceBindingValueSource
  424. */
  425. public String getStringBinding(String serviceName) throws NoSuchBindingException
  426. {
  427. return getStringBinding(serviceName, null, null);
  428. }
  429. /**
  430. * Gets the <code>String</code> binding value for the
  431. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  432. * and no binding name qualifier.
  433. * <p>
  434. * This is typically the {@link ServiceBinding#getHostName() host name}.
  435. * </p>
  436. *
  437. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  438. * to identify the appropriate binding. Cannot be <code>null</code>.
  439. * @param input string that should be used as a source for transformations
  440. * (e.g. string replacement), or <code>null</code> if no
  441. * transformation is needed
  442. *
  443. * @return the raw binding value, or a transformed string based on the raw
  444. * binding value and <code>input</code>.
  445. *
  446. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  447. *
  448. * @see StringServiceBindingValueSource
  449. */
  450. public String getStringBinding(String serviceName, String input) throws NoSuchBindingException
  451. {
  452. return getStringBinding(serviceName, null, input);
  453. }
  454. /**
  455. * Gets the <code>String</code> binding value for the
  456. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  457. * and <code>bindingName</code> qualifier.
  458. * <p>
  459. * This is typically the {@link ServiceBinding#getHostName() host name}.
  460. * </p>
  461. *
  462. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  463. * to identify the appropriate binding. Cannot be <code>null</code>.
  464. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  465. * to identify the appropriate binding. May be <code>null</code>.
  466. * @param input string that should be used as a source for transformations
  467. * (e.g. string replacement), or <code>null</code> if no
  468. * transformation is needed
  469. *
  470. * @return the raw binding value, or a transformed string based on the raw
  471. * binding value and <code>input</code>.
  472. *
  473. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  474. *
  475. * @see StringServiceBindingValueSource
  476. */
  477. public String getStringBinding(String serviceName, String bindingName, String input) throws NoSuchBindingException
  478. {
  479. ServiceBinding binding = store.getServiceBinding(serverName, serviceName, bindingName);
  480. ServiceBindingValueSource source = getServiceBindingValueSource(binding, BindingType.STRING);
  481. if (source instanceof StringServiceBindingValueSource)
  482. {
  483. return ((StringServiceBindingValueSource) source).getStringServiceBindingValue(binding, input);
  484. }
  485. else
  486. {
  487. return Util.getBindingValueWithInput(source, binding, input, String.class);
  488. }
  489. }
  490. /**
  491. * Same as {@link #getStringBinding(String, String, String)} but, if no matching
  492. * service binding is found, creates a new one using the given
  493. * <code>hostName</code> and <code>basePort</code>.
  494. *
  495. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  496. * to identify the appropriate binding. Cannot be <code>null</code>.
  497. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  498. * to identify the appropriate binding. May be <code>null</code>.
  499. * @param input string that should be used as a source for transformations
  500. * (e.g. string replacement), or <code>null</code> if no
  501. * transformation is needed
  502. * @param hostName Host name to use for new service binding if one is
  503. * created.
  504. * @param basePort base port to use for the binding; ServiceBindingStore
  505. * may adjust this.
  506. *
  507. * @return the raw binding value, or a transformed string based on the raw
  508. * binding value and <code>input</code>.
  509. *
  510. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  511. * to create same binding with different
  512. * binding values
  513. *
  514. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  515. */
  516. public String getStringBinding(String serviceName, String bindingName, String input,
  517. String hostName, int basePort) throws UnknownHostException, DuplicateServiceException
  518. {
  519. return getStringBinding(serviceName, bindingName, input, hostName, basePort, false, hostName != null);
  520. }
  521. /**
  522. * Same as {@link #getStringBinding(String, String, String)} but, if no matching
  523. * service binding is found, creates a new one using the given
  524. * <code>hostName</code> and <code>basePort</code>.
  525. *
  526. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  527. * to identify the appropriate binding. Cannot be <code>null</code>.
  528. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  529. * to identify the appropriate binding. May be <code>null</code>.
  530. * @param input string that should be used as a source for transformations
  531. * (e.g. string replacement), or <code>null</code> if no
  532. * transformation is needed
  533. * @param hostName Host name to use for new service binding if one is
  534. * created.
  535. * @param basePort base port to use for the binding; ServiceBindingStore
  536. * may adjust this.
  537. * @param fixedPort whether runtime @{link ServiceBinding}s created from this
  538. * metadata can alter the port value based on the server
  539. * on which the binding is running.
  540. * @param fixedHostName whether runtime @{link ServiceBinding}s created from
  541. * this metadata can alter the hostName value based on
  542. * the server on which the binding is running.
  543. *
  544. * @return the raw binding value, or a transformed string based on the raw
  545. * binding value and <code>input</code>.
  546. *
  547. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  548. * to create same binding with different
  549. * binding values
  550. *
  551. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  552. */
  553. public String getStringBinding(String serviceName, String bindingName, String input,
  554. String hostName, int basePort, boolean fixedPort, boolean fixedHostName) throws UnknownHostException, DuplicateServiceException
  555. {
  556. try
  557. {
  558. return getStringBinding(serviceName, bindingName, input);
  559. }
  560. catch (NoSuchBindingException e)
  561. {
  562. createBindingFromDefaults(serviceName, bindingName, hostName, basePort, fixedPort, fixedHostName);
  563. try
  564. {
  565. return getStringBinding(serviceName, bindingName, input);
  566. }
  567. catch (NoSuchBindingException e1)
  568. {
  569. // Shouldn't be possible
  570. throw new IllegalStateException("Newly created binding not found", e1);
  571. }
  572. }
  573. }
  574. /**
  575. * Gets an <code>Element</code> containing the binding values for the
  576. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  577. * and no binding name qualifier.
  578. * <p>
  579. * Used to perform transformations on values embedded in DOM elements.
  580. * </p>
  581. *
  582. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  583. * to identify the appropriate binding. Cannot be <code>null</code>.
  584. * @param input element that should be used as a source for transformations
  585. *
  586. * @return transformed element based on the raw binding value(s) and <code>input</code>.
  587. *
  588. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  589. *
  590. * @see ElementServiceBindingValueSource
  591. */
  592. public Element getElementBinding(String serviceName, Element input) throws NoSuchBindingException
  593. {
  594. return getElementBinding(serviceName, null, input);
  595. }
  596. /**
  597. * Gets an <code>Element</code> containing the binding values for the
  598. * <code>ServiceBinding</code> with the given <code>serviceName</code>
  599. * and <code>bindingName</code> qualifier.
  600. * <p>
  601. * Used to perform transformations on values embedded in DOM elements.
  602. * </p>
  603. *
  604. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  605. * to identify the appropriate binding. Cannot be <code>null</code>.
  606. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  607. * to identify the appropriate binding. May be <code>null</code>.
  608. * @param input element that should be used as a source for transformations
  609. *
  610. * @return transformed element based on the raw binding value(s) and <code>input</code>.
  611. *
  612. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  613. *
  614. * @see ElementServiceBindingValueSource
  615. */
  616. public Element getElementBinding(String serviceName, String bindingName, Element input) throws NoSuchBindingException
  617. {
  618. ServiceBinding binding = store.getServiceBinding(serverName, serviceName, bindingName);
  619. ServiceBindingValueSource source = getServiceBindingValueSource(binding, BindingType.ELEMENT);
  620. if (source instanceof ElementServiceBindingValueSource)
  621. {
  622. return ((ElementServiceBindingValueSource) source).getElementServiceBindingValue(binding, input);
  623. }
  624. else
  625. {
  626. return Util.getBindingValueWithInput(source, binding, input, Element.class);
  627. }
  628. }
  629. /**
  630. * Same as {@link #getElementBinding(String, String, Element)} but, if no matching
  631. * service binding is found, creates a new one using the given
  632. * <code>hostName</code> and <code>basePort</code>.
  633. *
  634. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  635. * to identify the appropriate binding. Cannot be <code>null</code>.
  636. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  637. * to identify the appropriate binding. May be <code>null</code>.
  638. * @param input string that should be used as a source for transformations
  639. * (e.g. string replacement), or <code>null</code> if no
  640. * transformation is needed
  641. * @param hostName Host name to use for new service binding if one is
  642. * created.
  643. * @param basePort base port to use for the binding; ServiceBindingStore
  644. * may adjust this.
  645. *
  646. * @return transformed element based on the raw binding value(s) and <code>input</code>.
  647. *
  648. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  649. * to create same binding with different
  650. * binding values
  651. *
  652. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  653. */
  654. public Element getElementBinding(String serviceName, String bindingName, Element input,
  655. String hostName, int basePort) throws UnknownHostException, DuplicateServiceException
  656. {
  657. return getElementBinding(serviceName, bindingName, input, hostName, basePort, false, hostName != null);
  658. }
  659. /**
  660. * Same as {@link #getElementBinding(String, String, Element)} but, if no matching
  661. * service binding is found, creates a new one using the given
  662. * <code>hostName</code> and <code>basePort</code>.
  663. *
  664. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  665. * to identify the appropriate binding. Cannot be <code>null</code>.
  666. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  667. * to identify the appropriate binding. May be <code>null</code>.
  668. * @param input string that should be used as a source for transformations
  669. * (e.g. string replacement), or <code>null</code> if no
  670. * transformation is needed
  671. * @param hostName Host name to use for new service binding if one is
  672. * created.
  673. * @param basePort base port to use for the binding; ServiceBindingStore
  674. * may adjust this.
  675. * @param fixedPort whether runtime @{link ServiceBinding}s created from this
  676. * metadata can alter the port value based on the server
  677. * on which the binding is running.
  678. * @param fixedHostName whether runtime @{link ServiceBinding}s created from
  679. * this metadata can alter the hostName value based on
  680. * the server on which the binding is running.
  681. *
  682. * @return transformed element based on the raw binding value(s) and <code>input</code>.
  683. *
  684. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  685. * to create same binding with different
  686. * binding values
  687. *
  688. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  689. */
  690. public Element getElementBinding(String serviceName, String bindingName, Element input,
  691. String hostName, int basePort, boolean fixedPort, boolean fixedHostName) throws UnknownHostException, DuplicateServiceException
  692. {
  693. try
  694. {
  695. return getElementBinding(serviceName, bindingName, input);
  696. }
  697. catch (NoSuchBindingException e)
  698. {
  699. createBindingFromDefaults(serviceName, bindingName, hostName, basePort, fixedPort, fixedHostName);
  700. try
  701. {
  702. return getElementBinding(serviceName, bindingName, input);
  703. }
  704. catch (NoSuchBindingException e1)
  705. {
  706. // Shouldn't be possible
  707. throw new IllegalStateException("Newly created binding not found", e1);
  708. }
  709. }
  710. }
  711. /**
  712. * Gets a <code>URL</code> pointing to content that contains the binding values
  713. * for the <code>ServiceBinding</code> with the given <code>serviceName</code>
  714. * and no binding name qualifier.
  715. * <p>
  716. * Typical usage is in file transformation operations, where the content
  717. * of the given <code>input</code> URL is read, transformed, written to a
  718. * temp file, and the URL of the temp file returned.
  719. * </p>
  720. *
  721. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  722. * to identify the appropriate binding. Cannot be <code>null</code>.
  723. * @param input URL of content that should be used as a source for transformations
  724. *
  725. * @return URL pointing to the output of the transformation.
  726. *
  727. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  728. *
  729. * @see URLServiceBindingValueSource
  730. */
  731. public URL getURLBinding(String serviceName, URL input) throws NoSuchBindingException
  732. {
  733. return getURLBinding(serviceName, null, input);
  734. }
  735. /**
  736. * Gets a <code>URL</code> pointing to content that contains the binding values
  737. * for the <code>ServiceBinding</code> with the given <code>serviceName</code>
  738. * and <code>bindingName</code> qualifier.
  739. * <p>
  740. * Typical usage is in file transformation operations, where the content
  741. * of the given <code>input</code> URL is read, transformed, written to a
  742. * temp file, and the URL of the temp file returned.
  743. * </p>
  744. *
  745. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  746. * to identify the appropriate binding. Cannot be <code>null</code>.
  747. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  748. * to identify the appropriate binding. May be <code>null</code>.
  749. * @param input URL of content that should be used as a source for transformations
  750. *
  751. * @return URL pointing to the output of the transformation.
  752. *
  753. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  754. *
  755. * @see URLServiceBindingValueSource
  756. */
  757. public URL getURLBinding(String serviceName, String bindingName, URL input) throws NoSuchBindingException
  758. {
  759. ServiceBinding binding = store.getServiceBinding(serverName, serviceName, bindingName);
  760. ServiceBindingValueSource source = getServiceBindingValueSource(binding, BindingType.URL);
  761. if (source instanceof URLServiceBindingValueSource)
  762. {
  763. return ((URLServiceBindingValueSource) source).getURLServiceBindingValue(binding, input);
  764. }
  765. else
  766. {
  767. return Util.getBindingValueWithInput(source, binding, input, URL.class);
  768. }
  769. }
  770. /**
  771. * Same as {@link #getURLBinding(String, String, URL)} but, if no matching
  772. * service binding is found, creates a new one using the given
  773. * <code>hostName</code> and <code>basePort</code>.
  774. *
  775. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  776. * to identify the appropriate binding. Cannot be <code>null</code>.
  777. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  778. * to identify the appropriate binding. May be <code>null</code>.
  779. * @param input string that should be used as a source for transformations
  780. * (e.g. string replacement), or <code>null</code> if no
  781. * transformation is needed
  782. * @param hostName Host name to use for new service binding if one is
  783. * created.
  784. * @param basePort base port to use for the binding; ServiceBindingStore
  785. * may adjust this.
  786. *
  787. * @return URL pointing to the output of the transformation.
  788. *
  789. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  790. * to create same binding with different
  791. * binding values
  792. *
  793. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  794. */
  795. public URL getURLBinding(String serviceName, String bindingName, URL input,
  796. String hostName, int basePort) throws UnknownHostException, DuplicateServiceException
  797. {
  798. return getURLBinding(serviceName, bindingName, input, hostName, basePort, false, hostName != null);
  799. }
  800. /**
  801. * Same as {@link #getURLBinding(String, String, URL)} but, if no matching
  802. * service binding is found, creates a new one using the given
  803. * <code>hostName</code> and <code>basePort</code>.
  804. *
  805. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  806. * to identify the appropriate binding. Cannot be <code>null</code>.
  807. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  808. * to identify the appropriate binding. May be <code>null</code>.
  809. * @param input string that should be used as a source for transformations
  810. * (e.g. string replacement), or <code>null</code> if no
  811. * transformation is needed
  812. * @param hostName Host name to use for new service binding if one is
  813. * created.
  814. * @param basePort base port to use for the binding; ServiceBindingStore
  815. * may adjust this.
  816. * @param fixedPort whether runtime @{link ServiceBinding}s created from this
  817. * metadata can alter the port value based on the server
  818. * on which the binding is running.
  819. * @param fixedHostName whether runtime @{link ServiceBinding}s created from
  820. * this metadata can alter the hostName value based on
  821. * the server on which the binding is running.
  822. *
  823. * @return URL pointing to the output of the transformation.
  824. *
  825. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  826. * to create same binding with different
  827. * binding values
  828. *
  829. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  830. */
  831. public URL getURLBinding(String serviceName, String bindingName, URL input,
  832. String hostName, int basePort, boolean fixedPort, boolean fixedHostName) throws UnknownHostException, DuplicateServiceException
  833. {
  834. try
  835. {
  836. return getURLBinding(serviceName, bindingName, input);
  837. }
  838. catch (NoSuchBindingException e)
  839. {
  840. createBindingFromDefaults(serviceName, bindingName, hostName, basePort, fixedPort, fixedHostName);
  841. try
  842. {
  843. return getURLBinding(serviceName, bindingName, input);
  844. }
  845. catch (NoSuchBindingException e1)
  846. {
  847. // Shouldn't be possible
  848. throw new IllegalStateException("Newly created binding not found", e1);
  849. }
  850. }
  851. }
  852. /**
  853. * Gets a filesystem path pointing to content that contains the binding values
  854. * for the <code>ServiceBinding</code> with the given <code>serviceName</code>
  855. * and no binding name qualifier.
  856. * <p>
  857. * Typical usage is in file transformation operations, where the content
  858. * of the given <code>input</code> classpath resource is read, transformed, written to a
  859. * temp file, and the filesystem path of the temp file returned.
  860. * </p>
  861. *
  862. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  863. * to identify the appropriate binding. Cannot be <code>null</code>.
  864. * @param input location of content that should be used as a source for transformations;
  865. * either a String representation of a URL or a value that
  866. * can be passed to {@link ClassLoader#getResourceAsStream(String)}.
  867. * Cannot be <code>null</code>.
  868. *
  869. * @return a filesystem path pointing to the output of the transformation.
  870. * May return <code>null</code>.
  871. *
  872. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  873. *
  874. * @see URLServiceBindingValueSource
  875. */
  876. public String getResourceBinding(String serviceName, String input) throws NoSuchBindingException
  877. {
  878. return getResourceBinding(serviceName, null, input);
  879. }
  880. /**
  881. * Gets a filesystem path pointing to content that contains the binding values
  882. * for the <code>ServiceBinding</code> with the given <code>serviceName</code>
  883. * and <code>bindingName</code> qualifier.
  884. * <p>
  885. * Typical usage is in file transformation operations, where the content
  886. * of the given <code>input</code> classpath resource is read, transformed, written to a
  887. * temp file, and the filesystem path of the temp file returned.
  888. * </p>
  889. *
  890. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  891. * to identify the appropriate binding. Cannot be <code>null</code>.
  892. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  893. * to identify the appropriate binding. May be <code>null</code>.
  894. * @param input location of content that should be used as a source for transformations;
  895. * either a String representation of a URL or a value that
  896. * can be passed to {@link ClassLoader#getResourceAsStream(String)}.
  897. * Cannot be <code>null</code>.
  898. *
  899. * @return a filesystem path pointing to the output of the transformation.
  900. * May return <code>null</code>.
  901. *
  902. * @throws NoSuchBindingException if a matching ServiceBinding could not be found
  903. *
  904. * @see URLServiceBindingValueSource
  905. */
  906. public String getResourceBinding(String serviceName, String bindingName, String input) throws NoSuchBindingException
  907. {
  908. ServiceBinding binding = store.getServiceBinding(serverName, serviceName, bindingName);
  909. ServiceBindingValueSource source = getServiceBindingValueSource(binding, BindingType.RESOURCE);
  910. if (source instanceof URLServiceBindingValueSource)
  911. {
  912. return ((URLServiceBindingValueSource) source).getResourceServiceBindingValue(binding, input);
  913. }
  914. else
  915. {
  916. return Util.getBindingValueWithInput(source, binding, input, String.class);
  917. }
  918. }
  919. /**
  920. * Same as {@link #getResourceBinding(String, String, String)} but, if no matching
  921. * service binding is found, creates a new one using the given
  922. * <code>hostName</code> and <code>basePort</code>.
  923. *
  924. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  925. * to identify the appropriate binding. Cannot be <code>null</code>.
  926. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  927. * to identify the appropriate binding. May be <code>null</code>.
  928. * @param input string that should be used as a source for transformations
  929. * (e.g. string replacement), or <code>null</code> if no
  930. * transformation is needed
  931. * @param hostName Host name to use for new service binding if one is
  932. * created.
  933. * @param basePort base port to use for the binding; ServiceBindingStore
  934. * may adjust this.
  935. *
  936. * @return a filesystem path pointing to the output of the transformation.
  937. * May return <code>null</code>.
  938. *
  939. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  940. * to create same binding with different
  941. * binding values
  942. *
  943. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  944. */
  945. public String getResourceBinding(String serviceName, String bindingName, String input,
  946. String hostName, int basePort) throws UnknownHostException, DuplicateServiceException
  947. {
  948. return getResourceBinding(serviceName, bindingName, input, hostName, basePort, false, hostName != null);
  949. }
  950. /**
  951. * Same as {@link #getResourceBinding(String, String, String)} but, if no matching
  952. * service binding is found, creates a new one using the given
  953. * <code>hostName</code> and <code>basePort</code>.
  954. *
  955. * @param serviceName value to match to {@link ServiceBinding#getServiceName()}
  956. * to identify the appropriate binding. Cannot be <code>null</code>.
  957. * @param bindingName value to match to {@link ServiceBinding#getBindingName()}
  958. * to identify the appropriate binding. May be <code>null</code>.
  959. * @param input string that should be used as a source for transformations
  960. * (e.g. string replacement), or <code>null</code> if no
  961. * transformation is needed
  962. * @param hostName Host name to use for new service binding if one is
  963. * created.
  964. * @param basePort base port to use for the binding; ServiceBindingStore
  965. * may adjust this.
  966. * @param fixedPort whether runtime @{link ServiceBinding}s created from this
  967. * metadata can alter the port value based on the server
  968. * on which the binding is running.
  969. * @param fixedHostName whether runtime @{link ServiceBinding}s created from
  970. * this metadata can alter the hostName value based on
  971. * the server on which the binding is running.
  972. *
  973. * @return a filesystem path pointing to the output of the transformation.
  974. * May return <code>null</code>.
  975. *
  976. * @throws DuplicateServiceException in unlikely event of concurrent attempts
  977. * to create same binding with different
  978. * binding values
  979. *
  980. * @throws UnknownHostException if no IP address for the <code>hostName</code> could be found
  981. */
  982. public String getResourceBinding(String serviceName, String bindingName, String input,
  983. String hostName, int basePort, boolean fixedPort, boolean fixedHostName) throws UnknownHostException, DuplicateServiceException
  984. {
  985. try
  986. {
  987. return getResourceBinding(serviceName, bindingName, input);
  988. }
  989. catch (NoSuchBindingException e)
  990. {
  991. createBindingFromDefaults(serviceName, bindingName, hostName, basePort, fixedPort, fixedHostName);
  992. try
  993. {
  994. return getResourceBinding(serviceName, bindingName, input);
  995. }
  996. catch (NoSuchBindingException e1)
  997. {
  998. // Shouldn't be possible
  999. throw new IllegalStateException("Newly created binding not found", e1);
  1000. }
  1001. }
  1002. }