KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.lang.ClassNotFoundException JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.File JavaDoc;
34 import java.nio.channels.FileChannel JavaDoc;
35 import java.io.EOFException JavaDoc;
36
37
38 class Locked implements LockingStore
39 {
40   Locked(PersistentStore s, RandomAccessFile JavaDoc r){
41     checkNotNull(s, "null store not allowed");
42     checkNotNull(r, "null Random Access File not allowed");
43     store = s;
44     raf = r;
45   }
46
47   public long lastModified() {return 0L;}
48
49       /**
50          Read the object from the store.
51          <p>
52          precondition - true
53          <p>
54          postcondition - store has not been modified
55          @return the single object that was in the store (or null if
56          the store is empty). Releases all resources except
57          lock. State is unchanged.
58          @throws TimeoutException if a lock on the store couldn't be
59          obtained in a reasonable time.
60          @throws IOException if there was a problem reading from the
61          store. Release all resources. State is <code>unlocked</code>
62          @throws ClassNotFoundException if the object could not be
63          restored from the store. Release all resources. State is
64          <code>unlocked</code>
65       */

66          
67   public Object JavaDoc readObject() throws IOException JavaDoc, ClassNotFoundException JavaDoc{
68     try {
69       Object JavaDoc o = null;
70       ObjectInputStream JavaDoc ois = null;
71       FileInputStream JavaDoc fis = null;
72       try {
73         fis = new FileInputStream JavaDoc(store.getStore());
74         if (fis.available() > 0){
75           ois = new ObjectInputStream JavaDoc(fis);
76           o = ois.readObject();
77         } else {
78           o = null;
79           fis.close();
80         }
81       }
82       catch (EOFException JavaDoc e){ // this occurs if the file is empty
83
o = null;
84       }
85       finally{
86         if (fis != null){
87           fis.close();
88         }
89         if (ois != null){
90           ois.close();
91         }
92       }
93       return o;
94     }
95     catch (IOException JavaDoc ioe){
96       unlock();
97       throw ioe;
98     }
99     catch (ClassNotFoundException JavaDoc cnfe){
100       unlock();
101       throw cnfe;
102     }
103     
104   }
105       /**
106          Write the given object to the store via serialization.
107          <p>
108          Precondition - store is locked
109          <p>
110          Postcondition - store contains this object, and only this
111          object. All resources except lock are released. State is unchanged.
112          @param o the object to be put into the store. Must implement
113          {@link Serializable}
114          @throws IOException if there was a problem writing to the
115          store. All resources released. State is <code>unlocked</code>
116          @throws IllegalStateException if the store is not
117          locked. All resources released. State is <code>unlocked</code>
118       */

119
120   public void writeObject(Object JavaDoc o) throws IOException JavaDoc, IllegalStateException JavaDoc{
121     ObjectOutputStream JavaDoc oos = null;
122     FileOutputStream JavaDoc fos = null;
123     try {
124       oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(store.getStore(),
125                                                         false));
126       oos.writeObject(o);
127       oos.flush();
128     }
129     catch (IOException JavaDoc ioe){
130       this.unlock();
131       throw ioe;
132     }
133     catch (IllegalStateException JavaDoc ise){
134       this.unlock();
135       throw ise;
136     }
137     finally {
138       if (fos != null) fos.close();
139       if (oos != null) oos.close();
140     }
141   }
142   
143   public void lock() throws IOException JavaDoc, TimeoutException{}
144   
145   public void unlock(){
146     try {
147       raf.close();
148     }
149     catch (IOException JavaDoc ioe){
150           // stomp on exceptions during closure - we don't care!
151
}
152     store.setState(new Unlocked(store));
153   }
154
155   protected void finalize() {
156     this.unlock();
157   }
158   
159
160   private void checkNotNull(Object JavaDoc o, String JavaDoc m) throws NullPointerException JavaDoc{
161     if (o == null){
162       throw new NullPointerException JavaDoc(m);
163     }
164   }
165
166   private PersistentStore store;
167   private RandomAccessFile JavaDoc raf;
168   
169 }
170
171
172
173   
174
Popular Tags