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: */package org.apache.geronimo.mail;
017:
018: import java.util.Enumeration;
019: import java.util.Properties;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: import org.apache.geronimo.gbean.GBeanInfo;
025: import org.apache.geronimo.gbean.GBeanInfoBuilder;
026: import org.apache.geronimo.gbean.GBeanLifecycle;
027:
028: /**
029: * A generic GBean that provides for the configuration of a JavaMail protocol.
030: * <p/>
031: * Values that are set in the individual member variables will override any of
032: * the corresponding values that have been set in the properties set.
033: *
034: * @version $Rev: 486195 $ $Date: 2006-12-12 07:42:02 -0800 (Tue, 12 Dec 2006) $
035: */
036: public class ProtocolGBean implements GBeanLifecycle {
037:
038: // common attributes exported by all ProtocolBeans
039: static public final String GBEAN_OBJECTNAME = "objectName";
040: static public final String GBEAN_PROTOCOL = "protocol";
041: static public final String GBEAN_PROPERTIES = "properties";
042: static public final String GBEAN_HOST = "host";
043: static public final String GBEAN_USER = "user";
044: static public final String GBEAN_ADD_OVERRIDES = "addOverrides";
045:
046: // common constants for GBEAN properties that are used by a number of transports.
047: static public final String GBEAN_PORT = "port";
048: static public final String GBEAN_CONNECTION_TIMEOUT = "connectionTimeout";
049: static public final String GBEAN_TIMEOUT = "timeout";
050: static public final String GBEAN_FROM = "from";
051: static public final String GBEAN_AUTH = "auth";
052: static public final String GBEAN_REALM = "saslRealm";
053: static public final String GBEAN_QUITWAIT = "quitWait";
054: static public final String GBEAN_FACTORY_CLASS = "socketFactoryClass";
055: static public final String GBEAN_FACTORY_FALLBACK = "socketFactoryFallback";
056: static public final String GBEAN_FACTORY_PORT = "socketFactoryPort";
057: static public final String GBEAN_LOCALHOST = "localhost";
058: static public final String GBEAN_LOCALADDRESS = "localaddress";
059: static public final String GBEAN_LOCALPORT = "localport";
060:
061: private final Log log = LogFactory.getLog(ProtocolGBean.class);
062:
063: private final String objectName;
064: private Properties properties;
065: private final String protocol;
066: private String host;
067: private String user;
068:
069: /**
070: * Construct an instance of ProtocolGBean
071: */
072: public ProtocolGBean() {
073: this .objectName = null;
074: this .protocol = null;
075: this .properties = null;
076: }
077:
078: /**
079: * Construct an instance of ProtocolGBean
080: * <p/>
081: * Values that are set in the individual member variables will override any of
082: * the corresponding values that have been set in the properties set.
083: *
084: * @param objectName the object name of the protocol
085: * @param protocol the name of the protocol
086: * @param properties the set of default properties for the protocol
087: * @param host the host the protocol connects to
088: * @param user the default name for the protocol
089: */
090: public ProtocolGBean(String objectName, String protocol,
091: Properties properties, String host, String user) {
092: assert protocol != null;
093:
094: this .objectName = objectName;
095: this .protocol = protocol;
096: this .properties = (properties == null ? new Properties()
097: : 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)
184: props.put("mail." + protocol + ".host", host);
185: if (user != null)
186: props.put("mail." + protocol + ".user", user);
187: }
188:
189: public void doStart() throws Exception {
190: log.debug("Started " + objectName);
191: }
192:
193: public void doStop() throws Exception {
194: log.debug("Stopped " + objectName);
195: }
196:
197: public void doFail() {
198: log.warn("Failed " + objectName);
199: }
200:
201: public static final GBeanInfo GBEAN_INFO;
202:
203: static {
204: GBeanInfoBuilder infoFactory = GBeanInfoBuilder
205: .createStatic(ProtocolGBean.class); //TODO just a gbean?
206:
207: infoFactory.addAttribute(GBEAN_OBJECTNAME, String.class, false);
208: infoFactory.addAttribute(GBEAN_PROTOCOL, String.class, true);
209: infoFactory.addAttribute(GBEAN_PROPERTIES, Properties.class,
210: true);
211: infoFactory.addAttribute(GBEAN_HOST, String.class, true);
212: infoFactory.addAttribute(GBEAN_USER, String.class, true);
213: infoFactory.addOperation(GBEAN_ADD_OVERRIDES,
214: new Class[] { Properties.class });
215:
216: infoFactory.setConstructor(new String[] { GBEAN_OBJECTNAME,
217: GBEAN_PROTOCOL, GBEAN_PROPERTIES, GBEAN_HOST,
218: GBEAN_USER });
219:
220: GBEAN_INFO = infoFactory.getBeanInfo();
221: }
222:
223: public static GBeanInfo getGBeanInfo() {
224: return GBEAN_INFO;
225: }
226: }
|