FileDocCategorySizeDatePackage
ElementChangeEventsFactory.javaAPI DocGlassfish v2 API25747Fri May 04 22:33:34 BST 2007com.sun.enterprise.admin.event

ElementChangeEventsFactory

public abstract class ElementChangeEventsFactory extends Object
ElementChangeEvent listener

Fields Summary
static final String
RESOURCE_REF_TYPE_NAME
static final String
APPLICATION_REF_TYPE_NAME
static final String
MBEAN_TYPE_NAME
Constructors Summary
Methods Summary
private static java.util.ArrayListbuildSelfConstructingEvent(java.lang.String event_type, java.lang.String instanceName, java.lang.String element_type, java.util.ArrayList changeList, com.sun.enterprise.config.ConfigContext ctx)

       try {
            //self-consrtructing event ?
            Class cl = Class.forName(event_type);
            Method m = cl.getMethod("getEventInstances", new Class[]{
                String.class, String.class, String.class,
                ArrayList.class, ConfigContext.class});
            return (ArrayList)m.invoke(null, new Object[]{event_type, instanceName, element_type, changeList, ctx});
        } catch (Exception e) { 
            if( !(e instanceof ClassNotFoundException) &&
                !(e instanceof NoSuchMethodException) )
                throw e;
        }
       return null;
    
private static AuditModuleEventcreateAuditModuleEvent(java.lang.String instanceName, java.lang.String id, int action)

        // create custom event
        if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            action = AuditModuleEvent.ACTION_CREATE;
        else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            action = AuditModuleEvent.ACTION_DELETE;
        else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            action = AuditModuleEvent.ACTION_UPDATE;
        return  new AuditModuleEvent(instanceName, id, action);
    
private static AuthRealmEventcreateAuthRealmEvent(java.lang.String instanceName, java.lang.String id, int action)

        // create custom event
        if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            action = AuthRealmEvent.ACTION_CREATE;
        else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            action = AuthRealmEvent.ACTION_DELETE;
        else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            action = AuthRealmEvent.ACTION_UPDATE;
        return  new AuthRealmEvent(instanceName, id, action);
    
static java.util.ArrayListcreateEvents(java.lang.String event_type, java.lang.String instanceName, java.lang.String element_type, java.util.ArrayList changeList, com.sun.enterprise.config.ConfigContext ctx, java.util.ArrayList globalChangeList)


        int      action          = ElementChangeHelper.getActionCodeForChanges(changeList);
        if(action==ElementChangeEvent.ACTION_ELEMENT_UNDEFINED)
        {
            return null; //ignore wrong type
        }

        ArrayList events = null;
        AdminEvent event = null;
        String   element_xpath   = ElementChangeHelper.getElementXPath(changeList);
        String   targetName      = ElementChangeHelper.getConfigElementTargetName(element_xpath, ctx);
        String   element_id      = ElementChangeHelper.getConfigElementPrimaryKey(element_xpath); //

        // Special treatment for custom MBean
        // only if application-ref changed
        // we will replace "ApplicationDeployEvent" to "MBeanElementChangeEvent" 
        if (event_type.equals(ApplicationDeployEvent.eventType))
        {
            if(APPLICATION_REF_TYPE_NAME.equals(element_type))
            {
                try {
                    if(isMBeanReference(ctx, element_id, globalChangeList))
                    {
                       event_type="com.sun.enterprise.admin.event.MBeanElementChangeEvent";
                       //targetName = TargetType.DOMAIN.getName();
                       events =  buildSelfConstructingEvent(event_type, 
                                instanceName, element_type, changeList, ctx);
                        if(events!=null && 
                           (action==ElementChangeEvent.ACTION_ELEMENT_CREATE ||
                            action==ElementChangeEvent.ACTION_ELEMENT_DELETE ) )
                        {
                            
                            //add application dependent elements
                            String actionCode = 
                              (action==ElementChangeEvent.ACTION_ELEMENT_CREATE)?
                                BaseDeployEvent.DEPLOY:BaseDeployEvent.UNDEPLOY;
                            for(int i=0; i<events.size(); i++ )
                            {
                               AdminEvent ae = (AdminEvent)events.get(i);
                               String effective = ae.getEffectiveDestination();
                               DependencyResolver dr = 
                                       new DependencyResolver(ctx, effective);
                               List list = dr.resolveApplications(element_id, actionCode);
                               ae.addDependentConfigChange(list);
                            }
                        }
                    }
               } catch (Exception ce){
                   //something wrong - leave it for standard proceeding
                   events = null;
               }
           }
        }
            
        //currently it generates only ElementChangeEvents
        events =  buildSelfConstructingEvent(event_type, 
                instanceName, element_type, changeList, ctx);
        if(events!=null)
        {
//            if(MBEAN_TYPE_NAME.equals(element_type) &&
//               action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
//            {
//                //this temporary solution for case of deletion of
//                //mbean only existing in remote instance
//                for(int i=0; i<events.size(); i++)
//                {
//                    AdminEvent ae = (AdminEvent)events.get(i);
//                    //broadcast to all
//                    ae.setTargetDestination("domain");
//                }
//            }

            return events;
        }

        boolean bMustSetChangeList = true; // config changes must be set into resulting event
        //redirect to custom event
        if(event_type.equals(SecurityServiceEvent.eventType))
        {
            event = (AdminEvent)createSecurityServiceEvent(instanceName, element_id, action);
        }
        else if(event_type.equals(AuditModuleEvent.eventType))
        {
            event = (AdminEvent)createAuditModuleEvent(instanceName, element_id, action);
        }
        else if(event_type.equals(AuthRealmEvent.eventType))
        {
            event = (AdminEvent)createAuthRealmEvent(instanceName, element_id, action);
        }
        else if(event_type.equals(LogLevelChangeEvent.eventType))
        {
            events = createLogLevelEvents(instanceName, element_id, action, changeList);
            bMustSetChangeList = false;
        }
        else if(event_type.equals(MonitoringLevelChangeEvent.eventType))
        {
            events = createMonitoringLevelEvents(instanceName, element_id, action, changeList);
            bMustSetChangeList = false;
        }
        else if(event_type.equals(ResourceDeployEvent.eventType))
        {
            event = (AdminEvent)createResourceDeployEvent(instanceName, element_type, element_id, action, changeList, targetName, ctx);
            bMustSetChangeList = false;
        }
        else if(event_type.equals(ModuleDeployEvent.eventType)  ||
               event_type.equals(ApplicationDeployEvent.eventType))
        {
            event = (AdminEvent)createModAppDeployEvent(instanceName, element_type, element_id, action, changeList, targetName, ctx);
            bMustSetChangeList = false;
        }
        else
        {
            return null; 
        }
           
       
       if( event!=null && (events==null || events.size()==0) )
       {
           events = new ArrayList();
           events.add(event);
       }
           
       if( events==null || events.size()==0 )
           return null;
        
       // set desctination target (can be null)
       setTargetDestinationAndConfigChange(events, targetName, changeList, bMustSetChangeList);

       //System.out.println("***********setTargetDestinationAndConfigChange->"+targetName);
       return events;
    
private static java.util.ArrayListcreateLogLevelEvents(java.lang.String instanceName, java.lang.String id, int action, java.util.ArrayList changeList)

        ArrayList events = new ArrayList();
        for(int i=0; i<changeList.size(); i++)
        {
            Object chg = changeList.get(i);

            if (ElementChangeHelper.isPropertyChange((ConfigChange)chg))
            {
                // constructs an event with property changed flag set to true
                LogLevelChangeEvent event=new LogLevelChangeEvent(instanceName);

                // sets the flag
                event.setPropertyChanged(true);

                // sets the property name
                String xpath = ((ConfigChange) chg).getXPath();
                event.setPropertyName( 
                    ElementChangeHelper.getConfigElementPrimaryKey(xpath) );

                // adds the config change objects
                event.addConfigChange((ConfigChange)chg);

                // adds the event to the event list
                events.add(event);
            }
        }

        if (action!=ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            return events;

        for(int i=0; i<changeList.size(); i++)
        {
            Object chg = changeList.get(i);
            if(!(chg instanceof ConfigUpdate) || 
                ElementChangeHelper.isPropertyChange((ConfigChange)chg))
               continue; //what if property changed ? 
            ConfigUpdate update = (ConfigUpdate)chg;
            Set attrs = update.getAttributeSet();
            if (attrs != null) 
            {
                Iterator iter = attrs.iterator();
                while (iter.hasNext()) {
                    String compName = (String)iter.next();
                    String oldValue = update.getOldValue(compName);
                    String newValue = update.getNewValue(compName);
                    LogLevelChangeEvent event = new LogLevelChangeEvent(instanceName);
                    event.setModuleName(compName);
                    event.setOldLogLevel(oldValue);
                    event.setNewLogLevel(newValue);
                    events.add(event);
                    ConfigUpdate upd = new ConfigUpdateImpl(update.getXPath(), 
                            compName, oldValue,newValue);
//System.out.println("***********createLogLevelEvents."+compName +":"+oldValue+"->"+newValue+" target="+instanceName);
                    event.addConfigChange(upd);
                }
            }
        }
        return events;
    
private static BaseDeployEventcreateModAppDeployEvent(java.lang.String instanceName, java.lang.String elemType, java.lang.String id, int action, java.util.ArrayList changeList, java.lang.String targetName, com.sun.enterprise.config.ConfigContext ctx)

        BaseDeployEvent bde = null;
        String actionCode = null;
        if(APPLICATION_REF_TYPE_NAME.equals(elemType))
        {
            //************************************
            // 1. for references' changes
            //************************************
            if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            {
//                actionCode = BaseDeployEvent.DEPLOY;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            {
//                actionCode = BaseDeployEvent.UNDEPLOY;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            {
                Boolean bEnabled = null;
                if (/* changeList.size()==1 && */  
                     (bEnabled=ElementChangeHelper.findEnabledChange(changeList))!=null)
                {
                    actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
                }
                     
//                else
//                    actionCode = BaseDeployEvent.REDEPLOY;
            } 
            EventBuilder builder = new EventBuilder();
            if(actionCode!=null)
                bde = builder.createModAppDeployEvent(actionCode, id, 
                            ctx, changeList, targetName);
        }
        else
        {
            //****************************************
            // now let's try to find if enable updated 
            //****************************************
            // resource change proceeding 
            if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            {
//                    actionCode = BaseDeployEvent.DEPLOY;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            {
//                    actionCode = BaseDeployEvent.UNDEPLOY;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            {
                Boolean bEnabled = null;
                if (changeList.size()==1 && (bEnabled = ElementChangeHelper.findEnabledChange(changeList))!=null) 
                {
                    actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
                }
                else
                    actionCode = BaseDeployEvent.REDEPLOY;
            } 
            EventBuilder builder = new EventBuilder();
            if(actionCode!=null)
                bde = builder.createModAppDeployEvent(actionCode, id, 
                            ctx, changeList, targetName);
        }
        return bde;
    
private static java.util.ArrayListcreateMonitoringLevelEvents(java.lang.String instanceName, java.lang.String id, int action, java.util.ArrayList changeList)

        if (action!=ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            return null;

        ArrayList events = new ArrayList();
        for(int i=0; i<changeList.size(); i++)
        {
            Object chg = changeList.get(i);
            if(!(chg instanceof ConfigUpdate) || 
                ElementChangeHelper.isPropertyChange((ConfigChange)chg))
               continue; //what if property changed ? 
            ConfigUpdate update = (ConfigUpdate)chg;
            Set attrs = update.getAttributeSet();
            if (attrs != null) 
            {
                Iterator iter = attrs.iterator();
                while (iter.hasNext()) {
                    String compName = (String)iter.next();
                    String oldValue = update.getOldValue(compName);
                    String newValue = update.getNewValue(compName);
                    MonitoringLevelChangeEvent event = new MonitoringLevelChangeEvent(instanceName);
                    event.setComponentName(compName);
                    event.setOldMonitoringLevel(oldValue);
                    event.setNewMonitoringLevel(newValue);
                    events.add(event);
                    ConfigUpdate upd = new ConfigUpdateImpl(update.getXPath(), 
                            compName, oldValue,newValue);
                    event.addConfigChange(upd);
                }
            }
        }
        return events;
    
private static ResourceDeployEventcreateResourceDeployEvent(java.lang.String instanceName, java.lang.String elemType, java.lang.String id, int action, java.util.ArrayList changeList, java.lang.String targetName, com.sun.enterprise.config.ConfigContext ctx)

    //******************************************
    // ResourceDeployment events
    //******************************************
         
                    
                  
    
        ResourceDeployEvent rde = null;
        
        // for security map 
        
        if(("security-map").equals(elemType)){
        	String actionCode = null;
        	if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE || 
        			action==ElementChangeEvent.ACTION_ELEMENT_DELETE){
        		
        	actionCode=BaseDeployEvent.REDEPLOY;
        	       	
        	String element_xpath = ElementChangeHelper.getElementXPath(changeList);
        	// get xpath corresponding to the jdbc-connection-pool
        	int endNewXpath = element_xpath.lastIndexOf("security-map");
        	String new_element_xpath= element_xpath.substring(0,endNewXpath-1);
        	id = ElementChangeHelper.getConfigElementPrimaryKey(new_element_xpath); 
        	}
        	
        	EventBuilder builder = new EventBuilder();
            if(actionCode!=null)
                rde = builder.createResourceDeployEvent(actionCode, id, 
                            ctx, changeList, targetName);
            return rde;
        }
        
        //************************************
        // 1. for references' changes
        //************************************
        if(RESOURCE_REF_TYPE_NAME.equals(elemType))
        {
            String actionCode = null;
            // "resource-ref" change proceeding 
            if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            {
                actionCode = BaseDeployEvent.ADD_REFERENCE;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            {
                // adds reference remove event to the stack
                actionCode = BaseDeployEvent.REMOVE_REFERENCE;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            {
                Boolean bEnabled = null;
                if (/* changeList.size()==1 && */  
                     (bEnabled=ElementChangeHelper.findEnabledChange(changeList))!=null)
                {
                    actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
                }
                     
//                else
//                    actionCode = BaseDeployEvent.REDEPLOY;
            } 
            EventBuilder builder = new EventBuilder();
            if(actionCode!=null)
            {
                rde = builder.createResourceDeployEvent(actionCode, id, 
                            ctx, changeList, targetName);
            }
        } else 
//?? should we check all possible resources values
//?? skipped for now        
//??        if(RESOURCE_TYPE_NAME.equals(elemType))
        {
            //************************************
            // 2. for resources' changes
            //************************************
            String actionCode = null;
            // resource change proceeding 
            if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            {
//                actionCode = ResourceDeployEvent.DEPLOY;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            {
//                actionCode = ResourceDeployEvent.UNDEPLOY;
            }
            else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            {
                Boolean bEnabled = null;
                if (changeList.size()==1 && (bEnabled = ElementChangeHelper.findEnabledChange(changeList))!=null) 
                {
                    actionCode = bEnabled.booleanValue()?BaseDeployEvent.ENABLE:BaseDeployEvent.DISABLE;
                }
                else
                    actionCode = BaseDeployEvent.REDEPLOY;
            } 
            EventBuilder builder = new EventBuilder();
            if(actionCode!=null)
                rde = builder.createResourceDeployEvent(actionCode, id, 
                            ctx, changeList, targetName);
        }
        return rde;
    
private static SecurityServiceEventcreateSecurityServiceEvent(java.lang.String instanceName, java.lang.String id, int action)

        // create custom event
        if(action==ElementChangeEvent.ACTION_ELEMENT_CREATE)
            action = SecurityServiceEvent.ACTION_CREATE;
        else if(action==ElementChangeEvent.ACTION_ELEMENT_DELETE)
            action = SecurityServiceEvent.ACTION_DELETE;
        else if(action==ElementChangeEvent.ACTION_ELEMENT_UPDATE)
            action = SecurityServiceEvent.ACTION_UPDATE;
        return new SecurityServiceEvent(instanceName, action);
    
private static booleanisMBeanReference(com.sun.enterprise.config.ConfigContext ctx, java.lang.String element_id, java.util.ArrayList globalChangeList)

        String typeInDomain = null;
        try {
            typeInDomain = ApplicationHelper.getApplicationType(ctx, element_id);
        } catch(Exception e)
        {
        }
        if(typeInDomain!=null)
            return Applications.MBEAN.equals(typeInDomain);
        //here we are only if there is no MBean found in config context
        //let's check globalChangeList
        String toCompare = 
              "/" + ServerTags.MBEAN + 
              "[@" + ServerTags.NAME + "='" + element_id + "']";
        for(int i=0; i<globalChangeList.size(); i++)
        {
            ConfigChange change = (ConfigChange)globalChangeList.get(i);
            String xpath;
            if(change!=null &&
               (xpath=change.getXPath())!=null &&
               xpath.endsWith(toCompare))
               return true;
        }
        return false;
    
private static voidsetTargetDestinationAndConfigChange(java.util.ArrayList events, java.lang.String targetName, java.util.ArrayList changeList, boolean bSetChanges)

        if(events!=null)
        {
            for(int i=0; i<events.size(); i++)
            {
                AdminEvent event = (AdminEvent)events.get(i); 
                event.setTargetDestination(targetName);
                if(bSetChanges && changeList!=null)
                    event.addConfigChange(changeList);
            }
        }