KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > event > ElementChangeEvent


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.event;
25
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.util.ArrayList JavaDoc;
28
29 import com.sun.enterprise.admin.event.AdminEvent;
30 import com.sun.enterprise.config.ConfigContext;
31 import com.sun.enterprise.config.ConfigChange;
32 import com.sun.enterprise.config.ConfigSet;
33 import com.sun.enterprise.config.ConfigAdd;
34 import com.sun.enterprise.config.ConfigUpdate;
35 import com.sun.enterprise.config.ConfigDelete;
36
37
38 //i18n import
39
import com.sun.enterprise.util.i18n.StringManager;
40
41 /**
42  * Config Element Change Event - emitted by DAS after comletion of
43  * create/delete/update operation on domain.xml element (node)
44  * only for those elements which have nonempty "ChangeListener" field
45  * in correspondent descriptor entry.
46  * Contains element(mbean) type, id (primary key value), and action type.
47  */

48 public class ElementChangeEvent extends AdminEvent {
49
50     /**
51      * Constant denoting action code
52      */

53     public static final int ACTION_ELEMENT_UNDEFINED = 0;
54     
55     public static final int ACTION_ELEMENT_CREATE = 1;
56     public static final int ACTION_ELEMENT_DELETE = 2;
57     public static final int ACTION_ELEMENT_UPDATE = 3;
58
59     /**
60      * Event type
61      */

62     static final String JavaDoc eventType = ElementChangeEvent.class.getName();
63
64     /**
65      * Attributes
66      */

67     private int change_action = ACTION_ELEMENT_UNDEFINED;
68     private String JavaDoc element_id = null;
69     private String JavaDoc element_type = null; //type from mbean descritors file
70
private String JavaDoc element_path = null;
71
72     // i18n StringManager
73
private static StringManager localStrings = StringManager.getManager( ElementChangeEvent.class );
74
75     /* *****************************************************
76      * default ElementChange events factory
77      * @param event_type - event type from mbean decriptor xml file
78      * @param instanceName
79      * @param changeList
80      * @param ctx
81      */

82     public static ArrayList JavaDoc getEventInstances(
83                 String JavaDoc event_type, String JavaDoc instanceName, String JavaDoc elementType,
84                 ArrayList JavaDoc changeList, ConfigContext ctx) throws Exception JavaDoc
85     {
86         int action = ElementChangeHelper.getActionCodeForChanges(changeList);
87         if(action==ElementChangeEvent.ACTION_ELEMENT_UNDEFINED)
88             return null; //ignore wrong type
89
String JavaDoc element_xpath = ElementChangeHelper.getElementXPath(changeList);
90         String JavaDoc element_id = ElementChangeHelper.getConfigElementPrimaryKey(element_xpath);
91
92         AdminEvent event = null;
93         if(event_type.equals(eventType))
94         {
95             //ElementChangeEvent ?
96
event = new ElementChangeEvent(instanceName, event_type, action, element_id);
97         }
98         else
99         {
100             //let's try construct
101
try {
102                 Class JavaDoc cl = Class.forName(event_type);
103                 Constructor JavaDoc contr;
104                 try {
105                     // unnamed element event ?
106
contr = cl.getConstructor(
107                       new Class JavaDoc[]{String JavaDoc.class, Integer.TYPE});
108                     event = (AdminEvent)contr.newInstance(
109                       new Object JavaDoc[]{instanceName, new Integer JavaDoc(action)});
110                 } catch (Exception JavaDoc e) {
111                 }
112                 
113                 if(event==null)
114                 {
115                     //maybe named
116
contr = cl.getConstructor(
117                         new Class JavaDoc[]{String JavaDoc.class, Integer.TYPE, String JavaDoc.class});
118                     event = (AdminEvent)contr.newInstance(
119                         new Object JavaDoc[]{instanceName, new Integer JavaDoc(action), element_id});
120                 }
121                 if(event!=null)
122                     event.addConfigChange(changeList);
123             } catch (Exception JavaDoc e) {
124                 //throw e;
125
return null; //?
126
}
127         }
128         if(event!=null)
129         {
130             String JavaDoc targetName = ElementChangeHelper.getConfigElementTargetName(element_xpath, ctx);
131             event.setTargetDestination(targetName);
132             ArrayList JavaDoc events = new ArrayList JavaDoc();
133             events.add(event);
134             return events;
135         }
136         return null;
137     }
138     
139     //*****************************************************
140
/* public String toString()
141     {
142         int s = 0;
143         if (this.getConfigChangeList() != null ) {
144             s = this.getConfigChangeList().size();
145         }
146
147         return "\nEvent type:" + eventType +
148                      "\nelement_id="+element_id +
149                      "\nchange_action=" + change_action +
150                      "\nchangeList.size()="+ s;
151         
152     }
153 */

154     //*****************************************************
155
// CONSTRUCTORS
156
//*****************************************************
157
public ElementChangeEvent(String JavaDoc instance, String JavaDoc evtType, int actionCode, String JavaDoc elementId)
158     {
159         super(evtType, instance);
160
161         //set element's id(primary key value) [can be null]
162
element_id = elementId;
163         //validate and set action code
164
setAction(actionCode);
165     }
166
167     //*****************************************************
168
// OVERRIDINGS
169
//*****************************************************
170
/*
171      * overriding the parent's class method
172      *
173      * Add specified changes to the event.
174      * @param changeList the list of changes to add to this event
175      */

176     public void addConfigChange(ArrayList JavaDoc changeList)
177     {
178         // only initial changes add allowed
179
if(getConfigChangeList()!=null)
180         {
181             String JavaDoc msg = localStrings.getString( "admin.event.wrong_configchange" );
182             throw new IllegalArgumentException JavaDoc( msg );
183         }
184         /* commented for now to avoid double check
185          if(ElementChangeHelpe.checkChangeListForElement(changeList);
186         {
187             String msg = localStrings.getString( "admin.event.wrong_configchange" );
188             throw new IllegalArgumentException( msg );
189         }*/

190         super.addConfigChange(changeList);
191     }
192
193     //*************************************************************************
194
// PUBLIC METHODS
195
//*************************************************************************
196

197     /**
198      * get element's xpath
199      */

200     public String JavaDoc getElementXPath()
201     {
202         return ElementChangeHelper.getElementXPath(this.getConfigChangeList());
203     }
204
205     /**
206      * Get element's id(primary key value)
207      */

208     public String JavaDoc getElementId()
209     {
210         return this.element_id;
211     }
212
213     /**
214      * Get action type for this event.
215      */

216     public int getActionType()
217     {
218         return change_action;
219     }
220
221     //*************************************************************************
222
// PRIVATE METHODS
223
//*************************************************************************
224

225     /**
226      * Set action to specified value. If action is not one of allowed,
227      * then IllegalArgumentException is thrown.
228      * @throws IllegalArgumentException if action is invalid
229      */

230     private void setAction(int action)
231     {
232         boolean valid = false;
233         if (action==ACTION_ELEMENT_CREATE ||
234             action==ACTION_ELEMENT_DELETE ||
235             action==ACTION_ELEMENT_UPDATE )
236             valid = true;
237         if (!valid) {
238             String JavaDoc msg = localStrings.getString( "admin.event.invalid_action", ""+action );
239             throw new IllegalArgumentException JavaDoc( msg );
240         }
241         this.change_action = action;
242     }
243
244 }
245
Popular Tags