1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.mail.util;
19
20 import java.security.Security;
21
22 import javax.mail.Session;
23
24 /**
25 * Simple utility class for managing session properties.
26 */
27 public class SessionUtil {
28
29 /**
30 * Get a property associated with this mail session. Returns
31 * the provided default if it doesn't exist.
32 *
33 * @param session The attached session.
34 * @param name The name of the property.
35 *
36 * @return The property value (returns null if the property has not been set).
37 */
38 static public String getProperty(Session session, String name) {
39
40
41 if (session == null) {
42 return null;
43 }
44
45 return session.getProperty(name);
46 }
47
48
49 /**
50 * Get a property associated with this mail session. Returns
51 * the provided default if it doesn't exist.
52 *
53 * @param session The attached session.
54 * @param name The name of the property.
55 * @param defaultValue
56 * The default value to return if the property doesn't exist.
57 *
58 * @return The property value (returns defaultValue if the property has not been set).
59 */
60 static public String getProperty(Session session, String name, String defaultValue) {
61 String result = getProperty(session, name);
62 if (result == null) {
63 return defaultValue;
64 }
65 return result;
66 }
67
68
69 /**
70 * Process a session property as a boolean value, returning
71 * either true or false.
72 *
73 * @param session The source session.
74 * @param name
75 *
76 * @return True if the property value is "true". Returns false for any
77 * other value (including null).
78 */
79 static public boolean isPropertyTrue(Session session, String name) {
80 String property = getProperty(session, name);
81 if (property != null) {
82 return property.equals("true");
83 }
84 return false;
85 }
86
87 /**
88 * Process a session property as a boolean value, returning
89 * either true or false.
90 *
91 * @param session The source session.
92 * @param name
93 *
94 * @return True if the property value is "false". Returns false for
95 * other value (including null).
96 */
97 static public boolean isPropertyFalse(Session session, String name) {
98 String property = getProperty(session, name);
99 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
121 return Integer.parseInt(result);
122 } catch (NumberFormatException e) {
123 }
124 }
125
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
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
169 }
170
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
193 }
194
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
212 }
213
214 return null;
215 }
216 }