Source Code Cross Referenced for CacheClient.java in  » Web-Server » xsocket » org » xcache » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » xsocket » org.xcache 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.