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