001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    package org.apache.geronimo.tomcat.connector;
020    
021    import java.net.InetAddress;
022    import java.net.InetSocketAddress;
023    import java.net.UnknownHostException;
024    import java.util.Map;
025    
026    import javax.management.j2ee.statistics.Stats;
027    
028    import org.apache.geronimo.gbean.GBeanInfo;
029    import org.apache.geronimo.gbean.GBeanInfoBuilder;
030    import org.apache.geronimo.management.StatisticsProvider;
031    import org.apache.geronimo.management.geronimo.WebManager;
032    import org.apache.geronimo.system.serverinfo.ServerInfo;
033    import org.apache.geronimo.tomcat.TomcatContainer;
034    import org.apache.geronimo.tomcat.stats.ConnectorStats;
035    
036    public class AJP13ConnectorGBean extends ConnectorGBean implements Ajp13Protocol, StatisticsProvider {
037        
038        // JSR77 stats
039        private ConnectorStats connStatsProvider = new ConnectorStats();
040    
041        private boolean reset = true;
042    
043        protected String connectHost;
044        
045        public AJP13ConnectorGBean(String name, Map initParams, String host, int port, TomcatContainer container, ServerInfo serverInfo) throws Exception {
046            super(name, initParams, "AJP/1.3", container, serverInfo);
047            
048            // Default the host to listen on all address is one was not specified
049            if (host == null) {
050                host = "0.0.0.0";
051            }
052    
053            // Must have a port
054            if (port == 0) {
055                throw new IllegalArgumentException("Must declare a port.");
056            }
057    
058            connector.setAttribute("host", host);
059            connector.setPort(port);
060    
061        }
062        
063        public String getGeronimoProtocol(){
064            return WebManager.PROTOCOL_AJP;
065        }
066        
067        public String getConnectUrl() {
068            if(connectHost == null) {
069                String host = getAddress();
070                if(host == null || host.equals("0.0.0.0") || host.equals("0:0:0:0:0:0:0:1")) {
071                    InetAddress address = null;
072                    try {
073                        address = InetAddress.getLocalHost();
074                    } catch (UnknownHostException e) {
075                        host = "unknown-host";
076                    }
077                    if(address != null) {
078                        host = address.getCanonicalHostName();
079                        if(host == null || host.equals("")) {
080                            host = address.getHostAddress();
081                        }
082                    }
083                }
084                // this host address could be in IPv6 format, 
085                // which means we need to wrap it in brackets
086                if (host.indexOf(":") >= 0) {
087                    host = "[" + host + "]"; 
088                }
089                connectHost = host;
090            }
091            return getScheme().toLowerCase()+"://"+connectHost+(getPort() == getDefaultPort() ? "" : ":"+getPort());
092        }
093        
094        public int getDefaultPort() {
095            return -1; 
096        }  
097        
098        public InetSocketAddress getListenAddress() {
099            return new InetSocketAddress(getHost(), getPort());
100        }
101        
102        public String getAddress() {
103            Object value = connector.getAttribute("address");
104            if (value == null) {
105                return "0.0.0.0";
106            } else if (value instanceof InetAddress) {
107                return ((InetAddress) value).getHostAddress();
108            } else
109                return value.toString();
110        } 
111    
112        public int getBacklog() {
113            Object value = connector.getAttribute("backlog");
114            return value == null ? 10 : Integer.parseInt(value.toString());
115        }
116    
117        public int getBufferSize() {
118            Object value = connector.getAttribute("bufferSize");
119            return value == null ? 2048 : Integer.parseInt(value.toString());
120        }
121    
122        public int getConnectionTimeout() {
123            Object value = connector.getAttribute("connectionTimeout");
124            return value == null ? org.apache.coyote.ajp.Constants.DEFAULT_CONNECTION_TIMEOUT : Integer.parseInt(value.toString());
125        }
126    
127        public String getExecutor() {
128            return (String) connector.getAttribute("Executor");
129        }
130        
131        public String getHost() {
132            return getAddress();
133        }
134    
135        public int getKeepAliveTimeout() {
136            Object value = connector.getAttribute("keepAliveTimeout");
137            return value == null ? getConnectionTimeout() : Integer.parseInt(value.toString());
138        }
139    
140        public int getMaxThreads() {
141            Object value = connector.getAttribute("maxThreads");
142            return value == null ? 200 : Integer.parseInt(value.toString());
143        }
144        
145        public int getMaxSpareThreads() {
146            Object value = connector.getAttribute("maxSpareThreads");
147            return value == null ? 100 : Integer.parseInt(value.toString());
148        }
149        
150        public int getMinSpareThreads() {
151            Object value = connector.getAttribute("minSpareThreads");
152            return value == null ? 10 : Integer.parseInt(value.toString());
153        }
154        
155        public int getPort() {
156            return connector.getPort();
157        }
158    
159        public boolean getTcpNoDelay() {
160            Object value = connector.getAttribute("tcpNoDelay");
161            return value == null ? true : new Boolean(value.toString()).booleanValue();
162        }
163    
164        public boolean getTomcatAuthentication() {
165            Object value = connector.getAttribute("tomcatAuthentication");
166            return value == null ? true : new Boolean(value.toString()).booleanValue();
167        }
168    
169        public void setAddress(String address) {
170            connector.setAttribute("address", address);
171        }
172    
173        public void setBacklog(int backlog) {
174            connector.setAttribute("backlog", new Integer(backlog));
175        }
176    
177        public void setBufferSize(int bufferSize) {
178            connector.setAttribute("bufferSize", new Integer(bufferSize));
179        }
180    
181        public void setConnectionTimeout(int connectionTimeout) {
182            connector.setAttribute("connectionTimeout", new Integer(connectionTimeout));
183        }
184    
185        public void setExecutor(String executor) {
186            connector.setAttribute("executor", executor);
187        }
188        
189        public void setHost(String host) {
190            setAddress(host);
191        }
192    
193        public void setKeepAliveTimeout(int keepAliveTimeout) {
194            connector.setAttribute("keepAliveTimeout", keepAliveTimeout);        
195        }
196    
197        public void setMaxThreads(int maxThreads) {
198            connector.setAttribute("maxThreads", maxThreads);        
199        }
200        
201        public void setMaxSpareThreads(int maxSpareThreads) {
202            connector.setAttribute("maxSpareThreads", new Integer(maxSpareThreads));
203        }
204        
205        public void setMinSpareThreads(int minSpareThreads) {
206            connector.setAttribute("minSpareThreads", new Integer(minSpareThreads));
207        }
208    
209        public void setNoCompressionUserAgents(String noCompressionUserAgents) {
210            connector.setAttribute("noCompressionUserAgents", noCompressionUserAgents);
211        }
212    
213        public void setPort(int port) {
214            connector.setPort(port);
215        }
216    
217        public void setTcpNoDelay(boolean tcpNoDelay) {
218            connector.setAttribute("tcpNoDelay", new Boolean(tcpNoDelay));
219        }
220    
221        public void setTomcatAuthentication(boolean tomcatAuthentication) {
222            connector.setAttribute("tomcatAuthentication", new Boolean(tomcatAuthentication));
223        }
224        
225        // Statistics Provider
226    
227        public boolean isStatisticsProvider() {
228            return true;
229        }
230    
231        public Stats getStats() {
232            String port = String.valueOf(getPort());
233            if (reset) {
234                reset = false;
235                return connStatsProvider.getStats(port);
236            } else
237                return connStatsProvider.updateStats(port);
238        }
239    
240        public void resetStats() {
241            reset = true;
242        }
243        
244        public static final GBeanInfo GBEAN_INFO;
245    
246        static {
247            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Tomcat Connector AJP", AJP13ConnectorGBean.class, ConnectorGBean.GBEAN_INFO);
248            infoFactory.addInterface(Ajp13Protocol.class, 
249                    new String[] {
250                        //AJP Attributes
251                        "address", 
252                        "backlog", 
253                        "bufferSize", 
254                        "connectionTimeout", 
255                        "executor", 
256                        "host",
257                        "keepAliveTimeout", 
258                        "maxThreads",
259                        "maxSpareThreads",
260                        "minSpareThreads",
261                        "port", 
262                        "tcpNoDelay", 
263                        "tomcatAuthentication", 
264                    },
265                    new String[] {
266                        //AJP Attributes
267                        "address", 
268                        "backlog", 
269                        "bufferSize", 
270                        "connectionTimeout", 
271                        "executor", 
272                        "host",
273                        "keepAliveTimeout", 
274                        "maxThreads",
275                        "maxSpareThreads",
276                        "minSpareThreads",
277                        "port", 
278                        "tcpNoDelay", 
279                        "tomcatAuthentication", 
280                    }
281            );
282            infoFactory.setConstructor(new String[] { "name", "initParams", "host", "port", "TomcatContainer", "ServerInfo"});
283            GBEAN_INFO = infoFactory.getBeanInfo();
284        }
285        
286        public static GBeanInfo getGBeanInfo() {
287            return GBEAN_INFO;
288        }
289    
290    }