PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/remoting/src/main/java/org/jboss/as/remoting/RemoteOutboundConnectionService.java

http://github.com/jbossas/jboss-as
Java | 145 lines | 86 code | 21 blank | 38 comment | 11 complexity | 93eb6bd48646b37dbb26faae46c91efa MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., 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.as.remoting;
  23. import static org.jboss.as.remoting.RemotingMessages.MESSAGES;
  24. import static org.xnio.Options.SASL_POLICY_NOANONYMOUS;
  25. import static org.xnio.Options.SASL_POLICY_NOPLAINTEXT;
  26. import java.io.IOException;
  27. import java.net.InetAddress;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import javax.net.ssl.SSLContext;
  31. import javax.security.auth.callback.CallbackHandler;
  32. import org.jboss.as.domain.management.CallbackHandlerFactory;
  33. import org.jboss.as.domain.management.SecurityRealm;
  34. import org.jboss.as.network.NetworkUtils;
  35. import org.jboss.as.network.OutboundSocketBinding;
  36. import org.jboss.msc.inject.Injector;
  37. import org.jboss.msc.service.ServiceName;
  38. import org.jboss.msc.value.InjectedValue;
  39. import org.jboss.remoting3.Connection;
  40. import org.jboss.remoting3.Endpoint;
  41. import org.xnio.IoFuture;
  42. import org.xnio.OptionMap;
  43. import org.xnio.Options;
  44. import org.xnio.Sequence;
  45. /**
  46. * A {@link RemoteOutboundConnectionService} manages a remoting connection created out of a remote:// URI scheme.
  47. *
  48. * @author Jaikiran Pai
  49. */
  50. public class RemoteOutboundConnectionService extends AbstractOutboundConnectionService<RemoteOutboundConnectionService> {
  51. public static final ServiceName REMOTE_OUTBOUND_CONNECTION_BASE_SERVICE_NAME = RemotingServices.SUBSYSTEM_ENDPOINT.append("remote-outbound-connection");
  52. private static final String REMOTE_URI_SCHEME = "remote://";
  53. private static final String JBOSS_LOCAL_USER = "JBOSS-LOCAL-USER";
  54. private final InjectedValue<OutboundSocketBinding> destinationOutboundSocketBindingInjectedValue = new InjectedValue<OutboundSocketBinding>();
  55. private final InjectedValue<SecurityRealm> securityRealmInjectedValue = new InjectedValue<SecurityRealm>();
  56. private final String username;
  57. private URI connectionURI;
  58. public RemoteOutboundConnectionService(final String connectionName, final OptionMap connectionCreationOptions, final String username) {
  59. super(connectionName, connectionCreationOptions);
  60. this.username = username;
  61. }
  62. @Override
  63. public IoFuture<Connection> connect() throws IOException {
  64. final URI uri;
  65. try {
  66. // we lazily generate the URI on first request to connect() instead of on start() of the service
  67. // in order to delay resolving the destination address. No point trying to resolve that address
  68. // if nothing really wants to create a connection out of it.
  69. uri = this.getConnectionURI();
  70. } catch (URISyntaxException e) {
  71. throw MESSAGES.couldNotConnect(e);
  72. }
  73. final Endpoint endpoint = this.endpointInjectedValue.getValue();
  74. final CallbackHandler callbackHandler;
  75. final CallbackHandlerFactory cbhFactory;
  76. SSLContext sslContext = null;
  77. SecurityRealm realm = securityRealmInjectedValue.getOptionalValue();
  78. if (realm != null && (cbhFactory = realm.getSecretCallbackHandlerFactory()) != null && username != null) {
  79. callbackHandler = cbhFactory.getCallbackHandler(username);
  80. } else {
  81. callbackHandler = getCallbackHandler();
  82. }
  83. if (realm != null) {
  84. sslContext = realm.getSSLContext();
  85. }
  86. OptionMap.Builder builder = OptionMap.builder();
  87. builder.addAll(this.connectionCreationOptions);
  88. builder.set(SASL_POLICY_NOANONYMOUS, Boolean.FALSE);
  89. builder.set(SASL_POLICY_NOPLAINTEXT, Boolean.FALSE);
  90. builder.set(Options.SASL_DISALLOWED_MECHANISMS, Sequence.of(JBOSS_LOCAL_USER));
  91. builder.set(Options.SSL_ENABLED, true);
  92. builder.set(Options.SSL_STARTTLS, true);
  93. return endpoint.connect(uri, builder.getMap(), callbackHandler, sslContext);
  94. }
  95. Injector<OutboundSocketBinding> getDestinationOutboundSocketBindingInjector() {
  96. return this.destinationOutboundSocketBindingInjectedValue;
  97. }
  98. Injector<SecurityRealm> getSecurityRealmInjector() {
  99. return securityRealmInjectedValue;
  100. }
  101. /**
  102. * Generates and returns the URI that corresponds to the remote outbound connection.
  103. * If the URI has already been generated in a previous request, then it returns that back.
  104. * Else the URI is constructed out of the outbound socket binding's destination address and destination port.
  105. *
  106. * @return
  107. * @throws IOException
  108. * @throws URISyntaxException
  109. */
  110. private synchronized URI getConnectionURI() throws IOException, URISyntaxException {
  111. if (this.connectionURI != null) {
  112. return this.connectionURI;
  113. }
  114. final OutboundSocketBinding destinationOutboundSocket = this.destinationOutboundSocketBindingInjectedValue.getValue();
  115. final InetAddress destinationAddress = destinationOutboundSocket.getDestinationAddress();
  116. final int port = destinationOutboundSocket.getDestinationPort();
  117. this.connectionURI = new URI(REMOTE_URI_SCHEME + NetworkUtils.formatPossibleIpv6Address( destinationAddress.getHostAddress()) + ":" + port);
  118. return this.connectionURI;
  119. }
  120. @Override
  121. public RemoteOutboundConnectionService getValue() throws IllegalStateException, IllegalArgumentException {
  122. return this;
  123. }
  124. }