View Javadoc

1   package org.apache.jcs.auxiliary.lateral.socket.tcp.discovery;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
28  import org.apache.jcs.engine.behavior.ICompositeCacheManager;
29  
30  /***
31   * This manages UDPDiscovery Services. We should end up with one service per
32   * Lateral Cache Manager Instance. One service works for multiple regions. We
33   * don't want a connection for each region.
34   *
35   * @author Aaron Smuts
36   *
37   */
38  public class UDPDiscoveryManager
39  {
40      private final static Log log = LogFactory.getLog( UDPDiscoveryManager.class );
41  
42      private static UDPDiscoveryManager INSTANCE = new UDPDiscoveryManager();
43  
44      private Map services = new HashMap();
45  
46      private UDPDiscoveryManager()
47      {
48          // noopt
49      }
50  
51      /***
52       * Singelton
53       *
54       * @return UDPDiscoveryManager
55       */
56      public static UDPDiscoveryManager getInstance()
57      {
58          return INSTANCE;
59      }
60  
61      /***
62       * Returns the UDP Discovery service associated with this instance.
63       *
64       * @param lca
65       *            ITCPLateralCacheAttributes
66       * @param cacheMgr
67       * @return
68       */
69      public synchronized UDPDiscoveryService getService( ITCPLateralCacheAttributes lca, ICompositeCacheManager cacheMgr )
70      {
71          UDPDiscoveryService service = getService( lca.getUdpDiscoveryAddr(), lca.getUdpDiscoveryPort(), lca.getTcpListenerPort(), cacheMgr );
72  
73          // TODO find a way to remote these attributes from the service, the manager needs it on disocvery.
74          service.setTcpLateralCacheAttributes( lca );
75          return service;
76      }
77  
78      /***
79       * Creates a service for the address and port if one doesn't exist already.
80       * <p>
81       * TODO we may need to key this using the listener port too
82       *
83       * @param discoveryAddress
84       * @param discoveryPort
85       * @param servicePort
86       * @param cacheMgr
87       * @return
88       */
89      public synchronized UDPDiscoveryService getService( String discoveryAddress, int discoveryPort, int servicePort,
90                                                         ICompositeCacheManager cacheMgr )
91      {
92          String key = discoveryAddress + ":" + discoveryPort;
93  
94          UDPDiscoveryService service = (UDPDiscoveryService) services.get( key );
95          if ( service == null )
96          {
97              if ( log.isInfoEnabled() )
98              {
99                  log.info( "Creating service for address:port [" + key + "]" );
100             }
101 
102             service = new UDPDiscoveryService( discoveryAddress, discoveryPort, servicePort, cacheMgr );
103             services.put( key, service );
104         }
105 
106         if ( log.isDebugEnabled() )
107         {
108             log.debug( "Returning service [" + service + "] for key [" + key + "]" );
109         }
110 
111         return service;
112 
113     }
114 
115 }