KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mail > ProtocolGBean


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 package org.apache.geronimo.mail;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.apache.geronimo.gbean.GBeanInfo;
26 import org.apache.geronimo.gbean.GBeanInfoBuilder;
27 import org.apache.geronimo.gbean.GBeanLifecycle;
28
29 /**
30  * A generic GBean that provides for the configuration of a JavaMail protocol.
31  * <p/>
32  * Values that are set in the individual member variables will override any of
33  * the corresponding values that have been set in the properties set.
34  *
35  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
36  */

37 public class ProtocolGBean implements GBeanLifecycle {
38
39     // common attributes exported by all ProtocolBeans
40
static public final String JavaDoc GBEAN_OBJECTNAME = "objectName";
41     static public final String JavaDoc GBEAN_PROTOCOL = "protocol";
42     static public final String JavaDoc GBEAN_PROPERTIES = "properties";
43     static public final String JavaDoc GBEAN_HOST = "host";
44     static public final String JavaDoc GBEAN_USER = "user";
45     static public final String JavaDoc GBEAN_ADD_OVERRIDES = "addOverrides";
46
47     // common constants for GBEAN properties that are used by a number of transports.
48
static public final String JavaDoc GBEAN_PORT = "port";
49     static public final String JavaDoc GBEAN_CONNECTION_TIMEOUT = "connectionTimeout";
50     static public final String JavaDoc GBEAN_TIMEOUT = "timeout";
51     static public final String JavaDoc GBEAN_FROM = "from";
52     static public final String JavaDoc GBEAN_AUTH = "auth";
53     static public final String JavaDoc GBEAN_REALM = "saslRealm";
54     static public final String JavaDoc GBEAN_QUITWAIT = "quitWait";
55     static public final String JavaDoc GBEAN_FACTORY_CLASS = "socketFactoryClass";
56     static public final String JavaDoc GBEAN_FACTORY_FALLBACK = "socketFactoryFallback";
57     static public final String JavaDoc GBEAN_FACTORY_PORT = "socketFactoryPort";
58     static public final String JavaDoc GBEAN_LOCALHOST = "localhost";
59     static public final String JavaDoc GBEAN_LOCALADDRESS = "localaddress";
60     static public final String JavaDoc GBEAN_LOCALPORT = "localport";
61
62     private final Log log = LogFactory.getLog(ProtocolGBean.class);
63
64     private final String JavaDoc objectName;
65     private Properties JavaDoc properties;
66     private final String JavaDoc protocol;
67     private String JavaDoc host;
68     private String JavaDoc user;
69
70     /**
71      * Construct an instance of ProtocolGBean
72      */

73     public ProtocolGBean() {
74         this.objectName = null;
75         this.protocol = null;
76         this.properties = null;
77     }
78
79     /**
80      * Construct an instance of ProtocolGBean
81      * <p/>
82      * Values that are set in the individual member variables will override any of
83      * the corresponding values that have been set in the properties set.
84      *
85      * @param objectName the object name of the protocol
86      * @param protocol the name of the protocol
87      * @param properties the set of default properties for the protocol
88      * @param host the host the protocol connects to
89      * @param user the default name for the protocol
90      */

91     public ProtocolGBean(String JavaDoc objectName, String JavaDoc protocol, Properties JavaDoc properties, String JavaDoc host, String JavaDoc user) {
92         assert protocol != null;
93
94         this.objectName = objectName;
95         this.protocol = protocol;
96         this.properties = (properties == null ? new Properties JavaDoc() : properties);
97         this.host = host;
98         this.user = user;
99     }
100
101     /**
102      * Returns the object name of this protocol GBean
103      */

104     public String JavaDoc getObjectName() {
105         return objectName;
106     }
107
108     /**
109      * Returns the set of default properties for the protocol.
110      * <p/>
111      * Values that are set in the individual member variables will override any of
112      * the corresponding values that have been set in the properties set.
113      */

114     public Properties JavaDoc getProperties() {
115         return properties;
116     }
117
118     /**
119      * Sets the set of default properties for the protocol.
120      * <p/>
121      * Values that are set in the individual member variables will override any of
122      * the corresponding values that have been set in the properties set.
123      *
124      * @param properties set of default properties for the protocol
125      */

126     public void setProperties(Properties JavaDoc properties) {
127         this.properties = properties;
128     }
129
130     /**
131      * Returns the name of the protocol
132      */

133     public String JavaDoc getProtocol() {
134         return protocol;
135     }
136
137     /**
138      * Returns the host the protocol connects to.
139      */

140     public String JavaDoc getHost() {
141         return host;
142     }
143
144     /**
145      * Set the host the protocol connects to.
146      *
147      * @param host the host the protocol connects to
148      */

149     public void setHost(String JavaDoc host) {
150         this.host = host;
151     }
152
153     /**
154      * Returns the default user name for the protocol.
155      */

156     public String JavaDoc getUser() {
157         return user;
158     }
159
160     /**
161      * Sets the default user name for the protocol.
162      *
163      * @param user the default user name for the protocol
164      */

165     public void setUser(String JavaDoc user) {
166         this.user = user;
167     }
168
169     /**
170      * Add the overrides from the member variables to the properties file.
171      */

172     public void addOverrides(Properties JavaDoc props) {
173         Enumeration JavaDoc keys = properties.propertyNames();
174
175         // copy the properties attribute into the over rides as well. These are copied
176
// with the key names unchanged, so they must be specified fully qualified.
177
while (keys.hasMoreElements()) {
178             String JavaDoc key = (String JavaDoc)keys.nextElement();
179             props.put(key, properties.getProperty(key));
180         }
181
182         if (host != null) props.put("mail." + protocol + ".host", host);
183         if (user != null) props.put("mail." + protocol + ".user", user);
184     }
185
186     public void doStart() throws Exception JavaDoc {
187         log.debug("Started " + objectName);
188     }
189
190     public void doStop() throws Exception JavaDoc {
191         log.debug("Stopped " + objectName);
192     }
193
194     public void doFail() {
195         log.warn("Failed " + objectName);
196     }
197
198     public static final GBeanInfo GBEAN_INFO;
199
200     static {
201         GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(ProtocolGBean.class); //TODO just a gbean?
202

203         infoFactory.addAttribute(GBEAN_OBJECTNAME, String JavaDoc.class, false);
204         infoFactory.addAttribute(GBEAN_PROTOCOL, String JavaDoc.class, true);
205         infoFactory.addAttribute(GBEAN_PROPERTIES, Properties JavaDoc.class, true);
206         infoFactory.addAttribute(GBEAN_HOST, String JavaDoc.class, true);
207         infoFactory.addAttribute(GBEAN_USER, String JavaDoc.class, true);
208         infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class JavaDoc[]{Properties JavaDoc.class});
209
210         infoFactory.setConstructor(new String JavaDoc[]{GBEAN_OBJECTNAME, GBEAN_PROTOCOL, GBEAN_PROPERTIES, GBEAN_HOST, GBEAN_USER});
211
212         GBEAN_INFO = infoFactory.getBeanInfo();
213     }
214
215     public static GBeanInfo getGBeanInfo() {
216         return GBEAN_INFO;
217     }
218 }
219
Popular Tags