Source Code Cross Referenced for ClusterCacheFactory.java in  » Cache » shiftone-cache » org » shiftone » cache » decorator » cluster » 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
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » shiftone cache » org.shiftone.cache.decorator.cluster 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.shiftone.cache.decorator.cluster;
002:
003:        import org.shiftone.cache.Cache;
004:        import org.shiftone.cache.CacheException;
005:        import org.shiftone.cache.CacheFactory;
006:        import org.shiftone.cache.util.Log;
007:        import org.shiftone.cache.util.WeakMap;
008:
009:        import java.util.Random;
010:
011:        /**
012:         * @version $Revision: 1.12 $
013:         * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
014:         */
015:        public class ClusterCacheFactory implements  CacheFactory {
016:
017:            private static final Log LOG = new Log(ClusterCacheFactory.class);
018:            private static final Random random = new Random();
019:            public static final String DEFAULT_BUS_NAME = "shiftone-cache";
020:            private WeakMap registeredCacheMap = new WeakMap();
021:            private final long instanceId = random.nextLong();
022:            private ClusterBus bus;
023:            private CacheFactory delegate = null; // configured
024:            private String channelProperties = ""; // configured
025:            private String busName = DEFAULT_BUS_NAME; // configured
026:
027:            private void init() throws CacheException {
028:
029:                // if the bus has not been initialized, do so now
030:                if (bus == null) {
031:                    try {
032:                        bus = new ClusterBus(this );
033:                    } catch (Exception e) {
034:                        throw new CacheException(
035:                                "error initializing JGroups NotificationBus", e);
036:                    }
037:                }
038:            }
039:
040:            public Cache newInstance(String cacheName,
041:                    long timeoutMilliSeconds, int maxSize) {
042:
043:                Cache delegateCache;
044:                ClusterCache clusterCache;
045:
046:                try {
047:                    init();
048:
049:                    clusterCache = getRegisteredCache(cacheName);
050:
051:                    // if another cache of the same name has been registered
052:                    // then return it.  The down side of this is that the second
053:                    // version time the cache is returned, the timeout and max size
054:                    // has no effect on the returned cache.
055:                    // Also, this class does not extends AbstractDecoratorCacheFactory
056:                    // because it needs to decide if the delegate cache should be
057:                    // created.
058:                    if (clusterCache == null) {
059:                        delegateCache = delegate.newInstance(cacheName,
060:                                timeoutMilliSeconds, maxSize);
061:                        clusterCache = new ClusterCache(cacheName,
062:                                delegateCache, this );
063:
064:                        registerCache(cacheName, clusterCache);
065:                    } else {
066:                        LOG.info("returning existing cache : " + cacheName);
067:                    }
068:                } catch (Exception e) {
069:                    throw new RuntimeException(
070:                            "unable to create cluster decorator");
071:                }
072:
073:                return clusterCache;
074:            }
075:
076:            private synchronized void registerCache(String cacheName,
077:                    ClusterCache clusterCache) {
078:                registeredCacheMap.put(cacheName, clusterCache);
079:            }
080:
081:            private synchronized ClusterCache getRegisteredCache(
082:                    String cacheName) {
083:                return (ClusterCache) registeredCacheMap.get(cacheName);
084:            }
085:
086:            public String getBusName() {
087:                return busName;
088:            }
089:
090:            public void setBusName(String busName) {
091:                this .busName = busName;
092:            }
093:
094:            public String getChannelProperties() {
095:                return channelProperties;
096:            }
097:
098:            public void setChannelProperties(String channelProperties) {
099:                this .channelProperties = channelProperties;
100:            }
101:
102:            public void setDelegate(CacheFactory delegate) {
103:                this .delegate = delegate;
104:            }
105:
106:            public String toString() {
107:                return "ClusterCacheFactory[" + busName + "]->" + delegate;
108:            }
109:
110:            //-----------------------------------------------------------
111:            public void sendRemoveNotification(String cacheName, Object key) {
112:                bus.sendNotification(new RemoveNotification(instanceId,
113:                        cacheName, key));
114:            }
115:
116:            public void sendClearNotification(String cacheName) {
117:                bus.sendNotification(new ClearNotification(instanceId,
118:                        cacheName));
119:            }
120:
121:            public void handleNotification(Notification notification) {
122:
123:                ClusterCache clusterCache;
124:
125:                // don't process self notification
126:                if (notification.getSenderInstanceId() != instanceId) {
127:                    clusterCache = getRegisteredCache(notification
128:                            .getCacheName());
129:
130:                    // if this factory doesn't have this cache, it isn't
131:                    // really possible to execute a notification on it
132:                    if (clusterCache != null) {
133:                        LOG.debug("execute : " + notification);
134:                        notification.execute(clusterCache.getCache());
135:                    }
136:                }
137:            }
138:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.