1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.activemq.transport.discovery.http; 18 19 import java.io.IOException; 20 import java.io.PrintWriter; 21 import java.util.ArrayList; 22 import java.util.Map; 23 import java.util.concurrent.ConcurrentHashMap; 24 25 import javax.servlet.ServletException; 26 import javax.servlet.http.HttpServlet; 27 import javax.servlet.http.HttpServletRequest; 28 import javax.servlet.http.HttpServletResponse; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 public class DiscoveryRegistryServlet extends HttpServlet { 34 35 private static final Log LOG = LogFactory.getLog(HTTPDiscoveryAgent.class); 36 long maxKeepAge = 1000*60*60; // 1 hour. 37 ConcurrentHashMap<String, ConcurrentHashMap<String, Long>> serviceGroups = new ConcurrentHashMap<String, ConcurrentHashMap<String, Long>>(); 38 39 @Override 40 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 41 String group = req.getPathInfo(); 42 String service = req.getHeader("service"); 43 LOG.debug("Registering: group="+group+", service="+service); 44 45 ConcurrentHashMap<String, Long> services = getServiceGroup(group); 46 services.put(service, System.currentTimeMillis()); 47 } 48 49 private ConcurrentHashMap<String, Long> getServiceGroup(String group) { 50 ConcurrentHashMap<String, Long> rc = serviceGroups.get(group); 51 if( rc == null ) { 52 rc = new ConcurrentHashMap<String, Long>(); 53 serviceGroups.put(group, rc); 54 } 55 return rc; 56 } 57 58 @Override 59 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 60 try { 61 long freshness = 1000*30; 62 String p = req.getParameter("freshness"); 63 if( p!=null ) { 64 freshness = Long.parseLong(p); 65 } 66 67 String group = req.getPathInfo(); 68 LOG.debug("group="+group); 69 ConcurrentHashMap<String, Long> services = getServiceGroup(group); 70 PrintWriter writer = resp.getWriter(); 71 72 long now = System.currentTimeMillis(); 73 long dropTime = now-maxKeepAge; 74 long minimumTime = now-freshness; 75 76 ArrayList<String> dropList = new ArrayList<String>(); 77 for (Map.Entry<String, Long> entry : services.entrySet()) { 78 if( entry.getValue() > minimumTime ) { 79 writer.println(entry.getKey()); 80 } else if( entry.getValue() < dropTime ) { 81 dropList.add(entry.getKey()); 82 } 83 } 84 85 // We might as well get rid of the really old entries. 86 for (String service : dropList) { 87 services.remove(service); 88 } 89 90 91 } catch (Exception e) { 92 resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occured: "+e); 93 } 94 } 95 96 @Override 97 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 98 String group = req.getPathInfo(); 99 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 }