1 21 package org.jsmtpd; 22 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.jsmtpd.config.ConfigErrorException; 32 import org.jsmtpd.config.ElementNotFoundException; 33 import org.jsmtpd.config.ModuleNotFoundException; 34 import org.jsmtpd.config.PluginLoader; 35 import org.jsmtpd.config.PluginLoaderException; 36 import org.jsmtpd.config.ReadConfig; 37 import org.jsmtpd.core.common.IGenericPlugin; 38 import org.jsmtpd.core.common.PluginStore; 39 import org.jsmtpd.core.receive.Receiver; 40 import org.jsmtpd.core.send.DeliveryPicker; 41 import org.xml.sax.SAXException ; 42 43 50 public class Controler extends Thread { 51 54 private Log log = LogFactory.getLog(Controler.class); 55 58 private Receiver rec = null; 59 62 private DeliveryPicker delivery = null; 63 64 private String configFile = "etc/jsmtpd.ini"; 65 66 private String xmlPluginFileName = "jsmtpd-plugin-config.xml"; 67 68 public synchronized void sleep() { 69 try { 70 this.wait(); 71 } catch (InterruptedException e) { 72 e.printStackTrace(); 73 } 74 } 75 public void startup() 76 { 77 Runtime.getRuntime().addShutdownHook(this); 78 if (!initEnvironement()) { 79 System.exit(-1); 80 return; 81 } 82 83 if (!initReceiver()) { 84 System.exit(-1); 85 return; 86 } 87 if (!initDeliveryService()) { 88 System.exit(-1); 89 return; 90 } 91 log.info("Server running"); 92 93 sleep(); 94 } 95 96 100 public boolean initDeliveryService() { 101 try { 102 delivery = new DeliveryPicker(); 103 } catch (InstantiationException e) { 104 return false; 105 } catch (IllegalAccessException e) { 106 return false; 107 } catch (ClassNotFoundException e) { 108 return false; 109 } 110 log.info("Delivery service started"); 111 return true; 112 } 113 114 119 public boolean initEnvironement() { 120 try { 121 ReadConfig.getInstance().loadConfig(configFile); 122 } catch (ConfigErrorException e) { 123 log.fatal("Could no load config file ",e); 124 return false; 125 } 126 log.info("Jsmtpd " + ReadConfig.getInstance().getVersion() + ", Copyright (C) 2005 Jean-Francois Poux - jfp@jsmtpd.org"); 127 log.info("Jsmtpd comes with ABSOLUTELY NO WARRANTY;"); 128 log.info("This is free software, and you are welcome to redistribute it under certain conditions;"); 129 log.info("See the LICENCE file for details"); 130 log.info("Server starting ..."); 131 132 134 PluginLoader ldr = new PluginLoader(); 135 ldr.setXmlFileName(xmlPluginFileName); 136 try { 137 ldr.init(); 138 } catch (SAXException e2) { 139 log.fatal("Parse error in plugin-config.xml, " + e2.getMessage()); 140 return false; 141 } catch (IOException e2) { 142 log.fatal("IO Error while loading, " + e2.getMessage()); 143 return false; 144 } catch (ParserConfigurationException e2) { 145 log.fatal("Parser configuration error while loading plugin-config.xml, " + e2.getMessage()); 146 return false; 147 } catch (ElementNotFoundException e2) { 148 log.fatal("In plugin-config.xml, can't find " + e2.getElementName()); 149 return false; 150 } 151 152 try { 154 ldr.loadDNSService(); 155 } catch (PluginLoaderException e) { 156 log.fatal("DNSService could not be loaded, " + e.getMessage(),e); 157 return false; 158 } 159 log.info("DNS Service plugin loaded : " + PluginStore.getInstance().getResolver().getPluginName()); 160 161 try { 163 ldr.loadACL(); 164 } catch (PluginLoaderException e) { 165 log.fatal("ACL plugin could not be loaded, " + e.getMessage(),e); 166 return false; 167 } 168 log.info("ACL Service plugin loaded : " + PluginStore.getInstance().getAcl().getPluginName()); 169 170 try { 172 ldr.loadLocalDeliveryService(); 173 } catch (PluginLoaderException e) { 174 log.fatal("LocalDeliveryService plugin could not be loaded, " + e.getMessage(),e); 175 return false; 176 } 177 log.info("Local Delivery Service plugin loaded : " + PluginStore.getInstance().getLocalDeliveryService().getPluginName()); 178 179 try { 181 ldr.loadRemoteDeliveryService(); 182 } catch (PluginLoaderException e) { 183 log.fatal("RemoteDeliveryService plugin could not be loaded, " + e.getMessage(),e); 184 return false; 185 } 186 log.info("Remote Delivery Service plugin loaded : " + PluginStore.getInstance().getRemoteDeliveryService().getPluginName()); 187 188 try { 190 ldr.loadFilters(); 191 } catch (PluginLoaderException e) { 192 log.fatal("filter plugin could not be loaded, " + e.getMessage(),e); 193 return false; 194 } 195 196 try { 197 ldr.loadSmtpExtensions(); 198 } catch (PluginLoaderException e) { 199 log.fatal("smtp extension plugin could not be loaded, " + e.getMessage(),e); 200 return false; 201 } 202 203 List <IGenericPlugin> tempList = PluginStore.getInstance().getFilterList(); 204 for (Iterator iter = tempList.iterator(); iter.hasNext();) { 205 IGenericPlugin element = (IGenericPlugin) iter.next(); 206 log.info("Plugin loaded : " + element.getPluginName()); 207 } 208 209 try { 210 ldr.loadInputIPFilterChain(); 211 } catch (ModuleNotFoundException e4) { 212 log.fatal( "Error while loading input IP filter chain , " + e4.getModName() 213 + " is declared in the inputIPFilter, but not configured in filtersetup"); 214 215 } 216 log.info("Input IP filter chain loaded"); 217 218 try { 219 ldr.loadFilterTree(); 220 } catch (ModuleNotFoundException e1) { 221 log.fatal("Error while loading filter tree, " + e1.getModName() 222 + " is declared in the filterchain tree, but not configured in filtersetup"); 223 return false; 224 } 225 return true; 226 } 227 228 231 public boolean initReceiver() { 232 try { 233 rec = new Receiver(ReadConfig.getInstance().getRPort(), ReadConfig.getInstance().getRMaxInstances()); 234 } catch (IOException e1) { 235 log.info("Network Listener is down"); 236 e1.printStackTrace(); 237 return false; 238 } catch (InstantiationException e) { 239 e.printStackTrace(); 240 return false; 241 } catch (IllegalAccessException e) { 242 e.printStackTrace(); 243 return false; 244 } catch (ClassNotFoundException e) { 245 e.printStackTrace(); 246 return false; 247 } 248 return true; 249 } 250 251 255 private void shutdownServices() { 256 if (rec != null) { 257 rec.shutdown(); 258 } 259 260 if (delivery != null) { 261 delivery.shutdown(); 262 } 263 264 List <IGenericPlugin> plugins = PluginStore.getInstance().getFilterList(); 265 for (Iterator iter = plugins.iterator(); iter.hasNext();) { 266 IGenericPlugin module = (IGenericPlugin) iter.next(); 267 log.info( "Shutting down plugin : " + module.getPluginName()); 268 module.shutdownPlugin(); 269 } 270 IGenericPlugin module = PluginStore.getInstance().getResolver(); 271 log.debug("Shutting down plugin : " + module.getPluginName()); 272 module.shutdownPlugin(); 273 274 log.info("Jsmtpd shutdown"); 275 } 276 277 280 public void run() { 281 shutdownServices(); 282 } 283 284 public String getXmlPluginFileName() { 285 return xmlPluginFileName; 286 } 287 public void setXmlPluginFileName(String xmlPluginFileName) { 288 this.xmlPluginFileName = xmlPluginFileName; 289 } 290 public String getConfigFile() { 291 return configFile; 292 } 293 public void setConfigFile(String configFile) { 294 this.configFile = configFile; 295 } 296 } | Popular Tags |