1 package org.apache.commons.jcs3.auxiliary.remote;
2
3 import java.io.IOException;
4 import java.rmi.registry.Registry;
5 import java.util.concurrent.ConcurrentHashMap;
6 import java.util.concurrent.ConcurrentMap;
7
8 import org.apache.commons.jcs3.auxiliary.remote.behavior.IRemoteCacheAttributes;
9 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
10 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
11 import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
12
13 /*
14 * Licensed to the Apache Software Foundation (ASF) under one
15 * or more contributor license agreements. See the NOTICE file
16 * distributed with this work for additional information
17 * regarding copyright ownership. The ASF licenses this file
18 * to you under the Apache License, Version 2.0 (the
19 * "License"); you may not use this file except in compliance
20 * with the License. You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing,
25 * software distributed under the License is distributed on an
26 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
27 * KIND, either express or implied. See the License for the
28 * specific language governing permissions and limitations
29 * under the License.
30 */
31 /**
32 * Test RemoteCache factory that skips actual connection attempt
33 */
34 public class TestRemoteCacheFactory extends RemoteCacheFactory
35 {
36 /** Contains mappings of RemoteLocation instance to RemoteCacheManager instance. */
37 protected ConcurrentMap<RemoteLocation, RemoteCacheManager> managers;
38
39 /**
40 * Returns an instance of RemoteCacheManager for the given connection parameters.
41 * <p>
42 * Host and Port uniquely identify a manager instance.
43 * <p>
44 * @param cattr
45 *
46 * @return The instance value or null if no such manager exists
47 */
48 @Override
49 public RemoteCacheManager getManager( final IRemoteCacheAttributes cattr )
50 {
51 final RemoteCacheAttributes rca = (RemoteCacheAttributes) cattr.clone();
52 if (rca.getRemoteLocation() == null)
53 {
54 rca.setRemoteLocation("", Registry.REGISTRY_PORT);
55 }
56
57 return managers.get(rca.getRemoteLocation());
58 }
59
60 /**
61 * Returns an instance of RemoteCacheManager for the given connection parameters.
62 * <p>
63 * Host and Port uniquely identify a manager instance.
64 * <p>
65 * If the connection cannot be established, zombie objects will be used for future recovery
66 * purposes.
67 * <p>
68 * @param cattr the cache configuration object
69 * @param cacheMgr the cache manager
70 * @param cacheEventLogger the event logger
71 * @param elementSerializer the serializer to use for sending and receiving
72 *
73 * @return The instance value, never null
74 */
75 @Override
76 public RemoteCacheManager getManager( final IRemoteCacheAttributes cattr,
77 final ICompositeCacheManager cacheMgr,
78 final ICacheEventLogger cacheEventLogger,
79 final IElementSerializer elementSerializer )
80 {
81 final RemoteCacheAttributes rca = (RemoteCacheAttributes) cattr.clone();
82 if (rca.getRemoteLocation() == null)
83 {
84 rca.setRemoteLocation("", Registry.REGISTRY_PORT);
85 }
86
87 return managers.computeIfAbsent(rca.getRemoteLocation(), key -> {
88
89 return new TestRemoteCacheManager(rca, cacheMgr, null, cacheEventLogger, elementSerializer);
90 });
91 }
92
93 /**
94 * @see org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheFactory#initialize()
95 */
96 @Override
97 public void initialize()
98 {
99 managers = new ConcurrentHashMap<>();
100 }
101
102 /**
103 * @see org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheFactory#dispose()
104 */
105 @Override
106 public void dispose()
107 {
108 managers.values().forEach(RemoteCacheManager::release);
109 managers.clear();
110 }
111
112 // Mock
113 public class TestRemoteCacheManager extends RemoteCacheManager
114 {
115 protected TestRemoteCacheManager(IRemoteCacheAttributes cattr, ICompositeCacheManager cacheMgr, RemoteCacheMonitor monitor, ICacheEventLogger cacheEventLogger,
116 IElementSerializer elementSerializer)
117 {
118 super(cattr, cacheMgr, monitor, cacheEventLogger, elementSerializer);
119 }
120
121 @Override
122 protected void lookupRemoteService() throws IOException
123 {
124 // Skip
125 }
126
127 @Override
128 public void removeRemoteCacheListener(IRemoteCacheAttributes cattr) throws IOException
129 {
130 // Skip
131 }
132 }
133 }