KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > resource > EditMailFactoryPropertiesAction


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id:
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin.resource;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.struts.action.ActionForm;
37 import org.apache.struts.action.ActionForward;
38 import org.apache.struts.action.ActionMapping;
39 import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
40
41 /**
42  * Helper for actions for the deployment of Mail Factories
43  * @author Adriana Danes
44  */

45 public class EditMailFactoryPropertiesAction extends BaseDeployAction {
46
47     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
48         , HttpServletRequest JavaDoc p_Request, HttpServletResponse JavaDoc p_Response)
49         throws IOException JavaDoc, ServletException JavaDoc {
50
51         String JavaDoc sAction = p_Request.getParameter("action");
52         MailFactoryPropertiesForm oForm = new MailFactoryPropertiesForm();
53         oForm.reset(p_Mapping, p_Request);
54         m_Session.setAttribute("mailFactoryPropertiesForm", oForm);
55         String JavaDoc name = "";
56         Properties JavaDoc props = new Properties JavaDoc();
57         try {
58             populate(name, props, oForm);
59         } catch (Throwable JavaDoc t) {
60             addGlobalError(t);
61             saveErrors(p_Request, m_Errors);
62             return (p_Mapping.findForward("Global Error"));
63         }
64         
65         // Forward to the jsp.
66
return (p_Mapping.findForward("MailFactory Properties"));
67     }
68     /**
69      * Populate a form with configuration properties read in a configuration file
70      *
71      * @param mailFactoryName the name of the mail factory
72      * @param props properties to configure the mail factory
73      * param pForm the form used to print-out the configuration properties
74      */

75     void populate(String JavaDoc mailFactoryName, Properties JavaDoc props, MailFactoryPropertiesForm pForm) throws Exception JavaDoc {
76         // Set configuration attributes
77
// - mail factory name
78
pForm.setMailFactoryName(mailFactoryName);
79         // - jndi name
80
pForm.setJndiName(getStringAttribute(props, "mail.factory.name", pForm.getJndiName()));
81         props.remove("mail.factory.name");
82         // - type
83
pForm.setType(getStringAttribute(props, "mail.factory.type", pForm.getType()));
84         props.remove("mail.factory.type");
85
86         // Set authentication properties
87
pForm.setUsername(getStringAttribute(props, "mail.authentication.username", pForm.getUsername()));
88         props.remove("mail.authentication.username");
89         pForm.setPassword(getStringAttribute(props, "mail.authentication.password", pForm.getPassword()));
90         props.remove("mail.authentication.password");
91         
92         // Set mime part properties
93
pForm.setTo(getStringAttribute(props, "mail.to", pForm.getTo()));
94         pForm.setSubject(getStringAttribute(props, "mail.subject", pForm.getSubject()));
95         pForm.setCc(getStringAttribute(props, "mail.cc", pForm.getCc()));
96         pForm.setBcc(getStringAttribute(props, "mail.bcc", pForm.getBcc()));
97         props.remove("mail.to");
98         props.remove("mail.subject");
99         props.remove("mail.cc");
100         props.remove("mail.bcc");
101
102         // Set the rest of the properties as session properties
103
String JavaDoc aName = null;
104         String JavaDoc aValue = null;
105         StringBuffer JavaDoc bufSessionProps = new StringBuffer JavaDoc();
106         for(Enumeration JavaDoc pNames = props.propertyNames(); pNames.hasMoreElements() ; ) {
107             aName = (String JavaDoc)pNames.nextElement();
108             aValue = (String JavaDoc)props.getProperty(aName);
109             bufSessionProps.append(aName);
110             bufSessionProps.append("=");
111             bufSessionProps.append(aValue);
112             bufSessionProps.append(",");
113             bufSessionProps.append("\n");
114         }
115         if (!props.isEmpty()) {
116             // delete the last 2 chars (, and \n)
117
bufSessionProps.delete(bufSessionProps.length() - 2, bufSessionProps.length() - 1);
118         } else {
119             // Actually no chars in sessionPropsBuf
120
//System.out.println("no session props, append mail.host= ");
121
bufSessionProps.append("mail.host=");
122         }
123         String JavaDoc sSessionProps = new String JavaDoc(bufSessionProps);
124         pForm.setSessionProps(sSessionProps);
125     }
126
127     /**
128      * Use a form's content to create the Properties object needed to create and load a Mail factory
129      *
130      */

131     Properties JavaDoc getPropsFromForm(MailFactoryPropertiesForm p_Form, boolean mimepart) throws Exception JavaDoc {
132         Properties JavaDoc oProps = new Properties JavaDoc();
133         Properties JavaDoc sProps = getPropsFromString(p_Form.getSessionProps());
134         oProps.putAll(sProps);
135         setStringAttribute(oProps, "mail.factory.name", p_Form.getJndiName());
136         setStringAttribute(oProps, "mail.factory.type", p_Form.getType());
137         setStringAttribute(oProps, "mail.authentication.username", p_Form.getUsername());
138         setStringAttribute(oProps, "mail.authentication.password", p_Form.getPassword());
139         String JavaDoc aName = null;
140         String JavaDoc aValue = null;
141         for(Enumeration JavaDoc pNames = oProps.propertyNames(); pNames.hasMoreElements() ; ) {
142             aName = (String JavaDoc) pNames.nextElement();
143             aValue = (String JavaDoc) oProps.getProperty(aName);
144         }
145         if (mimepart == true) {
146             setStringAttribute(oProps, "mail.to", p_Form.getTo());
147             setStringAttribute(oProps, "mail.subject", p_Form.getSubject());
148             setStringAttribute(oProps, "mail.cc", p_Form.getCc());
149             setStringAttribute(oProps, "mail.bcc", p_Form.getBcc());
150         }
151         return oProps;
152     }
153 }
154
Popular Tags