KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > fm > OpenFileManager


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.imagero.uio.buffer.fm;
34
35 import com.imagero.uio.RandomAccess;
36 import com.imagero.uio.RandomAccessFactory;
37 import com.imagero.uio.RandomAccessRO;
38 import com.imagero.uio.buffer.BufferManager;
39 import com.imagero.uio.buffer.MutableBufferManager;
40 import com.imagero.uio.io.IOutils;
41
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.awt.event.ActionListener JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.Hashtable JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 /**
50  * OpenFileManager manages open files.
51  * If too much files are opened, FileManager closes some of them and opens them later if needed.
52  * @author Andrey Kuznetsov
53  */

54 public class OpenFileManager {
55
56     static final String JavaDoc GET = "get";
57     static final String JavaDoc OPEN = "open";
58     static final String JavaDoc CLOSE = "close";
59
60     static boolean debug;
61
62     public static boolean isDebug() {
63         return debug;
64     }
65
66     public static void setDebug(boolean debug) {
67         OpenFileManager.debug = debug;
68     }
69
70     Vector JavaDoc v = new Vector JavaDoc();
71     Hashtable JavaDoc ros = new Hashtable JavaDoc();
72
73     int maxOpenCount = 100;
74
75     ActionListener JavaDoc handler = new ActionListener JavaDoc() {
76         public void actionPerformed(ActionEvent JavaDoc e) {
77             String JavaDoc actionCommand = e.getActionCommand();
78             Object JavaDoc manager = e.getSource();
79             if (actionCommand == GET || actionCommand == OPEN) {
80                 v.removeElement(manager);
81                 v.addElement(manager);
82             }
83             else if(actionCommand == CLOSE) {
84                 v.removeElement(manager);
85             }
86         }
87     };
88
89     /**
90      * create RandomAccessRO which will be managed by this FileManager
91      * @param f File
92      * @return created RandomAccessRO
93      * @throws IOException
94      */

95     public RandomAccessRO createRO(File JavaDoc f) throws IOException JavaDoc {
96         RandomAccessRO ro = openFileRO(f);
97         return ro;
98     }
99
100     /**
101      * create RandomAccess which will be managed by this FileManager
102      * @param f File
103      * @return created RandomAccess
104      * @throws IOException
105      */

106     public RandomAccess create(File JavaDoc f) throws IOException JavaDoc {
107         RandomAccess ra = openFile(f);
108         return ra;
109     }
110
111     private RandomAccessRO openFileRO(File JavaDoc f) throws IOException JavaDoc {
112         BufferManager manager = createBufferManager(f, false);
113         RandomAccessRO ro = RandomAccessFactory.createBufferedRO(manager);
114         ros.put(ro, manager);
115         v.addElement(manager);
116         checkSize();
117
118         return ro;
119     }
120
121     private RandomAccess openFile(File JavaDoc f) throws IOException JavaDoc {
122         BufferManager manager = createBufferManager(f, true);
123         RandomAccess ra = RandomAccessFactory.createBuffered((MutableBufferManager) manager);
124         ros.put(ra, manager);
125         v.addElement(manager);
126         checkSize();
127
128         return ra;
129     }
130
131     private void checkSize() {
132         if (v.size() > maxOpenCount) {
133             int closeCount = v.size() - maxOpenCount;
134             for (int i = 0; i < v.size(); i++) {
135                 BufferManager manager = (BufferManager) v.elementAt(i);
136                 if (manager instanceof FMBufferManager) {
137                     if (((FMBufferManager) manager).canClose()) {
138                         manager.close();
139                         v.removeElementAt(0);
140                         if (--closeCount == 0) {
141                             break;
142                         }
143                     }
144                 }
145             }
146         }
147     }
148
149     /**
150      * finally close RandomAccessRO and remove it from this FileManager
151      */

152     public void close(RandomAccessRO ro) {
153         Object JavaDoc mgr = ros.remove(ro);
154         if (mgr == null) {
155             throw new NullPointerException JavaDoc();
156         }
157         IOutils.closeStream(ro);
158         v.removeElement(mgr);
159         if (mgr instanceof FMBufferManagerRO) {
160             ((FMBufferManagerRO) mgr)._close();
161         }
162         else if (mgr instanceof FMBufferManager) {
163             ((FMBufferManager) mgr)._close();
164         }
165     }
166
167     /**
168      * get max count of simultaneously open files.
169      */

170     public int getMaxOpenCount() {
171         return maxOpenCount;
172     }
173
174     /**
175      * set max count of simultaneously open files.
176      */

177     public void setMaxOpenCount(int maxOpenCount) {
178         this.maxOpenCount = maxOpenCount;
179     }
180
181     private BufferManager createBufferManager(File JavaDoc f, boolean writeable) throws IOException JavaDoc {
182         if (writeable) {
183             return new FMBufferManager(f, handler);
184         }
185         else {
186             return new FMBufferManagerRO(f, handler);
187         }
188     }
189 }
190
Popular Tags