|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SessionUtil.java | 25% | 40.5% | 55.6% | 38.7% |
|
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 | 55 | static public String getProperty(Session session, String name) { |
39 | // occasionally, we get called with a null session if an object is not attached to | |
40 | // a session. In that case, treat this like an unknown parameter. | |
41 | 55 | if (session == null) { |
42 | 0 | return null; |
43 | } | |
44 | ||
45 | 55 | 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 | 0 | static public String getProperty(Session session, String name, String defaultValue) { |
61 | 0 | String result = getProperty(session, name); |
62 | 0 | if (result == null) { |
63 | 0 | return defaultValue; |
64 | } | |
65 | 0 | 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 | 0 | static public boolean isPropertyTrue(Session session, String name) { |
80 | 0 | String property = getProperty(session, name); |
81 | 0 | if (property != null) { |
82 | 0 | return property.equals("true"); |
83 | } | |
84 | 0 | 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 | 0 | static public boolean isPropertyFalse(Session session, String name) { |
98 | 0 | String property = getProperty(session, name); |
99 | 0 | if (property != null) { |
100 | 0 | return property.equals("false"); |
101 | } | |
102 | 0 | 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 | 0 | static public int getIntProperty(Session session, String name, int defaultValue) { |
117 | 0 | String result = getProperty(session, name); |
118 | 0 | if (result != null) { |
119 | 0 | try { |
120 | // convert into an int value. | |
121 | 0 | return Integer.parseInt(result); |
122 | } catch (NumberFormatException e) { | |
123 | } | |
124 | } | |
125 | // return default value if it doesn't exist is isn't convertable. | |
126 | 0 | 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 | 55 | static public boolean getBooleanProperty(Session session, String name, boolean defaultValue) { |
142 | 55 | String result = getProperty(session, name); |
143 | 55 | if (result != null) { |
144 | 0 | return Boolean.valueOf(result).booleanValue(); |
145 | } | |
146 | // return default value if it doesn't exist is isn't convertable. | |
147 | 55 | 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 | 284 | static public boolean getBooleanProperty(String name, boolean defaultValue) { |
162 | 284 | try { |
163 | 284 | String result = System.getProperty(name); |
164 | 284 | if (result != null) { |
165 | 0 | 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 | 284 | 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 | 2 | static public String getProperty(String name, String defaultValue) { |
186 | 2 | try { |
187 | 2 | String result = System.getProperty(name); |
188 | 2 | if (result != null) { |
189 | 2 | 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 | 0 | 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 | 2 | static public String getProperty(String name) { |
208 | 2 | try { |
209 | 2 | 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 | 0 | return null; |
215 | } | |
216 | } |
|