| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.config; |
|---|
| 34 | |
|---|
| 35 | import java.util.logging.Logger; |
|---|
| 36 | |
|---|
| 37 | import org.jcoderz.commons.ArgumentMalformedException; |
|---|
| 38 | import org.jcoderz.commons.util.Assert; |
|---|
| 39 | import org.jcoderz.commons.util.Constants; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Implementation of the common ConfigurationService business methods. |
|---|
| 44 | * |
|---|
| 45 | * This class holds all business logic of the service. |
|---|
| 46 | * Implementing the business interface ConfigurationServiceCommonInterface. |
|---|
| 47 | * |
|---|
| 48 | */ |
|---|
| 49 | public class ConfigurationServiceCommonImpl |
|---|
| 50 | implements ConfigurationServiceCommonInterface |
|---|
| 51 | { |
|---|
| 52 | /** class name for use in logging. */ |
|---|
| 53 | private static final String CLASSNAME |
|---|
| 54 | = ConfigurationServiceCommonImpl.class.getName(); |
|---|
| 55 | |
|---|
| 56 | /** class logger. */ |
|---|
| 57 | private static final Logger logger = Logger.getLogger(CLASSNAME); |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Name of a parameter in a few get.. methods. |
|---|
| 61 | * This member exists only for checkstyles's sake. |
|---|
| 62 | */ |
|---|
| 63 | private static final transient |
|---|
| 64 | String PARAM_KEY_OF_GET_METHODS = "key"; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * {@inheritDoc} |
|---|
| 69 | * FIXME: activate ejb tag if XDoclet BugFix is available |
|---|
| 70 | * @XXXejb.interface-method view-type="remote" |
|---|
| 71 | * @XXXejb.transaction type="Required" |
|---|
| 72 | */ |
|---|
| 73 | public boolean getBoolean (ConfigurationKey key) |
|---|
| 74 | throws ConfigurationValueNotFoundException, |
|---|
| 75 | ConfigurationTypeConversionFailedException, |
|---|
| 76 | ArgumentMalformedException |
|---|
| 77 | { |
|---|
| 78 | Assert.notNull(key, PARAM_KEY_OF_GET_METHODS); |
|---|
| 79 | boolean bReturn; |
|---|
| 80 | |
|---|
| 81 | final String valueAsString = getString(key); |
|---|
| 82 | final Boolean value = Boolean.valueOf(valueAsString); |
|---|
| 83 | if (value.booleanValue()) |
|---|
| 84 | { |
|---|
| 85 | bReturn = true; |
|---|
| 86 | } |
|---|
| 87 | else if (valueAsString.toUpperCase(Constants.SYSTEM_LOCALE) |
|---|
| 88 | .equals("FALSE")) |
|---|
| 89 | { |
|---|
| 90 | bReturn = false; |
|---|
| 91 | } |
|---|
| 92 | else |
|---|
| 93 | { |
|---|
| 94 | throw new ConfigurationTypeConversionFailedException( |
|---|
| 95 | valueAsString, key, "Boolean"); |
|---|
| 96 | } |
|---|
| 97 | return bReturn; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * {@inheritDoc} |
|---|
| 103 | * FIXME: activate ejb tag if XDoclet BugFix is available |
|---|
| 104 | * @XXXejb.interface-method view-type="remote" |
|---|
| 105 | * @XXXejb.transaction type="Required" |
|---|
| 106 | */ |
|---|
| 107 | public int getInt (ConfigurationKey key) |
|---|
| 108 | throws ConfigurationValueNotFoundException, |
|---|
| 109 | ConfigurationTypeConversionFailedException, |
|---|
| 110 | ArgumentMalformedException |
|---|
| 111 | { |
|---|
| 112 | Assert.notNull(key, PARAM_KEY_OF_GET_METHODS); |
|---|
| 113 | int iReturn; |
|---|
| 114 | |
|---|
| 115 | final String valueAsString = getString(key); |
|---|
| 116 | try |
|---|
| 117 | { |
|---|
| 118 | iReturn = Integer.parseInt(valueAsString); |
|---|
| 119 | } |
|---|
| 120 | catch (NumberFormatException nfe) |
|---|
| 121 | { |
|---|
| 122 | throw new ConfigurationTypeConversionFailedException( |
|---|
| 123 | valueAsString, key, "Integer", nfe); |
|---|
| 124 | } |
|---|
| 125 | return iReturn; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | * {@inheritDoc} |
|---|
| 131 | * FIXME: activate ejb tag if XDoclet BugFix is available |
|---|
| 132 | * @XXXejb.interface-method view-type="remote" |
|---|
| 133 | * @XXXejb.transaction type="Required" |
|---|
| 134 | */ |
|---|
| 135 | public long getLong (ConfigurationKey key) |
|---|
| 136 | throws ConfigurationValueNotFoundException, |
|---|
| 137 | ConfigurationTypeConversionFailedException, |
|---|
| 138 | ArgumentMalformedException |
|---|
| 139 | { |
|---|
| 140 | Assert.notNull(key, PARAM_KEY_OF_GET_METHODS); |
|---|
| 141 | long lReturn; |
|---|
| 142 | |
|---|
| 143 | final String valueAsString = getString(key); |
|---|
| 144 | try |
|---|
| 145 | { |
|---|
| 146 | lReturn = Long.parseLong(valueAsString); |
|---|
| 147 | } |
|---|
| 148 | catch (NumberFormatException nfe) |
|---|
| 149 | { |
|---|
| 150 | throw new ConfigurationTypeConversionFailedException( |
|---|
| 151 | valueAsString, key, "Long", nfe); |
|---|
| 152 | } |
|---|
| 153 | return lReturn; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * {@inheritDoc} |
|---|
| 159 | * FIXME: activate ejb tag if XDoclet BugFix is available |
|---|
| 160 | * @XXXejb.interface-method view-type="remote" |
|---|
| 161 | * @XXXejb.transaction type="Required" |
|---|
| 162 | */ |
|---|
| 163 | public String getString (ConfigurationKey key) |
|---|
| 164 | throws ConfigurationValueNotFoundException, |
|---|
| 165 | ArgumentMalformedException |
|---|
| 166 | { |
|---|
| 167 | Assert.notNull(key, PARAM_KEY_OF_GET_METHODS); |
|---|
| 168 | return getConfigurationCacheCurrent().getString(key.toString()); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | protected ConfigurationCacheInterface getConfigurationCacheCurrent () |
|---|
| 172 | { |
|---|
| 173 | // for database |
|---|
| 174 | return ConfigurationCacheByDbReadOnlyImpl.current(); |
|---|
| 175 | // for properties file |
|---|
| 176 | //return ConfigurationCacheByPropertiesImpl.current(); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | } |
|---|