KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.event;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.lang.reflect.Method JavaDoc;
38
39 import com.sun.enterprise.config.serverbeans.ServerTags;
40 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
41 import com.sun.enterprise.config.serverbeans.Applications;
42 import com.sun.enterprise.config.ConfigContext;
43 import com.sun.enterprise.config.ConfigChange;
44 import com.sun.enterprise.config.ConfigUpdate;
45 import com.sun.enterprise.config.impl.ConfigUpdateImpl;
46 import com.sun.enterprise.admin.target.TargetType;
47
48 /**
49  * ElementChangeEvent listener
50  */

51 public abstract class ElementChangeEventsFactory {
52     
53     /*
54      * create ElementChange related event
55      *
56      * @param instanceName source instance name
57      * @param targetName - destination target name (can be null)
58      * @param event_type -
59      * @param changeList
60      * @param ctx
61      */

62     static ArrayList JavaDoc createEvents(
63                 String JavaDoc event_type, String JavaDoc instanceName,
64                 String JavaDoc element_type,
65                 ArrayList JavaDoc changeList, ConfigContext ctx,
66                 ArrayList JavaDoc globalChangeList) throws Exception JavaDoc
67     {
68
69         int action = ElementChangeHelper.getActionCodeForChanges(changeList);
70         if(action==ElementChangeEvent.ACTION_ELEMENT_UNDEFINED)
71         {
72             return null; //ignore wrong type
73
}
74
75         ArrayList JavaDoc events = null;
76         AdminEvent event = null;
77         String JavaDoc element_xpath = ElementChangeHelper.getElementXPath(changeList);
78         String JavaDoc targetName = ElementChangeHelper.getConfigElementTargetName(element_xpath, ctx);
79         String JavaDoc element_id = ElementChangeHelper.getConfigElementPrimaryKey(element_xpath); //
80

81         // Special treatment for custom MBean
82
// only if application-ref changed
83
// we will replace "ApplicationDeployEvent" to "MBeanElementChangeEvent"
84
if (event_type.equals(ApplicationDeployEvent.eventType))
85         {
86             if(APPLICATION_REF_TYPE_NAME.equals(element_type))
87             {
88                 try {
89                     if(isMBeanReference(ctx, element_id, globalChangeList))
90                     {
91                        event_type="com.sun.enterprise.admin.event.MBeanElementChangeEvent";
92                        //targetName = TargetType.DOMAIN.getName();
93
events = buildSelfConstructingEvent(event_type,
94                                 instanceName, element_type, changeList, ctx);
95                         if(events!=null &&
96                            (action==ElementChangeEvent.ACTION_ELEMENT_CREATE ||
97                             action==ElementChangeEvent.ACTION_ELEMENT_DELETE ) )
98                         {
99                             
100                             //add application dependent elements
101
String JavaDoc actionCode =
102                               (action==ElementChangeEvent.ACTION_ELEMENT_CREATE)?
103                                 BaseDeployEvent.DEPLOY:BaseDeployEvent.UNDEPLOY;
104                             for(int i=0; i<events.size(); i++ )
105                             {
106                                AdminEvent ae = (AdminEvent)events.get(i);
107                                String JavaDoc effective = ae.getEffectiveDestination();
108                                DependencyResolver dr =
109                                        new DependencyResolver(ctx, effective);
110                                List JavaDoc list = dr.resolveApplications(element_id, actionCode);
111                                ae.addDependentConfigChange(list);
112                             }
113                         }
114                     }
115                } catch (Exception JavaDoc ce){
116                    //something wrong - leave it for standard proceeding
117
events = null;
118                }
119            }
120         }
121             
122         //currently it generates only ElementChangeEvents
123
events = buildSelfConstructingEvent(event_type,
124                 instanceName, element_type, changeList, ctx);
125         if(events!=null)
126         {
127 // if(MBEAN_TYPE_NAME.equals(element_type) &&
128
// action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
129
// {
130
// //this temporary solution for case of deletion of
131
// //mbean only existing in remote instance
132
// for(int i=0; i<events.size(); i++)
133
// {
134
// AdminEvent ae = (AdminEvent)events.get(i);
135
// //broadcast to all
136
// ae.setTargetDestination("domain");
137
// }
138
// }
139

140             return events;
141         }
142
143         boolean bMustSetChangeList = true; // config changes must be set into resulting event
144
//redirect to custom event
145
if(event_type.equals(SecurityServiceEvent.eventType))
146         {
147             event = (AdminEvent)createSecurityServiceEvent(instanceName, element_id, action);
148         }
149         else if(event_type.equals(AuditModuleEvent.eventType))
150         {
151             event = (AdminEvent)createAuditModuleEvent(instanceName, element_id, action);
152         }
153         else if(event_type.equals(AuthRealmEvent.eventType))
154         {
155             event = (AdminEvent)createAuthRealmEvent(instanceName, element_id, action);
156         }
157         else if(event_type.equals(LogLevelChangeEvent.eventType))
158         {
159             events = createLogLevelEvents(instanceName, element_id, action, changeList);
160             bMustSetChangeList = false;
161         }
162         else if(event_type.equals(MonitoringLevelChangeEvent.eventType))
163         {
164             events = createMonitoringLevelEvents(instanceName, element_id, action, changeList);
165             bMustSetChangeList = false;
166         }
167         else if(event_type.equals(ResourceDeployEvent.eventType))
168         {
169             event = (AdminEvent)createResourceDeployEvent(instanceName, element_type, element_id, action, changeList, targetName, ctx);
170             bMustSetChangeList = false;
171         }
172         else if(event_type.equals(ModuleDeployEvent.eventType) ||
173                event_type.equals(ApplicationDeployEvent.eventType))
174         {
175             event = (AdminEvent)createModAppDeployEvent(instanceName, element_type, element_id, action, changeList, targetName, ctx);
176             bMustSetChangeList = false;
177         }
178         else
179         {
180             return null;
181         }
182            
183        
184        if( event!=null && (events==null || events.size()==0) )
185        {
186            events = new ArrayList JavaDoc();
187            events.add(event);
188        }
189            
190        if( events==null || events.size()==0 )
191            return null;
192         
193        // set desctination target (can be null)
194
setTargetDestinationAndConfigChange(events, targetName, changeList, bMustSetChangeList);
195
196        //System.out.println("***********setTargetDestinationAndConfigChange->"+targetName);
197
return events;
198     }
199     
200 //=============================================================================
201
static private ArrayList JavaDoc buildSelfConstructingEvent(
202                 String JavaDoc event_type,
203                 String JavaDoc instanceName,
204                 String JavaDoc element_type,
205                 ArrayList JavaDoc changeList,
206                 ConfigContext ctx) throws Exception JavaDoc
207     {
208        try {
209             //self-consrtructing event ?
210
Class JavaDoc cl = Class.forName(event_type);
211             Method JavaDoc m = cl.getMethod("getEventInstances", new Class JavaDoc[]{
212                 String JavaDoc.class, String JavaDoc.class, String JavaDoc.class,
213                 ArrayList JavaDoc.class, ConfigContext.class});
214             return (ArrayList JavaDoc)m.invoke(null, new Object JavaDoc[]{event_type, instanceName, element_type, changeList, ctx});
215         } catch (Exception JavaDoc e) {
216             if( !(e instanceof ClassNotFoundException JavaDoc) &&
217                 !(e instanceof NoSuchMethodException JavaDoc) )
218                 throw e;
219         }
220        return null;
221     }
222
223 //=============================================================================
224
static private boolean isMBeanReference(
225                 ConfigContext ctx,
226                 String JavaDoc element_id,
227                 ArrayList JavaDoc globalChangeList)
228     {
229         String JavaDoc typeInDomain = null;
230         try {
231             typeInDomain = ApplicationHelper.getApplicationType(ctx, element_id);
232         } catch(Exception JavaDoc e)
233         {
234         }
235         if(typeInDomain!=null)
236             return Applications.MBEAN.equals(typeInDomain);
237         //here we are only if there is no MBean found in config context
238
//let's check globalChangeList
239
String JavaDoc toCompare =
240               "/" + ServerTags.MBEAN +
241               "[@" + ServerTags.NAME + "='" + element_id + "']";
242         for(int i=0; i<globalChangeList.size(); i++)
243         {
244             ConfigChange change = (ConfigChange)globalChangeList.get(i);
245             String JavaDoc xpath;
246             if(change!=null &&
247                (xpath=change.getXPath())!=null &&
248                xpath.endsWith(toCompare))
249                return true;
250         }
251         return false;
252     }
253                         
254 //=============================================================================
255
static private void setTargetDestinationAndConfigChange(ArrayList JavaDoc events, String JavaDoc targetName, ArrayList JavaDoc changeList, boolean bSetChanges)
256     {
257         if(events!=null)
258         {
259             for(int i=0; i<events.size(); i++)
260             {
261                 AdminEvent event = (AdminEvent)events.get(i);
262                 event.setTargetDestination(targetName);
263                 if(bSetChanges && changeList!=null)
264                     event.addConfigChange(changeList);
265             }
266         }
267     }
268     
269 //=============================================================================
270
//ELEMENT CREATORS
271

272     //******************************************
273
// security-service
274
//******************************************
275
static private SecurityServiceEvent createSecurityServiceEvent(String JavaDoc instanceName, String JavaDoc id, int action)
276     {
277         // create custom event
278
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
279             action = SecurityServiceEvent.ACTION_CREATE;
280         else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
281             action = SecurityServiceEvent.ACTION_DELETE;
282         else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
283             action = SecurityServiceEvent.ACTION_UPDATE;
284         return new SecurityServiceEvent(instanceName, action);
285     }
286     
287     //******************************************
288
// Audit Module
289
//******************************************
290
static private AuditModuleEvent createAuditModuleEvent(String JavaDoc instanceName, String JavaDoc id, int action)
291     {
292         // create custom event
293
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
294             action = AuditModuleEvent.ACTION_CREATE;
295         else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
296             action = AuditModuleEvent.ACTION_DELETE;
297         else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
298             action = AuditModuleEvent.ACTION_UPDATE;
299         return new AuditModuleEvent(instanceName, id, action);
300     }
301     
302     //******************************************
303
// Auth Realm
304
//******************************************
305
static private AuthRealmEvent createAuthRealmEvent(String JavaDoc instanceName, String JavaDoc id, int action)
306     {
307         // create custom event
308
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
309             action = AuthRealmEvent.ACTION_CREATE;
310         else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
311             action = AuthRealmEvent.ACTION_DELETE;
312         else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
313             action = AuthRealmEvent.ACTION_UPDATE;
314         return new AuthRealmEvent(instanceName, id, action);
315     }
316     
317     //******************************************
318
// Log Level change event
319
//******************************************
320
static private ArrayList JavaDoc createLogLevelEvents(String JavaDoc instanceName, String JavaDoc id, int action, ArrayList JavaDoc changeList)
321     {
322         ArrayList JavaDoc events = new ArrayList JavaDoc();
323         for(int i=0; i<changeList.size(); i++)
324         {
325             Object JavaDoc chg = changeList.get(i);
326
327             if (ElementChangeHelper.isPropertyChange((ConfigChange)chg))
328             {
329                 // constructs an event with property changed flag set to true
330
LogLevelChangeEvent event=new LogLevelChangeEvent(instanceName);
331
332                 // sets the flag
333
event.setPropertyChanged(true);
334
335                 // sets the property name
336
String JavaDoc xpath = ((ConfigChange) chg).getXPath();
337                 event.setPropertyName(
338                     ElementChangeHelper.getConfigElementPrimaryKey(xpath) );
339
340                 // adds the config change objects
341
event.addConfigChange((ConfigChange)chg);
342
343                 // adds the event to the event list
344
events.add(event);
345             }
346         }
347
348         if (action!=ElementChangeEvent.ACTION_ELEMENT_UPDATE)
349             return events;
350
351         for(int i=0; i<changeList.size(); i++)
352         {
353             Object JavaDoc chg = changeList.get(i);
354             if(!(chg instanceof ConfigUpdate) ||
355                 ElementChangeHelper.isPropertyChange((ConfigChange)chg))
356                continue; //what if property changed ?
357
ConfigUpdate update = (ConfigUpdate)chg;
358             Set JavaDoc attrs = update.getAttributeSet();
359             if (attrs != null)
360             {
361                 Iterator JavaDoc iter = attrs.iterator();
362                 while (iter.hasNext()) {
363                     String JavaDoc compName = (String JavaDoc)iter.next();
364                     String JavaDoc oldValue = update.getOldValue(compName);
365                     String JavaDoc newValue = update.getNewValue(compName);
366                     LogLevelChangeEvent event = new LogLevelChangeEvent(instanceName);
367                     event.setModuleName(compName);
368                     event.setOldLogLevel(oldValue);
369                     event.setNewLogLevel(newValue);
370                     events.add(event);
371                     ConfigUpdate upd = new ConfigUpdateImpl(update.getXPath(),
372                             compName, oldValue,newValue);
373 //System.out.println("***********createLogLevelEvents."+compName +":"+oldValue+"->"+newValue+" target="+instanceName);
374
event.addConfigChange(upd);
375                 }
376             }
377         }
378         return events;
379     }
380
381     //******************************************
382
// Monitoring Level Events change event
383
//******************************************
384
static private ArrayList JavaDoc createMonitoringLevelEvents(String JavaDoc instanceName, String JavaDoc id, int action, ArrayList JavaDoc changeList)
385     {
386         if (action!=ElementChangeEvent.ACTION_ELEMENT_UPDATE)
387             return null;
388
389         ArrayList JavaDoc events = new ArrayList JavaDoc();
390         for(int i=0; i<changeList.size(); i++)
391         {
392             Object JavaDoc chg = changeList.get(i);
393             if(!(chg instanceof ConfigUpdate) ||
394                 ElementChangeHelper.isPropertyChange((ConfigChange)chg))
395                continue; //what if property changed ?
396
ConfigUpdate update = (ConfigUpdate)chg;
397             Set JavaDoc attrs = update.getAttributeSet();
398             if (attrs != null)
399             {
400                 Iterator JavaDoc iter = attrs.iterator();
401                 while (iter.hasNext()) {
402                     String JavaDoc compName = (String JavaDoc)iter.next();
403                     String JavaDoc oldValue = update.getOldValue(compName);
404                     String JavaDoc newValue = update.getNewValue(compName);
405                     MonitoringLevelChangeEvent event = new MonitoringLevelChangeEvent(instanceName);
406                     event.setComponentName(compName);
407                     event.setOldMonitoringLevel(oldValue);
408                     event.setNewMonitoringLevel(newValue);
409                     events.add(event);
410                     ConfigUpdate upd = new ConfigUpdateImpl(update.getXPath(),
411                             compName, oldValue,newValue);
412                     event.addConfigChange(upd);
413                 }
414             }
415         }
416         return events;
417     }
418
419     static final String JavaDoc RESOURCE_REF_TYPE_NAME = ServerTags.RESOURCE_REF;
420     static final String JavaDoc APPLICATION_REF_TYPE_NAME = ServerTags.APPLICATION_REF;
421     static final String JavaDoc MBEAN_TYPE_NAME = ServerTags.MBEAN;
422     //******************************************
423
// ResourceDeployment events
424
//******************************************
425
static private ResourceDeployEvent createResourceDeployEvent(String JavaDoc instanceName,
426             String JavaDoc elemType, String JavaDoc id, int action, ArrayList JavaDoc changeList,
427             String JavaDoc targetName, ConfigContext ctx) throws Exception JavaDoc
428     {
429         ResourceDeployEvent rde = null;
430         
431         // for security map
432

433         if(("security-map").equals(elemType)){
434             String JavaDoc actionCode = null;
435             if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE ||
436                     action==ElementChangeEvent.ACTION_ELEMENT_DELETE){
437                 
438             actionCode=BaseDeployEvent.REDEPLOY;
439                     
440             String JavaDoc element_xpath = ElementChangeHelper.getElementXPath(changeList);
441             // get xpath corresponding to the jdbc-connection-pool
442
int endNewXpath = element_xpath.lastIndexOf("security-map");
443             String JavaDoc new_element_xpath= element_xpath.substring(0,endNewXpath-1);
444             id = ElementChangeHelper.getConfigElementPrimaryKey(new_element_xpath);
445             }
446             
447             EventBuilder builder = new EventBuilder();
448             if(actionCode!=null)
449                 rde = builder.createResourceDeployEvent(actionCode, id,
450                             ctx, changeList, targetName);
451             return rde;
452         }
453         
454         //************************************
455
// 1. for references' changes
456
//************************************
457
if(RESOURCE_REF_TYPE_NAME.equals(elemType))
458         {
459             String JavaDoc actionCode = null;
460             // "resource-ref" change proceeding
461
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
462             {
463                 actionCode = BaseDeployEvent.ADD_REFERENCE;
464             }
465             else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
466             {
467                 // adds reference remove event to the stack
468
actionCode = BaseDeployEvent.REMOVE_REFERENCE;
469             }
470             else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
471             {
472                 Boolean JavaDoc bEnabled = null;
473                 if (/* changeList.size()==1 && */
474                      (bEnabled=ElementChangeHelper.findEnabledChange(changeList))!=null)
475                 {
476                     actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
477                 }
478                      
479 // else
480
// actionCode = BaseDeployEvent.REDEPLOY;
481
}
482             EventBuilder builder = new EventBuilder();
483             if(actionCode!=null)
484             {
485                 rde = builder.createResourceDeployEvent(actionCode, id,
486                             ctx, changeList, targetName);
487             }
488         } else
489 //?? should we check all possible resources values
490
//?? skipped for now
491
//?? if(RESOURCE_TYPE_NAME.equals(elemType))
492
{
493             //************************************
494
// 2. for resources' changes
495
//************************************
496
String JavaDoc actionCode = null;
497             // resource change proceeding
498
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
499             {
500 // actionCode = ResourceDeployEvent.DEPLOY;
501
}
502             else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
503             {
504 // actionCode = ResourceDeployEvent.UNDEPLOY;
505
}
506             else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
507             {
508                 Boolean JavaDoc bEnabled = null;
509                 if (changeList.size()==1 && (bEnabled = ElementChangeHelper.findEnabledChange(changeList))!=null)
510                 {
511                     actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
512                 }
513                 else
514                     actionCode = BaseDeployEvent.REDEPLOY;
515             }
516             EventBuilder builder = new EventBuilder();
517             if(actionCode!=null)
518                 rde = builder.createResourceDeployEvent(actionCode, id,
519                             ctx, changeList, targetName);
520         }
521         return rde;
522     }
523
524     //******************************************
525
// ModuleDeployment events
526
//******************************************
527
static private BaseDeployEvent createModAppDeployEvent(String JavaDoc instanceName,
528             String JavaDoc elemType, String JavaDoc id, int action, ArrayList JavaDoc changeList,
529             String JavaDoc targetName, ConfigContext ctx) throws Exception JavaDoc
530     {
531         BaseDeployEvent bde = null;
532         String JavaDoc actionCode = null;
533         if(APPLICATION_REF_TYPE_NAME.equals(elemType))
534         {
535             //************************************
536
// 1. for references' changes
537
//************************************
538
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
539             {
540 // actionCode = BaseDeployEvent.DEPLOY;
541
}
542             else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
543             {
544 // actionCode = BaseDeployEvent.UNDEPLOY;
545
}
546             else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
547             {
548                 Boolean JavaDoc bEnabled = null;
549                 if (/* changeList.size()==1 && */
550                      (bEnabled=ElementChangeHelper.findEnabledChange(changeList))!=null)
551                 {
552                     actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
553                 }
554                      
555 // else
556
// actionCode = BaseDeployEvent.REDEPLOY;
557
}
558             EventBuilder builder = new EventBuilder();
559             if(actionCode!=null)
560                 bde = builder.createModAppDeployEvent(actionCode, id,
561                             ctx, changeList, targetName);
562         }
563         else
564         {
565             //****************************************
566
// now let's try to find if enable updated
567
//****************************************
568
// resource change proceeding
569
if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
570             {
571 // actionCode = BaseDeployEvent.DEPLOY;
572
}
573             else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
574             {
575 // actionCode = BaseDeployEvent.UNDEPLOY;
576
}
577             else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
578             {
579                 Boolean JavaDoc bEnabled = null;
580                 if (changeList.size()==1 && (bEnabled = ElementChangeHelper.findEnabledChange(changeList))!=null)
581                 {
582                     actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
583                 }
584                 else
585                     actionCode = BaseDeployEvent.REDEPLOY;
586             }
587             EventBuilder builder = new EventBuilder();
588             if(actionCode!=null)
589                 bde = builder.createModAppDeployEvent(actionCode, id,
590                             ctx, changeList, targetName);
591         }
592         return bde;
593     }
594
595 }
596
Popular Tags