KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > core > ConfigBasedConnectionAddressProvider


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.core;
5
6 import com.tc.config.schema.dynamic.ConfigItem;
7 import com.tc.config.schema.dynamic.ConfigItemListener;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Arrays JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.NoSuchElementException JavaDoc;
14
15 public class ConfigBasedConnectionAddressProvider implements ConnectionAddressProvider, ConfigItemListener {
16
17   private final ConfigItem source;
18
19   private int policy;
20   private List JavaDoc currentAddresses = new ArrayList JavaDoc();
21   private int current = -1;
22
23   public ConfigBasedConnectionAddressProvider(ConfigItem source) {
24     this(ROUND_ROBIN, source);
25   }
26
27   private ConfigBasedConnectionAddressProvider(int policy, ConfigItem source) {
28     this.policy = policy;
29     this.source = source;
30
31     this.source.addListener(this);
32     this.currentAddresses.addAll(Arrays.asList((ConnectionInfo[]) source.getObject()));
33
34     init();
35   }
36
37   private void init() {
38     switch (policy) {
39       case ROUND_ROBIN:
40         if (currentAddresses.size() == 0) {
41           current = -1;
42         } else if (current < 0 || current >= currentAddresses.size()) {
43           current = 0;
44         }
45         break;
46       case LINEAR:
47         if (currentAddresses.size() == 0) {
48           current = -1;
49         } else if (current < 0) {
50           current = 0;
51         }
52         break;
53       default:
54         throw new AssertionError JavaDoc("Unimplemented policy for ConnectionAddressProvider !");
55     }
56   }
57
58   public synchronized void valueChanged(Object JavaDoc oldValue, Object JavaDoc newValue) {
59     ConnectionInfo currentServer;
60
61     try {
62       currentServer = getConnectionInfo();
63     } catch (NoSuchElementException JavaDoc nsee) {
64       currentServer = null;
65     }
66
67     ConnectionInfo[] newAddresses = (ConnectionInfo[]) this.source.getObject();
68     this.currentAddresses.clear();
69     this.currentAddresses.addAll(Arrays.asList(newAddresses));
70
71     init();
72
73     // Go find the first server in the new list that's the same as the server in the old list, and put our position
74
// there. If we don't find one, the init() above will have simply made us start over anyway.
75
Iterator JavaDoc iter = this.currentAddresses.iterator();
76     int pos = 0;
77     while (iter.hasNext()) {
78       if (currentServer != null && iter.next().equals(currentServer)) {
79         current = pos;
80         break;
81       }
82       ++pos;
83     }
84   }
85
86   private void reinit() {
87     current = -1;
88     init();
89   }
90
91   public String JavaDoc getHostname() {
92     return getConnectionInfo().getHostname();
93   }
94
95   public int getPortNumber() {
96     return getConnectionInfo().getPort();
97   }
98
99   public synchronized int getCount() {
100     return currentAddresses.size();
101   }
102
103   public synchronized boolean hasNext() {
104     switch (policy) {
105       case ROUND_ROBIN:
106         return currentAddresses.size() > 0;
107       case LINEAR:
108         return currentAddresses.size() > 0 && current < currentAddresses.size() - 1;
109       default:
110         return false;
111     }
112   }
113
114   public synchronized ConnectionInfo getConnectionInfo() {
115     if (current > -1 && current < currentAddresses.size()) { return (ConnectionInfo) currentAddresses.get(current); }
116     throw new NoSuchElementException JavaDoc();
117   }
118
119   public synchronized ConnectionInfo next() {
120     goNext();
121     return getConnectionInfo();
122   }
123
124   private void goNext() {
125     switch (policy) {
126       case ROUND_ROBIN:
127         if (currentAddresses.size() == 0) {
128           current = -1;
129         } else if (current < 0 || current >= currentAddresses.size() - 1) {
130           current = 0;
131         } else {
132           current++;
133         }
134         break;
135       case LINEAR:
136         if (currentAddresses.size() == 0) {
137           current = -1;
138         } else if (current < currentAddresses.size()) {
139           current++;
140         }
141         break;
142       default:
143         throw new AssertionError JavaDoc("Unimplemented policy for ConnectionAddressProvider !");
144     }
145   }
146
147   public synchronized void setPolicy(int policy) {
148     switch (policy) {
149       case ROUND_ROBIN:
150       case LINEAR:
151         this.policy = policy;
152         break;
153       default:
154         throw new AssertionError JavaDoc("Unimplemented policy for ConnectionAddressProvider !");
155     }
156     reinit();
157   }
158
159   public String JavaDoc toString() {
160     return "ConnectionAddressProvider(current = " + current + ", policy = "
161            + (policy == ROUND_ROBIN ? "ROUND_ROBIN" : "LINEAR") + ")[" + currentAddresses + "]";
162   }
163 }
164
Popular Tags