1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.marshall; |
8 |
| |
9 |
| import org.apache.commons.logging.Log; |
10 |
| import org.apache.commons.logging.LogFactory; |
11 |
| import org.jboss.cache.Fqn; |
12 |
| import org.jboss.cache.Region; |
13 |
| import org.jboss.cache.RegionManager; |
14 |
| import org.jboss.cache.buddyreplication.BuddyManager; |
15 |
| import org.jboss.cache.transaction.GlobalTransaction; |
16 |
| |
17 |
| import java.io.ByteArrayOutputStream; |
18 |
| import java.io.InputStream; |
19 |
| import java.io.ObjectInputStream; |
20 |
| import java.io.ObjectOutputStream; |
21 |
| import java.util.List; |
22 |
| import java.util.Map; |
23 |
| import java.util.concurrent.ConcurrentHashMap; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public abstract class AbstractMarshaller implements Marshaller |
31 |
| { |
32 |
| protected boolean useRegionBasedMarshalling; |
33 |
| protected RegionManager regionManager; |
34 |
| protected boolean defaultInactive; |
35 |
| private static Log log = LogFactory.getLog(AbstractMarshaller.class); |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| private Map<GlobalTransaction, Fqn> transactions = new ConcurrentHashMap<GlobalTransaction, Fqn>(16); |
41 |
| |
42 |
1504
| protected void init(RegionManager manager, boolean defaultInactive, boolean useRegionBasedMarshalling)
|
43 |
| { |
44 |
1504
| this.useRegionBasedMarshalling = useRegionBasedMarshalling;
|
45 |
1504
| this.defaultInactive = defaultInactive;
|
46 |
1504
| this.regionManager = manager;
|
47 |
| } |
48 |
| |
49 |
| |
50 |
0
| public byte[] objectToByteBuffer(Object obj) throws Exception
|
51 |
| { |
52 |
0
| ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
53 |
0
| ObjectOutputStream out = ObjectSerializationFactory.createObjectOutputStream(baos);
|
54 |
0
| objectToObjectStream(obj, out);
|
55 |
0
| out.close();
|
56 |
0
| return baos.toByteArray();
|
57 |
| } |
58 |
| |
59 |
0
| public Object objectFromByteBuffer(byte[] bytes) throws Exception
|
60 |
| { |
61 |
0
| ObjectInputStream in = ObjectSerializationFactory.createObjectInputStream(bytes);
|
62 |
0
| return objectFromObjectStream(in);
|
63 |
| } |
64 |
| |
65 |
0
| public Object objectFromStream(InputStream in) throws Exception
|
66 |
| { |
67 |
| |
68 |
0
| if (in instanceof ObjectInputStream)
|
69 |
| { |
70 |
0
| return objectFromObjectStream((ObjectInputStream) in);
|
71 |
| } |
72 |
| else |
73 |
| { |
74 |
0
| return objectFromObjectStream(ObjectSerializationFactory.createObjectInputStream(in));
|
75 |
| } |
76 |
| } |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
20225
| protected Fqn extractFqnFromMethodCall(MethodCall call)
|
84 |
| { |
85 |
20225
| MethodCall c0 = (MethodCall) call.getArgs()[0];
|
86 |
20225
| return extractFqn(c0);
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
1
| protected Fqn extractFqnFromListOfMethodCall(MethodCall call)
|
95 |
| { |
96 |
1
| Object[] args = call.getArgs();
|
97 |
| |
98 |
1
| MethodCall c0 = (MethodCall) ((List) args[0]).get(0);
|
99 |
1
| return extractFqn(c0);
|
100 |
| } |
101 |
| |
102 |
140292
| protected Fqn extractFqn(MethodCall methodCall)
|
103 |
| { |
104 |
140292
| if (methodCall == null)
|
105 |
| { |
106 |
0
| throw new NullPointerException("method call is null");
|
107 |
| } |
108 |
| |
109 |
140292
| Fqn fqn = null;
|
110 |
140292
| Object[] args = methodCall.getArgs();
|
111 |
140292
| switch (methodCall.getMethodId())
|
112 |
| { |
113 |
0
| case MethodDeclarations.optimisticPrepareMethod_id:
|
114 |
10
| case MethodDeclarations.prepareMethod_id:
|
115 |
| |
116 |
10
| List modifications = (List) args[1];
|
117 |
10
| fqn = extractFqn((MethodCall) modifications.get(0));
|
118 |
| |
119 |
| |
120 |
10
| boolean one_phase_commit = (Boolean) args[args.length - 1];
|
121 |
| |
122 |
| |
123 |
| |
124 |
10
| if (!one_phase_commit)
|
125 |
| { |
126 |
4
| transactions.put((GlobalTransaction) args[0], fqn);
|
127 |
| } |
128 |
10
| break;
|
129 |
1
| case MethodDeclarations.rollbackMethod_id:
|
130 |
3
| case MethodDeclarations.commitMethod_id:
|
131 |
| |
132 |
4
| fqn = transactions.remove(args[0]);
|
133 |
4
| break;
|
134 |
0
| case MethodDeclarations.getPartialStateMethod_id:
|
135 |
1
| case MethodDeclarations.dataGravitationMethod_id:
|
136 |
8
| case MethodDeclarations.evictNodeMethodLocal_id:
|
137 |
0
| case MethodDeclarations.evictVersionedNodeMethodLocal_id:
|
138 |
40005
| case MethodDeclarations.getChildrenNamesMethodLocal_id:
|
139 |
20017
| case MethodDeclarations.getDataMapMethodLocal_id:
|
140 |
0
| case MethodDeclarations.getKeysMethodLocal_id:
|
141 |
1
| case MethodDeclarations.getKeyValueMethodLocal_id:
|
142 |
5
| case MethodDeclarations.existsMethod_id:
|
143 |
60037
| fqn = (Fqn) args[0];
|
144 |
60037
| break;
|
145 |
0
| case MethodDeclarations.dataGravitationCleanupMethod_id:
|
146 |
0
| fqn = (Fqn) args[1];
|
147 |
0
| break;
|
148 |
8
| case MethodDeclarations.remoteAnnounceBuddyPoolNameMethod_id:
|
149 |
8
| case MethodDeclarations.remoteAssignToBuddyGroupMethod_id:
|
150 |
0
| case MethodDeclarations.remoteRemoveFromBuddyGroupMethod_id:
|
151 |
16
| break;
|
152 |
1
| case MethodDeclarations.replicateMethod_id:
|
153 |
60028
| case MethodDeclarations.clusteredGetMethod_id:
|
154 |
| |
155 |
60029
| fqn = extractFqn((MethodCall) args[0]);
|
156 |
60029
| break;
|
157 |
20196
| default:
|
158 |
20196
| if (MethodDeclarations.isCrudMethod(methodCall.getMethodId()))
|
159 |
| { |
160 |
20196
| fqn = (Fqn) args[1];
|
161 |
| } |
162 |
| else |
163 |
| { |
164 |
0
| throw new IllegalArgumentException("AbstractMarshaller.extractFqn(): Unknown id in method call: " + methodCall);
|
165 |
| } |
166 |
20196
| break;
|
167 |
| |
168 |
| } |
169 |
| |
170 |
140292
| if (log.isTraceEnabled())
|
171 |
| { |
172 |
0
| log.trace("extract(): received " + methodCall + "extracted fqn: " + fqn);
|
173 |
| } |
174 |
| |
175 |
140292
| return fqn;
|
176 |
| } |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
220865
| protected Region getRegion(Fqn fqn)
|
186 |
| { |
187 |
16
| if (fqn == null) return null;
|
188 |
220849
| if (BuddyManager.isBackupFqn(fqn))
|
189 |
| { |
190 |
| |
191 |
4
| fqn = BuddyManager.getActualFqn(fqn);
|
192 |
| } |
193 |
220849
| Region r = regionManager.getRegion(fqn, Region.Type.MARSHALLING, false);
|
194 |
220847
| return r;
|
195 |
| |
196 |
| } |
197 |
| } |