| 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.hiveutils.service.impl; |
| 16 | |
| 17 | import org.apache.commons.logging.Log; |
| 18 | |
| 19 | import org.apache.hivemind.ApplicationRuntimeException; |
| 20 | import org.apache.hivemind.HiveMind; |
| 21 | import org.apache.hivemind.Location; |
| 22 | import org.apache.hivemind.internal.Module; |
| 23 | import org.apache.hivemind.service.ObjectProvider; |
| 24 | |
| 25 | /** |
| 26 | * An <code>org.apache.hivemind.service.ObjectProvider</code> |
| 27 | * that converts the passed string value into an appropriate primitive type. |
| 28 | * |
| 29 | * @author Jean-Francois Poilpret |
| 30 | */ |
| 31 | public class LiteralObjectProvider implements ObjectProvider |
| 32 | { |
| 33 | public LiteralObjectProvider(Log logger) |
| 34 | { |
| 35 | _logger = logger; |
| 36 | } |
| 37 | |
| 38 | public Object provideObject(Module contributingModule, |
| 39 | Class propertyType, |
| 40 | String inputValue, |
| 41 | Location location) |
| 42 | { |
| 43 | if (HiveMind.isBlank(inputValue)) |
| 44 | { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | int index = inputValue.indexOf(':'); |
| 49 | if (index == -1) |
| 50 | { |
| 51 | _logger.error("provideObject() invalid inputValue <" + inputValue + ">"); |
| 52 | throw new ApplicationRuntimeException( |
| 53 | "Bad input value <" + inputValue + ">", |
| 54 | location, |
| 55 | null); |
| 56 | } |
| 57 | |
| 58 | String type = inputValue.substring(0, index); |
| 59 | String value = inputValue.substring(index + 1); |
| 60 | |
| 61 | if ("string".equals(type)) |
| 62 | { |
| 63 | return value; |
| 64 | } |
| 65 | else if ("int".equals(type)) |
| 66 | { |
| 67 | return Integer.valueOf(value); |
| 68 | } |
| 69 | else if ("long".equals(type)) |
| 70 | { |
| 71 | return Long.valueOf(value); |
| 72 | } |
| 73 | else if ("short".equals(type)) |
| 74 | { |
| 75 | return Short.valueOf(value); |
| 76 | } |
| 77 | else if ("boolean".equals(type)) |
| 78 | { |
| 79 | return Boolean.valueOf(value); |
| 80 | } |
| 81 | else if ("float".equals(type)) |
| 82 | { |
| 83 | return Float.valueOf(value); |
| 84 | } |
| 85 | else if ("double".equals(type)) |
| 86 | { |
| 87 | return Double.valueOf(value); |
| 88 | } |
| 89 | else if ("byte".equals(type)) |
| 90 | { |
| 91 | return Byte.valueOf(value); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | _logger.error("provideObject() invalid inputValue <" + inputValue + ">"); |
| 96 | throw new ApplicationRuntimeException( |
| 97 | "Bad input value <" + inputValue + ">", |
| 98 | location, |
| 99 | null); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private final Log _logger; |
| 104 | } |