1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.geronimo.mail;
19
20 import java.util.Enumeration;
21 import java.util.Properties;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.apache.geronimo.gbean.GBeanInfo;
27 import org.apache.geronimo.gbean.GBeanInfoBuilder;
28 import org.apache.geronimo.gbean.GBeanLifecycle;
29
30 /**
31 * A generic GBean that provides for the configuration of a JavaMail protocol.
32 * <p/>
33 * Values that are set in the individual member variables will override any of
34 * the corresponding values that have been set in the properties set.
35 *
36 * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
37 */
38 public class ProtocolGBean implements GBeanLifecycle {
39
40
41 static public final String GBEAN_OBJECTNAME = "objectName";
42 static public final String GBEAN_PROTOCOL = "protocol";
43 static public final String GBEAN_PROPERTIES = "properties";
44 static public final String GBEAN_HOST = "host";
45 static public final String GBEAN_USER = "user";
46 static public final String GBEAN_ADD_OVERRIDES = "addOverrides";
47
48
49 static public final String GBEAN_PORT = "port";
50 static public final String GBEAN_CONNECTION_TIMEOUT = "connectionTimeout";
51 static public final String GBEAN_TIMEOUT = "timeout";
52 static public final String GBEAN_FROM = "from";
53 static public final String GBEAN_AUTH = "auth";
54 static public final String GBEAN_REALM = "saslRealm";
55 static public final String GBEAN_QUITWAIT = "quitWait";
56 static public final String GBEAN_FACTORY_CLASS = "socketFactoryClass";
57 static public final String GBEAN_FACTORY_FALLBACK = "socketFactoryFallback";
58 static public final String GBEAN_FACTORY_PORT = "socketFactoryPort";
59 static public final String GBEAN_LOCALHOST = "localhost";
60 static public final String GBEAN_LOCALADDRESS = "localaddress";
61 static public final String GBEAN_LOCALPORT = "localport";
62
63 private final Log log = LogFactory.getLog(ProtocolGBean.class);
64
65 private final String objectName;
66 private Properties properties;
67 private final String protocol;
68 private String host;
69 private String user;
70
71 /**
72 * Construct an instance of ProtocolGBean
73 */
74 public ProtocolGBean() {
75 this.objectName = null;
76 this.protocol = null;
77 this.properties = null;
78 }
79
80 /**
81 * Construct an instance of ProtocolGBean
82 * <p/>
83 * Values that are set in the individual member variables will override any of
84 * the corresponding values that have been set in the properties set.
85 *
86 * @param objectName the object name of the protocol
87 * @param protocol the name of the protocol
88 * @param properties the set of default properties for the protocol
89 * @param host the host the protocol connects to
90 * @param user the default name for the protocol
91 */
92 public ProtocolGBean(String objectName, String protocol, Properties properties, String host, String user) {
93 assert protocol != null;
94
95 this.objectName = objectName;
96 this.protocol = protocol;
97 this.properties = (properties == null ? new Properties() : properties);
98 this.host = host;
99 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
177
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);
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 }