Source Code Cross Referenced for PassivationObj.java in  » Database-JDBC-Connection-Pool » Space4J » org » space4j » passivation » 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 » Database JDBC Connection Pool » Space4J » org.space4j.passivation 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Space4J(TM) - Object Persistence in RAM
002:        // Copyright (C) 2003 Sergio Oliveira Junior
003:        // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
004:
005:        package org.space4j.passivation;
006:
007:        import java.util.*;
008:        import java.io.*;
009:        import java.nio.channels.*;
010:
011:        /**
012:         * If you want to make your object passivated, you must distribute it throughout your application as a PassivationObj.<br>
013:         * Actually, this is pretty simple. You have an object User. If you put your User in a PassivationMap, you will receive
014:         * a PassivationObj whenever you ask for it through the get(key) method. So there won't be any references to User throughout
015:         * your applicatoin, but only references to PassivationObj. Whenever you want to gain access to the raal object, you can
016:         * use the get() method. If the your Object is in memory, PassivationObj just return it. If it is passivated, PassivationObj
017:         * brings it back from disk.<br><br>
018:         * <b>Note:</b> <br><br>
019:         * - An object can be passivated if and only if it has a unique id returned by its hashCode, like a database primary key.<br><br>
020:         * - It is the programmer responsibility not to make strong references to the object itself. For example:<br><br>
021:         * public class House {<BR>
022:         *  User user = null; // WRONG !!!!<BR>
023:         * }<BR><BR>
024:         * public class House {<BR>
025:         *  PassivationObj user = null; // RIGHT !!!!<BR>
026:         * }<BR><BR>
027:         */
028:        public class PassivationObj implements  Serializable {
029:
030:            private final static boolean DEBUG = true;
031:
032:            private Object obj = null;
033:            private int oid = 0;
034:            private Class klass = null;
035:            private long lastAccessTime = 0;
036:            private boolean passivated = false;
037:
038:            /**
039:             * Creates a PassivationObj for an object.
040:             * @param obj The object to be passivated.
041:             */
042:            public PassivationObj(Object obj) {
043:                this .obj = obj;
044:                this .oid = obj.hashCode();
045:                this .klass = obj.getClass();
046:                setLastAccessTime();
047:            }
048:
049:            protected String getFilename() {
050:                String oid = String.valueOf(this .oid);
051:                if (oid.length() == 1) {
052:                    StringBuffer sb = new StringBuffer("0");
053:                    sb.append(oid);
054:                    oid = sb.toString();
055:                }
056:                String classname = klass.getName();
057:                int x = oid.length();
058:                String extrapath = oid.substring(x - 2, x);
059:                StringBuffer path = new StringBuffer(PassivationMap.DIR);
060:                File dir = new File(path.toString());
061:                if (!dir.exists())
062:                    dir.mkdir();
063:                path.append("/").append(classname);
064:                dir = new File(path.toString());
065:                if (!dir.exists())
066:                    dir.mkdir();
067:                path.append("/").append(extrapath);
068:                dir = new File(path.toString());
069:                if (!dir.exists())
070:                    dir.mkdir();
071:                path.append("/").append(oid);
072:                path.append(PassivationMap.PV_PREFIX);
073:                return path.toString();
074:            }
075:
076:            long getLastAccessTime() {
077:                return lastAccessTime;
078:            }
079:
080:            private void setLastAccessTime() {
081:                lastAccessTime = System.currentTimeMillis();
082:            }
083:
084:            /**
085:             * Return the object. If the object is passivated it will be activated before it is returned, of course.
086:             * @return The object itself.
087:             */
088:            public synchronized Object get() {
089:                setLastAccessTime();
090:                if (isPassivated())
091:                    activate();
092:                return obj;
093:            }
094:
095:            /**
096:             * Return the object without activating it. This will be usefull when the system needs to access the object.
097:             * In this situation, there is no need to activate the object.
098:             * @return The object itself.
099:             */
100:            public synchronized Object getWithoutActivating() {
101:                if (!isPassivated())
102:                    return this .obj;
103:                activate();
104:                Object save = this .obj;
105:                this .obj = null;
106:                passivated = true;
107:                return save;
108:            }
109:
110:            // When the VM brings back this object from serialization, it will call this function.
111:            private void readObject(ObjectInputStream stream)
112:                    throws ClassNotFoundException, IOException {
113:                stream.defaultReadObject();
114:                if (DEBUG)
115:                    System.out.println("inside readObject for " + oid);
116:                // This will always come with the object from the snapshot
117:                // Check if it is passivated...
118:                // If it is, write to disk now !!!
119:                if (isPassivated() && obj != null) {
120:                    passivate();
121:                } else {
122:                    setLastAccessTime();
123:                }
124:            }
125:
126:            // Before serializing...   
127:            private void writeObject(ObjectOutputStream stream)
128:                    throws IOException {
129:                if (DEBUG)
130:                    System.out.println("inside writeObject for " + oid);
131:                if (isPassivated()) {
132:                    this .obj = getWithoutActivating();
133:                }
134:                stream.defaultWriteObject();
135:            }
136:
137:            boolean canPassivate(long n) {
138:                if (System.currentTimeMillis() - lastAccessTime > n)
139:                    return true;
140:                return false;
141:            }
142:
143:            /**
144:             * Check if this object is passivated now.
145:             * @return true if this object is passivated
146:             */
147:            public synchronized boolean isPassivated() {
148:                return passivated;
149:            }
150:
151:            synchronized void passivate() {
152:                // we need a file lock here for the cluster !!!
153:                // synchronized won't help !!!
154:                // we can have many separate virtual machines running !!!
155:                ObjectOutputStream oos = null;
156:                FileLock lock = null;
157:                try {
158:                    File file = new File(getFilename());
159:                    FileOutputStream fos = new FileOutputStream(file);
160:                    lock = fos.getChannel().tryLock();
161:                    if (lock == null) {
162:                        // somebody is just passivating this right now !!!
163:                        // consider it passivated...
164:                        if (DEBUG)
165:                            System.out.println("Passivation Lock !!!");
166:                        return;
167:                    }
168:                    oos = new ObjectOutputStream(fos);
169:                    oos.writeObject(obj);
170:                    if (DEBUG)
171:                        System.out.println("Object " + obj.toString()
172:                                + " passivated !!!");
173:                    passivated = true;
174:                    this .obj = null; // very important !!!
175:                } catch (Exception e) {
176:                    e.printStackTrace();
177:                } finally {
178:                    try {
179:                        if (oos != null)
180:                            oos.close();
181:                    } catch (Exception e) {
182:                    }
183:                    try {
184:                        if (lock != null)
185:                            lock.release();
186:                    } catch (Exception e) {
187:                    }
188:                }
189:            }
190:
191:            private synchronized void activate() {
192:                ObjectInputStream ois = null;
193:                try {
194:                    File file = new File(getFilename());
195:                    FileInputStream fis = new FileInputStream(file);
196:                    ois = new ObjectInputStream(fis);
197:                    Object obj = ois.readObject();
198:                    if (DEBUG)
199:                        System.out.println("Object " + obj.toString()
200:                                + " activated !!!");
201:                    this .obj = obj;
202:                    passivated = false;
203:                } catch (Exception e) {
204:                    e.printStackTrace();
205:                } finally {
206:                    try {
207:                        if (ois != null)
208:                            ois.close();
209:                    } catch (Exception e) {
210:                    }
211:                }
212:            }
213:
214:            public boolean equals(Object obj) {
215:                if (obj instanceof  PassivationObj) {
216:                    PassivationObj po = (PassivationObj) obj;
217:                    if (po.hashCode() == this .hashCode())
218:                        return true;
219:                } else if (klass.isInstance(obj)) {
220:                    if (obj.hashCode() == this .hashCode())
221:                        return true;
222:                }
223:                return false;
224:            }
225:
226:            public int hashCode() {
227:                return oid;
228:            }
229:
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.