1 package org.apache.torque.util; 2 3 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 46 public final class VillageUtils 47 { 48 49 private static Log log = LogFactory.getLog(VillageUtils.class); 50 51 57 private VillageUtils() 58 { 59 } 60 61 67 public static final void close(final TableDataSet tds) 68 { 69 if (tds != null) 70 { 71 try 72 { 73 tds.close(); 74 } 75 catch (Exception ignored) 76 { 77 log.debug("Caught exception when closing a TableDataSet", 78 ignored); 79 } 80 } 81 } 82 83 89 public static final void close(final QueryDataSet qds) 90 { 91 if (qds != null) 92 { 93 try 94 { 95 qds.close(); 96 } 97 catch (Exception ignored) 98 { 99 log.debug("Caught exception when closing a QueryDataSet", 100 ignored); 101 } 102 } 103 } 104 105 111 public static final void close(final OutputStream os) 112 { 113 try 114 { 115 if (os != null) 116 { 117 os.close(); 118 } 119 } 120 catch (Exception ignored) 121 { 122 log.debug("Caught exception when closing an OutputStream", 123 ignored); 124 } 125 } 126 127 134 public static final byte[] hashtableToByteArray(final Hashtable hash) 135 throws Exception 136 { 137 Hashtable saveData = new Hashtable (hash.size()); 138 byte[] byteArray = null; 139 140 Iterator keys = hash.entrySet().iterator(); 141 while (keys.hasNext()) 142 { 143 Map.Entry entry = (Map.Entry ) keys.next(); 144 if (entry.getValue() instanceof Serializable ) 145 { 146 saveData.put(entry.getKey(), entry.getValue()); 147 } 148 } 149 150 ByteArrayOutputStream baos = null; 151 BufferedOutputStream bos = null; 152 ObjectOutputStream out = null; 153 try 154 { 155 baos = new ByteArrayOutputStream (); 157 bos = new BufferedOutputStream (baos); 158 out = new ObjectOutputStream (bos); 159 160 out.writeObject(saveData); 161 162 out.flush(); 163 bos.flush(); 164 baos.flush(); 165 byteArray = baos.toByteArray(); 166 } 167 finally 168 { 169 close(out); 170 close(bos); 171 close(baos); 172 } 173 return byteArray; 174 } 175 176 184 public static final void setVillageValue(final Criteria crit, 185 final String key, 186 final Record rec, 187 final String colName) 188 throws Exception 189 { 190 Object obj = crit.getValue(key); 193 if (obj instanceof SimpleKey) 194 { 195 obj = ((SimpleKey) obj).getValue(); 196 } 197 if (obj == null) 198 { 199 rec.setValueNull(colName); 200 } 201 else if (obj instanceof String ) 202 { 203 rec.setValue(colName, (String ) obj); 204 } 205 else if (obj instanceof Integer ) 206 { 207 rec.setValue(colName, 208 crit.getInt(key)); 209 } 210 else if (obj instanceof BigDecimal ) 211 { 212 rec.setValue(colName, (BigDecimal ) obj); 213 } 214 else if (obj instanceof Boolean ) 215 { 216 rec.setValue(colName, 217 ((Boolean ) obj).booleanValue()); 218 } 219 else if (obj instanceof java.util.Date ) 220 { 221 rec.setValue(colName, 222 (java.util.Date ) obj); 223 } 224 else if (obj instanceof Float ) 225 { 226 rec.setValue(colName, 227 crit.getFloat(key)); 228 } 229 else if (obj instanceof Double ) 230 { 231 rec.setValue(colName, 232 crit.getDouble(key)); 233 } 234 else if (obj instanceof Byte ) 235 { 236 rec.setValue(colName, 237 ((Byte ) obj).byteValue()); 238 } 239 else if (obj instanceof Long ) 240 { 241 rec.setValue(colName, 242 crit.getLong(key)); 243 } 244 else if (obj instanceof Short ) 245 { 246 rec.setValue(colName, 247 ((Short ) obj).shortValue()); 248 } 249 else if (obj instanceof Hashtable ) 250 { 251 rec.setValue(colName, 252 hashtableToByteArray((Hashtable ) obj)); 253 } 254 else if (obj instanceof byte[]) 255 { 256 rec.setValue(colName, (byte[]) obj); 257 } 258 } 259 } 260 | Popular Tags |