alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.j2ee.deployment.config;

import javax.enterprise.deploy.model.*;
import javax.enterprise.deploy.shared.*;
import javax.enterprise.deploy.spi.*;
import javax.enterprise.deploy.spi.exceptions.*;

import org.netbeans.modules.j2ee.deployment.impl.*;
import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
import org.netbeans.modules.j2ee.deployment.devmodules.spi.*;
import org.netbeans.modules.j2ee.deployment.plugins.api.*;
import org.netbeans.modules.j2ee.deployment.execution.DeploymentConfigurationProvider;
import org.netbeans.modules.j2ee.deployment.config.ui.ConfigBeanTopComponent;

import java.util.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.openide.util.NbBundle;
import org.openide.filesystems.*;
import org.openide.nodes.*;
import org.openide.*;
import org.openide.cookies.SaveCookie;

import org.xml.sax.SAXException;

/* ConfigurationStorage stores a DeploymentConfiguration.  It is responsible
 * for creating the DeployableObjects and the DConfigBeanRoots and tracking
 * changes at the application level. */
public class ConfigurationStorage implements DeploymentConfigurationProvider, ModuleListener, Node.Cookie {
    
    public static final String ROOT = "/"; // NOI18N
    final ServerString serverString;
    DeploymentConfiguration config;
    // Map of url -> ModuleDeploymentSupport
    Map moduleMap = new HashMap();
    final Map versionListeners = new HashMap();
    DeployableObject dobj;
    ConfigurationSaver saver;
    private boolean needsSave = false;
    final J2eeModuleProvider module;
    
    public ConfigurationStorage(J2eeModuleProvider module,ServerString server) throws ConfigurationException, InvalidModuleException, IOException, SAXException {
        this.serverString = server;
        this.module = module;
        init();
        // if(!configFile.exists()) save(); JDL does this already
        createVersionListeners();
    }
    
    private void init() throws ConfigurationException, InvalidModuleException, IOException, SAXException {
        Server server = serverString.getServer();
        ServerInstance instance = serverString.getServerInstance();
        // create all MDS/ADS classes here.  MDS's should be GC'ed if the DC changes.
        ModuleDeploymentSupport mds = new ModuleDeploymentSupport(module.getJ2eeModule());
        moduleMap.put(ROOT,mds);
        if(module.getJ2eeModule().getModuleType().equals(J2eeModule.EAR)) {
            J2eeModuleContainer appProvider = (J2eeModuleContainer) module.getJ2eeModule();
            dobj = new ApplImpl(mds,moduleMap);
            J2eeModule[] modules = appProvider.getModules(this);
            for(int i = 0; i < modules.length; i++) {
                moduleMap.put(modules[i].getUrl(),new ModuleDeploymentSupport(modules[i]));
            }
        } else {
            dobj = mds.getDeployableObject();
        }
        if(instance != null) {
            config = instance.getDeploymentManager().createConfiguration(dobj);
        } else {
            config = server.getDeploymentManager().createConfiguration(dobj);
        }
        load();
    }
    
    public void setSaver(ConfigurationSaver saver)  {
        this.saver = saver;
    }
    
    void setChanged() {
        needsSave = true;
        if(saver != null) saver.setChanged();
    }
    
    // J2eeModule.ModuleListener methods
    public void addModule(J2eeModule module) {
        ModuleDeploymentSupport mds = new ModuleDeploymentSupport(module);
        moduleMap.put(module.getUrl(), mds);
        createVersionListener(module);
        try {
            createDConfigBean(mds);
        } catch (ConfigurationException ce) {
            ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ce);
            // PENDING what do I do with this error?  need to somehow
            // abort configuration
        }
    }
    
    public void removeModule(J2eeModule module) {
        ModuleDeploymentSupport mds = (ModuleDeploymentSupport) moduleMap.get(module.getUrl());
        moduleMap.remove(module.getUrl());
        mds.dispose(config);
        removeVersionListener(module);
    }
    
    public Node getMainNode() {
        ModuleDeploymentSupport mds = (ModuleDeploymentSupport) moduleMap.get("/");
        return mds.getNodes()[0];
    }
    
    public Node[] getNodes(J2eeModule module) {
        ModuleDeploymentSupport mds = (ModuleDeploymentSupport) moduleMap.get(module.getUrl());
        return mds.getNodes();
    }
    
    private void createDConfigBean(ModuleDeploymentSupport mod) throws ConfigurationException {
        mod.createConfigs(config, serverString.getServer(), this);
    }
    
    public void saveOnDemand() throws IOException {
        if (needsSave)
            save();
    }
    
    private static File[] getSaveFiles(J2eeModuleProvider provider, Server server) throws IOException {
        return ConfigSupportImpl.getDeploymentConfigurationFiles(provider, server);
    }
    
    public void save() throws IOException {
        try {
            DeploymentPlanSplitter dps = serverString.getServer().getDeploymentPlanSplitter();
            if(module.getJ2eeModule().getModuleType().equals(J2eeModule.EAR)) {
                J2eeAppProvider appProvider = (J2eeAppProvider) module;
                J2eeModuleProvider[] modules = appProvider.getChildModuleProviders();
                for(int i = 0; i < modules.length; i++) {
                    ModuleDeploymentSupport mds = (ModuleDeploymentSupport) moduleMap.get(modules[i].getJ2eeModule().getUrl());
                    dps.writeDeploymentPlanFiles(config, mds.getDeployableObject(), getSaveFiles(modules[i],serverString.getServer()));
                }
            }
            File files[] = getSaveFiles((J2eeModuleProvider)module,serverString.getServer());
            dps.writeDeploymentPlanFiles(config, dobj, files);
            for (int j = 0; j < files.length; j++) {
                FileUtil.toFileObject (files [j]).refresh (true);
            }
            needsSave = false;
            if(saver != null) saver.resetChanged();
        } catch (ConfigurationException ce) {
            String msg = NbBundle.getMessage(ConfigurationStorage.class, "MSG_ConfigurationSaveFailed", serverString.getServer().getShortName(), "TBD");
            DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg));
        }
    }
    
    public void load() throws IOException, ConfigurationException {
        DeploymentPlanSplitter dps = serverString.getServer().getDeploymentPlanSplitter();
        if(module.getJ2eeModule().getModuleType().equals(J2eeModule.EAR)) {
            J2eeAppProvider appProvider = (J2eeAppProvider) module;
            J2eeModuleProvider[] modules = appProvider.getChildModuleProviders();
            for(int i = 0; i < modules.length; i++) {
                ModuleDeploymentSupport mds = (ModuleDeploymentSupport) moduleMap.get(modules[i].getJ2eeModule().getUrl());
                dps.readDeploymentPlanFiles(config, mds.getDeployableObject(), getSaveFiles(modules[i],serverString.getServer()));
            }
        }
        File files[] = getSaveFiles((J2eeModuleProvider)module,serverString.getServer());
        dps.readDeploymentPlanFiles(config, dobj, files);
        for (int j = 0; j < files.length; j++) {
            FileUtil.toFileObject (files [j]).refresh (true);
        }
        for(Iterator i = moduleMap.values().iterator();i.hasNext();) {
            ModuleDeploymentSupport mod = (ModuleDeploymentSupport) i.next();
            createDConfigBean(mod);
        }
    }
    
    public void dispose() {
        for(Iterator i = moduleMap.values().iterator();i.hasNext();) {
            ModuleDeploymentSupport mds = (ModuleDeploymentSupport) i.next();
            mds.cleanup();
        }
    }
    
    public void realDispose() {
        for(Iterator i = moduleMap.values().iterator();i.hasNext();) {
            ModuleDeploymentSupport mds = (ModuleDeploymentSupport) i.next();
            mds.dispose(config);
            mds.cleanup();
        }
        moduleMap = new HashMap();
    }
    
    
    public DeployableObject getDeployableObject(String moduleUri) {
        if (moduleUri == null)
            return config.getDeployableObject();
        ModuleDeploymentSupport mds = (ModuleDeploymentSupport) moduleMap.get(moduleUri);
        return mds.getDeployableObject();
    }
    
    public DeploymentConfiguration getDeploymentConfiguration() {
        return config;
    }
    
    public Server getServer() {
        return serverString.getServer();
    }
    
    public String getTargetServerName() {
        return serverString.getServer().getDisplayName();
    }
    
    private String getKey(J2eeModule mod) {
        String key = mod.getUrl();
        if (key == null || key.trim().equals("")) //NOI18N
            key = ROOT;
        return key;
    }
    
    private void createVersionListener(J2eeModule mod) {
        String key = getKey(mod);
        J2eeModule.VersionListener listener = new ModuleVersionListener(key);
        mod.addVersionListener(listener);
        versionListeners.put(key, listener);
    }
    
    private void removeVersionListener(J2eeModule mod) {
        J2eeModule.VersionListener vl = (J2eeModule.VersionListener) versionListeners.remove(getKey(mod));
        if (vl != null)
            mod.removeVersionListener(vl);
    }
    
    private void createVersionListeners() {
        createVersionListener(module.getJ2eeModule());
        if(module.getJ2eeModule().getModuleType().equals(J2eeModule.EAR)) {
            J2eeModuleContainer appProvider = (J2eeModuleContainer) module;
            J2eeModule[] modules = appProvider.getModules(this);
            for(int i = 0; i < modules.length; i++) {
                createVersionListener(modules[i]);
            }
        }
    }
    
    private class ModuleVersionListener implements J2eeModule.VersionListener {
        private String moduleUri;
        ModuleVersionListener(String moduleUri) {
            this.moduleUri = moduleUri;
        }
        public void versionChanged(String oldVersion, String newVersion) {
            try {
                saveOnDemand();
                realDispose();
                init();
            } catch(java.util.NoSuchElementException e) {
                String msg = NbBundle.getMessage(
                ConfigurationStorage.class, "MSG_DescriptorError", "TBD", e.getMessage());
                NotifyDescriptor nd = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
                DialogDisplayer.getDefault().notify(nd);
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            } catch (Exception e2) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e2);
            }
            
            ConfigBeanTopComponent tc = ConfigBeanTopComponent.findByConfigStorage(ConfigurationStorage.this);
            if (tc != null) {
                tc.refresh();
            }
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.