View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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.javamail.util;
19   
20  import java.util.Properties;
21  
22  import javax.mail.Session;
23  
24  /**
25   * Interface for providing access to protocol specific properties to 
26   * utility classes. 
27   */
28  public class ProtocolProperties {
29      // the protocol we're working with. 
30      protected String protocol; 
31      // a preconstructed prefix string to reduce concatenation operations.  
32      protected String protocolPrefix; 
33      // the Session that's the source of all of the properties 
34      protected Session session; 
35      // the sslConnection property.  This indicates this protocol is to use SSL for 
36      // all communications with the server. 
37      protected boolean sslConnection;
38      // the default port property.  The default port differs with the protocol 
39      // and the sslConnection property. 
40      protected int defaultPort; 
41      
42      
43      public ProtocolProperties(Session session, String protocol, boolean sslConnection, int defaultPort) {
44          this.session = session; 
45          this.protocol = protocol; 
46          this.sslConnection = sslConnection; 
47          this.defaultPort = defaultPort; 
48          // this helps avoid a lot of concatentates when retrieving properties. 
49          protocolPrefix = "mail." + protocol + ".";
50      }
51      
52      
53      /**
54       * Retrieve the Session associated with this property bundle instance.
55       * 
56       * @return A Session object that's the source of the accessed properties. 
57       */
58      public Session getSession() {
59          return session; 
60      }
61      
62      
63      /**
64       * Retrieve the name of the protocol used to access properties.
65       * 
66       * @return The protocol String name. 
67       */
68      public String getProtocol() {
69          return protocol; 
70      }
71      
72      
73      /**
74       * Retrieve the SSL Connection flag for this protocol; 
75       * 
76       * @return true if an SSL connection is required, false otherwise. 
77       */
78      public boolean getSSLConnection() {
79          return sslConnection; 
80      }
81      
82      
83      /**
84       * Return the default port to use with this connection.
85       * 
86       * @return The default port value. 
87       */
88      public int getDefaultPort() {
89          return defaultPort; 
90      }
91      
92      
93      /**
94       * Get a property associated with this mail protocol.
95       *
96       * @param name   The name of the property.
97       *
98       * @return The property value (returns null if the property has not been set).
99       */
100     public String getProperty(String name) {
101         // the name we're given is the least qualified part of the name.  
102         // We construct the full property name
103         // using the protocol
104         String fullName = protocolPrefix + name;
105         return session.getProperty(fullName);
106     }
107 
108     /**
109      * Get a property associated with this mail session.  Returns
110      * the provided default if it doesn't exist.
111      *
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 (returns defaultValue if the property has not been set).
117      */
118     public String getProperty(String name, String defaultValue) {
119         // the name we're given is the least qualified part of the name.  
120         // We construct the full property name
121         // using the protocol
122         String fullName = protocolPrefix + name;
123         String value = session.getProperty(fullName);
124         if (value == null) {
125             value = defaultValue; 
126         }
127         return value; 
128     }
129 
130 
131     /**
132      * Get a property associated with this mail session as an integer value.  Returns
133      * the default value if the property doesn't exist or it doesn't have a valid int value.
134      *
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 an int.
140      */
141     public int getIntProperty(String name, int defaultValue)
142     {
143         // retrieve the property 
144         String value = getProperty(name); 
145         // return the default value if not set. 
146         if (value == null) {
147             return defaultValue; 
148         }
149         return Integer.parseInt(value); 
150     }
151     
152 
153     /**
154      * Get a property associated with this mail session as an boolean value.  Returns
155      * the default value if the property doesn't exist or it doesn't have a valid int 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     public boolean getBooleanProperty(String name, boolean defaultValue)
164     {
165         // retrieve the property 
166         String value = getProperty(name); 
167         // return the default value if not set. 
168         if (value == null) {
169             return defaultValue; 
170         }
171         // just do a single test for true. 
172         if ("true".equals(value)) {
173             return true; 
174         }
175         // return false for anything other than true
176         return false; 
177     }
178     
179     
180     /**
181      * Get a property associated with this mail session.  Session 
182      * properties all begin with "mail."
183      *
184      * @param name   The name of the property.
185      *
186      * @return The property value (returns null if the property has not been set).
187      */
188     public String getSessionProperty(String name) {
189         // the name we're given is the least qualified part of the name.  
190         // We construct the full property name
191         // using the protocol
192         String fullName = "mail." + name;
193         return session.getProperty(fullName);
194     }
195 
196     /**
197      * Get a property associated with this mail session.  Returns
198      * the provided default if it doesn't exist.
199      *
200      * @param name   The name of the property.
201      * @param defaultValue
202      *               The default value to return if the property doesn't exist.
203      *
204      * @return The property value (returns defaultValue if the property has not been set).
205      */
206     public String getSessionProperty(String name, String defaultValue) {
207         // the name we're given is the least qualified part of the name.  
208         // We construct the full property name
209         // using the protocol
210         String fullName = "mail." + name;
211         String value = session.getProperty(fullName);
212         if (value == null) {
213             value = defaultValue; 
214         }
215         return value; 
216     }
217 
218 
219     /**
220      * Get a property associated with this mail session as an integer value.  Returns
221      * the default value if the property doesn't exist or it doesn't have a valid int value.
222      *
223      * @param name   The name of the property.
224      * @param defaultValue
225      *               The default value to return if the property doesn't exist.
226      *
227      * @return The property value converted to an int.
228      */
229     public int getIntSessionProperty(String name, int defaultValue)
230     {
231         // retrieve the property 
232         String value = getSessionProperty(name); 
233         // return the default value if not set. 
234         if (value == null) {
235             return defaultValue; 
236         }
237         return Integer.parseInt(value); 
238     }
239     
240 
241     /**
242      * Get a property associated with this mail session as an boolean value.  Returns
243      * the default value if the property doesn't exist or it doesn't have a valid int value.
244      *
245      * @param name   The name of the property.
246      * @param defaultValue
247      *               The default value to return if the property doesn't exist.
248      *
249      * @return The property value converted to a boolean
250      */
251     public boolean getBooleanSessionProperty(String name, boolean defaultValue)
252     {
253         // retrieve the property 
254         String value = getSessionProperty(name); 
255         // return the default value if not set. 
256         if (value == null) {
257             return defaultValue; 
258         }
259         // just do a single test for true. 
260         if ("true".equals(value)) {
261             return true; 
262         }
263         // return false for anything other than true
264         return false; 
265     }
266     
267     /**
268      * Get the complete set of properties associated with this Session.
269      * 
270      * @return The Session properties bundle. 
271      */
272     public Properties getProperties() {
273         return session.getProperties(); 
274     }
275     
276 }