KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > store > raw > data > RFResource


1 /*
2
3    Derby - Class org.apache.derby.impl.store.raw.data.RFResource
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.store.raw.data;
23
24 import org.apache.derby.iapi.reference.SQLState;
25
26 import org.apache.derby.iapi.services.context.ContextService;
27 import org.apache.derby.iapi.services.context.ContextManager;
28 import org.apache.derby.iapi.services.daemon.Serviceable;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.store.access.FileResource;
32 import org.apache.derby.iapi.store.raw.Transaction;
33 import org.apache.derby.iapi.store.access.AccessFactoryGlobals;
34 import org.apache.derby.iapi.store.access.DatabaseInstant;
35 import org.apache.derby.iapi.store.raw.xact.RawTransaction;
36
37 import org.apache.derby.io.StorageFactory;
38 import org.apache.derby.io.WritableStorageFactory;
39 import org.apache.derby.io.StorageFile;
40 import org.apache.derby.io.StorageRandomAccessFile;
41
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44 import java.io.FileNotFoundException JavaDoc;
45 import java.io.IOException JavaDoc;
46
47 class RFResource implements FileResource {
48
49     protected final BaseDataFileFactory factory;
50
51     public RFResource(BaseDataFileFactory dataFactory) {
52         this.factory = dataFactory;
53     }
54
55     /**
56       @see FileResource#add
57       @exception StandardException Oops
58     */

59     public long add(String JavaDoc name, InputStream JavaDoc source)
60          throws StandardException
61     {
62         OutputStream JavaDoc os = null;
63
64         if (factory.isReadOnly())
65         {
66             throw StandardException.newException(SQLState.FILE_READ_ONLY);
67         }
68
69         long generationId = factory.getNextId();
70
71         try
72         {
73             StorageFile file = getAsFile(name, generationId);
74             if (file.exists())
75             {
76                 throw StandardException.newException(
77                         SQLState.FILE_EXISTS, file);
78             }
79
80             ContextManager cm =
81                 ContextService.getFactory().getCurrentContextManager();
82
83             RawTransaction tran =
84                 factory.getRawStoreFactory().getXactFactory().findUserTransaction(
85                         factory.getRawStoreFactory(),
86                         cm,
87                         AccessFactoryGlobals.USER_TRANS_NAME);
88             
89             // Block the backup, If backup is already in progress wait
90
// for the backup to finish. Jar files are unlogged but the
91
// changes to the references to the jar file in the catalogs
92
// is logged. A consistent backup can not be made when jar file
93
// is being added.
94

95             tran.blockBackup(true);
96
97             StorageFile directory = file.getParentDir();
98             if (!directory.exists())
99             {
100                 if (!directory.mkdirs())
101                 {
102                     throw StandardException.newException(
103                             SQLState.FILE_CANNOT_CREATE_SEGMENT, directory);
104                 }
105             }
106
107             os = file.getOutputStream();
108             byte[] data = new byte[4096];
109             int len;
110
111             factory.writeInProgress();
112             try
113             {
114                 while ((len = source.read(data)) != -1) {
115                     os.write(data, 0, len);
116                 }
117                 factory.writableStorageFactory.sync( os, false);
118             }
119             finally
120             {
121                 factory.writeFinished();
122             }
123         }
124
125         catch (IOException JavaDoc ioe)
126         {
127             throw StandardException.newException(
128                     SQLState.FILE_UNEXPECTED_EXCEPTION, ioe);
129         }
130
131         finally
132         {
133             try {
134                 if (os != null) {
135                     os.close();
136                 }
137             } catch (IOException JavaDoc ioe2) {/*RESOLVE: Why ignore this?*/}
138
139             try {
140                 if (source != null)source.close();
141             } catch (IOException JavaDoc ioe2) {/* RESOLVE: Why ignore this?*/}
142         }
143         
144         return generationId;
145     }
146
147     /**
148       @see FileResource#remove
149       @exception StandardException Oops
150       */

151     public void remove(String JavaDoc name, long currentGenerationId, boolean purgeOnCommit)
152         throws StandardException
153     {
154         if (factory.isReadOnly())
155             throw StandardException.newException(SQLState.FILE_READ_ONLY);
156
157             
158         ContextManager cm = ContextService.getFactory().getCurrentContextManager();
159
160         RawTransaction tran =
161             factory.getRawStoreFactory().getXactFactory().findUserTransaction(
162                         factory.getRawStoreFactory(),
163                         cm,
164                         AccessFactoryGlobals.USER_TRANS_NAME);
165                     
166         // Block the backup, If backup is already in progress wait
167
// for the backup to finish. Jar files are unlogged but the
168
// changes to the references to the jar file in the catalogs
169
// is logged. A consistent backup can not be made when jar file
170
// is being removed.
171

172         tran.blockBackup(true);
173
174         tran.logAndDo(new RemoveFileOperation(name, currentGenerationId, purgeOnCommit));
175
176         if (purgeOnCommit) {
177
178             Serviceable s = new RemoveFile(getAsFile(name, currentGenerationId));
179
180             tran.addPostCommitWork(s);
181         }
182     }
183
184     /**
185       @see FileResource#replace
186       @exception StandardException Oops
187       */

188     public long replace(String JavaDoc name, long currentGenerationId, InputStream JavaDoc source, boolean purgeOnCommit)
189         throws StandardException
190     {
191         if (factory.isReadOnly())
192             throw StandardException.newException(SQLState.FILE_READ_ONLY);
193
194         remove(name, currentGenerationId, purgeOnCommit);
195
196         long generationId = add(name, source);
197
198         return generationId;
199     }
200
201
202     /**
203       @see FileResource#getAsFile
204       */

205     public StorageFile getAsFile(String JavaDoc name, long generationId)
206     {
207         String JavaDoc versionedFileName = factory.getVersionedName(name, generationId);
208
209         return factory.storageFactory.newStorageFile( versionedFileName);
210     }
211
212     /**
213       @see FileResource#getAsFile
214       */

215     private StorageFile getAsFile(String JavaDoc name)
216     {
217         return factory.storageFactory.newStorageFile( name);
218     }
219
220     /**
221       @see FileResource#getAsStream
222       @exception IOException trouble accessing file.
223       */

224     public InputStream JavaDoc getAsStream(String JavaDoc name, long generationId)
225          throws IOException JavaDoc
226     {
227         return getAsFile(name, generationId).getInputStream();
228     }
229
230     public char getSeparatorChar()
231     {
232         return factory.storageFactory.getSeparator();
233     }
234 } // end of class RFResource
235

236
237 class RemoveFile implements Serviceable
238 {
239     private final StorageFile fileToGo;
240
241     RemoveFile(StorageFile fileToGo)
242     {
243         this.fileToGo = fileToGo;
244     }
245
246     public int performWork(ContextManager context)
247         throws StandardException
248     {
249         // SECURITY PERMISSION - MP1, OP5
250
if (fileToGo.exists())
251         {
252             if (!fileToGo.delete())
253             {
254                 throw StandardException.newException(
255                     SQLState.FILE_CANNOT_REMOVE_FILE, fileToGo);
256             }
257         }
258         return Serviceable.DONE;
259     }
260
261     public boolean serviceASAP()
262     {
263         return false;
264     }
265
266     /**
267      * File deletion is a quick operation and typically releases substantial
268      * amount of space very quickly, this work should be done on the
269      * user thread.
270      * @return true, this work needs to done on user thread.
271      */

272     public boolean serviceImmediately()
273     {
274         return true;
275     }
276 }
277
Popular Tags