KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > resources > SetUpDataSourceAction


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.webapp.admin.resources;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URLDecoder JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import javax.management.AttributeNotFoundException JavaDoc;
25 import javax.management.MBeanServer JavaDoc;
26 import javax.management.MBeanServerFactory JavaDoc;
27 import javax.management.QueryExp JavaDoc;
28 import javax.management.Query JavaDoc;
29 import javax.management.ObjectInstance JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.JMException JavaDoc;
32 import javax.management.MBeanAttributeInfo JavaDoc;
33 import javax.management.MBeanOperationInfo JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import javax.servlet.http.HttpSession JavaDoc;
39 import org.apache.struts.action.Action;
40 import org.apache.struts.action.ActionErrors;
41 import org.apache.struts.action.ActionForm;
42 import org.apache.struts.action.ActionForward;
43 import org.apache.struts.action.ActionMapping;
44 import org.apache.struts.util.MessageResources;
45 import org.apache.webapp.admin.ApplicationServlet;
46
47
48 /**
49  * <p>Implementation of <strong>Action</strong> that sets up and stashes
50  * a <code>DataSourceForm</code> bean in request scope. The form bean will have
51  * a null <code>objectName</code> property if this form represents a DataSource
52  * being added, or a non-null value for an existing DataSource.</p>
53  *
54  * @author Manveen Kaur
55  * @version $Revision: 1.7 $ $Date: 2004/10/18 06:37:54 $
56  * @since 4.1
57  */

58
59 public final class SetUpDataSourceAction extends Action {
60
61     // ----------------------------------------------------- Instance Variables
62

63
64     /**
65      * The MBeanServer we will be interacting with.
66      */

67     private MBeanServer JavaDoc mserver = null;
68
69
70     // --------------------------------------------------------- Public Methods
71

72
73     /**
74      * Process the specified HTTP request, and create the corresponding HTTP
75      * response (or forward to another web component that will create it).
76      * Return an <code>ActionForward</code> instance describing where and how
77      * control should be forwarded, or <code>null</code> if the response has
78      * already been completed.
79      *
80      * @param mapping The ActionMapping used to select this instance
81      * @param actionForm The optional ActionForm bean for this request (if any)
82      * @param request The HTTP request we are processing
83      * @param response The HTTP response we are creating
84      *
85      * @exception IOException if an input/output error occurs
86      * @exception ServletException if a servlet exception occurs
87      */

88     public ActionForward execute(ActionMapping mapping,
89                                  ActionForm form,
90                                  HttpServletRequest JavaDoc request,
91                                  HttpServletResponse JavaDoc response)
92         throws IOException JavaDoc, ServletException JavaDoc {
93
94         // Look up the components we will be using as needed
95
if (mserver == null) {
96             mserver = ((ApplicationServlet) getServlet()).getServer();
97         }
98         MessageResources resources = getResources(request);
99         HttpSession JavaDoc session = request.getSession();
100         Locale JavaDoc locale = getLocale(request);
101
102         // Set up the form bean based on the creating or editing state
103
String JavaDoc objectName = request.getParameter("objectName");
104         String JavaDoc resourcetype = request.getParameter("resourcetype");
105         String JavaDoc path = request.getParameter("path");
106         String JavaDoc host = request.getParameter("host");
107         String JavaDoc domain = request.getParameter("domain");
108
109         DataSourceForm dataSourceForm = new DataSourceForm();
110         dataSourceForm.setResourcetype(resourcetype);
111         dataSourceForm.setPath(path);
112         dataSourceForm.setHost(host);
113         dataSourceForm.setDomain(domain);
114         dataSourceForm.setType(ResourceUtils.DATASOURCE_CLASS);
115
116         if (objectName == null) {
117             dataSourceForm.setNodeLabel
118                 (resources.getMessage(locale, "resources.actions.datasrc.create"));
119             dataSourceForm.setObjectName(null);
120             dataSourceForm.setActive("4");
121             dataSourceForm.setIdle("2");
122             dataSourceForm.setWait("5000");
123             dataSourceForm.setType(ResourceUtils.DATASOURCE_CLASS);
124
125         } else {
126             dataSourceForm.setNodeLabel
127                 (resources.getMessage(locale, "resources.actions.datasrc.edit"));
128             dataSourceForm.setObjectName(objectName);
129
130             String JavaDoc attribute = null;
131             try {
132                 ObjectName JavaDoc oname = new ObjectName JavaDoc(objectName);
133                 try {
134                     attribute = "name";
135                     dataSourceForm.setJndiName
136                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
137                     attribute = "url";
138                     dataSourceForm.setUrl
139                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
140                     attribute = "driverClassName";
141                     dataSourceForm.setDriverClass
142                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
143                     attribute = "username";
144                     dataSourceForm.setUsername
145                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
146                     attribute = "password";
147                     dataSourceForm.setPassword
148                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
149                     attribute = "validationQuery";
150                     dataSourceForm.setQuery
151                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
152                 } catch (AttributeNotFoundException JavaDoc ex) {
153                     // disply empty if attribute is not set yet
154
}
155                 try {
156                     attribute = "maxActive";
157                     dataSourceForm.setActive
158                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
159                 } catch (AttributeNotFoundException JavaDoc e) {
160                     // if maxActive not defined, display default value
161
dataSourceForm.setActive("4");
162                 }
163                 try {
164                     attribute = "maxIdle";
165                     dataSourceForm.setIdle
166                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
167                 } catch (AttributeNotFoundException JavaDoc e) {
168                     // if maxIdle not defined, display default value
169
dataSourceForm.setIdle("2");
170                 }
171                 try {
172                     attribute = "maxWait";
173                     dataSourceForm.setWait
174                         ((String JavaDoc) mserver.getAttribute(oname, attribute));
175                 } catch (AttributeNotFoundException JavaDoc e) {
176                     // if maxWait not defined, display default value
177
dataSourceForm.setWait("5000");
178                 }
179             } catch (Exception JavaDoc e) {
180                 getServlet().log
181                     (resources.getMessage(locale,
182                         "users.error.attribute.get", attribute), e);
183                 response.sendError
184                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
185                      resources.getMessage
186                          (locale, "users.error.attribute.get", attribute));
187                 return (null);
188             }
189         }
190
191         // Stash the form bean and forward to the display page
192
saveToken(request);
193         request.setAttribute("dataSourceForm", dataSourceForm);
194         return (mapping.findForward("DataSource"));
195
196     }
197 }
198
Popular Tags