1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.generator;
48
49 import java.io.ByteArrayOutputStream;
50 import java.io.DataInputStream;
51 import java.io.File;
52 import java.io.FileInputStream;
53 import java.io.IOException;
54 import java.util.ArrayList;
55 import java.util.Random;
56
57 import org.apache.commons.logging.Log;
58
59 import pl.kernelpanic.dbmonster.DBMonster;
60 import pl.kernelpanic.dbmonster.DBMonsterContext;
61
62 public class BinaryGenerator extends BasicDataGenerator implements Initializable {
63
64 /*** pointer into the fileList to keep track of which file to use next */
65 private int currentIndex = -1;
66
67 /*** holds the list of files that are used to populate binary columns */
68 private ArrayList fileList = new ArrayList();
69
70 /*** Random number generator. */
71 private Random random = null;
72
73 /*** logger for this class */
74 private Log log = null;
75
76 /*** the maximum length of any column */
77 private int maxLength = 0;
78 /***
79 * Initializes a class with <code>DBMonsterContext</code>.
80 *
81 * @param ctx context
82 *
83 * @throws Exception if initialization fails
84 */
85 public void initialize(DBMonsterContext ctx)
86 throws Exception
87 {
88 random = (Random) ctx.getProperty(DBMonster.RANDOM_KEY);
89 log = (Log) ctx.getProperty(DBMonster.LOGGER_KEY);
90 }
91
92 /***
93 * Generates byte[] from the next file in our list of configured files.
94 *
95 * @return value.
96 */
97 public Object generate() throws Exception {
98 if (nulls != 0 && random.nextInt(101) <= nulls) {
99 return null;
100 }
101
102 ByteArrayOutputStream baos = new ByteArrayOutputStream();
103 DataInputStream is = null;
104 String filename = (String)fileList.get(getNextIndex());
105 try {
106 File f = new File(filename);
107 is = new DataInputStream(new FileInputStream(f));
108 byte[] buffer = new byte[8192];
109 int result = is.read(buffer);
110 int bytesWritten = result;
111 while (result != -1) {
112 baos.write(buffer);
113 result = is.read(buffer);
114 bytesWritten += result;
115 }
116 if (log.isDebugEnabled()) {
117 StringBuffer sb = new StringBuffer();
118 sb.append("generate: bytesWritten=");
119 sb.append(bytesWritten);
120 sb.append(" for file=");
121 sb.append(filename);
122 log.debug(sb.toString());
123 }
124 } catch (IOException e) {
125 log.error("Unable to build inputstream for input file="+filename, e);
126 } finally {
127 if (is != null) try { is.close(); } catch (IOException e) {}
128 }
129 byte[] result = baos.toByteArray();
130
131 if (maxLength > 0 && result.length > maxLength) {
132 byte[] newresult = new byte[maxLength];
133 for (int i = 0; i < maxLength; i++) {
134 newresult[i] = result[i];
135 }
136 result = newresult;
137 }
138 return result;
139 }
140
141 public void reset() {
142 currentIndex = 0;
143 }
144
145 /***
146 * Sets a filename containing content to be used as a field value in the
147 * database.
148 *
149 * @param value the absolute path to a file that will be read in and stored
150 * in a table column.
151 */
152 public void setFile(String value) {
153 fileList.add(value);
154 }
155
156 /***
157 * Returns the maximal length of the string.
158 *
159 * @return the maximal value of generated string
160 */
161 public int getMaxLength() {
162 return maxLength;
163 }
164
165 /***
166 * Sets the maximal length of the string.
167 *
168 * @param length maximal length
169 */
170 public void setMaxLength(int length) {
171 maxLength = length;
172 }
173
174 /***
175 * Loop around to 0 if we've reached the end of the list.
176 * @return
177 */
178 private int getNextIndex() {
179 currentIndex++;
180 if (currentIndex >= fileList.size()) {
181 currentIndex = 0;
182 }
183 return currentIndex;
184 }
185 }