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    import org.springframework.context.support.AbstractApplicationContext;
030    import org.springframework.context.support.ClassPathXmlApplicationContext;
031    
032    public class CamelHttpTransportServlet extends CamelServlet {
033        private static final transient Log LOG = LogFactory.getLog(CamelHttpTransportServlet.class);
034        private static final Map<String, CamelServlet> CAMEL_SERVLET_MAP = new ConcurrentHashMap<String, CamelServlet>();
035        private String servletName;
036        private AbstractApplicationContext applicationContext;
037        
038        public void init(ServletConfig config) throws ServletException {
039            super.init(config);
040            servletName = config.getServletName();
041            // parser the servlet init parameters
042            CAMEL_SERVLET_MAP.put(servletName, this);
043            String contextConfigLocation = config.getInitParameter("contextConfigLocation");
044            if (contextConfigLocation != null) {
045                //Create a spring application context for it
046                applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation.split(","));
047                LOG.info("Started the application context rightly");
048            }
049        }
050        
051        public void destroy() {        
052            if (applicationContext != null) {
053                applicationContext.stop();
054            }
055            // Need to remove the servlet from map after 
056            // the ApplicationContext is removed
057            CAMEL_SERVLET_MAP.remove(servletName);
058        }
059        
060        public static CamelServlet getCamelServlet(String servletName) {
061            CamelServlet answer = null;
062            if (servletName != null) {
063                answer = CAMEL_SERVLET_MAP.get(servletName);
064            } else {
065                if (CAMEL_SERVLET_MAP.size() > 0) {
066                    // return the first one servlet
067                    Iterator<CamelServlet> iterator = CAMEL_SERVLET_MAP.values().iterator();
068                    answer = iterator.next();
069                    LOG.info("Since no servlet name is specified, using the first element of camelServlet map [" + answer.getServletName() + "]");
070                }
071            }        
072            return answer;
073        }
074        
075    }
076