1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 21 22 23 package org.apache.james.util.connection; 24 25 import java.util.HashMap; 26 27 /** 28 * Map which holds information about the connection count per ip 29 */ 30 public class ConnectionPerIpMap { 31 32 /** 33 * A HashMap of clientip and connections 34 */ 35 private final HashMap connectionPerIP = new HashMap(); 36 37 /** 38 * Raise the connectionCount for the given ipAdrress 39 * 40 * @param ip raise the connectionCount for the given ipAddress 41 */ 42 public synchronized void addConnectionPerIP (String ip) { 43 connectionPerIP.put(ip,new Integer(getConnectionPerIP(ip) +1)); 44 } 45 46 /** 47 * Get the count of connections for the given ip 48 * 49 * @param ip the ipAddress to get the connections for. 50 * @return count 51 */ 52 public synchronized int getConnectionPerIP(String ip) { 53 Integer value = (Integer) connectionPerIP.get(ip); 54 if (value == null) { 55 return 0; 56 } 57 return value.intValue(); 58 } 59 60 /** 61 * Set the connection count for the given ipAddress 62 * 63 * @param ip ipAddres for which we want to set the count 64 */ 65 public synchronized void removeConnectionPerIP (String ip) { 66 67 int count = getConnectionPerIP(ip); 68 if (count > 1) { 69 connectionPerIP.put(ip,new Integer(count -1)); 70 } else { 71 // not need this entry any more 72 connectionPerIP.remove(ip); 73 } 74 75 } 76 77 /** 78 * Clear the connection count map 79 */ 80 public synchronized void clearConnectionPerIP () { 81 connectionPerIP.clear(); 82 } 83 84 }