KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 package org.apache.webapp.admin.resources;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URLDecoder JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Locale JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29 import org.apache.struts.action.Action;
30 import org.apache.struts.action.ActionError;
31 import org.apache.struts.action.ActionErrors;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import javax.management.Attribute JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.MBeanServerFactory JavaDoc;
38 import javax.management.QueryExp JavaDoc;
39 import javax.management.Query JavaDoc;
40 import javax.management.ObjectInstance JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42 import javax.management.JMException JavaDoc;
43 import javax.management.MBeanAttributeInfo JavaDoc;
44 import javax.management.MBeanOperationInfo JavaDoc;
45 import javax.management.MBeanInfo JavaDoc;
46 import org.apache.struts.util.MessageResources;
47 import org.apache.webapp.admin.ApplicationServlet;
48
49
50 /**
51  * <p>Implementation of <strong>Action</strong> that saves a new or
52  * updated User database entry.</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 SaveUserDatabaseAction extends Action {
60
61
62     // ----------------------------------------------------- Instance Variables
63

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

67     private MBeanServer JavaDoc mserver = null;
68
69     /*
70      * Only one implementation of factory allowed to start with.
71      */

72     public static String JavaDoc USERDB_FACTORY = "org.apache.catalina.users.MemoryUserDatabaseFactory";
73
74     // --------------------------------------------------------- Public Methods
75

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

92     public ActionForward execute(ActionMapping mapping,
93                                  ActionForm form,
94                                  HttpServletRequest JavaDoc request,
95                                  HttpServletResponse JavaDoc response)
96         throws IOException JavaDoc, ServletException JavaDoc {
97
98         // Look up the components we will be using as needed
99
if (mserver == null) {
100             mserver = ((ApplicationServlet) getServlet()).getServer();
101         }
102         MessageResources resources = getResources(request);
103         HttpSession JavaDoc session = request.getSession();
104         Locale JavaDoc locale = getLocale(request);
105
106         // Has this transaction been cancelled?
107
if (isCancelled(request)) {
108             return (mapping.findForward("List UserDatabases Setup"));
109         }
110
111         // Check the transaction token
112
if (!isTokenValid(request)) {
113             response.sendError
114                 (HttpServletResponse.SC_BAD_REQUEST,
115                  resources.getMessage(locale, "users.error.token"));
116             return (null);
117         }
118
119         // Perform any extra validation that is required
120
UserDatabaseForm userDatabaseForm = (UserDatabaseForm) form;
121         String JavaDoc objectName = userDatabaseForm.getObjectName();
122
123         // Perform an "Add UserDatabase" transaction
124
if (objectName == null) {
125
126             String JavaDoc signature[] = new String JavaDoc[2];
127             signature[0] = "java.lang.String";
128             signature[1] = "java.lang.String";
129
130             Object JavaDoc params[] = new Object JavaDoc[2];
131             params[0] = userDatabaseForm.getName();
132             params[1] = ResourceUtils.USERDB_CLASS;
133            
134             ObjectName JavaDoc oname = null;
135
136             try {
137                 String JavaDoc domain = userDatabaseForm.getDomain();
138                 oname = new ObjectName JavaDoc( domain +
139                             ResourceUtils.RESOURCE_TYPE +
140                             ResourceUtils.GLOBAL_TYPE +
141                             ",class=" + ResourceUtils.USERDB_CLASS +
142                             ",name=" + params[0]);
143                             
144                 if (mserver.isRegistered(oname)) {
145                     ActionErrors errors = new ActionErrors();
146                     errors.add("name",
147                                new ActionError("resources.invalid.name"));
148                     saveErrors(request, errors);
149                     return (new ActionForward(mapping.getInput()));
150                 }
151                 
152                 // Construct the MBean Name for the naming source
153
oname = new ObjectName JavaDoc(domain +
154                             ResourceUtils.NAMINGRESOURCES_TYPE +
155                             ResourceUtils.GLOBAL_TYPE);
156  
157                 // Create the new object and associated MBean
158
objectName = (String JavaDoc) mserver.invoke(oname, "addResource",
159                                                      params, signature);
160                                      
161             } catch (Exception JavaDoc e) {
162
163                 getServlet().log
164                     (resources.getMessage(locale, "users.error.invoke",
165                                           "addResource"), e);
166                 response.sendError
167                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
168                      resources.getMessage(locale, "users.error.invoke",
169                                           "addResource"));
170                 return (null);
171             }
172
173         }
174         
175         // Perform an "Update User database" transaction
176
String JavaDoc attribute = null;
177         try {
178             
179             ObjectName JavaDoc oname = new ObjectName JavaDoc(objectName);
180
181             attribute = "pathname";
182             mserver.setAttribute
183                 (oname,
184                  new Attribute JavaDoc(attribute, userDatabaseForm.getPath()));
185             attribute = "factory";
186             mserver.setAttribute
187                 (oname,
188                  new Attribute JavaDoc(attribute, userDatabaseForm.getFactory()));
189             attribute = "description";
190             mserver.setAttribute
191                 (oname,
192                  new Attribute JavaDoc(attribute, userDatabaseForm.getDescription()));
193
194         } catch (Exception JavaDoc e) {
195
196             getServlet().log
197                 (resources.getMessage(locale, "users.error.set.attribute",
198                                       attribute), e);
199             response.sendError
200                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
201                  resources.getMessage(locale, "users.error.set.attribute",
202                                       attribute));
203             return (null);
204
205         }
206         
207         // Proceed to the list entries screen
208
return (mapping.findForward("UserDatabases List Setup"));
209
210     }
211
212
213 }
214
Popular Tags