KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > applicator > FileApplicator


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.lang.reflect.Field JavaDoc;
21
22 public class FileApplicator extends PhysicalApplicator {
23   private final static String JavaDoc FILE_SEPARATOR_FIELD = "File.fileSeparator";
24   private final static String JavaDoc 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 JavaDoc po) throws IOException JavaDoc,
31       ClassNotFoundException JavaDoc {
32     DNACursor cursor = dna.getCursor();
33     String JavaDoc fieldName;
34     Object JavaDoc 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 JavaDoc po, Object JavaDoc fieldValue) {
53     String JavaDoc remoteFileSeparator = (String JavaDoc) fieldValue;
54     if (!remoteFileSeparator.equals(File.separator)) {
55       try {
56         Field JavaDoc pathField = po.getClass().getDeclaredField(PATH_FIELD);
57         pathField.setAccessible(true);
58         String JavaDoc path = (String JavaDoc) pathField.get(po);
59         path = path.replace(remoteFileSeparator.charAt(0), File.separatorChar);
60         FieldUtils.tcSet(po, path, pathField);
61       } catch (SecurityException JavaDoc e) {
62         throw new TCRuntimeException(e);
63       } catch (NoSuchFieldException JavaDoc e) {
64         throw new TCRuntimeException(e);
65       } catch (IllegalArgumentException JavaDoc e) {
66         throw new TCRuntimeException(e);
67       } catch (IllegalAccessException JavaDoc e) {
68         throw new TCRuntimeException(e);
69       }
70     }
71   }
72
73   public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object JavaDoc pojo) {
74     super.dehydrate(objectManager, tcObject, writer, pojo);
75
76     String JavaDoc fieldName = FILE_SEPARATOR_FIELD;
77     String JavaDoc fieldValue = File.separator;
78     writer.addPhysicalAction(fieldName, fieldValue, false);
79   }
80 }
81
Popular Tags