001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.main;
029
030import org.opencms.i18n.CmsMessageContainer;
031
032import java.util.Locale;
033
034/**
035 * A replacement for <code>{@link java.lang.RuntimeException}</code> to obtain fully
036 * localized exception messages for OpenCms.<p>
037 *
038 * @since 6.0.0
039 */
040public class CmsRuntimeException extends RuntimeException implements I_CmsThrowable {
041
042    /** Serial version UID required for safe serialization. */
043    private static final long serialVersionUID = -7855345575622173787L;
044
045    /** The container for the localized message.  */
046    protected CmsMessageContainer m_message;
047
048    /**
049     * Creates a new localized Exception.<p>
050     *
051     * @param message the localized message container to use
052     */
053    public CmsRuntimeException(CmsMessageContainer message) {
054
055        super(message.getKey());
056        m_message = message;
057    }
058
059    /**
060     * Creates a new localized Exception that also containes a root cause.<p>
061     *
062     * @param message the localized message container to use
063     * @param cause the Exception root cause
064     */
065    public CmsRuntimeException(CmsMessageContainer message, Throwable cause) {
066
067        super(message.getKey(), cause);
068        m_message = message;
069    }
070
071    /**
072     * Creates a copied instance of this localized exception.<p>
073     *
074     * @param container the message container
075     * @param cause the root cause
076     *
077     * @return a copied instance of this localized exception
078     */
079    public CmsRuntimeException createException(CmsMessageContainer container, Throwable cause) {
080
081        return new CmsRuntimeException(container, cause);
082    }
083
084    /**
085     * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage()
086     */
087    @Override
088    public String getLocalizedMessage() {
089
090        return m_message.key();
091    }
092
093    /**
094     * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage(Locale)
095     */
096    public String getLocalizedMessage(Locale locale) {
097
098        return m_message.key(locale);
099    }
100
101    /**
102     * @see java.lang.Throwable#getMessage()
103     */
104    @Override
105    public String getMessage() {
106
107        return getLocalizedMessage();
108    }
109
110    /**
111     * @see org.opencms.main.I_CmsThrowable#getMessageContainer()
112     */
113    public CmsMessageContainer getMessageContainer() {
114
115        return m_message;
116    }
117}