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