KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > realm > SaveMemoryRealmAction


1 /*
2  * Copyright 2001-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.realm;
18
19
20 import java.net.URLEncoder JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.io.IOException JavaDoc;
24 import javax.management.Attribute 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.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36 import org.apache.struts.action.Action;
37 import org.apache.struts.action.ActionError;
38 import org.apache.struts.action.ActionErrors;
39 import org.apache.struts.action.ActionForm;
40 import org.apache.struts.action.ActionForward;
41 import org.apache.struts.action.ActionMapping;
42 import org.apache.struts.util.MessageResources;
43 import org.apache.webapp.admin.ApplicationServlet;
44 import org.apache.webapp.admin.TomcatTreeBuilder;
45 import org.apache.webapp.admin.TreeControl;
46 import org.apache.webapp.admin.TreeControlNode;
47 import org.apache.webapp.admin.valve.ValveUtil;
48
49 /**
50  * The <code>Action</code> that completes <em>Add Realm</em> and
51  * <em>Edit Realm</em> transactions for Memory realm.
52  *
53  * @author Manveen Kaur
54  * @version $Revision: 1.10 $ $Date: 2004/10/18 06:37:54 $
55  */

56
57 public final class SaveMemoryRealmAction extends Action {
58
59
60     // ----------------------------------------------------- Instance Variables
61

62     /**
63      * Signature for the <code>createStandardRealm</code> operation.
64      */

65     private String JavaDoc createStandardRealmTypes[] =
66     { "java.lang.String", // parent
67
};
68
69
70     /**
71      * The MBeanServer we will be interacting with.
72      */

73     private MBeanServer JavaDoc mBServer = null;
74     
75
76     // --------------------------------------------------------- Public Methods
77

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

94     public ActionForward execute(ActionMapping mapping,
95                                  ActionForm form,
96                                  HttpServletRequest JavaDoc request,
97                                  HttpServletResponse JavaDoc response)
98         throws IOException JavaDoc, ServletException JavaDoc {
99         
100         // Acquire the resources that we need
101
HttpSession JavaDoc session = request.getSession();
102         Locale JavaDoc locale = getLocale(request);
103         MessageResources resources = getResources(request);
104         
105         // Acquire a reference to the MBeanServer containing our MBeans
106
try {
107             mBServer = ((ApplicationServlet) getServlet()).getServer();
108         } catch (Throwable JavaDoc t) {
109             throw new ServletException JavaDoc
110             ("Cannot acquire MBeanServer reference", t);
111         }
112         
113         // Identify the requested action
114
MemoryRealmForm rform = (MemoryRealmForm) form;
115         String JavaDoc adminAction = rform.getAdminAction();
116         String JavaDoc rObjectName = rform.getObjectName();
117
118         // Perform a "Create Memory Realm" transaction (if requested)
119
if ("Create".equals(adminAction)) {
120
121             String JavaDoc operation = null;
122             String JavaDoc values[] = null;
123
124             try {
125
126                 String JavaDoc parent = rform.getParentObjectName();
127                 String JavaDoc objectName = ValveUtil.getObjectName(parent,
128                                     TomcatTreeBuilder.REALM_TYPE);
129                 
130                 ObjectName JavaDoc pname = new ObjectName JavaDoc(parent);
131                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(pname.getDomain());
132                 
133                 // For service, create the corresponding Engine mBean
134
// Parent in this case needs to be the container mBean for the service
135
try {
136                     if ("Service".equalsIgnoreCase(pname.getKeyProperty("type"))) {
137                         sb.append(":type=Engine");
138                         parent = sb.toString();
139                     }
140                 } catch (Exception JavaDoc e) {
141                     String JavaDoc message =
142                         resources.getMessage(locale, "error.engineName.bad",
143                                          sb.toString());
144                     getServlet().log(message);
145                     response.sendError(HttpServletResponse.SC_BAD_REQUEST, message);
146                     return (null);
147                 }
148                                                 
149                 // Ensure that the requested user database name is unique
150
ObjectName JavaDoc oname =
151                     new ObjectName JavaDoc(objectName);
152                 if (mBServer.isRegistered(oname)) {
153                     ActionErrors errors = new ActionErrors();
154                     errors.add("realmName",
155                                new ActionError("error.realmName.exists"));
156                     saveErrors(request, errors);
157                     return (new ActionForward(mapping.getInput()));
158                 }
159
160                 String JavaDoc domain = oname.getDomain();
161                 // Look up our MBeanFactory MBean
162
ObjectName JavaDoc fname = TomcatTreeBuilder.getMBeanFactory();
163
164                 // Create a new StandardRealm object
165
values = new String JavaDoc[1];
166                 values[0] = parent;
167                 operation = "createMemoryRealm";
168                 rObjectName = (String JavaDoc)
169                     mBServer.invoke(fname, operation,
170                                     values, createStandardRealmTypes);
171                                     
172                 // Add the new Realm to our tree control node
173
TreeControl control = (TreeControl)
174                     session.getAttribute("treeControlTest");
175                 if (control != null) {
176                     TreeControlNode parentNode = control.findNode(rform.getParentObjectName());
177                     if (parentNode != null) {
178                         String JavaDoc nodeLabel = rform.getNodeLabel();
179                         String JavaDoc encodedName =
180                             URLEncoder.encode(rObjectName,TomcatTreeBuilder.URL_ENCODING);
181                         TreeControlNode childNode =
182                             new TreeControlNode(rObjectName,
183                                                 "Realm.gif",
184                                                 nodeLabel,
185                                                 "EditRealm.do?select=" +
186                                                 encodedName,
187                                                 "content",
188                                                 true, domain);
189                         parentNode.addChild(childNode);
190                         // FIXME - force a redisplay
191
} else {
192                         getServlet().log
193                             ("Cannot find parent node '" + parent + "'");
194                     }
195                 } else {
196                     getServlet().log
197                         ("Cannot find TreeControlNode!");
198                 }
199
200             } catch (Exception JavaDoc e) {
201
202                 getServlet().log
203                     (resources.getMessage(locale, "users.error.invoke",
204                                           operation), e);
205                 response.sendError
206                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
207                      resources.getMessage(locale, "users.error.invoke",
208                                           operation));
209                 return (null);
210
211             }
212
213         }
214
215         // Perform attribute updates as requested
216
String JavaDoc attribute = null;
217         try {
218
219             ObjectName JavaDoc roname = new ObjectName JavaDoc(rObjectName);
220
221             attribute = "pathname";
222             mBServer.setAttribute(roname,
223                                   new Attribute JavaDoc("pathname", rform.getPathName()));
224
225         } catch (Exception JavaDoc e) {
226
227             getServlet().log
228                 (resources.getMessage(locale, "users.error.attribute.set",
229                                       attribute), e);
230             response.sendError
231                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
232                  resources.getMessage(locale, "users.error.attribute.set",
233                                       attribute));
234             return (null);
235         }
236         
237         // Forward to the success reporting page
238
session.removeAttribute(mapping.getAttribute());
239         return (mapping.findForward("Save Successful"));
240         
241     }
242     
243 }
244
Popular Tags