View Javadoc

1   /*
2    * Version 1.0 based on Apache Software License 1.1
3    *
4    * Copyright (C) 2005 Rob Manning
5    * manningr@users.sourceforge.net
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are
9    * met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in the
16   *    documentation and/or other materials provided with the distribution.
17   *
18   * 3. The end-user documentation included with the redistribution, if any,
19   *    must include the following acknowledgment:
20   *
21   *    "This product includes software developed by DBMonster developers
22   *    (http://dbmonster.kernelpanic.pl/)."
23   *
24   *  Alternately, this acknowledgment may appear in the software itself,
25   *  if and wherever such third-party acknowledgments normally appear.
26   *
27   * 4. The name "DBMonster" must not be used to endorse or promote products
28   *    derived from this software without prior written permission. For
29   *    written permission, please contact pm@jcake.com.
30   *
31   * 5. Products derived from this software may not be called "DBMonster",
32   *    nor may "DBMonster" appear in their name, without prior written
33   *    permission of Piotr Maj.
34   *
35   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
38   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
39   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
44   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45   * POSSIBILITY OF SUCH DAMAGE.
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         // shrink the array if it exceeds the maxLength 
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 }