FileDocCategorySizeDatePackage
AddonConfigurationController.javaAPI DocGlassfish v2 API17411Mon Aug 13 13:18:22 BST 2007com.sun.enterprise.addons

AddonConfigurationController

public class AddonConfigurationController extends AddonInstallationController
Controller class that invoke configurator api. It is always called either before DAS starts up or before a remote instance starts up.
see
com.sun.appserv.addons.Configurator.
since
9.1
authod
binod@dev.java.net

Fields Summary
private final String
SERVICEINTERFACE
private final String
METAINFSERVICE
private AddonRegistry
ar
private com.sun.appserv.management.DomainRoot
amxDomainRoot
Constructors Summary
Methods Summary
private voidcheckDependencies(java.lang.String addonName)

        try {
            JarFile jar = AddOnUtils.getAddonJarFile(addonName,
                                getServiceJarLocation(), getFilenameFilter());
            if (jar == null) return;
            String [] dependencies = AddOnUtils.getDependencies(jar);
            if (dependencies == null) return;
            for (String dependency: dependencies) {
                if (ar.getStatus(dependency).equals(AddonRegistry.status.CONFIGURE)) {
                    throw new AddonFatalException
                    (localStrings.getString("addon.dependency.missing", addonName, dependency));
                }
            }
        } catch (Exception e) {
            throw new AddonFatalException(e);
        }
    
private voidconfigure(java.io.File installRoot, java.io.File domainRoot, ConfigurationContext.ConfigurationType type)
Private method that actually invokes the configurator.


        setup(installRoot, domainRoot, type);
        loadServices(getServiceJarLocation());

        Set<Map.Entry<Object, String>> mainBasedServices =
           getMapSortedByValue(getMainClassBasedServices()).entrySet();
        for (Map.Entry<Object, String> entry : mainBasedServices) {
            invokeMain(entry.getKey(), entry.getValue(), type);
        }

        Set<Map.Entry<Object, String>> apiBasedServices =
           getMapSortedByValue(getApiBasedServices()).entrySet();
        for (Map.Entry<Object, String> entry : apiBasedServices) {
            try {
                invokeApi(entry.getKey(), entry.getValue(), type);
            } catch (Exception e) {
                getLogger().warning
                (localStrings.getString("addon.configurationcomplete.error", 
                "configure", entry.getKey(), e.getLocalizedMessage() ));
            } finally {
                ar.store();
                ar.close();
            }
        }

        ar.store();
        ar.close();
    
public voidconfigureDAS(java.io.File installRoot, java.io.File domainRoot)
Invoke the configurator plugin just before starting DAS.


        if (getLogger().isLoggable(Level.FINER)) {
            getLogger().log
            (Level.FINER, "InstallRoot for configureDAS :" + installRoot);
            getLogger().log
            (Level.FINER, "DomainRoot for configureDAS :" + domainRoot);
        }

        configure(installRoot, domainRoot, 
                  ConfigurationContext.ConfigurationType.DAS);
    
public voidconfigureInstances(java.io.File installRoot, java.io.File domainRoot)
Invoke the configurator plugin just before starting a remote instance.

        configure(installRoot, domainRoot, 
                  ConfigurationContext.ConfigurationType.INSTANCE);
    
private com.sun.appserv.management.DomainRootgetAMXDomainRoot()

        if (this.amxDomainRoot == null)
            setAMXDomainRoot();
        return this.amxDomainRoot;
    
protected java.io.FilenameFiltergetFilenameFilter()
Return a file filter that returns all configurator plugins whose state has been modified in the domain-registry.

        return new ConfigurableJarFileFilter(ar);
    
private java.util.HashMapgetMapSortedByValue(java.util.Map hmap)

        HashMap map = new LinkedHashMap();
        if ((hmap == null) || (hmap.size() < 1)) {
            return map;
        }
        List mapKeys = new ArrayList(hmap.keySet());
        List mapValues = new ArrayList(hmap.values());
        hmap.clear();
        TreeSet sortedSet = new TreeSet(mapValues);
        Object[] sortedArray = sortedSet.toArray();
        int size = sortedArray.length;
        // Ascending sort
        for (int i=0; i<size; i++) {
            map.put(mapKeys.get(mapValues.indexOf(sortedArray[i])), sortedArray[i]);
        }
        return map;
    
protected java.lang.StringgetName(java.io.File f)
Retrieve the addon name. It is the file name without .jar extension.

        String name = f.getName();
        String regex = "\\.([j|J][a|A][r|R])";

        //Match any thing that ends with .jar
        if (name.matches(".*"+regex)) {
            return name.split(regex)[0];
        } else {
            throw new AddonFatalException
            (localStrings.getString("addon.notjarfile", name));
        }
    
protected java.lang.StringgetServiceInterface()
Return the service interface. It is META-INF/services/com.sun.appserv.addons.Configurator

        return METAINFSERVICE + SERVICEINTERFACE;
    
protected java.io.FilegetServiceJarLocation()
Return the location of the configuration plugins. It is AS_HOME/lib/addons folder.


                    
       
        return new File(new File(getInstallRoot(),"lib"), "addons");
    
private voidinvokeApi(java.lang.Object obj, java.lang.String addonName, ConfigurationContext.ConfigurationType type)
Invokes the com.sun.appserv.Configurator api.


        AddonRegistry.status addonStatus = ar.getStatus(addonName);
        if (getLogger().isLoggable(Level.FINER)) {
             getLogger().finer("Starting " + addonStatus + " on " + addonName);
        }

        // check for upgrade
        AddonVersionImpl newVersion, oldVersion;
        try {
            newVersion = new AddonVersionImpl(addonName);
            oldVersion = ar.getOldVersion(newVersion);
            if (oldVersion != null) {
                if (oldVersion.isHigher(newVersion)) {
                    addonStatus = AddonRegistry.status.UPGRADE;
                } else if (oldVersion.isLower(newVersion)) {
                    addonStatus = AddonRegistry.status.UNCHANGED;
                }
            }
        } catch (AddonException aoe) {
            throw new AddonFatalException(aoe);
        }

        Configurator conf = Configurator.class.cast(obj);
        InstallationContext ic = new InstallationContext();
        ic.setInstallationDirectory(getInstallRoot());
        ConfigurationContext cc = new ConfigurationContext();
        cc.setInstallationContext(ic);
        cc.setDomainDirectory(getDomainRoot());
        cc.setConfigurationType(type);
        if (type.equals(ConfigurationContext.ConfigurationType.INSTANCE))
            cc.setAMXDomainRoot(getAMXDomainRoot());
        setAdminCredentials(cc);
        try {
            switch (addonStatus) {
                case CONFIGURE :
                    checkDependencies(addonName);
                    conf.configure(cc);
                    ar.setStatus(addonName, AddonRegistry.status.CONFIGURE);
                    ar.setStatus(addonName, AddonRegistry.status.ENABLE);
                    break;
                case ENABLE :
                    conf.enable(cc);
                    ar.setStatus(addonName, AddonRegistry.status.ENABLE);
                    break;
                case DISABLE :
                    conf.disable(cc);
                    ar.setStatus(addonName, AddonRegistry.status.DISABLE);
                    break;
                case UNCONFIGURE :
                    conf.unconfigure(cc);
                    ar.setStatus(addonName, AddonRegistry.status.UNCONFIGURE);
                    break;
                case UPGRADE :
                    ar.setStatus(oldVersion.getName(), AddonRegistry.status.REMOVE);
                    conf.upgrade(cc, oldVersion);
                    ar.setStatus(addonName, AddonRegistry.status.CONFIGURE);
                    ar.setStatus(addonName, AddonRegistry.status.ENABLE);
                    // move old component to .deleted directory
                    break;
                default :
            }
            if (getLogger().isLoggable(Level.INFO)) {
                if (! addonStatus.equals(AddonRegistry.status.UNCHANGED)) {
                    String s = localStrings.getString("addon.configurecomplete",
                           addonStatus.toString(), addonName);
                    getLogger().info(s);
                }
            }
        } catch (AddonFatalException afe) {
            getLogger().log(Level.FINE, "Fatal Exception while " +
            "configuring the addon " + addonName, afe);
            throw afe;
        } catch (AddonException ae) {
            getLogger().warning
            (localStrings.getString("addon.configurationcomplete.error", 
             addonStatus.toString(), addonName, ae.getLocalizedMessage() ));
        } catch (Exception e) {
            getLogger().log(Level.FINE, "Fatal Exception while " +
            "configuring the addon " + addonName, e);
            throw new AddonFatalException(e);
        }
    
private voidinvokeMain(java.lang.Object obj, java.lang.String addonName, ConfigurationContext.ConfigurationType type)
Invoke main method of the configurator plugin. New plugins will be invoked using the api.

        AddonRegistry.status addonStatus = ar.getStatus(addonName);
        if (addonStatus.equals(AddonRegistry.status.CONFIGURE)) {
            if (getLogger().isLoggable(Level.FINER)) {
                getLogger().finer("Starting " + addonStatus + " on " + addonName);
            }

            try {
                Method m = obj.getClass().getMethod("main", 
                       new Class[]{String[].class});
                String[] args = new String[] {getInstallRoot().
                getCanonicalPath(),getDomainRoot().getCanonicalPath()};
                m.invoke(obj, new Object[] {args});
                ar.setStatus(addonName, AddonRegistry.status.CONFIGURE);
                ar.setStatus(addonName, AddonRegistry.status.ENABLE);
                if (getLogger().isLoggable(Level.INFO)) {
                    String s = localStrings.getString("addon.configurecomplete",
                                addonStatus.toString(), addonName);
                    getLogger().info(s);
                }
            } catch (Exception e) {
                getLogger().log(Level.FINE, "Fatal Exception while " + 
                "configuring the addon " + addonName, e);
                throw new AddonFatalException (e);
            }
        }
    
private voidsetAMXDomainRoot()

        try {
            MBeanServer mbs = MBeanServerFactory.getMBeanServer();
            this.amxDomainRoot = ProxyFactory.getInstance(mbs).getDomainRoot();
        } catch (Exception e) {
            throw new AddonFatalException(e);
        }
    
private voidsetup(java.io.File installRoot, java.io.File domainRoot, ConfigurationContext.ConfigurationType type)

        setInstallRoot(installRoot);
        setDomainRoot(domainRoot);
        if (type.equals(ConfigurationContext.ConfigurationType.DAS)) {
            ar = new AddonRegistry(domainRoot, getLogger());
        } else {
            ar = new AddonInstanceRegistry(domainRoot, getLogger());
        }
    
private voidunconfigure(java.io.File installRoot, java.io.File domainRoot, ConfigurationContext.ConfigurationType type)
Private method that actually invokes the configurator.


        setup(installRoot, domainRoot, type);

        File deletedDir = new File(getServiceJarLocation() + File.separator + ".deleted");

        if (! deletedDir.exists()) return;

        loadServices(deletedDir);

        Set<Map.Entry<Object, String>> apiBasedServices =
           getMapSortedByValue(getApiBasedServices()).entrySet();

        AddonRegistry.status addonStatus;
        String addonName;
        for (Map.Entry<Object, String> entry : apiBasedServices) {
            addonName = entry.getValue();
            if (! ar.isInRegistry(addonName)) continue;
            if (ar.isUnConfigurationRequired(addonName)) {
                Configurator conf = Configurator.class.cast(entry.getKey());
                InstallationContext ic = new InstallationContext();
                ic.setInstallationDirectory(getInstallRoot());
                ConfigurationContext cc = new ConfigurationContext();
                cc.setInstallationContext(ic);
                cc.setDomainDirectory(getDomainRoot());
                cc.setConfigurationType(type);
                try {
                    conf.unconfigure(cc);
                    ar.setStatus(addonName, AddonRegistry.status.REMOVE);
                } catch (AddonFatalException afe) {
                    getLogger().log(Level.SEVERE, "Fatal Exception while " +
                    "unconfiguring the addon " + addonName, afe);
                    throw afe;
                } catch (AddonException ae) {
                    getLogger().warning
                    (localStrings.getString("addon.unconfigurationcomplete.error", 
                     "unconfigure", addonName, ae.getLocalizedMessage() ));
                } catch (Exception e) {
                    getLogger().log(Level.WARNING, "Fatal Exception while " +
                    "unconfiguring the addon " + addonName, e);
                    throw new AddonFatalException(e);
                }
            }
        }
        ar.store();
        ar.close();
    
public voidunconfigureDAS(java.io.File installRoot, java.io.File domainRoot)
Invoke the configurator plugin just before stopping DAS.


        if (getLogger().isLoggable(Level.FINER)) {
            getLogger().log
            (Level.FINER, "InstallRoot for unconfigureDAS :" + installRoot);
            getLogger().log
            (Level.FINER, "DomainRoot for unconfigureDAS :" + domainRoot);
        }

        unconfigure(installRoot, domainRoot, 
                  ConfigurationContext.ConfigurationType.DAS);