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 package org.apache.geronimo.mail; 018 019 import java.util.Enumeration; 020 import java.util.Properties; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 import org.apache.geronimo.gbean.GBeanInfo; 026 import org.apache.geronimo.gbean.GBeanInfoBuilder; 027 import org.apache.geronimo.gbean.GBeanLifecycle; 028 029 /** 030 * A generic GBean that provides for the configuration of a JavaMail protocol. 031 * <p/> 032 * Values that are set in the individual member variables will override any of 033 * the corresponding values that have been set in the properties set. 034 * 035 * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $ 036 */ 037 public class ProtocolGBean implements GBeanLifecycle { 038 039 // common attributes exported by all ProtocolBeans 040 static public final String GBEAN_OBJECTNAME = "objectName"; 041 static public final String GBEAN_PROTOCOL = "protocol"; 042 static public final String GBEAN_PROPERTIES = "properties"; 043 static public final String GBEAN_HOST = "host"; 044 static public final String GBEAN_USER = "user"; 045 static public final String GBEAN_ADD_OVERRIDES = "addOverrides"; 046 047 // common constants for GBEAN properties that are used by a number of transports. 048 static public final String GBEAN_PORT = "port"; 049 static public final String GBEAN_CONNECTION_TIMEOUT = "connectionTimeout"; 050 static public final String GBEAN_TIMEOUT = "timeout"; 051 static public final String GBEAN_FROM = "from"; 052 static public final String GBEAN_AUTH = "auth"; 053 static public final String GBEAN_REALM = "saslRealm"; 054 static public final String GBEAN_QUITWAIT = "quitWait"; 055 static public final String GBEAN_FACTORY_CLASS = "socketFactoryClass"; 056 static public final String GBEAN_FACTORY_FALLBACK = "socketFactoryFallback"; 057 static public final String GBEAN_FACTORY_PORT = "socketFactoryPort"; 058 static public final String GBEAN_LOCALHOST = "localhost"; 059 static public final String GBEAN_LOCALADDRESS = "localaddress"; 060 static public final String GBEAN_LOCALPORT = "localport"; 061 062 private final Log log = LogFactory.getLog(ProtocolGBean.class); 063 064 private final String objectName; 065 private Properties properties; 066 private final String protocol; 067 private String host; 068 private String user; 069 070 /** 071 * Construct an instance of ProtocolGBean 072 */ 073 public ProtocolGBean() { 074 this.objectName = null; 075 this.protocol = null; 076 this.properties = null; 077 } 078 079 /** 080 * Construct an instance of ProtocolGBean 081 * <p/> 082 * Values that are set in the individual member variables will override any of 083 * the corresponding values that have been set in the properties set. 084 * 085 * @param objectName the object name of the protocol 086 * @param protocol the name of the protocol 087 * @param properties the set of default properties for the protocol 088 * @param host the host the protocol connects to 089 * @param user the default name for the protocol 090 */ 091 public ProtocolGBean(String objectName, String protocol, 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() : properties); 097 this.host = host; 098 this.user = user; 099 } 100 101 /** 102 * Returns the object name of this protocol GBean 103 */ 104 public String 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 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 properties) { 127 this.properties = properties; 128 } 129 130 /** 131 * Returns the name of the protocol 132 */ 133 public String getProtocol() { 134 return protocol; 135 } 136 137 /** 138 * Returns the host the protocol connects to. 139 */ 140 public String 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 host) { 150 this.host = host; 151 } 152 153 /** 154 * Returns the default user name for the protocol. 155 */ 156 public String 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 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 props) { 173 Enumeration 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 key = (String)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 { 187 log.debug("Started " + objectName); 188 } 189 190 public void doStop() throws Exception { 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.class, false); 204 infoFactory.addAttribute(GBEAN_PROTOCOL, String.class, true); 205 infoFactory.addAttribute(GBEAN_PROPERTIES, Properties.class, true); 206 infoFactory.addAttribute(GBEAN_HOST, String.class, true); 207 infoFactory.addAttribute(GBEAN_USER, String.class, true); 208 infoFactory.addOperation(GBEAN_ADD_OVERRIDES, new Class[]{Properties.class}); 209 210 infoFactory.setConstructor(new String[]{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 }