KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > remote > hessian > EnumSerializerFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.remote.hessian;
21
22 import java.util.HashMap JavaDoc;
23
24 import com.caucho.hessian.io.AbstractSerializerFactory;
25 import com.caucho.hessian.io.Deserializer;
26 import com.caucho.hessian.io.HessianProtocolException;
27 import com.caucho.hessian.io.Serializer;
28
29 /**
30  * A Hessian SerializerFactory extension that supports serializing Enums.
31  * <p>
32  * <i>Requires Java 1.5 or newer</i>
33  * </p>
34  *
35  * @since 1.2
36  * @author Andrus Adamchik
37  */

38 class EnumSerializerFactory extends AbstractSerializerFactory {
39
40     private final EnumSerializer enumSerializer = new EnumSerializer();
41     private HashMap JavaDoc<Class JavaDoc, Deserializer> cachedDeserializerMap;
42
43     public Serializer getSerializer(Class JavaDoc cl) throws HessianProtocolException {
44         return (cl.isEnum()) ? enumSerializer : null;
45     }
46
47     public Deserializer getDeserializer(Class JavaDoc cl) throws HessianProtocolException {
48         if (cl.isEnum()) {
49             Deserializer deserializer = null;
50             
51             synchronized (this) {
52                 
53                 if (cachedDeserializerMap != null) {
54                     deserializer = cachedDeserializerMap.get(cl);
55                 }
56
57                 if (deserializer == null) {
58                     deserializer = new EnumDeserializer(cl);
59
60                     if (cachedDeserializerMap == null) {
61                         cachedDeserializerMap = new HashMap JavaDoc<Class JavaDoc, Deserializer>();
62                     }
63
64                     cachedDeserializerMap.put(cl, deserializer);
65                 }
66             }
67
68             return deserializer;
69         }
70
71         return null;
72     }
73 }
74
Popular Tags