KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > Controler


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
42
43 /**
44  *
45  * Initialises config & plugins<br>
46  * Starts and shutdown listening service and delivery service<br>
47  * TODO: Check paths, rebuild them, ensure they are writable<br>
48  * @author Jean-Francois POUX
49  */

50 public class Controler extends Thread JavaDoc {
51     /**
52      * Log4j instance
53      */

54     private Log log = LogFactory.getLog(Controler.class);
55     /**
56      * smtp receiver services
57      */

58     private Receiver rec = null;
59     /**
60      * mail delivery services
61      */

62     private DeliveryPicker delivery = null;
63
64     private String JavaDoc configFile = "etc/jsmtpd.ini";
65     
66     private String JavaDoc xmlPluginFileName = "jsmtpd-plugin-config.xml";
67     
68     public synchronized void sleep() {
69         try {
70             this.wait();
71         } catch (InterruptedException JavaDoc 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     /**
97      * Initialises the delivery services
98      *
99      */

100     public boolean initDeliveryService() {
101         try {
102             delivery = new DeliveryPicker();
103         } catch (InstantiationException JavaDoc e) {
104             return false;
105         } catch (IllegalAccessException JavaDoc e) {
106             return false;
107         } catch (ClassNotFoundException JavaDoc e) {
108             return false;
109         }
110         log.info("Delivery service started");
111         return true;
112     }
113
114     /**
115      * Read config
116      * Set up Log4J
117      * Load the plugins
118      */

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         // Init plugin Loader
133

134         PluginLoader ldr = new PluginLoader();
135         ldr.setXmlFileName(xmlPluginFileName);
136         try {
137             ldr.init();
138         } catch (SAXException JavaDoc e2) {
139             log.fatal("Parse error in plugin-config.xml, " + e2.getMessage());
140             return false;
141         } catch (IOException JavaDoc e2) {
142             log.fatal("IO Error while loading, " + e2.getMessage());
143             return false;
144         } catch (ParserConfigurationException JavaDoc 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         //Load the DNSService plugin
153
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         // Load the ACL plugin
162
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         // Load the LocalDelivery plugin
171
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         // Load the RemoteDelivery plugin
180
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         // Load the RemoteDelivery plugin
189
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 JavaDoc<IGenericPlugin> tempList = PluginStore.getInstance().getFilterList();
204         for (Iterator JavaDoc 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     /**
229      * Starts the receiver services
230      */

231     public boolean initReceiver() {
232         try {
233             rec = new Receiver(ReadConfig.getInstance().getRPort(), ReadConfig.getInstance().getRMaxInstances());
234         } catch (IOException JavaDoc e1) {
235             log.info("Network Listener is down");
236             e1.printStackTrace();
237             return false;
238         } catch (InstantiationException JavaDoc e) {
239             e.printStackTrace();
240             return false;
241         } catch (IllegalAccessException JavaDoc e) {
242             e.printStackTrace();
243             return false;
244         } catch (ClassNotFoundException JavaDoc e) {
245             e.printStackTrace();
246             return false;
247         }
248         return true;
249     }
250
251     /**
252      * Shutdown JSMTPD services
253      *
254      */

255     private void shutdownServices() {
256         if (rec != null) {
257             rec.shutdown();
258         }
259
260         if (delivery != null) {
261             delivery.shutdown();
262         }
263
264         List JavaDoc<IGenericPlugin> plugins = PluginStore.getInstance().getFilterList();
265         for (Iterator JavaDoc 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     /**
278      * Shutdown hook invoked on VM shutdown
279      */

280     public void run() {
281         shutdownServices();
282     }
283
284     public String JavaDoc getXmlPluginFileName() {
285         return xmlPluginFileName;
286     }
287     public void setXmlPluginFileName(String JavaDoc xmlPluginFileName) {
288         this.xmlPluginFileName = xmlPluginFileName;
289     }
290     public String JavaDoc getConfigFile() {
291         return configFile;
292     }
293     public void setConfigFile(String JavaDoc configFile) {
294         this.configFile = configFile;
295     }
296 }
Popular Tags