summaryrefslogtreecommitdiffstats
path: root/sandbox/sebastien/java/sca-node/modules/binding-corba/src/main/java/org/apache/tuscany/sca/binding/corba/types/util/TypeHelpersProxy.java
blob: c81f101a7e9d71a0955192b1669ebe9f0db7966b (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * 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.tuscany.sca.binding.corba.types.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.tuscany.sca.binding.corba.types.NodeType;
import org.apache.tuscany.sca.binding.corba.types.TypeTreeNode;
import org.omg.CORBA.TypeCode;
import org.omg.CORBA.portable.InputStream;
import org.omg.CORBA.portable.OutputStream;

/**
 * @version $Rev$ $Date$
 * Proxies getType(), write(), read() methods to appropriate TypeHelper implementation.
 */
public class TypeHelpersProxy {

	/**
	 * Maps primitive types to its TypeHelper implementations
	 */
	private static Map<Class<?>, TypeHelper> primitiveTypes = null;

	/**
	 * Maps other types to its TypeHelper implementations
	 */
	private static Map<NodeType, TypeHelper> complexTypes = null;

	static {
		// initiate type helpers
		primitiveTypes = new HashMap<Class<?>, TypeHelper>();
		complexTypes = new HashMap<NodeType, TypeHelper>();

		primitiveTypes.put(boolean.class, new BooleanTypeHelper());
		primitiveTypes.put(char.class, new CharTypeHelper());
		primitiveTypes.put(byte.class, new ByteTypeHelper());
		primitiveTypes.put(short.class, new ShortTypeHelper());
		primitiveTypes.put(int.class, new IntTypeHelper());
		primitiveTypes.put(long.class, new LongTypeHelper());
		primitiveTypes.put(float.class, new FloatTypeHelper());
		primitiveTypes.put(double.class, new DoubleTypeHelper());
		primitiveTypes.put(String.class, new StringTypeHelper());
		primitiveTypes.put(Boolean.class, primitiveTypes.get(boolean.class));
		primitiveTypes.put(Character.class, primitiveTypes.get(char.class));
		primitiveTypes.put(Byte.class, primitiveTypes.get(byte.class));
		primitiveTypes.put(Short.class, primitiveTypes.get(short.class));
		primitiveTypes.put(Integer.class, primitiveTypes.get(int.class));
		primitiveTypes.put(Long.class, primitiveTypes.get(long.class));
		primitiveTypes.put(Float.class, primitiveTypes.get(float.class));
		primitiveTypes.put(Double.class, primitiveTypes.get(double.class));
		primitiveTypes.put(String.class, primitiveTypes.get(String.class));
		complexTypes.put(NodeType.array, new ArrayTypeHelper());
		complexTypes.put(NodeType.struct, new StructTypeHelper());
		complexTypes.put(NodeType.reference, new ReferenceTypeHelper());
		complexTypes.put(NodeType.sequence, new SequenceTypeHelper());
	}

	/**
	 * Gets type helper basing on given type
	 * 
	 * @param node
	 * @return
	 */
	private static TypeHelper getTypeHelper(TypeTreeNode node) {
		TypeHelper typeHelper = null;
		NodeType type = node.getNodeType();
		if (type.equals(NodeType.primitive)) {
			typeHelper = primitiveTypes.get(node.getJavaClass());
		} else {
			typeHelper = complexTypes.get(type);
		}
		return typeHelper;
	}

	/**
	 * Proxies getType method invocation to appropriate TypeHelper
	 * implementation. It's not being used since I've changed method for passing
	 * arguments. May be useful while implementing CORBA service bindings.
	 * 
	 * @param node
	 * @return
	 */
	public static final TypeCode getType(TypeTreeNode node) {
		TypeHelper typeHelper = getTypeHelper(node);
		TypeCode typeCode = typeHelper.getType(node);
		return typeCode;
	}

	/**
	 * Proxies read method invocation to appropriate TypeHelper implementation.
	 * 
	 * @param node
	 * @param is
	 * @return
	 */
	public static final Object read(TypeTreeNode node, InputStream is) {
		TypeHelper helper = getTypeHelper(node);
		return helper.read(node, is);
	}

	/**
	 * Proxies write method invocation to appropriate TypeHelper implementation.
	 * 
	 * @param node
	 * @param os
	 * @param data
	 */
	public static final void write(TypeTreeNode node, OutputStream os,
			Object data) {
		TypeHelper helper = getTypeHelper(node);
		helper.write(node, os, data);
	}

}