1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.terracotta;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.concurrent.CopyOnWriteArrayList;
23
24 import net.sf.ehcache.cluster.CacheCluster;
25 import net.sf.ehcache.cluster.ClusterNode;
26 import net.sf.ehcache.cluster.ClusterScheme;
27 import net.sf.ehcache.cluster.ClusterTopologyListener;
28
29 public class MockCacheCluster implements CacheCluster {
30
31 private final List<ClusterTopologyListener> listeners = new CopyOnWriteArrayList<ClusterTopologyListener>();
32 private final ClusterNode currentNode = new ClusterNode() {
33
34 public String getIp() {
35 return "127.0.0.1";
36 }
37
38 public String getId() {
39 return "1";
40 }
41
42 public String getHostname() {
43 return "dummyHostName";
44 }
45 };
46
47 public void removeAllListeners() {
48 this.listeners.clear();
49 }
50
51 public void fireCurrentNodeLeft() {
52 for (ClusterTopologyListener listener : listeners) {
53 listener.clusterOffline(currentNode);
54 }
55 for (ClusterTopologyListener listener : listeners) {
56 listener.nodeLeft(currentNode);
57 }
58 }
59
60 public void fireClusterOffline() {
61 for (ClusterTopologyListener listener : listeners) {
62 listener.clusterOffline(currentNode);
63 }
64 }
65
66 public void fireClusterOnline() {
67 for (ClusterTopologyListener listener : listeners) {
68 listener.clusterOnline(currentNode);
69 }
70 }
71
72 public void fireThisNodeJoined() {
73 for (ClusterTopologyListener listener : listeners) {
74 listener.nodeJoined(currentNode);
75 }
76 }
77
78 public boolean addTopologyListener(ClusterTopologyListener listener) {
79 boolean rv = listeners.add(listener);
80 if (rv) {
81 listener.nodeJoined(currentNode);
82 listener.clusterOnline(currentNode);
83 }
84 return rv;
85 }
86
87 public ClusterNode getCurrentNode() {
88 return currentNode;
89 }
90
91 public Collection<ClusterNode> getNodes() {
92 return Collections.singletonList(currentNode);
93 }
94
95 public ClusterScheme getScheme() {
96 return ClusterScheme.TERRACOTTA;
97 }
98
99 public boolean isClusterOnline() {
100 return true;
101 }
102
103 public boolean removeTopologyListener(ClusterTopologyListener listener) {
104 return listeners.remove(listener);
105 }
106
107 public ClusterNode waitUntilNodeJoinsCluster() {
108 return currentNode;
109 }
110
111 public List<ClusterTopologyListener> getTopologyListeners() {
112 return this.listeners;
113 }
114
115 }