001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.geronimo.javamail.util;
019     
020    import java.util.Properties;
021    
022    import javax.mail.Session;
023    
024    /**
025     * Interface for providing access to protocol specific properties to 
026     * utility classes. 
027     */
028    public class ProtocolProperties {
029        // the protocol we're working with. 
030        protected String protocol; 
031        // a preconstructed prefix string to reduce concatenation operations.  
032        protected String protocolPrefix; 
033        // the Session that's the source of all of the properties 
034        protected Session session; 
035        // the sslConnection property.  This indicates this protocol is to use SSL for 
036        // all communications with the server. 
037        protected boolean sslConnection;
038        // the default port property.  The default port differs with the protocol 
039        // and the sslConnection property. 
040        protected int defaultPort; 
041        
042        
043        public ProtocolProperties(Session session, String protocol, boolean sslConnection, int defaultPort) {
044            this.session = session; 
045            this.protocol = protocol; 
046            this.sslConnection = sslConnection; 
047            this.defaultPort = defaultPort; 
048            // this helps avoid a lot of concatentates when retrieving properties. 
049            protocolPrefix = "mail." + protocol + ".";
050        }
051        
052        
053        /**
054         * Retrieve the Session associated with this property bundle instance.
055         * 
056         * @return A Session object that's the source of the accessed properties. 
057         */
058        public Session getSession() {
059            return session; 
060        }
061        
062        
063        /**
064         * Retrieve the name of the protocol used to access properties.
065         * 
066         * @return The protocol String name. 
067         */
068        public String getProtocol() {
069            return protocol; 
070        }
071        
072        
073        /**
074         * Retrieve the SSL Connection flag for this protocol; 
075         * 
076         * @return true if an SSL connection is required, false otherwise. 
077         */
078        public boolean getSSLConnection() {
079            return sslConnection; 
080        }
081        
082        
083        /**
084         * Return the default port to use with this connection.
085         * 
086         * @return The default port value. 
087         */
088        public int getDefaultPort() {
089            return defaultPort; 
090        }
091        
092        
093        /**
094         * Get a property associated with this mail protocol.
095         *
096         * @param name   The name of the property.
097         *
098         * @return The property value (returns null if the property has not been set).
099         */
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    }