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.http;
018    
019    import java.io.IOException;
020    import java.net.URI;
021    import java.util.Map;
022    
023    import org.apache.activemq.transport.Transport;
024    import org.apache.activemq.transport.TransportFactory;
025    import org.apache.activemq.transport.TransportLoggerFactory;
026    import org.apache.activemq.transport.TransportServer;
027    import org.apache.activemq.transport.util.TextWireFormat;
028    import org.apache.activemq.transport.xstream.XStreamWireFormat;
029    import org.apache.activemq.wireformat.WireFormat;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    /**
034     * @author David Martin Clavo david(dot)martin(dot)clavo(at)gmail.com (logging improvement modifications)
035     * @version $Revision$
036     */
037    public class HttpTransportFactory extends TransportFactory {
038        
039        private static final Log LOG = LogFactory.getLog(HttpTransportFactory.class);
040    
041        public TransportServer doBind(URI location) throws IOException {
042            return new HttpTransportServer(location);
043        }
044    
045        protected TextWireFormat asTextWireFormat(WireFormat wireFormat) {
046            if (wireFormat instanceof TextWireFormat) {
047                return (TextWireFormat)wireFormat;
048            }
049            LOG.trace("Not created with a TextWireFormat: " + wireFormat);
050            return new XStreamWireFormat();
051        }
052    
053        protected String getDefaultWireFormatType() {
054            return "xstream";
055        }
056    
057        protected Transport createTransport(URI location, WireFormat wf) throws IOException {
058            TextWireFormat textWireFormat = asTextWireFormat(wf);
059            return new HttpClientTransport(textWireFormat, location);
060        }
061    
062        public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
063            HttpClientTransport httpTransport = (HttpClientTransport) super.compositeConfigure(transport, format, options);
064            transport = httpTransport;
065            if( httpTransport.isTrace() ) {
066                try {
067                    transport = TransportLoggerFactory.getInstance().createTransportLogger(transport);
068                } catch (Throwable e) {
069                    LOG.error("Could not create TransportLogger object for: " + TransportLoggerFactory.defaultLogWriterName + ", reason: " + e, e);
070                }
071            }
072            return transport;
073        }
074    
075    }