001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.applicator;
006:
007: import com.tc.object.ClientObjectManager;
008: import com.tc.object.TCClass;
009: import com.tc.object.TCObject;
010: import com.tc.object.dna.api.DNA;
011: import com.tc.object.dna.api.DNACursor;
012: import com.tc.object.dna.api.DNAWriter;
013: import com.tc.object.dna.api.DNAEncoding;
014: import com.tc.object.dna.api.PhysicalAction;
015: import com.tc.util.Assert;
016:
017: import java.io.EOFException;
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.lang.reflect.Method;
022:
023: public class FileApplicator extends PhysicalApplicator {
024: private final static Method FILE_READ_OBJECT = findReadObjectMethod();
025:
026: private final static String FILE_SEPARATOR_FIELD = "java.io.File._tcFileSeparator";
027:
028: public FileApplicator(TCClass clazz, DNAEncoding encoding) {
029: super (clazz, encoding);
030: }
031:
032: private static Method findReadObjectMethod() {
033: try {
034: Method m = File.class.getDeclaredMethod("readObject",
035: new Class[] { ObjectInputStream.class });
036: m.setAccessible(true);
037: return m;
038: } catch (Exception e) {
039: throw new RuntimeException(e);
040: }
041: }
042:
043: public void hydrate(ClientObjectManager objectManager,
044: TCObject tcObject, DNA dna, Object po) throws IOException,
045: ClassNotFoundException {
046: DNACursor cursor = dna.getCursor();
047: boolean remoteFileSeparatorObtained = false;
048: char sepChar = 0;
049:
050: while (cursor.next(encoding)) {
051: PhysicalAction a = cursor.getPhysicalAction();
052: Assert.eval(a.isTruePhysical());
053: String fieldName = a.getFieldName();
054: Object fieldValue = a.getObject();
055: if (FILE_SEPARATOR_FIELD.equals(fieldName)) {
056: sepChar = ((String) fieldValue).charAt(0);
057: remoteFileSeparatorObtained = true;
058: } else {
059: tcObject.setValue(fieldName, fieldValue);
060: }
061: }
062:
063: if (!dna.isDelta()) {
064: Assert.assertTrue(remoteFileSeparatorObtained);
065: try {
066: FILE_READ_OBJECT.invoke(po,
067: new Object[] { new FileObjectInputStream(
068: sepChar) });
069: } catch (Exception e) {
070: throw new RuntimeException(e);
071: }
072: }
073: }
074:
075: public void dehydrate(ClientObjectManager objectManager,
076: TCObject tcObject, DNAWriter writer, Object pojo) {
077: super .dehydrate(objectManager, tcObject, writer, pojo);
078:
079: String fieldName = FILE_SEPARATOR_FIELD;
080: String fieldValue = File.separator;
081: writer.addPhysicalAction(fieldName, fieldValue, false);
082: }
083:
084: private static class FileObjectInputStream extends
085: ObjectInputStream {
086:
087: private final char sep;
088: private boolean charRead;
089:
090: protected FileObjectInputStream(char sep) throws IOException,
091: SecurityException {
092: super ();
093: this .sep = sep;
094: }
095:
096: public void defaultReadObject() {
097: //
098: }
099:
100: public char readChar() throws IOException {
101: if (charRead) {
102: throw new EOFException();
103: }
104: charRead = true;
105: return sep;
106: }
107: }
108:
109: }
|