Coverage report

  %line %branch
org.apache.torque.util.VillageUtils
0% 
0% 

 1  
 package org.apache.torque.util;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with 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,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.BufferedOutputStream;
 23  
 import java.io.ByteArrayOutputStream;
 24  
 import java.io.ObjectOutputStream;
 25  
 import java.io.OutputStream;
 26  
 import java.io.Serializable;
 27  
 import java.math.BigDecimal;
 28  
 import java.util.Hashtable;
 29  
 import java.util.Iterator;
 30  
 import java.util.Map;
 31  
 
 32  
 import org.apache.commons.logging.Log;
 33  
 import org.apache.commons.logging.LogFactory;
 34  
 import org.apache.torque.om.SimpleKey;
 35  
 
 36  
 import com.workingdogs.village.QueryDataSet;
 37  
 import com.workingdogs.village.Record;
 38  
 import com.workingdogs.village.TableDataSet;
 39  
 
 40  
 /**
 41  
  * Some Village related code factored out of the BasePeer.
 42  
  *
 43  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 44  
  * @version $Id: VillageUtils.java 476550 2006-11-18 16:08:37Z tfischer $
 45  
  */
 46  
 public final class VillageUtils
 47  
 {
 48  
     /** The log. */
 49  0
     private static Log log = LogFactory.getLog(VillageUtils.class);
 50  
 
 51  
     /**
 52  
      * Private constructor to prevent instantiation.
 53  
      *
 54  
      * Class contains only static method ans should therefore not be
 55  
      * instantiated.
 56  
      */
 57  
     private VillageUtils()
 58  0
     {
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Convenience Method to close a Table Data Set without
 63  
      * Exception check.
 64  
      *
 65  
      * @param tds A TableDataSet
 66  
      */
 67  
     public static final void close(class="keyword">final TableDataSet tds)
 68  
     {
 69  0
         if (tds != null)
 70  
         {
 71  
             try
 72  
             {
 73  0
                 tds.close();
 74  
             }
 75  0
             catch (Exception ignored)
 76  
             {
 77  0
                 log.debug("Caught exception when closing a TableDataSet",
 78  
                         ignored);
 79  0
             }
 80  
         }
 81  0
     }
 82  
 
 83  
     /**
 84  
      * Convenience Method to close a Table Data Set without
 85  
      * Exception check.
 86  
      *
 87  
      * @param qds A TableDataSet
 88  
      */
 89  
     public static final void close(class="keyword">final QueryDataSet qds)
 90  
     {
 91  0
         if (qds != null)
 92  
         {
 93  
             try
 94  
             {
 95  0
                 qds.close();
 96  
             }
 97  0
             catch (Exception ignored)
 98  
             {
 99  0
                 log.debug("Caught exception when closing a QueryDataSet",
 100  
                         ignored);
 101  0
             }
 102  
         }
 103  0
     }
 104  
 
 105  
     /**
 106  
      * Convenience Method to close an Output Stream without
 107  
      * Exception check.
 108  
      *
 109  
      * @param os An OutputStream
 110  
      */
 111  
     public static final void close(class="keyword">final OutputStream os)
 112  
     {
 113  
         try
 114  
         {
 115  0
             if (os != null)
 116  
             {
 117  0
                 os.close();
 118  
             }
 119  
         }
 120  0
         catch (Exception ignored)
 121  
         {
 122  0
             log.debug("Caught exception when closing an OutputStream",
 123  
                     ignored);
 124  0
         }
 125  0
     }
 126  
 
 127  
     /**
 128  
      * Converts a hashtable to a byte array for storage/serialization.
 129  
      *
 130  
      * @param hash The Hashtable to convert.
 131  
      * @return A byte[] with the converted Hashtable.
 132  
      * @throws Exception If an error occurs.
 133  
      */
 134  
     public static final byte[] hashtableToByteArray(class="keyword">final Hashtable hash)
 135  
         throws Exception
 136  
     {
 137  0
         Hashtable saveData = new Hashtable(hash.size());
 138  0
         byte[] byteArray = null;
 139  
 
 140  0
         Iterator keys = hash.entrySet().iterator();
 141  0
         while (keys.hasNext())
 142  
         {
 143  0
             Map.Entry entry = (Map.Entry) keys.next();
 144  0
             if (entry.getValue() instanceof Serializable)
 145  
             {
 146  0
                 saveData.put(entry.getKey(), entry.getValue());
 147  
             }
 148  0
         }
 149  
 
 150  0
         ByteArrayOutputStream baos = null;
 151  0
         BufferedOutputStream bos = null;
 152  0
         ObjectOutputStream out = null;
 153  
         try
 154  
         {
 155  
             // These objects are closed in the finally.
 156  0
             baos = new ByteArrayOutputStream();
 157  0
             bos = new BufferedOutputStream(baos);
 158  0
             out = new ObjectOutputStream(bos);
 159  
 
 160  0
             out.writeObject(saveData);
 161  
 
 162  0
             out.flush();
 163  0
             bos.flush();
 164  0
             baos.flush();
 165  0
             byteArray = baos.toByteArray();
 166  
         }
 167  
         finally
 168  
         {
 169  0
             close(out);
 170  0
             close(bos);
 171  0
             close(baos);
 172  0
         }
 173  0
         return byteArray;
 174  
     }
 175  
 
 176  
     /**
 177  
      * Factored out setting of a Village Record column from a Criteria Key
 178  
      *
 179  
      * @param crit The Criteria
 180  
      * @param key The Criterion Key
 181  
      * @param rec The Village Record
 182  
      * @param colName The name of the Column in the Record
 183  
      */
 184  
     public static final void setVillageValue(class="keyword">final Criteria crit,
 185  
             final String key,
 186  
             final Record rec,
 187  
             final String colName)
 188  
             throws Exception
 189  
     {
 190  
         // A village Record.setValue( String, Object ) would
 191  
         // be nice here.
 192  0
         Object obj = crit.getValue(key);
 193  0
         if (obj instanceof SimpleKey)
 194  
         {
 195  0
             obj = ((SimpleKey) obj).getValue();
 196  
         }
 197  0
         if (obj == null)
 198  
         {
 199  0
             rec.setValueNull(colName);
 200  0
         }
 201  0
         else if (obj instanceof String)
 202  
         {
 203  0
             rec.setValue(colName, (String) obj);
 204  0
         }
 205  0
         else if (obj instanceof Integer)
 206  
         {
 207  0
             rec.setValue(colName,
 208  
                     crit.getInt(key));
 209  0
         }
 210  0
         else if (obj instanceof BigDecimal)
 211  
         {
 212  0
             rec.setValue(colName, (BigDecimal) obj);
 213  0
         }
 214  0
         else if (obj instanceof Boolean)
 215  
         {
 216  0
             rec.setValue(colName,
 217  
                     ((Boolean) obj).booleanValue());
 218  0
         }
 219  0
         else if (obj instanceof java.util.Date)
 220  
         {
 221  0
             rec.setValue(colName,
 222  
                     (java.util.Date) obj);
 223  0
         }
 224  0
         else if (obj instanceof Float)
 225  
         {
 226  0
             rec.setValue(colName,
 227  
                     crit.getFloat(key));
 228  0
         }
 229  0
         else if (obj instanceof Double)
 230  
         {
 231  0
             rec.setValue(colName,
 232  
                     crit.getDouble(key));
 233  0
         }
 234  0
         else if (obj instanceof Byte)
 235  
         {
 236  0
             rec.setValue(colName,
 237  
                     ((Byte) obj).byteValue());
 238  0
         }
 239  0
         else if (obj instanceof Long)
 240  
         {
 241  0
             rec.setValue(colName,
 242  
                     crit.getLong(key));
 243  0
         }
 244  0
         else if (obj instanceof Short)
 245  
         {
 246  0
             rec.setValue(colName,
 247  
                     ((Short) obj).shortValue());
 248  0
         }
 249  0
         else if (obj instanceof Hashtable)
 250  
         {
 251  0
             rec.setValue(colName,
 252  
                     hashtableToByteArray((Hashtable) obj));
 253  0
         }
 254  0
         else if (obj instanceof byte[])
 255  
         {
 256  0
             rec.setValue(colName, (byte[]) obj);
 257  
         }
 258  0
     }
 259  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.