-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
GridClientOptimizedMarshaller.java
118 lines (97 loc) · 3.79 KB
/
GridClientOptimizedMarshaller.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.internal.client.marshaller.optimized;
import org.apache.ignite.*;
import org.apache.ignite.internal.*;
import org.apache.ignite.internal.client.marshaller.*;
import org.apache.ignite.internal.processors.rest.client.message.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.optimized.*;
import java.io.*;
import java.nio.*;
/**
* Wrapper, that adapts {@link org.apache.ignite.marshaller.optimized.OptimizedMarshaller} to
* {@link GridClientMarshaller} interface.
*/
public class GridClientOptimizedMarshaller implements GridClientMarshaller {
/** ID. */
public static final byte ID = 1;
/** Optimized marshaller. */
private final OptimizedMarshaller opMarsh;
/**
* Default constructor.
*/
public GridClientOptimizedMarshaller() {
opMarsh = new OptimizedMarshaller();
opMarsh.setContext(new ClientMarshallerContext());
}
/**
* Constructs optimized marshaller with specific parameters.
*
* @param requireSer Require serializable flag.
* @param poolSize Object streams pool size.
* @throws IOException If an I/O error occurs while writing stream header.
* @throws IgniteException If this marshaller is not supported on the current JVM.
* @see OptimizedMarshaller
*/
public GridClientOptimizedMarshaller(boolean requireSer, int poolSize) throws IOException {
opMarsh = new OptimizedMarshaller();
opMarsh.setContext(new ClientMarshallerContext());
opMarsh.setRequireSerializable(requireSer);
opMarsh.setPoolSize(poolSize);
}
/** {@inheritDoc} */
@Override public ByteBuffer marshal(Object obj, int off) throws IOException {
try {
if (!(obj instanceof GridClientMessage))
throw new IOException("Message serialization of given type is not supported: " +
obj.getClass().getName());
byte[] bytes = opMarsh.marshal(obj);
ByteBuffer buf = ByteBuffer.allocate(off + bytes.length);
buf.position(off);
buf.put(bytes);
buf.flip();
return buf;
}
catch (IgniteCheckedException e) {
throw new IOException(e);
}
}
/** {@inheritDoc} */
@Override public <T> T unmarshal(byte[] bytes) throws IOException {
try {
return opMarsh.unmarshal(bytes, null);
}
catch (IgniteCheckedException e) {
throw new IOException(e);
}
}
/**
*/
private static class ClientMarshallerContext extends MarshallerContextAdapter {
/** {@inheritDoc} */
@Override public void registerClass(int id, Class cls) {
// No-op.
}
/** {@inheritDoc} */
@Override public Class className(int id, ClassLoader ldr) throws ClassNotFoundException {
String clsName = clsNameById.get(id);
assert clsName != null : id;
return U.forName(clsName, ldr);
}
}
}