1 17 18 package org.apache.geronimo.jetty6.connector; 19 20 import java.net.InetAddress ; 21 import java.net.InetSocketAddress ; 22 import java.net.UnknownHostException ; 23 24 import org.apache.geronimo.gbean.GBeanInfo; 25 import org.apache.geronimo.gbean.GBeanInfoBuilder; 26 import org.apache.geronimo.gbean.GBeanLifecycle; 27 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 28 import org.apache.geronimo.jetty6.JettyContainer; 29 import org.apache.geronimo.jetty6.JettyWebConnector; 30 import org.mortbay.jetty.AbstractConnector; 31 import org.mortbay.jetty.Connector; 32 import org.mortbay.jetty.bio.SocketConnector; 33 import org.mortbay.jetty.nio.SelectChannelConnector; 34 35 40 public abstract class JettyConnector implements GBeanLifecycle, JettyWebConnector { 41 public final static String CONNECTOR_CONTAINER_REFERENCE = "JettyContainer"; 42 private final JettyContainer container; 43 protected final Connector listener; 44 private String connectHost; 45 46 49 public JettyConnector() { 50 container = null; 51 listener = null; 52 } 53 54 public JettyConnector(JettyContainer container) { 55 this.container = container; 56 this.listener = null; 57 } 58 59 public JettyConnector(JettyContainer container, Connector listener) { 60 this.container = container; 61 this.listener = listener; 62 } 63 64 public String getHost() { 66 return listener.getHost(); 67 } 68 69 public void setHost(String host) throws UnknownHostException { 70 listener.setHost(host); 72 } 73 74 public int getPort() { 75 return listener.getPort(); 76 } 77 78 public void setPort(int port) { 79 listener.setPort(port); 80 } 81 82 public abstract int getDefaultPort(); 83 84 public String getDefaultScheme() { 85 return null; 86 } 87 88 public String getConnectUrl() { 89 if (connectHost == null) { 90 String host = getHost(); 91 if (host == null || host.equals("0.0.0.0")) { 92 InetAddress address = null; 93 try { 94 address = InetAddress.getLocalHost(); 95 } catch (UnknownHostException e) { 96 host = "unknown-host"; 97 } 98 if (address != null) { 99 host = address.getHostName(); 100 if (host == null || host.equals("")) { 101 host = address.getHostAddress(); 102 } 103 } 104 } 105 connectHost = host; 106 } 107 return getProtocol().toLowerCase() + "://" + connectHost + (getPort() == getDefaultPort() ? "" : ":" + getPort()); 108 } 109 110 public int getMaxIdleTimeMs() { 111 return ((AbstractConnector) listener).getMaxIdleTime(); 112 } 113 114 public void setMaxIdleTimeMs(int idleTime) { 115 ((AbstractConnector) listener).setMaxIdleTime(idleTime); 116 } 117 118 public int getBufferSizeBytes() { 119 return listener.getRequestBufferSize(); 121 } 122 123 public void setBufferSizeBytes(int bytes) { 124 listener.setRequestBufferSize(bytes); 126 } 127 128 public int getAcceptQueueSize() { 129 return ((AbstractConnector) listener).getAcceptQueueSize(); 130 } 131 132 public void setAcceptQueueSize(int size) { 133 ((AbstractConnector) listener).setAcceptQueueSize(size); 134 } 135 136 public int getLingerMillis() { 137 return (int) ((AbstractConnector) listener).getSoLingerTime(); 138 } 139 140 public void setLingerMillis(int millis) { 141 ((AbstractConnector) listener).setSoLingerTime(millis); 142 } 143 144 public boolean isTcpNoDelay() { 145 return true; 146 } 147 148 public void setTcpNoDelay(boolean enable) { 149 throw new UnsupportedOperationException (listener == null ? "No Listener" : listener.getClass().getName()); 150 } 151 152 public void setMaxThreads(int maxThreads) { 153 ((AbstractConnector) listener).setAcceptors(maxThreads); 155 } 156 157 public int getMaxThreads() { 158 return ((AbstractConnector) listener).getAcceptors(); 160 } 161 162 public int getRedirectPort() { 163 return listener.getConfidentialPort(); 164 } 165 166 public InetSocketAddress getListenAddress() { 167 try { 168 return new InetSocketAddress (InetAddress.getByName(listener.getHost()), listener.getPort()); 169 } catch (UnknownHostException e) { 170 throw new IllegalStateException ("InetSocketAddress cannot be determined for host=" + listener.getHost()); 171 } 172 } 173 174 175 public void setRedirectPort(int port) { 176 if (listener instanceof SocketConnector) { 177 SocketConnector socketListener = (SocketConnector) listener; 178 socketListener.setConfidentialPort(port); 179 socketListener.setIntegralPort(port); 180 socketListener.setIntegralScheme("https"); 181 socketListener.setConfidentialScheme("https"); 182 } else if (listener instanceof SelectChannelConnector) { 183 SelectChannelConnector connector = (SelectChannelConnector) listener; 184 connector.setConfidentialPort(port); 185 connector.setIntegralPort(port); 186 connector.setIntegralScheme("https"); 187 connector.setConfidentialScheme("https"); 188 } 189 198 else { 199 throw new UnsupportedOperationException (listener == null ? "No Listener" : listener.getClass().getName()); } 201 } 202 203 public abstract String getProtocol(); 204 205 public void doStart() throws Exception { 206 container.addListener(listener); 207 listener.start(); 208 } 209 210 public void doStop() { 211 while (true) { 212 try { 213 listener.stop(); 214 container.removeListener(listener); 215 return; 216 } catch (Exception e) { 217 continue; 218 } 219 } 220 } 221 222 public void doFail() { 223 while (true) { 224 try { 225 listener.stop(); 226 container.removeListener(listener); 227 return; 228 } catch (Exception e) { 229 continue; 230 } 231 } 232 } 233 234 public static final GBeanInfo GBEAN_INFO; 235 236 static { 237 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Jetty HTTP Connector", JettyConnector.class); 238 infoFactory.addReference(CONNECTOR_CONTAINER_REFERENCE, JettyContainer.class, NameFactory.GERONIMO_SERVICE); 239 infoFactory.addInterface(JettyWebConnector.class, new String []{"host", "port", "minThreads", "maxThreads", "bufferSizeBytes", "acceptQueueSize", "lingerMillis", "tcpNoDelay", "redirectPort", "connectUrl", "maxIdleTimeMs"}, 240 new String []{"host", "port", "redirectPort", "maxThreads", "minThreads"}); 241 infoFactory.setConstructor(new String []{"JettyContainer"}); 242 GBEAN_INFO = infoFactory.getBeanInfo(); 243 } 244 } 245 | Popular Tags |