001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport;
018    
019    import java.net.InetAddress;
020    import java.net.URI;
021    
022    import org.apache.activemq.util.InetAddressUtil;
023    import org.eclipse.jetty.server.Connector;
024    import org.eclipse.jetty.server.Server;
025    
026    abstract public class WebTransportServerSupport extends TransportServerSupport {
027    
028        protected URI bindAddress;
029        protected Server server;
030        protected Connector connector;
031        protected SocketConnectorFactory socketConnectorFactory;
032        protected String host;
033    
034        public WebTransportServerSupport(URI location) {
035            super(location);
036        }
037    
038        public void bind() throws Exception {
039    
040            URI bind = getBindLocation();
041    
042            String bindHost = bind.getHost();
043            bindHost = (bindHost == null || bindHost.length() == 0) ? "localhost" : bindHost;
044            InetAddress addr = InetAddress.getByName(bindHost);
045            host = addr.getCanonicalHostName();
046    
047            connector.setHost(host);
048            connector.setPort(bindAddress.getPort());
049            connector.setServer(server);
050            server.addConnector(connector);
051            if (addr.isAnyLocalAddress()) {
052                host = InetAddressUtil.getLocalHostName();
053            }
054            setConnectURI(new URI(bind.getScheme(), bind.getUserInfo(), host, bindAddress.getPort(), bind.getPath(), bind.getQuery(), bind.getFragment()));
055        }
056    }