KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > admin > connector > SaveConnectorAction


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.connector;
18
19 import java.net.URLEncoder JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.io.IOException JavaDoc;
23 import javax.management.Attribute JavaDoc;
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.MBeanServerFactory JavaDoc;
26 import javax.management.QueryExp JavaDoc;
27 import javax.management.Query JavaDoc;
28 import javax.management.ObjectInstance JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.JMException JavaDoc;
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35 import org.apache.struts.action.Action;
36 import org.apache.struts.action.ActionError;
37 import org.apache.struts.action.ActionErrors;
38 import org.apache.struts.action.ActionForm;
39 import org.apache.struts.action.ActionForward;
40 import org.apache.struts.action.ActionMapping;
41 import org.apache.struts.util.MessageResources;
42 import org.apache.webapp.admin.ApplicationServlet;
43 import org.apache.webapp.admin.TomcatTreeBuilder;
44 import org.apache.webapp.admin.TreeControl;
45 import org.apache.webapp.admin.TreeControlNode;
46
47
48 /**
49  * The <code>Action</code> that completes <em>Add Connector</em> and
50  * <em>Edit Connector</em> transactions.
51  *
52  * @author Manveen Kaur
53  * @version $Revision: 1.19 $ $Date: 2004/10/18 06:37:53 $
54  */

55
56 public final class SaveConnectorAction extends Action {
57
58
59     // ----------------------------------------------------- Instance Variables
60

61     /**
62      * Signature for the <code>createStandardConnector</code> operation.
63      */

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

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

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

93     public ActionForward execute(ActionMapping mapping,
94                                  ActionForm form,
95                                  HttpServletRequest JavaDoc request,
96                                  HttpServletResponse JavaDoc response)
97         throws IOException JavaDoc, ServletException JavaDoc {
98         
99         // Acquire the resources that we need
100
HttpSession JavaDoc session = request.getSession();
101         Locale JavaDoc locale = getLocale(request);
102         MessageResources resources = getResources(request);
103         
104         // Acquire a reference to the MBeanServer containing our MBeans
105
try {
106             mBServer = ((ApplicationServlet) getServlet()).getServer();
107         } catch (Throwable JavaDoc t) {
108             throw new ServletException JavaDoc
109             ("Cannot acquire MBeanServer reference", t);
110         }
111         
112         // Identify the requested action
113
ConnectorForm cform = (ConnectorForm) form;
114         String JavaDoc adminAction = cform.getAdminAction();
115         String JavaDoc cObjectName = cform.getObjectName();
116         String JavaDoc connectorType = cform.getConnectorType();
117         ObjectName JavaDoc coname = null;
118
119         // Perform a "Create Connector" transaction (if requested)
120
if ("Create".equals(adminAction)) {
121
122             String JavaDoc operation = null;
123             Object JavaDoc values[] = null;
124
125             try {
126                 // get service name which is same as domain
127
String JavaDoc serviceName = cform.getServiceName();
128                 ObjectName JavaDoc soname = new ObjectName JavaDoc(serviceName);
129                 String JavaDoc domain = soname.getDomain();
130                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(domain);
131                 StringBuffer JavaDoc searchSB = new StringBuffer JavaDoc("*");
132                 sb.append(TomcatTreeBuilder.CONNECTOR_TYPE);
133                 searchSB.append(TomcatTreeBuilder.CONNECTOR_TYPE);
134                 sb.append(",port=" + cform.getPortText());
135                 searchSB.append(",port=" + cform.getPortText());
136                 
137                 ObjectName JavaDoc search = new ObjectName JavaDoc(searchSB.toString()+",*");
138                 
139                 String JavaDoc address = cform.getAddress();
140                 if ((address!=null) && (address.length()>0) &&
141                         (!address.equalsIgnoreCase(" "))) {
142                     sb.append(",address=" + address);
143                 } else {
144                     address = null;
145                 }
146                 ObjectName JavaDoc oname = new ObjectName JavaDoc(sb.toString());
147                                                 
148                 // Ensure that the requested connector name and port is unique
149
if (mBServer.isRegistered(oname) ||
150                     (!mBServer.queryNames(search, null).isEmpty())) {
151                     ActionErrors errors = new ActionErrors();
152                     errors.add("connectorName",
153                                new ActionError("error.connectorName.exists"));
154                     saveErrors(request, errors);
155                     return (new ActionForward(mapping.getInput()));
156                 }
157
158                 // Look up our MBeanFactory MBean
159
ObjectName JavaDoc fname = TomcatTreeBuilder.getMBeanFactory();
160
161                 // Create a new Connector object
162
values = new Object JavaDoc[3];
163                 values[0] = serviceName; //service parent object name
164
values[1] = address;
165                 values[2] = new Integer JavaDoc(cform.getPortText());
166
167                 if ("HTTP".equalsIgnoreCase(connectorType)) {
168                         operation = "createHttpConnector"; // HTTP
169
} else if ("HTTPS".equalsIgnoreCase(connectorType)) {
170                         operation = "createHttpsConnector"; // HTTPS
171
} else {
172                         operation = "createAjpConnector"; // AJP(HTTP)
173
}
174                 
175                 cObjectName = (String JavaDoc)
176                     mBServer.invoke(fname, operation,
177                                     values, createStandardConnectorTypes);
178                 
179                 // Add the new Connector to our tree control node
180
TreeControl control = (TreeControl)
181                     session.getAttribute("treeControlTest");
182                 if (control != null) {
183                     String JavaDoc parentName = serviceName;
184                     TreeControlNode parentNode = control.findNode(parentName);
185                     if (parentNode != null) {
186                         String JavaDoc nodeLabel = resources.getMessage(locale,
187                             "server.service.treeBuilder.connector") + " (" +
188                             cform.getPortText() + ")";
189                         String JavaDoc encodedName =
190                             URLEncoder.encode(cObjectName,TomcatTreeBuilder.URL_ENCODING);
191                         TreeControlNode childNode =
192                             new TreeControlNode(cObjectName,
193                                                 "Connector.gif",
194                                                 nodeLabel,
195                                                 "EditConnector.do?select=" +
196                                                 encodedName,
197                                                 "content",
198                                                 true, domain);
199                         // FIXME--the node should be next to the rest of
200
// the Connector nodes..
201
parentNode.addChild(childNode);
202                         // FIXME - force a redisplay
203
} else {
204                         getServlet().log
205                             ("Cannot find parent node '" + parentName + "'");
206                     }
207                 } else {
208                     getServlet().log
209                         ("Cannot find TreeControlNode!");
210                 }
211
212             } catch (Exception JavaDoc e) {
213
214                 getServlet().log
215                     (resources.getMessage(locale, "users.error.invoke",
216                                           operation), e);
217                 response.sendError
218                     (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
219                      resources.getMessage(locale, "users.error.invoke",
220                                           operation));
221                 return (null);
222
223             }
224
225         }
226
227         // Perform attribute updates as requested
228
String JavaDoc attribute = null;
229         try {
230
231             coname = new ObjectName JavaDoc(cObjectName);
232
233             attribute = "acceptCount";
234             int acceptCount = 60000;
235             try {
236                 acceptCount = Integer.parseInt(cform.getAcceptCountText());
237             } catch (Throwable JavaDoc t) {
238                 acceptCount = 60000;
239             }
240             mBServer.setAttribute(coname,
241                                   new Attribute JavaDoc("acceptCount", new Integer JavaDoc(acceptCount)));
242             attribute = "compression";
243             String JavaDoc compression = cform.getCompression();
244             if ((compression != null) && (compression.length()>0)) {
245                 mBServer.setAttribute(coname,
246                                       new Attribute JavaDoc("compression", compression));
247             }
248             attribute = "connectionLinger";
249             int connectionLinger = -1;
250             try {
251                 connectionLinger = Integer.parseInt(cform.getConnLingerText());
252             } catch (Throwable JavaDoc t) {
253                 connectionLinger = 0;
254             }
255             mBServer.setAttribute(coname,
256                                   new Attribute JavaDoc("connectionLinger", new Integer JavaDoc(connectionLinger)));
257             attribute = "connectionTimeout";
258             int connectionTimeout = 0;
259             try {
260                 connectionTimeout = Integer.parseInt(cform.getConnTimeOutText());
261             } catch (Throwable JavaDoc t) {
262                 connectionTimeout = 0;
263             }
264             mBServer.setAttribute(coname,
265                                   new Attribute JavaDoc("connectionTimeout", new Integer JavaDoc(connectionTimeout)));
266             attribute = "connectionUploadTimeout";
267             int connectionUploadTimeout = 0;
268             try {
269                 connectionUploadTimeout = Integer.parseInt(cform.getConnUploadTimeOutText());
270             } catch (Throwable JavaDoc t) {
271                 connectionUploadTimeout = 0;
272             }
273             mBServer.setAttribute(coname,
274                                   new Attribute JavaDoc("connectionUploadTimeout", new Integer JavaDoc(connectionUploadTimeout)));
275             attribute = "bufferSize";
276             int bufferSize = 2048;
277             try {
278                 bufferSize = Integer.parseInt(cform.getBufferSizeText());
279             } catch (Throwable JavaDoc t) {
280                 bufferSize = 2048;
281             }
282             mBServer.setAttribute(coname,
283                                   new Attribute JavaDoc("bufferSize", new Integer JavaDoc(bufferSize)));
284             attribute = "disableUploadTimeout";
285             mBServer.setAttribute(coname,
286                                   new Attribute JavaDoc("disableUploadTimeout", new Boolean JavaDoc(cform.getDisableUploadTimeout())));
287             attribute = "enableLookups";
288             mBServer.setAttribute(coname,
289                                   new Attribute JavaDoc("enableLookups", new Boolean JavaDoc(cform.getEnableLookups())));
290
291             attribute = "redirectPort";
292             int redirectPort = 0;
293             try {
294                 redirectPort = Integer.parseInt(cform.getRedirectPortText());
295             } catch (Throwable JavaDoc t) {
296                 redirectPort = 0;
297             }
298             mBServer.setAttribute(coname,
299                                   new Attribute JavaDoc("redirectPort", new Integer JavaDoc(redirectPort)));
300             attribute = "minProcessors";
301             int minProcessors = 5;
302             try {
303                 minProcessors = Integer.parseInt(cform.getMinProcessorsText());
304             } catch (Throwable JavaDoc t) {
305                 minProcessors = 5;
306             }
307             //mBServer.setAttribute(coname,
308
// new Attribute("minProcessors", new Integer(minProcessors)));
309
attribute = "maxProcessors";
310             int maxProcessors = 20;
311             try {
312                 maxProcessors = Integer.parseInt(cform.getMaxProcessorsText());
313             } catch (Throwable JavaDoc t) {
314                 maxProcessors = 20;
315             }
316             //mBServer.setAttribute(coname,
317
// new Attribute("maxProcessors", new Integer(maxProcessors)));
318

319             attribute = "maxKeepAliveRequests";
320             int maxKeepAliveRequests = 100;
321             try {
322                 maxKeepAliveRequests = Integer.parseInt(cform.getMaxKeepAliveText());
323             } catch (Throwable JavaDoc t) {
324                 maxKeepAliveRequests = 100;
325             }
326             mBServer.setAttribute(coname,
327                                   new Attribute JavaDoc("maxKeepAliveRequests", new Integer JavaDoc(maxKeepAliveRequests)));
328             attribute = "maxSpareThreads";
329             int maxSpare = 50;
330             try {
331                 maxSpare = Integer.parseInt(cform.getMaxSpare());
332             } catch (Throwable JavaDoc t) {
333                 maxSpare = 50;
334             }
335             mBServer.setAttribute(coname,
336                                   new Attribute JavaDoc(attribute, (new Integer JavaDoc(maxSpare)).toString()));
337             attribute = "maxThreads";
338             int maxThreads = 200;
339             try {
340                 maxThreads = Integer.parseInt(cform.getMaxThreads());
341             } catch (Throwable JavaDoc t) {
342                 maxThreads = 200;
343             }
344             mBServer.setAttribute(coname,
345                                   new Attribute JavaDoc(attribute, (new Integer JavaDoc(maxThreads)).toString()));
346             
347             attribute = "minSpareThreads";
348             int minSpare = 4;
349             try {
350                 minSpare = Integer.parseInt(cform.getMinSpare());
351             } catch (Throwable JavaDoc t) {
352                 minSpare = 4;
353             }
354             mBServer.setAttribute(coname,
355                                   new Attribute JavaDoc(attribute, (new Integer JavaDoc(minSpare)).toString()));
356
357             attribute = "threadPriority";
358             int threadPriority = Thread.NORM_PRIORITY;
359             try {
360                 threadPriority = Integer.parseInt(cform.getThreadPriority());
361             } catch (Throwable JavaDoc t) {
362                 threadPriority = Thread.NORM_PRIORITY;
363             }
364             mBServer.setAttribute(coname,
365                                   new Attribute JavaDoc(attribute, (new Integer JavaDoc(threadPriority))));
366                   
367             attribute = "secure";
368             mBServer.setAttribute(coname,
369                                   new Attribute JavaDoc("secure", new Boolean JavaDoc(cform.getSecure())));
370             attribute = "tcpNoDelay";
371             mBServer.setAttribute(coname,
372                                   new Attribute JavaDoc("tcpNoDelay", new Boolean JavaDoc(cform.getTcpNoDelay())));
373             
374             attribute = "xpoweredBy";
375             mBServer.setAttribute(coname,
376                                   new Attribute JavaDoc("xpoweredBy", new Boolean JavaDoc(cform.getXpoweredBy())));
377
378             attribute = "URIEncoding";
379             String JavaDoc uriEnc = cform.getURIEncodingText();
380             if ((uriEnc != null) && (uriEnc.length()==0)) {
381                 uriEnc = null;
382             }
383             mBServer.setAttribute(coname,
384                                   new Attribute JavaDoc(attribute, uriEnc));
385
386             attribute = "useBodyEncodingForURI";
387             mBServer.setAttribute(coname,
388                                   new Attribute JavaDoc(attribute, new Boolean JavaDoc(cform.getUseBodyEncodingForURIText())));
389
390             attribute = "allowTrace";
391             mBServer.setAttribute(coname,
392                                   new Attribute JavaDoc(attribute, new Boolean JavaDoc(cform.getAllowTraceText())));
393
394             // proxy name and port do not exist for AJP connector
395
if (!("AJP".equalsIgnoreCase(connectorType))) {
396                 attribute = "proxyName";
397                 String JavaDoc proxyName = cform.getProxyName();
398                 if ((proxyName != null) && (proxyName.length()>0)) {
399                     mBServer.setAttribute(coname,
400                                   new Attribute JavaDoc("proxyName", proxyName));
401                 }
402                 
403                 attribute = "proxyPort";
404                 int proxyPort = 0;
405                 try {
406                     proxyPort = Integer.parseInt(cform.getProxyPortText());
407                 } catch (Throwable JavaDoc t) {
408                     proxyPort = 0;
409                 }
410                 mBServer.setAttribute(coname,
411                               new Attribute JavaDoc("proxyPort", new Integer JavaDoc(proxyPort)));
412             }
413             
414             // HTTPS specific properties
415
if("HTTPS".equalsIgnoreCase(connectorType)) {
416                 attribute = "algorithm";
417                 String JavaDoc algorithm = cform.getAlgorithm();
418                 if ((algorithm != null) && (algorithm.length()>0))
419                     mBServer.setAttribute(coname,
420                               new Attribute JavaDoc("algorithm", algorithm));
421                 
422                 attribute = "clientAuth";
423                 mBServer.setAttribute(coname,
424                               new Attribute JavaDoc("clientAuth",
425                                              cform.getClientAuthentication()));
426                 
427                 attribute = "ciphers";
428                 String JavaDoc ciphers = cform.getCiphers();
429                 if ((ciphers != null) && (ciphers.length()>0))
430                     mBServer.setAttribute(coname,
431                               new Attribute JavaDoc("ciphers", ciphers));
432                 
433                 attribute = "keystoreFile";
434                 String JavaDoc keyFile = cform.getKeyStoreFileName();
435                 if ((keyFile != null) && (keyFile.length()>0))
436                     mBServer.setAttribute(coname,
437                               new Attribute JavaDoc("keystoreFile", keyFile));
438                 
439                 attribute = "keystorePass";
440                 String JavaDoc keyPass = cform.getKeyStorePassword();
441                 if ((keyPass != null) && (keyPass.length()>0))
442                     mBServer.setAttribute(coname,
443                               new Attribute JavaDoc("keystorePass", keyPass));
444                 // request.setAttribute("warning", "connector.keyPass.warning");
445

446                 attribute = "keystoreType";
447                 String JavaDoc keyType = cform.getKeyStoreType();
448                 if ((keyType != null) && (keyType.length()>0))
449                     mBServer.setAttribute(coname,
450                               new Attribute JavaDoc("keystoreType", keyType));
451                 
452                 attribute = "sslProtocol";
453                 String JavaDoc sslProtocol = cform.getSslProtocol();
454                 if ((sslProtocol != null) && (sslProtocol.length()>0))
455                     mBServer.setAttribute(coname,
456                               new Attribute JavaDoc("sslProtocol", sslProtocol));
457              }
458  
459         } catch (Exception JavaDoc e) {
460
461             getServlet().log
462                 (resources.getMessage(locale, "users.error.attribute.set",
463                                       attribute), e);
464             response.sendError
465                 (HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
466                  resources.getMessage(locale, "users.error.attribute.set",
467                                       attribute));
468             return (null);
469         }
470         // Forward to the success reporting page
471
session.removeAttribute(mapping.getAttribute());
472         return (mapping.findForward("Save Successful"));
473         
474     }
475     
476 }
477
Popular Tags