001package org.apache.commons.jcs.utils.discovery; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.HashMap; 023import java.util.Map; 024 025import org.apache.commons.jcs.engine.behavior.ICompositeCacheManager; 026import org.apache.commons.jcs.engine.behavior.IProvideScheduler; 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029 030/** 031 * This manages UDPDiscovery Services. We should end up with one service per Lateral Cache Manager 032 * Instance. One service works for multiple regions. We don't want a connection for each region. 033 * <p> 034 * @author Aaron Smuts 035 */ 036public class UDPDiscoveryManager 037{ 038 /** The logger */ 039 private static final Log log = LogFactory.getLog( UDPDiscoveryManager.class ); 040 041 /** Singleton instance */ 042 private static UDPDiscoveryManager INSTANCE = new UDPDiscoveryManager(); 043 044 /** Known services */ 045 private final Map<String, UDPDiscoveryService> services = new HashMap<String, UDPDiscoveryService>(); 046 047 /** private for singleton */ 048 private UDPDiscoveryManager() 049 { 050 // noopt 051 } 052 053 /** 054 * Singleton 055 * <p> 056 * @return UDPDiscoveryManager 057 */ 058 public static UDPDiscoveryManager getInstance() 059 { 060 return INSTANCE; 061 } 062 063 /** 064 * Creates a service for the address and port if one doesn't exist already. 065 * <p> 066 * We need to key this using the listener port too. TODO think of making one discovery service 067 * work for multiple types of clients. 068 * <p> 069 * @param discoveryAddress 070 * @param discoveryPort 071 * @param servicePort 072 * @param cacheMgr 073 * @return UDPDiscoveryService 074 */ 075 public synchronized UDPDiscoveryService getService( String discoveryAddress, int discoveryPort, int servicePort, 076 ICompositeCacheManager cacheMgr ) 077 { 078 String key = discoveryAddress + ":" + discoveryPort + ":" + servicePort; 079 080 UDPDiscoveryService service = services.get( key ); 081 if ( service == null ) 082 { 083 if ( log.isInfoEnabled() ) 084 { 085 log.info( "Creating service for address:port:servicePort [" + key + "]" ); 086 } 087 088 UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes(); 089 attributes.setUdpDiscoveryAddr( discoveryAddress ); 090 attributes.setUdpDiscoveryPort( discoveryPort ); 091 attributes.setServicePort( servicePort ); 092 093 service = new UDPDiscoveryService( attributes ); 094 095 // register for shutdown notification 096 cacheMgr.registerShutdownObserver( service ); 097 098 // inject scheduler 099 if ( cacheMgr instanceof IProvideScheduler) 100 { 101 service.setScheduledExecutorService(((IProvideScheduler)cacheMgr).getScheduledExecutorService()); 102 } 103 104 service.startup(); 105 services.put( key, service ); 106 } 107 108 if ( log.isDebugEnabled() ) 109 { 110 log.debug( "Returning service [" + service + "] for key [" + key + "]" ); 111 } 112 113 return service; 114 } 115}