View Javadoc

1   package org.apache.jcs.auxiliary.lateral.socket.tcp;
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.ArrayList;
23  import java.util.StringTokenizer;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.jcs.auxiliary.AuxiliaryCache;
28  import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
29  import org.apache.jcs.auxiliary.lateral.LateralCacheAbstractFactory;
30  import org.apache.jcs.auxiliary.lateral.LateralCacheNoWait;
31  import org.apache.jcs.auxiliary.lateral.LateralCacheNoWaitFacade;
32  import org.apache.jcs.auxiliary.lateral.behavior.ILateralCacheAttributes;
33  import org.apache.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
34  import org.apache.jcs.auxiliary.lateral.socket.tcp.discovery.UDPDiscoveryManager;
35  import org.apache.jcs.auxiliary.lateral.socket.tcp.discovery.UDPDiscoveryService;
36  import org.apache.jcs.engine.behavior.ICache;
37  import org.apache.jcs.engine.behavior.ICompositeCacheManager;
38  
39  /***
40   * Constructs a LateralCacheNoWaitFacade for the given configuration. Each
41   * lateral service / local relationship is managed by one manager. This manager
42   * can have multiple caches. The remote relationships are consolidated and
43   * restored via these managers.
44   * <p>
45   * The facade provides a front to the composite cache so the implementation is
46   * transparent.
47   *
48   */
49  public class LateralTCPCacheFactory
50      extends LateralCacheAbstractFactory
51  {
52      private final static Log log = LogFactory.getLog( LateralTCPCacheFactory.class );
53  
54      /*
55       * (non-Javadoc)
56       *
57       * @see org.apache.jcs.auxiliary.AuxiliaryCacheFactory#createCache(org.apache.jcs.auxiliary.AuxiliaryCacheAttributes,
58       *      org.apache.jcs.engine.behavior.ICompositeCacheManager)
59       */
60      public AuxiliaryCache createCache( AuxiliaryCacheAttributes iaca, ICompositeCacheManager cacheMgr )
61      {
62          ITCPLateralCacheAttributes lac = (ITCPLateralCacheAttributes) iaca;
63          ArrayList noWaits = new ArrayList();
64  
65          // pars up the tcp servers and set the tcpServer value and
66          // get the manager and then get the cache
67          // no servers are required.
68          if ( lac.getTcpServers() != null )
69          {
70              StringTokenizer it = new StringTokenizer( lac.getTcpServers(), "," );
71              if ( log.isDebugEnabled() )
72              {
73                  log.debug( "Configured for [" + it.countTokens() + "]  servers." );
74              }
75              while ( it.hasMoreElements() )
76              {
77                  String server = (String) it.nextElement();
78                  if ( log.isDebugEnabled() )
79                  {
80                      log.debug( "tcp server = " + server );
81                  }
82                  ITCPLateralCacheAttributes lacC = (ITCPLateralCacheAttributes) lac.copy();
83                  lacC.setTcpServer( server );
84                  LateralTCPCacheManager lcm = LateralTCPCacheManager.getInstance( lacC, cacheMgr );
85                  ICache ic = lcm.getCache( lacC.getCacheName() );
86                  if ( ic != null )
87                  {
88                      noWaits.add( ic );
89                  }
90                  else
91                  {
92                      if ( log.isDebugEnabled() )
93                      {
94                          log.debug( "noWait is null, no lateral connection made" );
95                      }
96                  }
97              }
98          }
99  
100         createListener( (ILateralCacheAttributes) iaca, cacheMgr );
101 
102         // create the no wait facade.
103         LateralCacheNoWaitFacade lcnwf = new LateralCacheNoWaitFacade( (LateralCacheNoWait[]) noWaits
104             .toArray( new LateralCacheNoWait[0] ), (ILateralCacheAttributes)iaca );
105 
106         // create udp discovery if available.
107         createDiscoveryService( lac, lcnwf, cacheMgr );
108 
109         return lcnwf;
110     }
111 
112     /*
113      * (non-Javadoc)
114      *
115      * @see org.apache.jcs.auxiliary.lateral.LateralCacheAbstractFactory#createListener(org.apache.jcs.auxiliary.lateral.LateralCacheAttributes,
116      *      org.apache.jcs.engine.behavior.ICompositeCacheManager)
117      */
118     public void createListener( ILateralCacheAttributes lac, ICompositeCacheManager cacheMgr )
119     {
120         ITCPLateralCacheAttributes attr = (ITCPLateralCacheAttributes) lac;
121         // don't create a listener if we are not receiving.
122         if ( attr.isReceive() )
123         {
124             if ( log.isInfoEnabled() )
125             {
126                 log.info( "Creating listener for " + lac );
127             }
128 
129             try
130             {
131                 // make a listener. if one doesn't exist
132                 LateralTCPListener.getInstance( attr, cacheMgr );
133             }
134             catch ( Exception e )
135             {
136                 log.error( "Problem creating lateral listener", e );
137             }
138         }
139         else
140         {
141             if ( log.isDebugEnabled() )
142             {
143                 log.debug( "Not creating a listener since we are not receiving." );
144             }
145         }
146     }
147 
148     /***
149      * Creates the discovery service. Only creates this for tcp laterals right
150      * now.
151      *
152      * @param lac
153      *            ITCPLateralCacheAttributes
154      * @param lcnwf
155      * @param cacheMgr
156      * @return null if none is created.
157      */
158     private UDPDiscoveryService createDiscoveryService( ITCPLateralCacheAttributes lac, LateralCacheNoWaitFacade lcnwf,
159                                                        ICompositeCacheManager cacheMgr )
160     {
161         UDPDiscoveryService discovery = null;
162 
163         // create the UDP discovery for the TCP lateral
164         if ( lac.isUdpDiscoveryEnabled() )
165         {
166             // need a factory for this so it doesn't
167             // get dereferenced, also we don't want one for every region.
168             discovery = UDPDiscoveryManager.getInstance().getService( lac, cacheMgr );
169 
170             discovery.addNoWaitFacade( lcnwf, lac.getCacheName() );
171 
172             if ( log.isInfoEnabled() )
173             {
174                 log.info( "Created UDPDiscoveryService for TCP lateral cache." );
175             }
176         }
177         return discovery;
178     }
179 }