001: package org.xcache;
002:
003: import java.io.IOException;
004: import java.util.Collection;
005: import java.util.Map;
006: import java.util.Set;
007: import java.util.logging.Level;
008: import java.util.logging.Logger;
009:
010: import net.sf.jsr107cache.Cache;
011: import net.sf.jsr107cache.CacheEntry;
012: import net.sf.jsr107cache.CacheException;
013: import net.sf.jsr107cache.CacheListener;
014: import net.sf.jsr107cache.CacheStatistics;
015:
016: import org.xsocket.stream.IBlockingConnection;
017:
018: final class CacheClient implements Cache {
019:
020: private static final Logger LOG = Logger
021: .getLogger(CacheClient.class.getName());
022:
023: private ICacheServerLocator locator = null;
024:
025: public CacheClient(ICacheServerLocator locator) {
026: this .locator = locator;
027: }
028:
029: ICacheServerLocator getLocator() {
030: return locator;
031: }
032:
033: public Object put(Object key, Object value) {
034:
035: Object result = null;
036:
037: Item keyItem = new Item(key);
038: Item valueItem = new Item(value);
039:
040: int segment = locator.computeSegement(key);
041:
042: IBlockingConnection connection = null;
043: try {
044: Address address = locator.getServiceAddress(segment);
045: connection = ConnectionPool.getInstance().getConnection(
046: address);
047:
048: connection.markWritePosition();
049: connection.write((int) 0); // emtpy length field
050: int length = 0;
051:
052: length += connection.write(CacheServer.CMD_PUT);
053: length += connection.write(segment);
054: length += keyItem.writeTo(connection);
055: length += valueItem.writeTo(connection);
056:
057: connection.resetToWriteMark();
058: connection.write(length);
059:
060: connection.flush();
061:
062: connection.readInt(); // first position is length
063: byte response = connection.readByte();
064: if (response == CacheServer.RESULT_OK_WITHOUT_RETURNVALUE) {
065: result = null;
066:
067: } else if (response == CacheServer.RESULT_OK_WITH_RETURNVALUE) {
068: Item oldValue = Item.readFrom(connection);
069: result = oldValue.getValue();
070:
071: } else {
072: throw new RuntimeException("unexpected result");
073: }
074:
075: if (LOG.isLoggable(Level.FINE)) {
076: LOG.fine("item for key " + key + " (segment " + segment
077: + ") has been inserted (cache server "
078: + connection.getRemoteAddress() + "/"
079: + connection.getRemotePort() + ")");
080: }
081: connection.close();
082:
083: } catch (Exception e) {
084: if (connection != null) {
085: try {
086: ConnectionPool.getInstance().destroyConnection(
087: connection);
088: } catch (IOException ignore) {
089: }
090: ;
091: }
092: throw new RuntimeException("error occured by put " + key
093: + " into the cache. Reason: " + e.toString());
094: }
095: return result;
096: }
097:
098: public Object get(Object key) {
099: Object result = null;
100:
101: Item keyItem = new Item(key);
102:
103: int segment = locator.computeSegement(key);
104:
105: IBlockingConnection connection = null;
106: try {
107: Address address = locator.getServiceAddress(segment);
108: connection = ConnectionPool.getInstance().getConnection(
109: address);
110:
111: connection.markWritePosition();
112: connection.write((int) 0); // emtpy length field
113: int length = 0;
114:
115: length += connection.write(CacheServer.CMD_GET);
116:
117: length += connection.write(segment);
118: length += keyItem.writeTo(connection);
119:
120: connection.resetToWriteMark();
121: connection.write(length);
122:
123: connection.flush();
124:
125: connection.readInt(); // first position is length
126: byte response = connection.readByte();
127:
128: if (response == CacheServer.RESULT_OK_WITHOUT_RETURNVALUE) {
129: result = null;
130:
131: } else if (response == CacheServer.RESULT_OK_WITH_RETURNVALUE) {
132: Item value = Item.readFrom(connection);
133: result = value.getValue();
134:
135: } else {
136: throw new RuntimeException("unexpected result");
137: }
138:
139: if (LOG.isLoggable(Level.FINE)) {
140: if (result != null) {
141: LOG.fine("item for key " + key + " (segment "
142: + segment
143: + ") has retrieved (cache server "
144: + connection.getRemoteAddress() + "/"
145: + connection.getRemotePort() + ")");
146: } else {
147: LOG.fine("no item for key " + key + " (segment "
148: + segment
149: + ") found on server (cache server "
150: + connection.getRemoteAddress() + "/"
151: + connection.getRemotePort() + ")");
152: }
153: }
154:
155: connection.close();
156:
157: } catch (Exception e) {
158: if (connection != null) {
159: try {
160: ConnectionPool.getInstance().destroyConnection(
161: connection);
162: } catch (IOException ignore) {
163: }
164: ;
165: }
166: throw new RuntimeException("error occured by put " + key
167: + " into the cache. Reason: " + e.toString());
168: }
169: return result;
170: }
171:
172: public boolean containsKey(Object key) {
173: // TODO Auto-generated method stub
174: return false;
175: }
176:
177: public boolean containsValue(Object value) {
178: // TODO Auto-generated method stub
179: return false;
180: }
181:
182: public Set entrySet() {
183: // TODO Auto-generated method stub
184: return null;
185: }
186:
187: public Map getAll(Collection keys) throws CacheException {
188: // TODO Auto-generated method stub
189: return null;
190: }
191:
192: public CacheEntry getCacheEntry(Object key) {
193: // TODO Auto-generated method stub
194: return null;
195: }
196:
197: public CacheStatistics getCacheStatistics() {
198: // TODO Auto-generated method stub
199: return null;
200: }
201:
202: public boolean isEmpty() {
203: // TODO Auto-generated method stub
204: return false;
205: }
206:
207: public Set keySet() {
208: // TODO Auto-generated method stub
209: return null;
210: }
211:
212: public void load(Object key) throws CacheException {
213: // TODO Auto-generated method stub
214:
215: }
216:
217: public void loadAll(Collection keys) throws CacheException {
218: // TODO Auto-generated method stub
219:
220: }
221:
222: public Object peek(Object key) {
223: // TODO Auto-generated method stub
224: return null;
225: }
226:
227: public void putAll(Map t) {
228: // TODO Auto-generated method stub
229:
230: }
231:
232: public Object remove(Object key) {
233: // TODO Auto-generated method stub
234: return null;
235: }
236:
237: public int size() {
238: // TODO Auto-generated method stub
239: return 0;
240: }
241:
242: public void evict() {
243: // TODO Auto-generated method stub
244:
245: }
246:
247: public void clear() {
248: // TODO Auto-generated method stub
249:
250: }
251:
252: public Collection values() {
253: // TODO Auto-generated method stub
254: return null;
255: }
256:
257: public void addListener(CacheListener listener) {
258: // TODO Auto-generated method stub
259:
260: }
261:
262: public void removeListener(CacheListener listener) {
263: // TODO Auto-generated method stub
264:
265: }
266: }
|