KickJava   Java API By Example, From Geeks To Geeks.

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


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.bytecode.JavaUtilConcurrentHashMapSegmentAdapter;
11 import com.tc.object.dna.api.DNA;
12 import com.tc.object.dna.api.DNACursor;
13 import com.tc.object.dna.api.DNAWriter;
14 import com.tc.object.dna.api.PhysicalAction;
15 import com.tc.object.dna.impl.DNAEncoding;
16 import com.tc.util.Assert;
17
18 import java.io.IOException JavaDoc;
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22
23 public class ConcurrentHashMapSegmentApplicator extends PhysicalApplicator {
24   private static final String JavaDoc TABLE_LENGTH_FIELD_NAME = "java.util.concurrent.ConcurrentHashMap$Segment.capacity";
25   private static final String JavaDoc TABLE_FIELD_NAME = "table";
26   
27   private final TCClass clazz;
28
29   public ConcurrentHashMapSegmentApplicator(TCClass clazz, DNAEncoding encoding) {
30     super(clazz, encoding);
31     this.clazz = clazz;
32   }
33
34   public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object JavaDoc po) throws IOException JavaDoc,
35       ClassNotFoundException JavaDoc {
36     DNACursor cursor = dna.getCursor();
37     String JavaDoc fieldName;
38     Object JavaDoc fieldValue;
39     
40     Integer JavaDoc capacity = null;
41
42     while (cursor.next(encoding)) {
43       PhysicalAction a = cursor.getPhysicalAction();
44       Assert.eval(a.isTruePhysical());
45       fieldName = a.getFieldName();
46       fieldValue = a.getObject();
47       if (TABLE_LENGTH_FIELD_NAME.equals(fieldName)) {
48         capacity = (Integer JavaDoc)fieldValue;
49       } else {
50         tcObject.setValue(fieldName, fieldValue);
51       }
52     }
53     initializeTable(capacity, po);
54   }
55   
56   private void initializeTable(Integer JavaDoc capacity, Object JavaDoc pojo) {
57     Assert.assertNotNull(capacity);
58     Class JavaDoc peerClass = clazz.getPeerClass();
59     try {
60       Method JavaDoc method = peerClass.getDeclaredMethod(JavaUtilConcurrentHashMapSegmentAdapter.INITIAL_TABLE_METHOD_NAME, new Class JavaDoc[]{Integer.TYPE});
61       method.setAccessible(true);
62       method.invoke(pojo, new Object JavaDoc[]{capacity});
63     } catch (SecurityException JavaDoc e) {
64       throw new TCRuntimeException(e);
65     } catch (NoSuchMethodException JavaDoc e) {
66       throw new TCRuntimeException(e);
67     } catch (IllegalArgumentException JavaDoc e) {
68       throw new TCRuntimeException(e);
69     } catch (IllegalAccessException JavaDoc e) {
70       throw new TCRuntimeException(e);
71     } catch (InvocationTargetException JavaDoc e) {
72       throw new TCRuntimeException(e);
73     }
74   }
75   
76   private Field JavaDoc getTableField() {
77     Class JavaDoc peerClass = clazz.getPeerClass();
78     try {
79       Field JavaDoc field = peerClass.getDeclaredField(TABLE_FIELD_NAME);
80       field.setAccessible(true);
81       return field;
82     } catch (NoSuchFieldException JavaDoc e) {
83       throw new TCRuntimeException(e);
84     }
85   }
86
87   public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object JavaDoc pojo) {
88     super.dehydrate(objectManager, tcObject, writer, pojo);
89     
90     try {
91       Field JavaDoc field = getTableField();
92       Object JavaDoc[] tableArray = (Object JavaDoc[])field.get(pojo);
93       int tableLength = tableArray.length;
94       writer.addPhysicalAction(TABLE_LENGTH_FIELD_NAME, new Integer JavaDoc(tableLength));
95     } catch (SecurityException JavaDoc e) {
96       throw new TCRuntimeException(e);
97     } catch (IllegalArgumentException JavaDoc e) {
98       throw new TCRuntimeException(e);
99     } catch (IllegalAccessException JavaDoc e) {
100       throw new TCRuntimeException(e);
101     }
102     
103   }
104
105 }
106
Popular Tags