Source Code Cross Referenced for LockHelper.java in  » Workflow-Engines » pegasus-2.1.0 » org » griphyn » vdl » util » 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 » Workflow Engines » pegasus 2.1.0 » org.griphyn.vdl.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file or a portion of this file is licensed under the terms of
003:         * the Globus Toolkit Public License, found in file ../GTPL, or at
004:         * http://www.globus.org/toolkit/download/license.html. This notice must
005:         * appear in redistributions of this file, with or without modification.
006:         *
007:         * Redistributions of this Software, with or without modification, must
008:         * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009:         * some other similar material which is provided with the Software (if
010:         * any).
011:         *
012:         * Copyright 1999-2004 University of Chicago and The University of
013:         * Southern California. All rights reserved.
014:         */
015:        package org.griphyn.vdl.util;
016:
017:        import org.griphyn.vdl.util.*;
018:        import java.util.*;
019:        import java.io.*;
020:
021:        /**
022:         * @author Jens-S. Vöckler
023:         * @author Yong Zhao
024:         * @version $Revision: 50 $
025:         *
026:         * @see java.io.File
027:         */
028:        public class LockHelper {
029:            /**
030:             * The suffix to use for the regular lock file.
031:             */
032:            public static final String LOCK_SUFFIX = ".lock";
033:
034:            /**
035:             * The initial timeout to use when retrying with exponential backoff.
036:             */
037:            private long m_timeout;
038:
039:            /**
040:             * The number of retries to attempt obtaining a lock.
041:             */
042:            private int m_retries;
043:
044:            /**
045:             * default ctor.
046:             */
047:            public LockHelper() {
048:                this .m_timeout = 250;
049:                this .m_retries = 10;
050:            }
051:
052:            /**
053:             * ctor.
054:             */
055:            public LockHelper(long timeout, int retries) {
056:                this .m_timeout = timeout;
057:                this .m_retries = retries;
058:            }
059:
060:            /**
061:             * Creates a file-based lock file in an NFS secure fashion. Do not use
062:             * this function for locking, use {@link #lockFile(String)} instead.
063:             * One exception is to use this method for test-and-set locking.
064:             *
065:             * @param filename is the file to create a lockfile for
066:             * @return the representation of the lock file, or <code>null</code> 
067:             * in case of error.
068:             */
069:            public File createLock(String filename) {
070:                // create local names from basename
071:                File result = null;
072:                File lock = new File(filename + LockHelper.LOCK_SUFFIX);
073:
074:                // exclusively create new file
075:                boolean created = false;
076:                try {
077:                    created = lock.createNewFile();
078:                } catch (IOException ioe) {
079:                    Logging.instance().log(
080:                            "lock",
081:                            0,
082:                            "while creating lock " + lock.getPath() + ": "
083:                                    + ioe.getMessage());
084:                    created = false; // make extra sure
085:                }
086:
087:                // if the lock was created, continue
088:                if (created) {
089:                    // postcondition: file was created
090:                    Logging.instance().log("lock", 2,
091:                            "created lock " + lock.getPath());
092:                    LockFileSet.instance().add(lock);
093:                    lock.setLastModified(System.currentTimeMillis()); // force NFS
094:                    result = lock;
095:                } else {
096:                    // unable to rename file to lock file: lock exists
097:                    Logging.instance().log("lock", 1,
098:                            "lock " + lock.getPath() + " already exists");
099:                }
100:
101:                // may be null
102:                return result;
103:            }
104:
105:            /**
106:             * Locks a file using an empty lockfile. This method repeatedly retries
107:             * to lock a file, and gives up after a few seconds. 
108:             *
109:             * @param filename is the basename of the file to lock.
110:             * @return true, if the file was locked successfully, false otherwise.
111:             */
112:            public boolean lockFile(String filename) {
113:                long timeout = this .m_timeout;
114:
115:                // do five retries
116:                for (int i = 0; i < this .m_retries; ++i) {
117:                    File lock = this .createLock(filename);
118:                    if (lock == null) {
119:                        // lock is busy, try again later
120:                        Logging.instance()
121:                                .log(
122:                                        "lock",
123:                                        1,
124:                                        "file " + filename + " is busy, "
125:                                                + "sleeping "
126:                                                + Long.toString(timeout)
127:                                                + " ms " + "(retry "
128:                                                + Integer.toString(i) + ')');
129:                        try {
130:                            Thread.sleep(timeout);
131:                        } catch (InterruptedException ie) {
132:                            // ignore, as usual
133:                        }
134:                        timeout <<= 1;
135:                    } else {
136:                        // we got the lock, go ahead, and write our pid into it
137:                        // FIXME: we don't have a pid in Java (in Windows)
138:                        return true;
139:                    }
140:                }
141:
142:                // postcondition: timeout out after retries
143:                Logging.instance().log(
144:                        "default",
145:                        0,
146:                        "unable to lock " + filename
147:                                + ", still busy, giving up!");
148:                return false;
149:            }
150:
151:            /**
152:             * Releases a lock file from its basename.
153:             * @param filename is the name of the basename of the file to unlock.
154:             * The locking extension will be added internally. 
155:             * @return true, if the lock was removed successfully, false otherwise.
156:             */
157:            public boolean unlockFile(String filename) {
158:                File lock = new File(filename + LockHelper.LOCK_SUFFIX);
159:                LockFileSet.instance().remove(lock);
160:
161:                boolean result = lock.delete();
162:                if (result)
163:                    Logging.instance().log("lock", 2,
164:                            "removed lock " + lock.getPath());
165:                return result;
166:            }
167:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.