001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.mail.util;
019    
020    import java.security.Security;
021    
022    import javax.mail.Session;
023    
024    /**
025     * Simple utility class for managing session properties.
026     */
027    public class SessionUtil {
028    
029        /**
030         * Get a property associated with this mail session.  Returns
031         * the provided default if it doesn't exist.
032         *
033         * @param session The attached session.
034         * @param name    The name of the property.
035         *
036         * @return The property value (returns null if the property has not been set).
037         */
038        static public String getProperty(Session session, String name) {
039            // occasionally, we get called with a null session if an object is not attached to
040            // a session.  In that case, treat this like an unknown parameter.
041            if (session == null) {
042                return null;
043            }
044    
045            return session.getProperty(name);
046        }
047    
048    
049        /**
050         * Get a property associated with this mail session.  Returns
051         * the provided default if it doesn't exist.
052         *
053         * @param session The attached session.
054         * @param name    The name of the property.
055         * @param defaultValue
056         *                The default value to return if the property doesn't exist.
057         *
058         * @return The property value (returns defaultValue if the property has not been set).
059         */
060        static public String getProperty(Session session, String name, String defaultValue) {
061            String result = getProperty(session, name);
062            if (result == null) {
063                return defaultValue;
064            }
065            return result;
066        }
067    
068    
069        /**
070         * Process a session property as a boolean value, returning
071         * either true or false.
072         *
073         * @param session The source session.
074         * @param name
075         *
076         * @return True if the property value is "true".  Returns false for any
077         *         other value (including null).
078         */
079        static public boolean isPropertyTrue(Session session, String name) {
080            String property = getProperty(session, name);
081            if (property != null) {
082                return property.equals("true");
083            }
084            return false;
085        }
086    
087        /**
088         * Process a session property as a boolean value, returning
089         * either true or false.
090         *
091         * @param session The source session.
092         * @param name
093         *
094         * @return True if the property value is "false".  Returns false for
095         *         other value (including null).
096         */
097        static public boolean isPropertyFalse(Session session, String name) {
098            String property = getProperty(session, name);
099            if (property != null) {
100                return property.equals("false");
101            }
102            return false;
103        }
104    
105        /**
106         * Get a property associated with this mail session as an integer value.  Returns
107         * the default value if the property doesn't exist or it doesn't have a valid int value.
108         *
109         * @param session The source session.
110         * @param name    The name of the property.
111         * @param defaultValue
112         *                The default value to return if the property doesn't exist.
113         *
114         * @return The property value converted to an int.
115         */
116        static public int getIntProperty(Session session, String name, int defaultValue) {
117            String result = getProperty(session, name);
118            if (result != null) {
119                try {
120                    // convert into an int value.
121                    return Integer.parseInt(result);
122                } catch (NumberFormatException e) {
123                }
124            }
125            // return default value if it doesn't exist is isn't convertable.
126            return defaultValue;
127        }
128    
129    
130        /**
131         * Get a property associated with this mail session as a boolean value.  Returns
132         * the default value if the property doesn't exist or it doesn't have a valid boolean value.
133         *
134         * @param session The source session.
135         * @param name    The name of the property.
136         * @param defaultValue
137         *                The default value to return if the property doesn't exist.
138         *
139         * @return The property value converted to a boolean.
140         */
141        static public boolean getBooleanProperty(Session session, String name, boolean defaultValue) {
142            String result = getProperty(session, name);
143            if (result != null) {
144                return Boolean.valueOf(result).booleanValue();
145            }
146            // return default value if it doesn't exist is isn't convertable.
147            return defaultValue;
148        }
149    
150    
151        /**
152         * Get a system property associated with this mail session as a boolean value.  Returns
153         * the default value if the property doesn't exist or it doesn't have a valid boolean value.
154         *
155         * @param name    The name of the property.
156         * @param defaultValue
157         *                The default value to return if the property doesn't exist.
158         *
159         * @return The property value converted to a boolean.
160         */
161        static public boolean getBooleanProperty(String name, boolean defaultValue) {
162            try {
163                String result = System.getProperty(name);
164                if (result != null) {
165                    return Boolean.valueOf(result).booleanValue();
166                }
167            } catch (SecurityException e) {
168                // we can't access the property, so for all intents, it doesn't exist.
169            }
170            // return default value if it doesn't exist is isn't convertable.
171            return defaultValue;
172        }
173    
174    
175        /**
176         * Get a system property associated with this mail session as a boolean value.  Returns
177         * the default value if the property doesn't exist.
178         *
179         * @param name    The name of the property.
180         * @param defaultValue
181         *                The default value to return if the property doesn't exist.
182         *
183         * @return The property value
184         */
185        static public String getProperty(String name, String defaultValue) {
186            try {
187                String result = System.getProperty(name);
188                if (result != null) {
189                    return result;
190                }
191            } catch (SecurityException e) {
192                // we can't access the property, so for all intents, it doesn't exist.
193            }
194            // return default value if it doesn't exist is isn't convertable.
195            return defaultValue;
196        }
197    
198    
199        /**
200         * Get a system property associated with this mail session as a boolean value.  Returns
201         * the default value if the property doesn't exist.
202         *
203         * @param name    The name of the property.
204         *
205         * @return The property value
206         */
207        static public String getProperty(String name) {
208            try {
209                return System.getProperty(name);
210            } catch (SecurityException e) {
211                // we can't access the property, so for all intents, it doesn't exist.
212            }
213            // return null if we got an exception.
214            return null;
215        }
216    }