1 4 package com.tc.object.applicator; 5 6 import com.tc.exception.TCRuntimeException; 7 import com.tc.object.ClientObjectManager; 8 import com.tc.object.TCClass; 9 import com.tc.object.TCObject; 10 import com.tc.object.dna.api.DNA; 11 import com.tc.object.dna.api.DNACursor; 12 import com.tc.object.dna.api.DNAWriter; 13 import com.tc.object.dna.api.PhysicalAction; 14 import com.tc.object.dna.impl.DNAEncoding; 15 import com.tc.util.Assert; 16 import com.tc.util.FieldUtils; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.lang.reflect.Field ; 21 22 public class FileApplicator extends PhysicalApplicator { 23 private final static String FILE_SEPARATOR_FIELD = "File.fileSeparator"; 24 private final static String PATH_FIELD = "path"; 25 26 public FileApplicator(TCClass clazz, DNAEncoding encoding) { 27 super(clazz, encoding); 28 } 29 30 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object po) throws IOException , 31 ClassNotFoundException { 32 DNACursor cursor = dna.getCursor(); 33 String fieldName; 34 Object fieldValue; 35 boolean remoteFileSeparatorObtained = false; 36 37 while (cursor.next(encoding)) { 38 PhysicalAction a = cursor.getPhysicalAction(); 39 Assert.eval(a.isTruePhysical()); 40 fieldName = a.getFieldName(); 41 fieldValue = a.getObject(); 42 if (FILE_SEPARATOR_FIELD.equals(fieldName)) { 43 replaceFileSeparator(po, fieldValue); 44 remoteFileSeparatorObtained = true; 45 } else { 46 tcObject.setValue(fieldName, fieldValue); 47 } 48 } 49 Assert.assertTrue(remoteFileSeparatorObtained); 50 } 51 52 private void replaceFileSeparator(Object po, Object fieldValue) { 53 String remoteFileSeparator = (String ) fieldValue; 54 if (!remoteFileSeparator.equals(File.separator)) { 55 try { 56 Field pathField = po.getClass().getDeclaredField(PATH_FIELD); 57 pathField.setAccessible(true); 58 String path = (String ) pathField.get(po); 59 path = path.replace(remoteFileSeparator.charAt(0), File.separatorChar); 60 FieldUtils.tcSet(po, path, pathField); 61 } catch (SecurityException e) { 62 throw new TCRuntimeException(e); 63 } catch (NoSuchFieldException e) { 64 throw new TCRuntimeException(e); 65 } catch (IllegalArgumentException e) { 66 throw new TCRuntimeException(e); 67 } catch (IllegalAccessException e) { 68 throw new TCRuntimeException(e); 69 } 70 } 71 } 72 73 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 74 super.dehydrate(objectManager, tcObject, writer, pojo); 75 76 String fieldName = FILE_SEPARATOR_FIELD; 77 String fieldValue = File.separator; 78 writer.addPhysicalAction(fieldName, fieldValue, false); 79 } 80 } 81 | Popular Tags |