1 package org.apache.jcs.auxiliary.remote;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }