KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > webui > actions > config > AddAlertAction


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.config;
17
18 import org.jmanage.webui.actions.BaseAction;
19 import org.jmanage.webui.util.*;
20 import org.jmanage.webui.forms.AlertForm;
21 import org.jmanage.core.services.MBeanService;
22 import org.jmanage.core.services.ServiceFactory;
23 import org.jmanage.core.config.AlertConfig;
24 import org.jmanage.core.config.ApplicationConfig;
25 import org.jmanage.core.config.ApplicationConfigManager;
26 import org.jmanage.core.config.AlertSourceConfig;
27 import org.jmanage.core.alert.AlertEngine;
28 import org.jmanage.core.util.Expression;
29 import org.jmanage.core.util.CoreUtils;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionForm;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * Date: May 25, 2005 3:43:21 PM
39  * @author Bhavana
40  */

41 public class AddAlertAction extends BaseAction{
42     /**
43      * @param context
44      * @param mapping
45      * @param actionForm
46      * @param request
47      * @param response
48      * @return
49      * @throws Exception
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         AlertForm form = (AlertForm)actionForm;
58         ApplicationConfig appConfig = context.getApplicationConfig();
59         AlertConfig alertConfig = null;
60         String JavaDoc alertId = request.getParameter(RequestParams.ALERT_ID);
61         if(alertId==null || alertId.equals("")){
62             alertConfig = new AlertConfig(AlertConfig.getNextAlertId(),
63                     form.getAlertName(),
64                     form.getAlertDelivery(),
65                     form.getEmailAddress());
66             alertConfig.setAlertSourceConfig(getAlertSourceConfig(context,form));
67             appConfig.addAlert(alertConfig);
68         }else{
69             alertConfig = appConfig.findAlertById(form.getAlertId());
70             alertConfig.setAlertName(form.getAlertName());
71             alertConfig.setAlertDelivery(form.getAlertDelivery());
72             if(form.getEmailAddress()!=null){
73                 alertConfig.setEmailAddress(form.getEmailAddress());
74             }
75             alertConfig.setAlertSourceConfig(getAlertSourceConfig(context,form));
76         }
77         ApplicationConfigManager.updateApplication(appConfig);
78
79         /* tell the AlertEngine about the new or modified AlertConfig*/
80         AlertEngine.getInstance().updateAlertConfig(alertConfig);
81
82         return mapping.findForward(Forwards.SUCCESS);
83     }
84
85     private AlertSourceConfig getAlertSourceConfig(WebContext context,
86                                                    AlertForm form){
87         AlertSourceConfig sourceConfig = null;
88         Expression expression = new Expression(form.getExpression());
89         if(AlertSourceConfig.SOURCE_TYPE_NOTIFICATION.equals(
90                 form.getAlertSourceType())){
91             sourceConfig = new AlertSourceConfig(expression.getMBeanName(),
92                     expression.getTargetName());
93         }else if(AlertSourceConfig.SOURCE_TYPE_GAUGE_MONITOR.equals(
94                 form.getAlertSourceType())){
95             MBeanService mbeanService = ServiceFactory.getMBeanService();
96             String JavaDoc attributeDataType = mbeanService.getAttributeDataType(
97                     Utils.getServiceContext(context),expression.getTargetName(),
98                     expression.getMBeanName());
99             sourceConfig = new AlertSourceConfig(expression.getMBeanName(),
100                     expression.getTargetName(),
101                     CoreUtils.valueOf(form.getMinAttributeValue(),attributeDataType),
102                     CoreUtils.valueOf(form.getMaxAttributeValue(),attributeDataType),
103                     attributeDataType);
104         }else if(AlertSourceConfig.SOURCE_TYPE_STRING_MONITOR.equals(
105                 form.getAlertSourceType())){
106             sourceConfig = new AlertSourceConfig(expression.getMBeanName(),
107                     expression.getTargetName(), form.getStringAttributeValue());
108
109         }else {
110             assert false: "not supported type";
111         }
112         sourceConfig.setApplicationConfig(context.getApplicationConfig());
113         return sourceConfig;
114     }
115
116
117 }
118
Popular Tags