001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.exceptions;
034:
035: import com.flexive.shared.EJBLookup;
036: import com.flexive.shared.FxArrayUtils;
037: import com.flexive.shared.FxLanguage;
038: import com.flexive.shared.FxSharedUtils;
039: import org.apache.commons.lang.ArrayUtils;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042:
043: import java.io.Serializable;
044: import java.util.Arrays;
045:
046: /**
047: * Localized Exception message handling
048: *
049: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: */
051: public class FxExceptionMessage implements Serializable {
052: private static final long serialVersionUID = -545689173017222846L;
053: private static transient Log LOG = LogFactory
054: .getLog(FxExceptionMessage.class);
055:
056: /**
057: * Default language of a resource if no _locale file is present
058: */
059: private static final String EXCEPTION_BUNDLE = "FxExceptionMessages";
060: private String key;
061: private Object[] values;
062:
063: /**
064: * Ctor
065: *
066: * @param key resource key
067: * @param values optional values for placeholders in key ({x})
068: */
069: public FxExceptionMessage(String key, Object... values) {
070: this .key = key;
071: this .values = values != null ? FxArrayUtils.clone(values)
072: : new Object[0];
073: }
074:
075: /**
076: * Getter for the key
077: *
078: * @return key resource key
079: */
080: public String getKey() {
081: return key;
082: }
083:
084: /**
085: * Get the localized message for a given language code
086: *
087: * @param localeId locale id of the desired output
088: * @return localized message
089: */
090: public String getLocalizedMessage(long localeId) {
091: FxLanguage locale;
092: try {
093: locale = EJBLookup.getLanguageEngine().load(localeId);
094: } catch (FxApplicationException e) {
095: String msg = "[Invalid locale id (" + localeId
096: + ") requested for " + key + "!]";
097: LOG.warn(msg);
098: return msg;
099: }
100: return getLocalizedMessage(locale.getId(), locale
101: .getIso2digit());
102: }
103:
104: /**
105: * Get the localized message for given ISO code
106: *
107: * @param localeIso requested ISO code for desired output
108: * @return localized message
109: */
110: public String getLocalizedMessage(String localeIso) {
111: FxLanguage locale;
112: try {
113: locale = EJBLookup.getLanguageEngine().load(localeIso);
114: } catch (FxApplicationException e) {
115: String msg = "[Invalid locale code (" + localeIso
116: + ") requested for " + key + "!]";
117: LOG.warn(msg);
118: return msg;
119: }
120: return getLocalizedMessage(locale.getId(), locale
121: .getIso2digit());
122: }
123:
124: /**
125: * Get the localized message for a given language
126: *
127: * @param locale locale of the desired output
128: * @return localized message
129: */
130: public String getLocalizedMessage(FxLanguage locale) {
131: return getLocalizedMessage(locale.getId(), locale
132: .getIso2digit());
133: }
134:
135: /**
136: * Get the localized message for a given language code and ISO
137: *
138: * @param localeId id of the requested locale
139: * @param localeIso ISO code of the requested locale
140: * @return localized message
141: */
142: public String getLocalizedMessage(long localeId, String localeIso) {
143: return FxSharedUtils.getLocalizedMessage(EXCEPTION_BUNDLE,
144: localeId, localeIso, key, values);
145: }
146:
147: /**
148: * {@inheritDoc}
149: */
150: @Override
151: public boolean equals(Object obj) {
152: if (!(obj instanceof FxExceptionMessage))
153: return false;
154: FxExceptionMessage o = (FxExceptionMessage) obj;
155: return o.getKey().equals(this .getKey())
156: && ArrayUtils.isEquals(o.values, this .values);
157: }
158:
159: @Override
160: public int hashCode() {
161: int result;
162: result = key.hashCode();
163: result = 31 * result
164: + (values != null ? Arrays.hashCode(values) : 0);
165: return result;
166: }
167: }
|