/undertow/src/main/java/org/wildfly/extension/undertow/ListenerService.java

https://github.com/kylape/jboss-as · Java · 187 lines · 127 code · 36 blank · 24 comment · 1 complexity · 6db03ae985013c1296d067d7ac50bb56 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2013, 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.wildfly.extension.undertow;
  23. import java.io.IOException;
  24. import java.net.InetSocketAddress;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import io.undertow.server.HandlerWrapper;
  28. import io.undertow.server.HttpHandler;
  29. import io.undertow.server.OpenListener;
  30. import org.jboss.as.network.ManagedBinding;
  31. import org.jboss.as.network.SocketBinding;
  32. import org.jboss.msc.service.Service;
  33. import org.jboss.msc.service.StartContext;
  34. import org.jboss.msc.service.StartException;
  35. import org.jboss.msc.service.StopContext;
  36. import org.jboss.msc.value.InjectedValue;
  37. import org.xnio.ChannelListener;
  38. import org.xnio.ChannelListeners;
  39. import org.xnio.OptionMap;
  40. import org.xnio.Options;
  41. import org.xnio.Pool;
  42. import org.xnio.StreamConnection;
  43. import org.xnio.XnioWorker;
  44. import org.xnio.channels.AcceptingChannel;
  45. /**
  46. * @author Tomaz Cerar
  47. */
  48. public abstract class ListenerService<T> implements Service<T> {
  49. protected static final OptionMap commonOptions = OptionMap.builder()
  50. .set(Options.TCP_NODELAY, true)
  51. .set(Options.REUSE_ADDRESSES, true)
  52. .set(Options.BALANCING_TOKENS, 1)
  53. .set(Options.BALANCING_CONNECTIONS, 2)
  54. .getMap();
  55. protected final InjectedValue<XnioWorker> worker = new InjectedValue<>();
  56. protected final InjectedValue<SocketBinding> binding = new InjectedValue<>();
  57. protected final InjectedValue<SocketBinding> redirectSocket = new InjectedValue<>();
  58. @SuppressWarnings("rawtypes")
  59. protected final InjectedValue<Pool> bufferPool = new InjectedValue<>();
  60. protected final InjectedValue<Server> serverService = new InjectedValue<>();
  61. private final List<HandlerWrapper> listenerHandlerWrappers = new ArrayList<>();
  62. private final String name;
  63. protected final OptionMap listenerOptions;
  64. protected final OptionMap socketOptions;
  65. protected volatile OpenListener openListener;
  66. protected ListenerService(String name, OptionMap listenerOptions, OptionMap socketOptions) {
  67. this.name = name;
  68. this.listenerOptions = listenerOptions;
  69. this.socketOptions = socketOptions;
  70. }
  71. public InjectedValue<XnioWorker> getWorker() {
  72. return worker;
  73. }
  74. public InjectedValue<SocketBinding> getBinding() {
  75. return binding;
  76. }
  77. public InjectedValue<SocketBinding> getRedirectSocket() {
  78. return redirectSocket;
  79. }
  80. @SuppressWarnings("rawtypes")
  81. public InjectedValue<Pool> getBufferPool() {
  82. return bufferPool;
  83. }
  84. public InjectedValue<Server> getServerService() {
  85. return serverService;
  86. }
  87. protected int getBufferSize() {
  88. return 8192;
  89. }
  90. public String getName() {
  91. return name;
  92. }
  93. public abstract boolean isSecure();
  94. protected void registerBinding() {
  95. binding.getValue().getSocketBindings().getNamedRegistry().registerBinding(new ListenerBinding(binding.getValue()));
  96. }
  97. protected void unregisterBinding() {
  98. final SocketBinding binding = this.binding.getValue();
  99. binding.getSocketBindings().getNamedRegistry().unregisterBinding(binding.getName());
  100. }
  101. protected abstract void preStart(StartContext context);
  102. @Override
  103. public void start(StartContext context) throws StartException {
  104. preStart(context);
  105. serverService.getValue().registerListener(this);
  106. try {
  107. final InetSocketAddress socketAddress = binding.getValue().getSocketAddress();
  108. openListener = createOpenListener();
  109. final ChannelListener<AcceptingChannel<StreamConnection>> acceptListener = ChannelListeners.openListenerAdapter(openListener);
  110. HttpHandler handler = serverService.getValue().getRoot();
  111. for(HandlerWrapper wrapper : listenerHandlerWrappers) {
  112. handler = wrapper.wrap(handler);
  113. }
  114. openListener.setRootHandler(handler);
  115. startListening(worker.getValue(), socketAddress, acceptListener);
  116. registerBinding();
  117. } catch (IOException e) {
  118. throw new StartException("Could not start http listener", e);
  119. }
  120. }
  121. @Override
  122. public void stop(StopContext context) {
  123. serverService.getValue().unregisterListener(this);
  124. stopListening();
  125. unregisterBinding();
  126. }
  127. void addWrapperHandler(HandlerWrapper wrapper){
  128. listenerHandlerWrappers.add(wrapper);
  129. }
  130. protected abstract OpenListener createOpenListener();
  131. abstract void startListening(XnioWorker worker, InetSocketAddress socketAddress, ChannelListener<AcceptingChannel<StreamConnection>> acceptListener) throws IOException;
  132. abstract void stopListening();
  133. protected abstract String getProtocol();
  134. private static class ListenerBinding implements ManagedBinding {
  135. private final SocketBinding binding;
  136. private ListenerBinding(final SocketBinding binding) {
  137. this.binding = binding;
  138. }
  139. @Override
  140. public String getSocketBindingName() {
  141. return binding.getName();
  142. }
  143. @Override
  144. public InetSocketAddress getBindAddress() {
  145. return binding.getSocketAddress();
  146. }
  147. @Override
  148. public void close() throws IOException {
  149. }
  150. }
  151. }