KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionMapping;
20 import org.apache.struts.action.ActionForward;
21 import org.jmanage.webui.util.WebContext;
22 import org.jmanage.webui.util.Forwards;
23 import org.jmanage.webui.util.Utils;
24 import org.jmanage.webui.actions.BaseAction;
25 import org.jmanage.core.util.Expression;
26 import org.jmanage.core.services.*;
27 import org.jmanage.core.management.ObjectAttribute;
28
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * Reads the "attributes" parameter and creates ObjectAttribute object for
38  * each attribute. The "attributes" parameter is of the following format:
39  * <p>
40  * [app1/mbean1/attribute1],[app2/mbean2/attribute2]
41  * <p>
42  * This is used by the GraphApplet to retrieve the attribute values.
43  *
44  * @see org.jmanage.webui.applets.GraphApplet
45  * Date: Jun 11, 2005
46  * @author Rakesh Kalra
47  */

48 public class MBeanAttributeValuesAction extends BaseAction {
49
50
51     public ActionForward execute(WebContext context,
52                                  ActionMapping mapping,
53                                  ActionForm actionForm,
54                                  HttpServletRequest JavaDoc request,
55                                  HttpServletResponse JavaDoc response)
56         throws Exception JavaDoc{
57
58         String JavaDoc attributes = request.getParameter("attributes");
59         assert attributes != null;
60         List JavaDoc exprList = parse(attributes);
61         List JavaDoc objectAttrList = new LinkedList JavaDoc();
62         MBeanService mbeanService = ServiceFactory.getMBeanService();
63         for(Iterator JavaDoc it=exprList.iterator(); it.hasNext();){
64             Expression expression = (Expression)it.next();
65             ServiceContext srvcContext = null;
66             ObjectAttribute objAttribute = null;
67             try {
68                 srvcContext = Utils.getServiceContext(context, expression);
69                 objAttribute = mbeanService.getObjectAttribute(srvcContext,
70                                             expression.getTargetName());
71             } finally {
72                 if(srvcContext != null)
73                     srvcContext.getServerConnection().close();
74             }
75             assert objAttribute != null;
76             assert objAttribute.getStatus() == ObjectAttribute.STATUS_OK;
77             objectAttrList.add(objAttribute);
78         }
79         request.setAttribute("objectAttrList", objectAttrList);
80         return mapping.findForward(Forwards.SUCCESS);
81     }
82
83     /**
84      *
85      * @param attributes
86      * @return list of Expression objects
87      */

88     private List JavaDoc parse(String JavaDoc attributes){
89         List JavaDoc exprList = new LinkedList JavaDoc();
90         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(attributes, ",");
91         while(tokenizer.hasMoreTokens()){
92             String JavaDoc expression = tokenizer.nextToken();
93             assert expression.startsWith("[");
94             while(!expression.endsWith("]")){
95                 assert tokenizer.hasMoreTokens();
96                 expression += "," + tokenizer.nextToken();
97             }
98             expression = expression.substring(1, expression.length()-1);
99             exprList.add(new Expression(expression));
100         }
101         return exprList;
102     }
103
104 }
105
Popular Tags