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.camel.component.servlet;
018    
019    import java.util.Iterator;
020    import java.util.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import javax.servlet.ServletConfig;
024    import javax.servlet.ServletException;
025    
026    import org.apache.camel.component.http.CamelServlet;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    public class CamelHttpTransportServlet extends CamelServlet {
031        private static final transient Log LOG = LogFactory.getLog(CamelHttpTransportServlet.class);
032        private static final Map<String, CamelServlet> CAMEL_SERVLET_MAP = new ConcurrentHashMap<String, CamelServlet>();
033        private String servletName;
034        
035        public CamelHttpTransportServlet() {
036            super(false);        
037        }
038        
039        public void init(ServletConfig config) throws ServletException {
040            super.init(config);
041            servletName = config.getServletName();
042            String matchOnUriPrefix = config.getInitParameter("matchOnUriPrefix");
043            this.setMatchOnUriPrefix(Boolean.valueOf(matchOnUriPrefix));
044            // parser the servlet init parameters
045            CAMEL_SERVLET_MAP.put(servletName, this);
046        }
047        
048        public static CamelServlet getCamelServlet(String servletName) {
049            CamelServlet answer = null;
050            if (servletName != null) {
051                answer = CAMEL_SERVLET_MAP.get(servletName);
052            } else {
053                if (CAMEL_SERVLET_MAP.size() > 0) {
054                    // return the first one servlet
055                    Iterator<CamelServlet> iterator = CAMEL_SERVLET_MAP.values().iterator();
056                    answer = iterator.next();
057                    LOG.info("Since no servlet name is specified, using the first element of camelServlet map [" + answer.getServletName() + "]");
058                }
059            }        
060            return answer;
061        }
062        
063    }
064