View Javadoc

1   package org.apache.jcs.auxiliary.remote;
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.HashMap;
24  import java.util.StringTokenizer;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.jcs.auxiliary.AuxiliaryCache;
29  import org.apache.jcs.auxiliary.AuxiliaryCacheAttributes;
30  import org.apache.jcs.auxiliary.AuxiliaryCacheFactory;
31  import org.apache.jcs.engine.behavior.ICache;
32  import org.apache.jcs.engine.behavior.ICompositeCacheManager;
33  
34  /***
35   * The RemoteCacheFactory creates remote caches for the cache hub. It returns a no wait facade which
36   * is a wrapper around a no wait. The no wait object is either an active connection to a remote
37   * cache or a balking zombie if the remote cache is not accessible. It should be transparent to the
38   * clients.
39   */
40  public class RemoteCacheFactory
41      implements AuxiliaryCacheFactory
42  {
43      private final static Log log = LogFactory.getLog( RemoteCacheFactory.class );
44  
45      private String name;
46  
47      /*** store reference of facades to initiate failover */
48      private final static HashMap facades = new HashMap();
49  
50      /***
51       * For LOCAL clients we get a handle to all the failovers, but we do not register a listener
52       * with them. We create the RemoteCacheManager, but we do not get a cache. The failover runner
53       * will get a cache from the manager. When the primary is restored it will tell the manager for
54       * the failover to deregister the listener.
55       * <p>
56       * (non-Javadoc)
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          RemoteCacheAttributes rca = (RemoteCacheAttributes) iaca;
63  
64          ArrayList noWaits = new ArrayList();
65  
66          // if LOCAL
67          if ( rca.getRemoteType() == RemoteCacheAttributes.LOCAL )
68          {
69              // a list toi be turned into an array of failover server information
70              ArrayList failovers = new ArrayList();
71  
72              // not necessary if a failover list is defined
73              // REGISTER PRIMARY LISTENER
74              // if it is a primary
75              boolean primayDefined = false;
76              if ( rca.getRemoteHost() != null )
77              {
78                  primayDefined = true;
79  
80                  failovers.add( rca.getRemoteHost() + ":" + rca.getRemotePort() );
81  
82                  RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr );
83                  ICache ic = rcm.getCache( rca );
84                  if ( ic != null )
85                  {
86                      noWaits.add( ic );
87                  }
88                  else
89                  {
90                      log.info( "noWait is null" );
91                  }
92              }
93  
94              // GET HANDLE BUT DONT REGISTER A LISTENER FOR FAILOVERS
95              String failoverList = rca.getFailoverServers();
96              if ( failoverList != null )
97              {
98                  StringTokenizer fit = new StringTokenizer( failoverList, "," );
99                  int fCnt = 0;
100                 while ( fit.hasMoreElements() )
101                 {
102                     fCnt++;
103 
104                     String server = (String) fit.nextElement();
105                     failovers.add( server );
106 
107                     rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
108                     rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
109                     RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr );
110                     // add a listener if there are none, need to tell rca what
111                     // number it is at
112                     if ( ( !primayDefined && fCnt == 1 ) || noWaits.size() <= 0 )
113                     {
114                         ICache ic = rcm.getCache( rca );
115                         if ( ic != null )
116                         {
117                             noWaits.add( ic );
118                         }
119                         else
120                         {
121                             log.info( "noWait is null" );
122                         }
123                     }
124                 }
125                 // end while
126             }
127             // end if failoverList != null
128 
129             rca.setFailovers( (String[]) failovers.toArray( new String[0] ) );
130 
131             // if CLUSTER
132         }
133         else if ( rca.getRemoteType() == RemoteCacheAttributes.CLUSTER )
134         {
135             // REGISTER LISTENERS FOR EACH SYSTEM CLUSTERED CACHEs
136             StringTokenizer it = new StringTokenizer( rca.getClusterServers(), "," );
137             while ( it.hasMoreElements() )
138             {
139                 // String server = (String)it.next();
140                 String server = (String) it.nextElement();
141                 // p( "tcp server = " + server );
142                 rca.setRemoteHost( server.substring( 0, server.indexOf( ":" ) ) );
143                 rca.setRemotePort( Integer.parseInt( server.substring( server.indexOf( ":" ) + 1 ) ) );
144                 RemoteCacheManager rcm = RemoteCacheManager.getInstance( rca, cacheMgr );
145                 rca.setRemoteType( RemoteCacheAttributes.CLUSTER );
146                 ICache ic = rcm.getCache( rca );
147                 if ( ic != null )
148                 {
149                     noWaits.add( ic );
150                 }
151                 else
152                 {
153                     log.info( "noWait is null" );
154                 }
155             }
156 
157         }
158         // end if CLUSTER
159 
160         RemoteCacheNoWaitFacade rcnwf = new RemoteCacheNoWaitFacade( (RemoteCacheNoWait[]) noWaits
161             .toArray( new RemoteCacheNoWait[0] ), rca, cacheMgr );
162 
163         getFacades().put( rca.getCacheName(), rcnwf );
164 
165         return rcnwf;
166     }
167 
168     // end createCache
169 
170     /***
171      * Gets the name attribute of the RemoteCacheFactory object
172      * @return The name value
173      */
174     public String getName()
175     {
176         return this.name;
177     }
178 
179     /***
180      * Sets the name attribute of the RemoteCacheFactory object
181      * @param name The new name value
182      */
183     public void setName( String name )
184     {
185         this.name = name;
186     }
187 
188     /***
189      * The facades are what the cache hub talks to.
190      * @return Returns the facades.
191      */
192     public static HashMap getFacades()
193     {
194         return facades;
195     }
196 
197 }