001    /*
002     * Copyright 2004-2005 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.transit;
020    
021    /**
022     * A simple wrapper exception around exceptions that could occur while accessing
023     * environment parameters.
024     *
025     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
026     * @version 1.1.0
027     */
028    public class EnvironmentException extends RuntimeException
029    {
030       /**
031        * Serial version identifier.
032        */
033        static final long serialVersionUID = 1L;
034    
035       /**
036        * the environment variable name if available
037        */
038        private final String m_variable;
039    
040       /**
041        * root cause
042        */
043        private final Throwable m_cause;
044    
045        /**
046         * Creates an exception denoting a failure while attempting to access an
047         * environment variable within an operating system and shell specific
048         * environment that is caused by another exception.
049         *
050         * @param cause the underlying exception that caused the failure
051         */
052        EnvironmentException( final Throwable cause )
053        {
054            super();
055    
056            m_variable = null;
057            m_cause = cause;
058        }
059    
060    
061        /**
062         * Creates an exception denoting a failure while attempting to access an
063         * environment variable within an operating system and shell specific
064         * environment.
065         *
066         * @param message the reason for the access failure
067         */
068        EnvironmentException( final String message )
069        {
070            super( message );
071    
072            m_variable = null;
073            m_cause = null;
074        }
075    
076    
077        /**
078         * Creates an exception denoting a failure while attempting to access an
079         * environment variable within an operating system and shell specific
080         * environment that is caused by another exception.
081         *
082         * @param variable the variable whose value was to be accessed
083         * @param cause the underlying exception that caused the failure
084         */
085        EnvironmentException( final String variable, final Throwable cause )
086        {
087            super();
088    
089            m_variable = variable;
090            m_cause = cause;
091        }
092    
093    
094        /**
095         * Creates an exception denoting a failure while attempting to access an
096         * environment variable within an operating system and shell specific
097         * environment.
098         *
099         * @param variable the variable whose value was to be accessed
100         * @param message the reason for the access failure
101         */
102        EnvironmentException( final String variable, final String message )
103        {
104            super( message );
105    
106            m_variable = variable;
107            m_cause = null;
108        }
109    
110    
111        /**
112         * Gets the variable that was to be accessed.
113         *
114         * @return the value of the variable
115         */
116        public String getVariable()
117        {
118            return m_variable;
119        }
120    
121    
122        /**
123         * Return the causal exception.
124         *
125         * @return the exception that caused this exception (possibly null)
126         */
127        public Throwable getCause()
128        {
129            return m_cause;
130        }
131    
132    
133        /**
134         * Prepends variable name to the base message.
135         * @return the exception message
136         * @see java.lang.Throwable#getMessage()
137         */
138        public String getMessage()
139        {
140            String base = super.getMessage();
141    
142            if ( null == base )
143            {
144                return "Failed to access " + m_variable + " environment variable";
145            }
146    
147            return "Failed to access " + m_variable
148                + " environment variable - " + base;
149        }
150    }
151    
152    
153