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