KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > actions > app > ShowMBeanAction


1 /**
2  * Copyright 2004-2005 jManage.org
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 package org.jmanage.webui.actions.app;
17
18 import org.apache.struts.action.ActionForm;
19 import org.apache.struts.action.ActionForward;
20 import org.apache.struts.action.ActionMapping;
21 import org.jmanage.core.config.ApplicationConfig;
22 import org.jmanage.core.config.MBeanConfig;
23 import org.jmanage.core.management.*;
24 import org.jmanage.core.util.Loggers;
25 import org.jmanage.core.services.AccessController;
26 import org.jmanage.core.services.ServiceUtils;
27 import org.jmanage.webui.actions.BaseAction;
28 import org.jmanage.webui.forms.MBeanConfigForm;
29 import org.jmanage.webui.util.Forwards;
30 import org.jmanage.webui.util.WebContext;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import java.util.*;
34 import java.util.logging.Logger JavaDoc;
35 import java.util.logging.Level JavaDoc;
36
37 /**
38  *
39  * date: Jun 13, 2004
40  * @author Rakesh Kalra
41  */

42 public class ShowMBeanAction extends BaseAction {
43
44     private static final Logger JavaDoc logger = Loggers.getLogger(ShowMBeanAction.class);
45
46     public ActionForward execute(WebContext context,
47                                  ActionMapping mapping,
48                                  ActionForm actionForm,
49                                  HttpServletRequest JavaDoc request,
50                                  HttpServletResponse JavaDoc response)
51             throws Exception JavaDoc {
52
53         makeResponseNotCacheable(response);
54
55         final ObjectName objectName = context.getObjectName();
56         final ApplicationConfig config = context.getApplicationConfig();
57         final MBeanConfig configuredMBean =
58                 config.findMBeanByObjectName(objectName.getCanonicalName());
59         AccessController.checkAccess(context.getServiceContext(),
60                 ACL_VIEW_APPLICATIONS);
61         if(configuredMBean != null)
62             AccessController.checkAccess(context.getServiceContext(),
63                     ACL_VIEW_MBEANS);
64         List applications = null;
65         if(config.isCluster()){
66             applications = config.getApplications();
67         }else{
68             applications = new ArrayList(1);
69             applications.add(config);
70         }
71
72         /* the ObjectInfo for the mbean that is being viewed */
73         ObjectInfo objInfo = null;
74         /* array that will be initialized with all attribute names for this
75             mbean */

76         String JavaDoc[] attributeNames = null;
77         /* a Map which constains list of attribute values for each application
78             in the cluster.
79             ApplicationConfig is the key and attribute List is the value*/

80         final Map appConfigToAttrListMap = new HashMap(applications.size());
81         for(Iterator it=applications.iterator(); it.hasNext(); ){
82             ApplicationConfig childAppConfig = (ApplicationConfig)it.next();
83             ServerConnection serverConnection = null;
84             try {
85                 serverConnection =
86                         ServerConnector.getServerConnection(childAppConfig);
87                 /* assuming that all servers in this cluster have exact same
88                     object info, we will get the ObjectInfo from the first
89                     server in the list (could be further down in the list,
90                     if the first server(s) is down */

91                 if(objInfo == null){
92                     objInfo = serverConnection.getObjectInfo(objectName);
93                     assert objInfo != null;
94                     ObjectAttributeInfo[] attributes = objInfo.getAttributes();
95                     attributeNames = new String JavaDoc[attributes.length];
96                     for (int i = 0; i < attributes.length; i++) {
97                         // TODO: we should only add the readable attributes here
98
attributeNames[i] = attributes[i].getName();
99                     }
100                 }
101                 /* add attribute values of this application to the map*/
102                 appConfigToAttrListMap.put(childAppConfig,
103                         serverConnection.getAttributes(objectName, attributeNames));
104             } catch (ConnectionFailedException e){
105                 logger.log(Level.FINE, "Error retrieving attributes for:" +
106                         childAppConfig.getName(), e);
107                 /* add null, indicating that the server is down */
108                 appConfigToAttrListMap.put(childAppConfig, null);
109             } finally{
110                 ServiceUtils.close(serverConnection);
111             }
112         }
113         /* if objInfo is null, that means that we couldn't get connection to
114             any server */

115         if(objInfo == null){
116             throw new ConnectionFailedException(null);
117         }
118
119         request.setAttribute("objInfo", objInfo);
120         request.setAttribute("appConfigToAttrListMap", appConfigToAttrListMap);
121
122         /* setup the form to be used in the html form */
123         MBeanConfigForm mbeanConfigForm = (MBeanConfigForm)actionForm;
124         mbeanConfigForm.setObjectName(objectName.getCanonicalName());
125
126         ApplicationConfig appConfig = context.getApplicationConfig();
127         MBeanConfig mbeanConfig =
128                 appConfig.findMBeanByObjectName(objectName.getCanonicalName());
129         if(mbeanConfig != null){
130             if(appConfig.isCluster()){
131                 request.setAttribute("mbeanIncludedIn", "cluster");
132             }else{
133                 request.setAttribute("mbeanIncludedIn", "application");
134             }
135             request.setAttribute("mbeanConfig", mbeanConfig);
136         }else{
137             ApplicationConfig clusterConfig = appConfig.getClusterConfig();
138             if(clusterConfig != null){
139                 mbeanConfig =
140                     clusterConfig.findMBeanByObjectName(objectName.getCanonicalName());
141             }
142             if(mbeanConfig != null){
143                 request.setAttribute("mbeanIncludedIn", "cluster");
144                 request.setAttribute("mbeanConfig", mbeanConfig);
145             }
146         }
147
148         return mapping.findForward(Forwards.SUCCESS);
149     }
150 }
151
Popular Tags