KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > corruptio > CorruptibleIo


1 /*
2
3    Derby - Derby - Class org.apache.derbyTesting.functionTests.util.corruptio.CorruptibleIo
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.derbyTesting.functionTests.util.corruptio;
23 import java.io.File JavaDoc;
24
25 /*
26  * This is a helper class to instrument the CorruptDiskStorageFactory
27  * to modify the i/o opertions before the request are sent to
28  * a real storage factory.
29  *
30  * Tests can specify what type of corruption is required like log/data files
31  * and the at what and offset and the length of the corruption to be
32  * done in the write requests.
33  *
34  * Only one instance of this class will exist in the system, Tests should hold
35  * onto the instance of this class until they are done sending the i/o
36  * requests by executing statement that will actuall will trigger i/o ,
37  * for example a commit will flush the log buffers. Otherwise class garbage
38  * collector can reinitialize the values.
39  *
40  * @author <a HREF="mailto:suresh.thalamati@gmail.com">Suresh Thalamati</a>
41  * @version 1.0
42  * @see WritableStorageFactory
43  * @see StorageFactory
44  */

45
46 public class CorruptibleIo {
47
48     private static CorruptibleIo instance = new CorruptibleIo();
49     private boolean corruptLog = false; //corrupt the log i/o to log*.dat files
50
private boolean corruptData = false; //corrupt the files under seg0(data)
51
private int corruptLength; // no of bytes to corrupt
52
private int corruptOffset; // offset inside the write request
53

54
55     private CorruptibleIo() {
56     }
57
58     public static CorruptibleIo getInstance() {
59         return instance;
60     }
61
62     
63     public void setLogCorruption(boolean corrupt) {
64         corruptLog = corrupt;
65     }
66
67     public void setDataCorruption(boolean corrupt) {
68         corruptData = corrupt;
69     }
70     
71     public void setOffset(int off) {
72         corruptOffset = off ;
73     }
74
75     public void setLength(int len) {
76         corruptLength = len;
77     }
78         
79     public int getOffset() {
80         return corruptOffset;
81     }
82
83     public int getLength(){
84         return corruptLength;
85     }
86
87     public boolean isCorruptibleFile(File JavaDoc file)
88     {
89         String JavaDoc name = file.getName();
90         String JavaDoc parentName = file.getParent();
91         if (parentName.endsWith("log") && name.endsWith("dat")) {
92             return corruptLog;
93         }
94         else if (parentName.endsWith("seg0")) {
95             return corruptData;
96         }
97
98         return false;
99     }
100
101     /**
102      * corrupt the byte array at the specified bytes, currenly this
103      * metods just complemetns the bits at the specified offsets.
104      */

105     public byte[] corrupt(byte b[], int off, int len)
106     {
107         if (corruptOffset >= off && (corruptOffset + corruptLength) < (off + len))
108         {
109             for(int i = corruptOffset ; i < corruptOffset + corruptLength ; i++)
110             {
111                 //System.out.println(b[i]);
112
b[i] = (byte)~b[i];
113                 //System.out.println(b[i]);
114
}
115             // System.out.println("Corrupted the write request : Off = " + off + " Length = " + len);
116
}else{
117             System.out.println("Not valid corrupt request :" +
118                                "Write Request" + "Off=" + off + "size = " + len +
119                                "Corrupt Request" + "Off=" + corruptOffset +
120                                "size = " + corruptLength);
121         }
122         return b;
123     }
124
125 }
126
Popular Tags