KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > common > domains > registry > Unlocked


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.common.domains.registry;
25
26 import java.io.RandomAccessFile JavaDoc;
27 import java.lang.ClassNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.nio.channels.FileChannel JavaDoc;
31 import java.nio.channels.FileLock JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.EOFException JavaDoc;
34
35
36 class Unlocked implements LockingStore
37 {
38
39   private PersistentStore storeImpl;
40   
41   Unlocked(PersistentStore s){
42     if (s == null) {
43       throw new NullPointerException JavaDoc("store is null");
44     }
45     
46     storeImpl = s;
47   }
48
49   public synchronized Object JavaDoc readObject() throws IOException JavaDoc, TimeoutException, ClassNotFoundException JavaDoc {
50     ObjectInputStream JavaDoc in = null;
51     Object JavaDoc o = null;
52     try{
53       in = getIn();
54       o = (in != null ? in.readObject() : null) ;
55     }
56     catch (EOFException JavaDoc e){
57       o = null; // this occurs if file is empty
58
}
59     finally {
60       if (in != null) {
61         in.close();
62       }
63     }
64     return o;
65   }
66
67   public long lastModified(){ return 0L ;}
68   public void unlock(){}
69   
70
71   public void writeObject(Object JavaDoc o) throws IllegalStateException JavaDoc, IOException JavaDoc, TimeoutException {
72     throw new IllegalStateException JavaDoc("Unlocked state - writeObject() not allowed");
73     
74   }
75
76   public void lock() throws TimeoutException, IOException JavaDoc {
77     final RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(storeImpl.getLock(),
78                                                       "rws");
79     getWriteLock(raf.getChannel());
80     storeImpl.setState(new Locked(storeImpl, raf));
81   }
82
83   private ObjectInputStream JavaDoc getIn() throws IOException JavaDoc {
84     final FileInputStream JavaDoc fis = new FileInputStream JavaDoc(storeImpl.getStore());
85     getReadLock(fis.getChannel());
86     if (fis.available() > 0){
87       return new ObjectInputStream JavaDoc(fis);
88     } else {
89       fis.close();
90       return null;
91     }
92   }
93
94   private FileLock JavaDoc getReadLock(FileChannel JavaDoc ch) throws TimeoutException, IOException JavaDoc {
95     return obtainLock(ch, true);
96   }
97
98       /**
99          get a write lock on the given channel
100       */

101   private FileLock JavaDoc getWriteLock(FileChannel JavaDoc ch) throws TimeoutException, IOException JavaDoc {
102     return obtainLock(ch, false);
103   }
104
105     private FileLock JavaDoc obtainLock(FileChannel JavaDoc channel, boolean shared) throws TimeoutException, IOException JavaDoc{
106     FileLock JavaDoc lock = null;
107     int attempts = 0;
108     try{
109 /** Using 0x7fffffff in place of Long.MAX_VALUE. Bug # 4719967,4719605 **/
110       while ((lock = channel.tryLock(0L, 0x7fffffff, shared)) == null && attempts++ < ATTEMPTS){
111         Thread.currentThread().sleep(TIMEOUT);
112       }
113     }
114     catch (InterruptedException JavaDoc e){
115       throw new TimeoutException();
116     }
117     
118     if (lock == null) {
119       throw new TimeoutException();
120     }
121     return lock;
122   }
123
124       // # of milliseconds to wait between attempts to obtain the file
125
// # lock
126
private static final int TIMEOUT = 3; // 3 X 10^^-3 = 0.003 S
127

128
129       //# of times to attempt to obtain the file lock
130
private static final int ATTEMPTS = 50;
131
132 }
133
134
135   
136
Popular Tags