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