01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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: */package org.apache.cxf.jaxws.handler;
19:
20: import java.io.InputStream;
21: import java.util.Map;
22: import java.util.logging.Logger;
23:
24: import org.apache.cxf.common.logging.LogUtils;
25: import org.apache.cxf.resource.ResourceResolver;
26:
27: public class InitParamResourceResolver implements ResourceResolver {
28:
29: private static final Logger LOG = LogUtils.getL7dLogger(
30: InitParamResourceResolver.class, "APIMessages");
31:
32: Map<String, String> params;
33:
34: public InitParamResourceResolver(Map<String, String> map) {
35: params = map;
36: }
37:
38: public <T> T resolve(String resourceName, Class<T> resourceType) {
39:
40: String value = params.get(resourceName);
41: return convertToType(value, resourceType);
42: }
43:
44: public InputStream getAsStream(String name) {
45: // returning these as a stream does not make much sense
46: return null;
47: }
48:
49: /**
50: * Convert the string representation of value to type T
51: */
52: private <T> T convertToType(String value, Class<T> type) {
53:
54: /*
55: char, byte, short, long, float, double, boolean
56: */
57: T ret = null;
58: try {
59: if (String.class.equals(type)) {
60: ret = type.cast(value);
61: } else if (Integer.class.equals(type)
62: || Integer.TYPE.equals(type)) {
63: ret = type.cast(Integer.valueOf(value));
64: } else if (Byte.class.equals(type)
65: || Byte.TYPE.equals(type)) {
66: ret = type.cast(Byte.valueOf(value));
67: } else if (Short.class.equals(type)
68: || Short.TYPE.equals(type)) {
69: ret = type.cast(Short.valueOf(value));
70: } else if (Long.class.equals(type)
71: || Long.TYPE.equals(type)) {
72: ret = type.cast(Long.valueOf(value));
73: } else if (Float.class.equals(type)
74: || Float.TYPE.equals(type)) {
75: ret = type.cast(Float.valueOf(value));
76: } else if (Double.class.equals(type)
77: || Double.TYPE.equals(type)) {
78: ret = type.cast(Double.valueOf(value));
79: } else if (Boolean.class.equals(type)
80: || Boolean.TYPE.equals(type)) {
81: ret = type.cast(Boolean.valueOf(value));
82: } else if (Character.class.equals(type)
83: || Character.TYPE.equals(type)) {
84: ret = type.cast(value.charAt(0));
85: } else {
86: LOG.severe("do not know how to treat type: " + type);
87: }
88: } catch (NumberFormatException ex) {
89: LOG.severe("badly formed init param: " + value);
90: }
91: return ret;
92: }
93: }
|