001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.geronimo.mail;
019
020 import java.util.Enumeration;
021 import java.util.Properties;
022
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025
026 import org.apache.geronimo.gbean.GBeanInfo;
027 import org.apache.geronimo.gbean.GBeanInfoBuilder;
028 import org.apache.geronimo.gbean.GBeanLifecycle;
029
030 /**
031 * A generic GBean that provides for the configuration of a JavaMail protocol.
032 * <p/>
033 * Values that are set in the individual member variables will override any of
034 * the corresponding values that have been set in the properties set.
035 *
036 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
037 */
038 public class ProtocolGBean implements GBeanLifecycle {
039
040 // common attributes exported by all ProtocolBeans
041 static public final String GBEAN_OBJECTNAME = "objectName";
042 static public final String GBEAN_PROTOCOL = "protocol";
043 static public final String GBEAN_PROPERTIES = "properties";
044 static public final String GBEAN_HOST = "host";
045 static public final String GBEAN_USER = "user";
046 static public final String GBEAN_ADD_OVERRIDES = "addOverrides";
047
048 // common constants for GBEAN properties that are used by a number of transports.
049 static public final String GBEAN_PORT = "port";
050 static public final String GBEAN_CONNECTION_TIMEOUT = "connectionTimeout";
051 static public final String GBEAN_TIMEOUT = "timeout";
052 static public final String GBEAN_FROM = "from";
053 static public final String GBEAN_AUTH = "auth";
054 static public final String GBEAN_REALM = "saslRealm";
055 static public final String GBEAN_QUITWAIT = "quitWait";
056 static public final String GBEAN_FACTORY_CLASS = "socketFactoryClass";
057 static public final String GBEAN_FACTORY_FALLBACK = "socketFactoryFallback";
058 static public final String GBEAN_FACTORY_PORT = "socketFactoryPort";
059 static public final String GBEAN_LOCALHOST = "localhost";
060 static public final String GBEAN_LOCALADDRESS = "localaddress";
061 static public final String GBEAN_LOCALPORT = "localport";
062
063 private final Log log = LogFactory.getLog(ProtocolGBean.class);
064
065 private final String objectName;
066 private Properties properties;
067 private final String protocol;
068 private String host;
069 private String user;
070
071 /**
072 * Construct an instance of ProtocolGBean
073 */
074 public ProtocolGBean() {
075 this.objectName = null;
076 this.protocol = null;
077 this.properties = null;
078 }
079
080 /**
081 * Construct an instance of ProtocolGBean
082 * <p/>
083 * Values that are set in the individual member variables will override any of
084 * the corresponding values that have been set in the properties set.
085 *
086 * @param objectName the object name of the protocol
087 * @param protocol the name of the protocol
088 * @param properties the set of default properties for the protocol
089 * @param host the host the protocol connects to
090 * @param user the default name for the protocol
091 */
092 public ProtocolGBean(String objectName, String protocol, Properties properties, String host, String user) {
093 assert protocol != null;
094
095 this.objectName = objectName;
096 this.protocol = protocol;
097 this.properties = (properties == null ? new Properties() : properties);
098 this.host = host;
099 this.user = user;
100 }
101
102 /**
103 * Returns the object name of this protocol GBean
104 */
105 public String getObjectName() {
106 return objectName;
107 }
108
109 /**
110 * Returns the set of default properties for the protocol.
111 * <p/>
112 * Values that are set in the individual member variables will override any of
113 * the corresponding values that have been set in the properties set.
114 */
115 public Properties getProperties() {
116 return properties;
117 }
118
119 /**
120 * Sets the set of default properties for the protocol.
121 * <p/>
122 * Values that are set in the individual member variables will override any of
123 * the corresponding values that have been set in the properties set.
124 *
125 * @param properties set of default properties for the protocol
126 */
127 public void setProperties(Properties properties) {
128 this.properties = properties;
129 }
130
131 /**
132 * Returns the name of the protocol
133 */
134 public String getProtocol() {
135 return protocol;
136 }
137
138 /**
139 * Returns the host the protocol connects to.
140 */
141 public String getHost() {
142 return host;
143 }
144
145 /**
146 * Set the host the protocol connects to.
147 *
148 * @param host the host the protocol connects to
149 */
150 public void setHost(String host) {
151 this.host = host;
152 }
153
154 /**
155 * Returns the default user name for the protocol.
156 */
157 public String getUser() {
158 return user;
159 }
160
161 /**
162 * Sets the default user name for the protocol.
163 *
164 * @param user the default user name for the protocol
165 */
166 public void setUser(String user) {
167 this.user = user;
168 }
169
170 /**
171 * Add the overrides from the member variables to the properties file.
172 */
173 public void addOverrides(Properties props) {
174 Enumeration keys = properties.propertyNames();
175
176 // copy the properties attribute into the over rides as well. These are copied
177 // with the key names unchanged, so they must be specified fully qualified.
178 while (keys.hasMoreElements()) {
179 String key = (String)keys.nextElement();
180 props.put(key, properties.getProperty(key));
181 }
182
183 if (host != null) props.put("mail." + protocol + ".host", host);
184 if (user != null) props.put("mail." + protocol + ".user", user);
185 }
186
187 public void doStart() throws Exception {
188 log.debug("Started " + objectName);
189 }
190
191 public void doStop() throws Exception {
192 log.debug("Stopped " + objectName);
193 }
194
195 public void doFail() {
196 log.warn("Failed " + objectName);
197 }
198
199 public static final GBeanInfo GBEAN_INFO;
200
201 static {
202 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ProtocolGBean.class); //TODO just a gbean?
203
204 infoFactory.addAttribute(GBEAN_OBJECTNAME, String.class, false);
205 infoFactory.addAttribute(GBEAN_PROTOCOL, String.class, true);
206 infoFactory.addAttribute(GBEAN_PROPERTIES, Properties.class, true);
207 infoFactory.addAttribute(GBEAN_HOST, String.class, true);
208 infoFactory.addAttribute(GBEAN_USER, String.class, true);
209 infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class[]{Properties.class});
210
211 infoFactory.setConstructor(new String[]{GBEAN_OBJECTNAME, GBEAN_PROTOCOL, GBEAN_PROPERTIES, GBEAN_HOST, GBEAN_USER});
212
213 GBEAN_INFO = infoFactory.getBeanInfo();
214 }
215
216 public static GBeanInfo getGBeanInfo() {
217 return GBEAN_INFO;
218 }
219 }