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