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.discovery.http;
018    
019    import java.io.IOException;
020    import java.io.PrintWriter;
021    import java.util.ArrayList;
022    import java.util.Map;
023    import java.util.concurrent.ConcurrentHashMap;
024    
025    import javax.servlet.ServletException;
026    import javax.servlet.http.HttpServlet;
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    
033    public class DiscoveryRegistryServlet extends HttpServlet {
034        
035        private static final Log LOG = LogFactory.getLog(HTTPDiscoveryAgent.class);
036        long maxKeepAge = 1000*60*60; // 1 hour.
037        ConcurrentHashMap<String, ConcurrentHashMap<String, Long>> serviceGroups = new ConcurrentHashMap<String, ConcurrentHashMap<String, Long>>();
038        
039        @Override
040        protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
041            String group = req.getPathInfo();
042            String service = req.getHeader("service");
043            LOG.debug("Registering: group="+group+", service="+service);
044            
045            ConcurrentHashMap<String, Long> services = getServiceGroup(group);
046            services.put(service, System.currentTimeMillis());
047        }
048    
049        private ConcurrentHashMap<String, Long> getServiceGroup(String group) {
050            ConcurrentHashMap<String, Long> rc = serviceGroups.get(group);
051            if( rc == null ) {
052                rc = new ConcurrentHashMap<String, Long>();
053                serviceGroups.put(group, rc);
054            }
055            return rc;
056        }
057    
058        @Override
059        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
060            try {
061                long freshness = 1000*30;
062                String p = req.getParameter("freshness");
063                if( p!=null ) {
064                    freshness = Long.parseLong(p);
065                }
066                
067                String group = req.getPathInfo();
068                LOG.debug("group="+group);
069                ConcurrentHashMap<String, Long> services = getServiceGroup(group);
070                PrintWriter writer = resp.getWriter();
071                
072                long now = System.currentTimeMillis();
073                long dropTime = now-maxKeepAge;             
074                long minimumTime = now-freshness;
075                
076                ArrayList<String> dropList = new ArrayList<String>();
077                for (Map.Entry<String, Long> entry : services.entrySet()) {
078                    if( entry.getValue() > minimumTime ) {
079                        writer.println(entry.getKey());
080                    } else if( entry.getValue() < dropTime ) {
081                        dropList.add(entry.getKey());
082                    }
083                }
084                
085                // We might as well get rid of the really old entries.
086                for (String service : dropList) {
087                    services.remove(service);
088                }
089                
090                
091            } catch (Exception e) {
092                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occured: "+e);
093            }
094        }
095        
096        @Override
097        protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
098            String group = req.getPathInfo();
099            String service = req.getHeader("service");
100            LOG.debug("Unregistering: group="+group+", service="+service);
101            
102            ConcurrentHashMap<String, Long> services = getServiceGroup(group);
103            services.remove(service);
104        }
105            
106    }