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 public class SessionUtil {
030
031 /**
032 * Get a property associated with this mail session. Returns
033 * the provided default if it doesn't exist.
034 *
035 * @param session The attached session.
036 * @param name The name of the property.
037 *
038 * @return The property value (returns null if the property has not been set).
039 */
040 static public String getProperty(Session session, String name) {
041 // occasionally, we get called with a null session if an object is not attached to
042 // a session. In that case, treat this like an unknown parameter.
043 if (session == null) {
044 return null;
045 }
046
047 return session.getProperty(name);
048 }
049
050
051 /**
052 * Get a property associated with this mail session. Returns
053 * the provided default if it doesn't exist.
054 *
055 * @param session The attached session.
056 * @param name The name of the property.
057 * @param defaultValue
058 * The default value to return if the property doesn't exist.
059 *
060 * @return The property value (returns defaultValue if the property has not been set).
061 */
062 static public String getProperty(Session session, String name, String defaultValue) {
063 String result = getProperty(session, name);
064 if (result == null) {
065 return defaultValue;
066 }
067 return result;
068 }
069
070
071 /**
072 * Process a session property as a boolean value, returning
073 * either true or false.
074 *
075 * @param session The source session.
076 * @param name
077 *
078 * @return True if the property value is "true". Returns false for any
079 * other value (including null).
080 */
081 static public boolean isPropertyTrue(Session session, String name) {
082 String property = getProperty(session, name);
083 if (property != null) {
084 return property.equals("true");
085 }
086 return false;
087 }
088
089 /**
090 * Process a session property as a boolean value, returning
091 * either true or false.
092 *
093 * @param session The source session.
094 * @param name
095 *
096 * @return True if the property value is "false". Returns false for
097 * other value (including null).
098 */
099 static public boolean isPropertyFalse(Session session, String name) {
100 String property = getProperty(session, name);
101 if (property != null) {
102 return property.equals("false");
103 }
104 return false;
105 }
106
107 /**
108 * Get a property associated with this mail session as an integer value. Returns
109 * the default value if the property doesn't exist or it doesn't have a valid int value.
110 *
111 * @param session The source session.
112 * @param name The name of the property.
113 * @param defaultValue
114 * The default value to return if the property doesn't exist.
115 *
116 * @return The property value converted to an int.
117 */
118 static public int getIntProperty(Session session, String name, int defaultValue) {
119 String result = getProperty(session, name);
120 if (result != null) {
121 try {
122 // convert into an int value.
123 return Integer.parseInt(result);
124 } catch (NumberFormatException e) {
125 }
126 }
127 // return default value if it doesn't exist is isn't convertable.
128 return defaultValue;
129 }
130
131
132 /**
133 * Get a property associated with this mail session as a boolean value. Returns
134 * the default value if the property doesn't exist or it doesn't have a valid boolean value.
135 *
136 * @param session The source session.
137 * @param name The name of the property.
138 * @param defaultValue
139 * The default value to return if the property doesn't exist.
140 *
141 * @return The property value converted to a boolean.
142 */
143 static public boolean getBooleanProperty(Session session, String name, boolean defaultValue) {
144 String result = getProperty(session, name);
145 if (result != null) {
146 return Boolean.valueOf(result).booleanValue();
147 }
148 // return default value if it doesn't exist is isn't convertable.
149 return defaultValue;
150 }
151
152
153 /**
154 * Get a system property associated with this mail session as a boolean value. Returns
155 * the default value if the property doesn't exist or it doesn't have a valid boolean value.
156 *
157 * @param name The name of the property.
158 * @param defaultValue
159 * The default value to return if the property doesn't exist.
160 *
161 * @return The property value converted to a boolean.
162 */
163 static public boolean getBooleanProperty(String name, boolean defaultValue) {
164 try {
165 String result = System.getProperty(name);
166 if (result != null) {
167 return Boolean.valueOf(result).booleanValue();
168 }
169 } catch (SecurityException e) {
170 // we can't access the property, so for all intents, it doesn't exist.
171 }
172 // return default value if it doesn't exist is isn't convertable.
173 return defaultValue;
174 }
175
176
177 /**
178 * Get a system property associated with this mail session as a boolean value. Returns
179 * the default value if the property doesn't exist.
180 *
181 * @param name The name of the property.
182 * @param defaultValue
183 * The default value to return if the property doesn't exist.
184 *
185 * @return The property value
186 */
187 static public String getProperty(String name, String defaultValue) {
188 try {
189 String result = System.getProperty(name);
190 if (result != null) {
191 return result;
192 }
193 } catch (SecurityException e) {
194 // we can't access the property, so for all intents, it doesn't exist.
195 }
196 // return default value if it doesn't exist is isn't convertable.
197 return defaultValue;
198 }
199
200
201 /**
202 * Get a system property associated with this mail session as a boolean value. Returns
203 * the default value if the property doesn't exist.
204 *
205 * @param name The name of the property.
206 *
207 * @return The property value
208 */
209 static public String getProperty(String name) {
210 try {
211 return System.getProperty(name);
212 } catch (SecurityException e) {
213 // we can't access the property, so for all intents, it doesn't exist.
214 }
215 // return null if we got an exception.
216 return null;
217 }
218 }