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.impl;

import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
import javax.enterprise.deploy.spi.factories.DeploymentFactory;
import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
import javax.enterprise.deploy.spi.*;
import org.netbeans.modules.j2ee.deployment.impl.gen.nbd.*;
import org.netbeans.modules.j2ee.deployment.plugins.api.*;
import org.netbeans.modules.j2ee.deployment.impl.ui.RegistryNodeProvider;
import org.openide.filesystems.*;
import org.openide.loaders.*;
import org.openide.util.Lookup;
import org.openide.cookies.InstanceCookie;
import org.openide.nodes.Node;
import org.openide.ErrorManager;
import org.openide.util.NbBundle;
import java.util.*;
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
import javax.enterprise.deploy.shared.ModuleType;


public class Server implements Node.Cookie {
    
    static public final String ATTR_needsFindServerUI = "needsFindServerUI";
    
    final NetbeansDeployment dep;
    final Class factoryCls;
    DeploymentFactory factory = null;
    DeploymentManager manager = null;
    RegistryNodeProvider nodeProvider = null;
    final String name;
    Map configMap;
    Map customMap;
    Lookup lkp;
    boolean needsFindServerUI = false;
    
    public Server(FileObject fo) throws Exception {
        //long t0 = System.currentTimeMillis();
        
        name = fo.getName();
        FileObject descriptor = fo.getFileObject("Descriptor");
        if(descriptor == null) {
            String msg = NbBundle.getMessage(Server.class, "MSG_InvalidServerPlugin", name);
            throw new IllegalStateException(msg);
        }
        needsFindServerUI = getBooleanValue(descriptor.getAttribute(ATTR_needsFindServerUI), false);
        
        dep = NetbeansDeployment.createGraph(descriptor.getInputStream());
        
        lkp = new FolderLookup (DataFolder.findContainer (fo)).getLookup ();
        factory = (DeploymentFactory) lkp.lookup (DeploymentFactory.class);
        if (factory != null) {
            factoryCls = factory.getClass ();
        } else {
            FileObject factoryinstance = fo.getFileObject("Factory.instance");
            if(factoryinstance == null) {
                String msg = NbBundle.getMessage(Server.class, "MSG_NoFactoryInstanceClass", name);
                ErrorManager.getDefault().log(ErrorManager.ERROR, msg);
                factoryCls = null;
                return;
            }
            DataObject dobj = DataObject.find(factoryinstance);
            InstanceCookie cookie = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
            if(cookie == null) {
                String msg = NbBundle.getMessage(Server.class, "MSG_FactoryFailed", name, cookie);
                ErrorManager.getDefault().log(ErrorManager.ERROR, msg);
                factoryCls = null;
                return;
            }
            factoryCls = cookie.instanceClass();

            // speculative code depending on the DF implementation and if it registers
            // itself with DFM or not

            try {
                factory = (DeploymentFactory) cookie.instanceCreate();
            } catch (Exception e) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            }
        }
        //System.out.println("Create plugin "+name+" in "+(System.currentTimeMillis() - t0));
    }
    
    
    private DeploymentFactory getFactory() {
        if (factory == null) {
            DeploymentFactoryManager dfm = DeploymentFactoryManager.getInstance();
//            System.err.println(dfm);
//            System.err.println(dfm.getDeploymentFactories().length);
//            System.err.println(factoryCls);
//            System.err.println(factoryCls.getName());
            try {
//                System.err.println(Class.forName(factoryCls.getName()));
                Thread.sleep(5000);
            } catch (Exception e) {}
//            System.err.println(dfm.getDeploymentFactories().length);
//            System.err.println(DeploymentFactoryManager.getInstance());
            DeploymentFactory[] factories = DeploymentFactoryManager.getInstance().getDeploymentFactories();
            for(int i = 0; i < factories.length; i++) {
//                System.err.println("Checking factory " + factories[i]);
                if(factoryCls.isInstance(factories[i])) {
                    factory = factories[i];
                    break;
                }
            }
        }
        if(factory == null) {
            //          ServerRegistry.getInstance().removePlugin(sfo);
            throw new IllegalStateException();
        }
        return factory;
    }
    
    public DeploymentManager getDeploymentManager() {
        if(manager == null) {
            getFactory();
            if (manager == null) {
                try {
                    manager = factory.getDisconnectedDeploymentManager(dep.getDisconnectedString());
                } catch (Exception e) {
                    ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
                }
            }
        }
        return manager;
    }
    
    public boolean handlesUri(String uri) {
        try {
            return getFactory().handlesURI(uri);
        } catch (Exception e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            return false;
        }
    }
    
    public DeploymentManager getDeploymentManager(String uri, String username, String password) throws DeploymentManagerCreationException {
        return getFactory().getDeploymentManager(uri,username, password);
    }
    
    public String getDisplayName() {
        return getFactory().getDisplayName();
    }
    
    public String getShortName() {
        return name;
    }
    
    public String getIconBase() {
        return dep.getIcon(); 
    }
    
    public boolean canDeployEars() {
        return dep.getContainerLimitation() == null || dep.getContainerLimitation().isEarDeploy();
    }
    
    public boolean canDeployWars() {
        return dep.getContainerLimitation() == null || dep.getContainerLimitation().isWarDeploy();
    }
    
    public boolean canDeployEjbJars() {
        return dep.getContainerLimitation() == null || dep.getContainerLimitation().isEjbjarDeploy();
    }
    
    public ConfigBeanDescriptor getConfigBeanDescriptor(String className) {
        if(configMap == null) {
            ConfigBean[] beans = dep.getConfigBean();
            configMap = new HashMap();
            for(int i = 0; i < beans.length; i++)
                configMap.put(beans[i].getClassName(),new ConfigBeanDescriptor(beans[i]));
        }
        return (ConfigBeanDescriptor) configMap.get(className);
    }
    
    private Object getClassFromPlugin(String className) {
        if (className == null || "".equals(className.trim())) return null; //NOI18N
        try {
        return factory.getClass().getClassLoader().loadClass(className).newInstance();
        } catch (Exception e) {
            ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, e.getMessage());
            return null;
        }
    }
    
    // PENDING should be cached?
    public String getHelpId(String beanClass) {
        ConfigBean[] beans = dep.getConfigBean();
        for(int i = 0; i < beans.length; i++) {
            if(beans[i].getClassName().equals(beanClass))
                return beans[i].getHelpid();
        }
        return null;
    }
    
    public DeploymentPlanSplitter getDeploymentPlanSplitter() {
        DeploymentPlanSplitter o = (DeploymentPlanSplitter) lkp.lookup (DeploymentPlanSplitter.class);
        if (o != null) {
            return o;
        } else {
            String msg = NbBundle.getMessage(Server.class, "MSG_NoInstance", name, DeploymentPlanSplitter.class);
            ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg);
            return null;
        }
    }
    
    public RegistryNodeProvider getNodeProvider() {
        if (nodeProvider != null)
            return nodeProvider;
        
        RegistryNodeFactory nodeFact = (RegistryNodeFactory) lkp.lookup(RegistryNodeFactory.class);
        if (nodeFact == null) {
            String msg = NbBundle.getMessage(Server.class, "MSG_NoInstance", name, RegistryNodeFactory.class);
            ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg);
        }
        nodeProvider = new RegistryNodeProvider(nodeFact); //null is acceptable
        return nodeProvider;
    }
    
    public ManagementMapper getManagementMapper() {
        ManagementMapper o = (ManagementMapper) lkp.lookup (ManagementMapper.class);
        if (o != null) {
            return o;
        } else {
            String msg = NbBundle.getMessage(Server.class, "MSG_NoInstance", name, ManagementMapper.class);
            ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, msg);
            return null;
        }
    }

    /** returns OptionalDeploymentManagerFactory or null it is not provided by the plugin */
    public OptionalDeploymentManagerFactory getOptionalFactory () {
        OptionalDeploymentManagerFactory o = (OptionalDeploymentManagerFactory) lkp.lookup (OptionalDeploymentManagerFactory.class);
        return o;
    }
    
    /** returns DConfigBeanUIFactory or null it is not provided by the plugin */
    public DConfigBeanUIFactory getDConfigBeanUIFactory () {
        DConfigBeanUIFactory o = (DConfigBeanUIFactory) lkp.lookup (DConfigBeanUIFactory.class);
        return o;
    }
    
    public DConfigBeanProperties getDConfigBeanProperties(DConfigBean bean) {
        DConfigBeanUIFactory beanUIFactory = getDConfigBeanUIFactory();
        if (beanUIFactory == null) return null;
        return beanUIFactory.getUICustomization(bean);
    }
    
    public ServerInstance[] getInstances() {
        Collection ret = new ArrayList();
        for (Iterator i=ServerRegistry.getInstance().getInstances().iterator(); i.hasNext();) {
            ServerInstance inst = (ServerInstance) i.next();
            if (name.equals(inst.getServer().getShortName()))
                ret.add(inst);
        }
        return (ServerInstance[]) ret.toArray(new ServerInstance[ret.size()]);
    }
    
    public WebContextRoot getWebContextRoot() {
        return dep.getWebContextRoot();
    }
    
    public DeploymentFactory getDeploymentFactory() {
        return factory;
    }

    static public boolean getBooleanValue(Object v, boolean dvalue) {
        if (v instanceof Boolean) 
            return ((Boolean)v).booleanValue();
        if (v instanceof String)
            return Boolean.valueOf((String) v).booleanValue();
        return dvalue;
    }
    
    public boolean needsFindServerUI() {
        return needsFindServerUI;
    }
    
    public String toString () {
        return getShortName ();
    }
    
    public boolean supportsModuleType(ModuleType type) {
        if (J2eeModule.WAR.equals(type)) {
            return this.canDeployWars();
        } else if (J2eeModule.EJB.equals(type)) {
            return this.canDeployEjbJars();
        } else if (J2eeModule.EAR.equals(type)) {
            return this.canDeployEars();
        } else {
            // PENDING, precise answer for other module types, for now assume true
            return true;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.