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.rmi.Naming;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheObserver;
27  import org.apache.jcs.auxiliary.remote.behavior.IRemoteCacheService;
28  import org.apache.jcs.engine.behavior.ICacheRestore;
29  
30  /***
31   * Used to repair the remote caches managed by the associated instance of RemoteCacheManager.
32   * <p>
33   * When there is an error the monitor kicks off. The Failover runner starts looks for a manager with
34   * a connection to a remote cache that is not in error. If a manager's connection to a remote cache
35   * is found to be in error, the restorer kicks off and tries to reconnect. When it is succesful, the
36   * status of the manager changes.
37   * <p>
38   * When the failover runner finds that the primary is in good shape, it will switch back. Switching
39   * back invovles setting the first no wait on the no wait facade.
40   */
41  public class RemoteCacheRestore
42      implements ICacheRestore
43  {
44      private final static Log log = LogFactory.getLog( RemoteCacheRestore.class );
45  
46      private final RemoteCacheManager rcm;
47  
48      // private final AuxiliaryCacheManager rcm;
49      private boolean canFix = true;
50  
51      private Object remoteObj;
52  
53      /***
54       * Constructs with the given instance of RemoteCacheManager.
55       * @param rcm
56       */
57      public RemoteCacheRestore( RemoteCacheManager rcm )
58      {
59          // public RemoteCacheRestore(AuxiliaryCacheManager rcm) {
60          this.rcm = rcm;
61      }
62  
63      /***
64       * Returns true if the connection to the remote host for the corresponding cache manager can be
65       * successfully re-established.
66       * <p>
67       * @return true if we found a failover server
68       */
69      public boolean canFix()
70      {
71          if ( !canFix )
72          {
73              return canFix;
74          }
75          String registry = "//" + rcm.host + ":" + rcm.port + "/" + rcm.service;
76          if ( log.isInfoEnabled() )
77          {
78              log.info( "looking up server " + registry );
79          }
80          try
81          {
82              remoteObj = Naming.lookup( registry );
83              if ( log.isInfoEnabled() )
84              {
85                  log.info( "looking up server " + registry );
86              }
87          }
88          catch ( Exception ex )
89          {
90              log.error( "host=" + rcm.host + "; port" + rcm.port + "; service=" + rcm.service );
91              canFix = false;
92          }
93          return canFix;
94      }
95  
96      /***
97       * Fixes up all the caches managed by the associated cache manager.
98       */
99      public void fix()
100     {
101         if ( !canFix )
102         {
103             return;
104         }
105         rcm.fixCaches( (IRemoteCacheService) remoteObj, (IRemoteCacheObserver) remoteObj );
106 
107         if ( log.isInfoEnabled() )
108         {
109             String msg = "Remote connection to " + "//" + rcm.host + ":" + rcm.port + "/" + rcm.service + " resumed.";
110             log.info( msg );
111         }
112     }
113 }