KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > mail > struts > MailSetupForm


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * ---------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * ---------------------------------------------------------
30  * $Log: MailSetupForm.java,v $
31  * Revision 1.2.2.1 2005/10/08 17:35:43 colinmacleod
32  * Extended for hMailServer v4.x
33  * Now uses JDBC to set settings (rather than attempting to re-initializse).
34  *
35  * Revision 1.5 2005/10/03 10:21:16 colinmacleod
36  * Fixed some style and javadoc issues.
37  *
38  * Revision 1.4 2005/10/02 14:08:59 colinmacleod
39  * Added/improved log4j logging.
40  *
41  * Revision 1.3 2005/09/14 16:15:35 colinmacleod
42  * Added constructor.
43  * Added mailDomains.
44  *
45  * Revision 1.2 2005/04/27 15:20:40 colinmacleod
46  * Fix for (no) scripts path on Windows.
47  *
48  * Revision 1.1 2005/04/11 10:03:43 colinmacleod
49  * Added setup feature.
50  *
51  * ---------------------------------------------------------
52  */

53 package com.ivata.groupware.business.mail.struts;
54
55 import java.io.File JavaDoc;
56 import java.util.Arrays JavaDoc;
57 import java.util.List JavaDoc;
58 import java.util.Vector JavaDoc;
59
60 import javax.servlet.http.HttpServletRequest JavaDoc;
61 import javax.servlet.http.HttpSession JavaDoc;
62
63 import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
64 import jp.ne.so_net.ga2.no_ji.jcom.JComException;
65
66 import org.apache.log4j.Logger;
67 import org.apache.struts.action.ActionMapping;
68 import org.picocontainer.PicoContainer;
69
70 import com.ivata.groupware.admin.security.server.SecuritySession;
71 import com.ivata.groupware.admin.setting.Settings;
72 import com.ivata.groupware.admin.struts.HibernateSetupForm;
73 import com.ivata.groupware.business.mail.server.HMailServer;
74 import com.ivata.groupware.container.PicoContainerFactory;
75 import com.ivata.mask.util.StringHandling;
76 import com.ivata.mask.util.SystemException;
77 import com.ivata.mask.validation.ValidationError;
78 import com.ivata.mask.validation.ValidationErrors;
79
80 /**
81  * Contains all of the values you need to setup a basic <strong>ivata
82  * groupware</strong> installation.
83  *
84  * @since ivata groupware 0.11 (2005-03-27)
85  * @author Colin MacLeod
86  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
87  * @version $Revision: 1.2.2.1 $
88  */

89 public class MailSetupForm extends HibernateSetupForm {
90     /**
91      * Serialization version (for <code>Serializable</code> interface).
92      */

93     private static final long serialVersionUID = 1L;
94     /**
95      * Logger for this class.
96      */

97     private static final Logger logger = Logger.getLogger(MailSetupForm.class);
98     /**
99      * <copyDoc>Refer to {@link #getMailDomain}.</copyDoc>
100      */

101     private String JavaDoc mailDomain;
102     /**
103      * <copyDoc>Refer to {@link #getMailDomains}.</copyDoc>
104      */

105     private List JavaDoc mailDomains = null;
106     /**
107      * <copyDoc>Refer to {@link #getMailHostIMAP}.</copyDoc>
108      */

109     private String JavaDoc mailHostIMAP;
110     /**
111      * <copyDoc>Refer to {@link #getMailHostSMTP}.</copyDoc>
112      */

113     private String JavaDoc mailHostSMTP;
114     /**
115      * <copyDoc>Refer to {@link #getMailServerScriptsPath}.</copyDoc>
116      */

117     private String JavaDoc scriptsPath;
118     /**
119      * <copyDoc>Refer to {@link #isWindows}.</copyDoc>
120      */

121     private boolean windows = false;
122     /**
123      * Prepare the form.
124      *
125      * @see com.ivata.mask.web.struts.DialogForm#clear()
126      */

127     protected void clear() {
128         if (logger.isDebugEnabled()) {
129             logger.debug("clear() - start");
130         }
131
132         if (logger.isDebugEnabled()) {
133             logger.debug("clear() - end");
134         }
135     }
136     /**
137      * <p>
138      * Get the domain name of the email address for users of this system.
139      * The domain name is that part which comes afer the 'at symbol' (@) in
140      * their email addresses.
141      * </p>
142      * <p>
143      * On Windows, this must match the domain name set up in
144      * <strong>hMailServer</strong>.
145      * </p>
146      * @return Returns the mail domain.
147      */

148     public String JavaDoc getMailDomain() {
149         if (logger.isDebugEnabled()) {
150             logger.debug("getMailDomain() - start");
151         }
152
153         if (logger.isDebugEnabled()) {
154             logger
155                     .debug("getMailDomain() - end - return value = "
156                             + mailDomain);
157         }
158         return mailDomain;
159     }
160     /**
161      * On <strong>Windows</strong>, this returns a list of all email domains
162      * as strings. On other platforms, it just returns <code>null</code>.
163      *
164      * @return Returns the mail domains for <strong>hMailServer</strong> on
165      * <strong>Windows</strong>
166      */

167     public List JavaDoc getMailDomains() {
168         if (logger.isDebugEnabled()) {
169             logger.debug("getMailDomains() - start");
170         }
171
172         if (logger.isDebugEnabled()) {
173             logger.debug("getMailDomains() - end - return value = "
174                     + mailDomains);
175         }
176         return mailDomains;
177     }
178     /**
179      * Name or IP address of the host used to receive <strong>IMAP</strong>
180      * messages.
181      *
182      * @return Returns the mail host used for IMAP.
183      */

184     public String JavaDoc getMailHostIMAP() {
185         if (logger.isDebugEnabled()) {
186             logger.debug("getMailHostIMAP() - start");
187         }
188
189         if (logger.isDebugEnabled()) {
190             logger.debug("getMailHostIMAP() - end - return value = "
191                     + mailHostIMAP);
192         }
193         return mailHostIMAP;
194     }
195     /**
196      * Name or IP address of the host used to send mail messages.
197      *
198      * @return Returns the mail host used for SMTP.
199      */

200     public String JavaDoc getMailHostSMTP() {
201         if (logger.isDebugEnabled()) {
202             logger.debug("getMailHostSMTP() - start");
203         }
204
205         if (logger.isDebugEnabled()) {
206             logger.debug("getMailHostSMTP() - end - return value = "
207                     + mailHostSMTP);
208         }
209         return mailHostSMTP;
210     }
211     /**
212      * On <strong>UNIX-style</strong> systems, this is the path to scripts which
213      * are used to add/remove users from the mail system. (The implementation
214      * for <strong>Windows</strong> uses <strong>COM</strong> to communicate
215      * with <strong>hMailServer</strong> directly.
216      *
217      * @return Returns the mail server scripts path.
218      */

219     public String JavaDoc getScriptsPath() {
220         if (logger.isDebugEnabled()) {
221             logger.debug("getScriptsPath() - start");
222         }
223
224         if (logger.isDebugEnabled()) {
225             logger.debug("getScriptsPath() - end - return value = "
226                     + scriptsPath);
227         }
228         return scriptsPath;
229     }
230     /**
231      * Returns <code>true</code> if this is a <strong>Microsoft Windows</strong>
232      * installation.
233      * @return Returns <code>true</code> if this is a <strong>Microsoft
234      * Windows</strong> installation.
235      */

236     public boolean isWindows() {
237         if (logger.isDebugEnabled()) {
238             logger.debug("isWindows() - start");
239         }
240
241         if (logger.isDebugEnabled()) {
242             logger.debug("isWindows() - end - return value = " + windows);
243         }
244         return windows;
245     }
246
247     /**
248      * {@inheritDoc}
249      *
250      * @param mappingParam {@inheritDoc}
251      * @param requestParam {@inheritDoc}
252      */

253     public void reset(
254             final ActionMapping mappingParam,
255             final HttpServletRequest JavaDoc requestParam) {
256         if (logger.isDebugEnabled()) {
257             logger.debug("reset(ActionMapping mappingParam = " + mappingParam
258                     + ", HttpServletRequest requestParam = " + requestParam
259                     + ") - start");
260         }
261
262         super.reset(mappingParam, requestParam);
263         SecuritySession securitySession = (SecuritySession)
264             requestParam.getSession().getAttribute("securitySession");
265         // don't use constructor injection as the factory may reset
266
PicoContainerFactory factory;
267         try {
268             factory = PicoContainerFactory.getInstance();
269         } catch (SystemException e) {
270             logger.error("reset(ActionMapping, HttpServletRequest)", e);
271
272             throw new RuntimeException JavaDoc(e);
273         }
274         assert (factory != null);
275         PicoContainer container = factory.getGlobalContainer();
276         assert (container != null);
277         Settings settings = (Settings)
278             container.getComponentInstance(Settings.class);
279         assert (settings != null);
280         windows = System.getProperty("os.name")
281             .toLowerCase().startsWith("windows");
282
283         try {
284             mailDomain =
285                     settings.getStringSetting(securitySession,
286                             "emailAddressHost",
287                             null);
288             // on windows, get all the domains from hMailServer
289
if (windows) {
290                 mailDomains = new Vector JavaDoc();
291                 try {
292                     IDispatch domains = HMailServer.getDomains();
293                     int count;
294                     if (domains == null) {
295                         logger.error("Null domains object returned from"
296                                 + " HMailServer. Could not connect to "
297                                 + "hMailServer via COM.");
298                         count = 0;
299                     } else {
300                         count = ((Integer JavaDoc) domains.get("Count")).intValue();
301                     }
302                     for (int i = 0; i < count; ++i) {
303                         IDispatch domain = (IDispatch) domains.get("Item",
304                                 new Object JavaDoc[] {
305                                     new Integer JavaDoc(i)
306                         });
307                         mailDomains.add(domain.get("Name"));
308                     }
309                 } catch (JComException e) {
310                     logger.error(e.getClass().getName()
311                             + ": getting Windows domains.",
312                             e);
313                     throw new RuntimeException JavaDoc(e);
314                 }
315             } else if (StringHandling.isNullOrEmpty(mailDomain)) {
316                 // only default the mail domain in UNIX; on windows it will
317
// default to the first domain found in hMailServer
318
mailDomain = MailSetupConstants.DEFAULT_MAIL_DOMAIN;
319                 mailDomains = null;
320             }
321         } catch (SystemException e) {
322             logger.error("reset(ActionMapping, HttpServletRequest)", e);
323
324             throw new RuntimeException JavaDoc(e);
325         }
326         try {
327             mailHostIMAP =
328                     settings.getStringSetting(securitySession, "emailHost",
329                             null);
330         } catch (SystemException e) {
331             logger.error("reset(ActionMapping, HttpServletRequest)", e);
332
333             throw new RuntimeException JavaDoc(e);
334         }
335         try {
336             mailHostSMTP =
337                     settings.getStringSetting(securitySession, "emailHostSmtp",
338                             null);
339         } catch (SystemException e) {
340             logger.error("reset(ActionMapping, HttpServletRequest)", e);
341
342             throw new RuntimeException JavaDoc(e);
343         }
344         // script mail server is not for windows - only UNIX style machines
345
try {
346             String JavaDoc pathScriptMailServer =
347                     settings.getStringSetting(securitySession,
348                             "pathScriptMailServer",
349                             null);
350             int pos;
351             // normall the script mail server path contains the sudo subpath
352
// if not, or the setting is empty, default
353
if (StringHandling.isNullOrEmpty(pathScriptMailServer)
354                     || ((pos = pathScriptMailServer
355                             .indexOf(MailSetupConstants
356                                     .SCRIPT_PATH_SUDO)) == -1)) {
357                 scriptsPath = MailSetupConstants.DEFAULT_SCRIPTS_PATH;
358             } else {
359                 scriptsPath = pathScriptMailServer.substring(0, pos);
360             }
361         } catch (SystemException e) {
362             logger.error("reset(ActionMapping, HttpServletRequest)", e);
363
364             throw new RuntimeException JavaDoc(e);
365         }
366
367         if (logger.isDebugEnabled()) {
368             logger.debug("reset(ActionMapping, HttpServletRequest) - end");
369         }
370     }
371
372     /**
373      * <copyDoc>Refer to {@link #getMailDomain}.</copyDoc>
374      * @param mailDomainParam
375      * <copyDoc>Refer to {@link #getMailDomain}.</copyDoc>
376      */

377     public void setMailDomain(final String JavaDoc mailDomainParam) {
378         if (logger.isDebugEnabled()) {
379             logger.debug("Setting mailDomain. Before '" + mailDomain
380                     + "', after '" + mailDomainParam + "'");
381         }
382         mailDomain = mailDomainParam;
383
384         if (logger.isDebugEnabled()) {
385             logger.debug("setMailDomain(String) - end");
386         }
387     }
388     /**
389      * <copyDoc>Refer to {@link #getMailDomains}.</copyDoc>
390      * @param mailDomainsParam
391      * <copyDoc>Refer to {@link #getMailDomains}.</copyDoc>
392      */

393     public void setMailDomains(final List JavaDoc mailDomainsParam) {
394         if (logger.isDebugEnabled()) {
395             logger.debug("Setting mailDomains. Before '" + mailDomains
396                     + "', after '" + mailDomainsParam + "'");
397         }
398         mailDomains = mailDomainsParam;
399
400         if (logger.isDebugEnabled()) {
401             logger.debug("setMailDomains(List) - end");
402         }
403     }
404     /**
405      * <copyDoc>Refer to {@link #getMailHostIMAP}.</copyDoc>
406      * @param mailHostIMAPParam
407      * <copyDoc>Refer to {@link #getMailHostIMAP}.</copyDoc>
408      */

409     public void setMailHostIMAP(final String JavaDoc mailHostIMAPParam) {
410         if (logger.isDebugEnabled()) {
411             logger.debug("Setting mailHostIMAP. Before '" + mailHostIMAP
412                     + "', after '" + mailHostIMAPParam + "'");
413         }
414         mailHostIMAP = mailHostIMAPParam;
415
416         if (logger.isDebugEnabled()) {
417             logger.debug("setMailHostIMAP(String) - end");
418         }
419     }
420     /**
421      * <copyDoc>Refer to {@link #getMailHostSMTP}.</copyDoc>
422      * @param mailHostSMTPParam
423      * <copyDoc>Refer to {@link #getMailHostSMTP}.</copyDoc>
424      */

425     public void setMailHostSMTP(final String JavaDoc mailHostSMTPParam) {
426         if (logger.isDebugEnabled()) {
427             logger.debug("Setting mailHostSMTP. Before '" + mailHostSMTP
428                     + "', after '" + mailHostSMTPParam + "'");
429         }
430         mailHostSMTP = mailHostSMTPParam;
431
432         if (logger.isDebugEnabled()) {
433             logger.debug("setMailHostSMTP(String) - end");
434         }
435     }
436
437     /**
438      * <copyDoc>Refer to {@link #getMailServerScriptsPath}.</copyDoc>
439      * @param mailServerScriptsPathParam <copyDoc>Refer to
440      * {@link #getMailServerScriptsPath}.</copyDoc>
441      */

442     public void setScriptsPath(final String JavaDoc mailServerScriptsPathParam) {
443         if (logger.isDebugEnabled()) {
444             logger.debug("Setting mailServerScriptsPath. Before '"
445                     + scriptsPath + "', after '"
446                     + mailServerScriptsPathParam + "'");
447         }
448         scriptsPath = mailServerScriptsPathParam;
449
450         if (logger.isDebugEnabled()) {
451             logger.debug("setScriptsPath(String) - end");
452         }
453     }
454     /**
455      * <copyDoc>Refer to {@link #isWindows}.</copyDoc>
456      * @param windowsParam <copyDoc>Refer to {@link #isWindows}.</copyDoc>
457      */

458     public void setWindows(final boolean windowsParam) {
459         if (logger.isDebugEnabled()) {
460             logger.debug("Setting windows. Before '" + windows + "', after '"
461                     + windowsParam + "'");
462         }
463         windows = windowsParam;
464
465         if (logger.isDebugEnabled()) {
466             logger.debug("setWindows(boolean) - end");
467         }
468     }
469     /**
470      * This method has been implemented to check that the mail server fields
471      * are either all blank, or all preasent.
472      * @param requestParam current request we are processing.
473      * @param sessionParam current HTTP session.
474      * @return a validation errors collection containing any errors which
475      * would prevent the form from being successfully processed.
476      */

477     public ValidationErrors validate(
478             final HttpServletRequest JavaDoc requestParam,
479             final HttpSession JavaDoc sessionParam) {
480         if (logger.isDebugEnabled()) {
481             logger.debug("validate(HttpServletRequest requestParam = "
482                     + requestParam + ", HttpSession sessionParam = "
483                     + sessionParam + ") - start");
484         }
485
486         ValidationErrors errors = super.validate(requestParam, sessionParam);
487
488         boolean mailDomainEmpty = StringHandling.isNullOrEmpty(mailDomain);
489         boolean mailHostSMTPEmpty = StringHandling.isNullOrEmpty(mailHostSMTP);
490         boolean mailHostIMAPEmpty = StringHandling.isNullOrEmpty(mailHostIMAP);
491         if (!(mailDomainEmpty
492                     && mailHostIMAPEmpty
493                     && mailHostSMTPEmpty)
494                 && (mailDomainEmpty
495                     || mailHostIMAPEmpty
496                     || mailHostSMTPEmpty)) {
497             errors.add(
498                     new ValidationError("errors.setup.mailParameters",
499                             Arrays.asList(new String JavaDoc[] {})));
500         }
501         // now check the scripts path, if a scripts path was specified
502
if (!(StringHandling.isNullOrEmpty(scriptsPath)
503                 || isWindows())) {
504             // strip off trailing slash
505
if (scriptsPath.endsWith("/")) {
506                 scriptsPath = scriptsPath.substring(0,
507                         scriptsPath.length() - 1);
508             }
509             File JavaDoc sudoScriptMailServerDir = new File JavaDoc(scriptsPath
510                     + MailSetupConstants.SCRIPT_PATH_SUDO);
511             File JavaDoc eximScriptMailServerDir = new File JavaDoc(scriptsPath
512                     + MailSetupConstants.SCRIPT_PATH_EXIM);
513             if (!sudoScriptMailServerDir.exists()) {
514                 errors.add(
515                         new ValidationError("errors.setup.sudoPath",
516                                 Arrays.asList(new String JavaDoc[] {
517                                         MailSetupConstants.SCRIPT_PATH_SUDO,
518                                         scriptsPath
519                                 })));
520             } else if (!sudoScriptMailServerDir.isDirectory()) {
521                 errors.add(
522                         new ValidationError("errors.setup.sudoPathDirectory",
523                                 Arrays.asList(new String JavaDoc[] {
524                                         MailSetupConstants.SCRIPT_PATH_SUDO,
525                                         scriptsPath
526                                 })));
527             } else if (!eximScriptMailServerDir.exists()) {
528                 // note - if one is missing they likely both are - I used an
529
// else here so only one rror would show
530
errors.add(
531                         new ValidationError("errors.setup.eximPath",
532                                 Arrays.asList(new String JavaDoc[] {
533                                         MailSetupConstants.SCRIPT_PATH_EXIM,
534                                         scriptsPath
535                                 })));
536             } else if (!eximScriptMailServerDir.isDirectory()) {
537                 errors.add(
538                         new ValidationError("errors.setup.eximPathDirectory",
539                                 Arrays.asList(new String JavaDoc[] {
540                                         MailSetupConstants.SCRIPT_PATH_EXIM,
541                                         scriptsPath
542                                 })));
543             }
544         }
545
546
547         if (logger.isDebugEnabled()) {
548             logger.debug("validate - end - return value = "
549                             + errors);
550         }
551         return errors;
552     }
553 }
554
Popular Tags