1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveremoting.caucho; |
16 | |
17 | import java.util.Map; |
18 | |
19 | import org.apache.commons.logging.Log; |
20 | import org.apache.hivemind.ApplicationRuntimeException; |
21 | import org.apache.hivemind.util.ConstructorUtils; |
22 | |
23 | import com.caucho.hessian.io.Deserializer; |
24 | import com.caucho.hessian.io.Serializer; |
25 | import com.caucho.hessian.io.SerializerFactory; |
26 | |
27 | /** |
28 | * Specific Caucho <code>SerializerFactory</code> to handle special |
29 | * Serializers and Deserializers as defined in configuration point |
30 | * <code>hiveremoting.caucho.CauchoSerializers</code>. |
31 | * @author Jean-Francois Poilpret |
32 | */ |
33 | public class CauchoSerializerFactory extends SerializerFactory |
34 | { |
35 | public CauchoSerializerFactory(Map config, Log logger) |
36 | { |
37 | _serializers = config; |
38 | _logger = logger; |
39 | } |
40 | |
41 | // Overridden |
42 | @Override protected Deserializer getDefaultDeserializer(Class clazz) |
43 | { |
44 | CauchoSerializerContribution contrib = findSerializer(clazz); |
45 | if (contrib != null) |
46 | { |
47 | if (contrib.getDeserializer() != null) |
48 | { |
49 | return contrib.getDeserializer(); |
50 | } |
51 | else if (contrib.getDeserializerClass() != null) |
52 | { |
53 | // Instantiate a deserializer for this class |
54 | return (Deserializer) instantiate(contrib.getDeserializerClass(), clazz); |
55 | } |
56 | } |
57 | // If no deserializer found or created before, use Hessian default one |
58 | return super.getDefaultDeserializer(clazz); |
59 | } |
60 | |
61 | // Overridden |
62 | @Override protected Serializer getDefaultSerializer(Class clazz) |
63 | { |
64 | CauchoSerializerContribution contrib = findSerializer(clazz); |
65 | if (contrib != null) |
66 | { |
67 | if (contrib.getSerializer() != null) |
68 | { |
69 | return contrib.getSerializer(); |
70 | } |
71 | else if (contrib.getSerializerClass() != null) |
72 | { |
73 | // Instantiate a serializer for this class |
74 | return (Serializer) instantiate(contrib.getSerializerClass(), clazz); |
75 | } |
76 | } |
77 | // If no serializer found or created before, use Hessian default one |
78 | return super.getDefaultSerializer(clazz); |
79 | } |
80 | |
81 | protected Object instantiate(Class clazz, Class arg) |
82 | { |
83 | try |
84 | { |
85 | return ConstructorUtils.invokeConstructor(clazz, new Object[]{arg}); |
86 | } |
87 | catch (ApplicationRuntimeException e) |
88 | { |
89 | _logger.warn("instantiate(): " + clazz, e); |
90 | return null; |
91 | } |
92 | } |
93 | |
94 | protected CauchoSerializerContribution findSerializer(Class clazz) |
95 | { |
96 | // The first time strict-class true is permitted, but not for next iterations |
97 | return findSerializer(clazz, true); |
98 | } |
99 | |
100 | protected CauchoSerializerContribution findSerializer(Class clazz, boolean first) |
101 | { |
102 | CauchoSerializerContribution contrib = |
103 | (CauchoSerializerContribution) _serializers.get(clazz); |
104 | if ( (contrib != null) |
105 | && ((!contrib.getStrictClass()) || first)) |
106 | { |
107 | return contrib; |
108 | } |
109 | |
110 | // Time to stop recursivity? |
111 | if (Object.class.equals(clazz)) |
112 | { |
113 | return null; |
114 | } |
115 | |
116 | // Check interfaces if first time |
117 | if (first) |
118 | { |
119 | Class[] interfaces = clazz.getInterfaces(); |
120 | for (int i = 0; i < interfaces.length; i++) |
121 | { |
122 | contrib = (CauchoSerializerContribution) _serializers.get(clazz); |
123 | if (contrib != null) |
124 | { |
125 | return contrib; |
126 | } |
127 | } |
128 | } |
129 | |
130 | // Check superclass(es) then (recursively) |
131 | return findSerializer(clazz.getSuperclass(), false); |
132 | } |
133 | |
134 | private final Map _serializers; |
135 | protected final Log _logger; |
136 | } |