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-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.modules;

import java.beans.Beans;
import java.util.*;
import java.util.jar.Attributes;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.openide.util.NbBundle;
import org.openide.loaders.DataLoader;
import org.openide.options.SystemOption;
import org.openide.filesystems.FileSystem;
import org.openide.debugger.Debugger;
import org.openide.ServiceType;
import org.openide.util.actions.SystemAction;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.datatransfer.ExClipboard;
import org.openide.util.SharedClassObject;

/** Class representing one specially-treated section in a module's manifest file.
 * For example, one section may describe a single action provided by the module.
*
* @author Jaroslav Tulach, Jesse Glick
* @deprecated No longer used.
*/
public abstract class ManifestSection extends Object {

    /** name of the class file, e.g. foo/Bar.class */
    String name;
    /** name of the class, e.g. foo.bar */
    private String className;
    /** instance of the class or exeception if not possible to create it */
    private Object result;
    /** associated module */
    private ModuleDescription descr;

    /** Package private constructor to allow creation only from this package.
    * @param name name of section, should be a class file, e.g. foo/Bar.class
    * @param descr associated module description
    * @throws IllegalModuleException if the name is not valid for an OpenIDE section
    */
    ManifestSection (String name, ModuleDescription descr) throws IllegalModuleException {
        this.name = name;
        this.descr = descr;
        className = ModuleDescription.createPackageName (name);
    }

    /** Get the class which the generated instances will have.
     * @return the class
     * @throws Exception for various reasons
     */
    public Class getSectionClass () throws Exception {
        if (name.endsWith (".class")) { // NOI18N
            return descr.cl.loadClass (className);
        } else {
            return getInstance ().getClass ();
        }
    }
                           
    /** Getter for instance of this class.
    * @return the insttance
    * @exception Exception if there is an error
    */
    Object createInstance () throws Exception {
        /*
        System.err.println("ManifestSection.createInstance; cl=" + cl);
        if (cl != null && cl instanceof java.net.URLClassLoader) {
          java.net.URL[] urls = ((java.net.URLClassLoader) cl).getURLs ();
          for (int i = 0; i < urls.length; i++)
            System.err.println("\t" + urls[i]);
        }
        */
        Object o = null;
        try {
            // First check for SharedClassObject.
            if (name.endsWith (".class")) { // NOI18N
                // May throw ClassNotFoundException:
                Class clazz = descr.cl.loadClass (className);
                if (SharedClassObject.class.isAssignableFrom (clazz)) {
                    // May throw IllegalArgumentException:
                    o = SharedClassObject.findObject (clazz, true);
                }
            }
            if (o == null) {
                // .ser or non-SharedClassObject:
                o = Beans.instantiate (descr.cl, className);
            }
        } catch (ClassNotFoundException cnfe) {
            descr.showLoader ();
            throw cnfe;
        }
        // Don't try to check .ser files: it is quite legitimate to
        // serialize in a module objects whose class is from elsewhere
        // (e.g. the core).
        if (name.endsWith (".class") && o.getClass ().getClassLoader () != descr.cl) // NOI18N
            descr.badClasses.add (o.getClass ());
        return o;
    }

    /** Getter for the only installed instance.
    *
    * @return the insttance
    * @exception Exception if there is an error
    */
    synchronized Object getInstance () throws Exception {
        if (result instanceof Exception) {
            ((Exception)result).fillInStackTrace ();
            throw (Exception)result;
        }
        if (result == null) {
            try {
                result = createInstance ();
            } catch (Exception ex) {
                // remember the exception
                result = ex;
                throw ex;
            } catch (Throwable ex) {
                result = new ClassNotFoundException (exceptionMessage (ex));
                throw (Exception) result;
            }
        }
        return result;
    }

    // Releases any loaded object from this section.
    void release () {
        result = null;
    }

    /** Creates a description of an exception.
    * @param ex exception
    * @return the string representation of the exception
    */
    static String exceptionMessage (Throwable ex) {
        StringWriter sw = new StringWriter ();
        PrintWriter w = new PrintWriter (sw);

        w.println (ex.getClass ().getName ());
        ex.printStackTrace (w);
        w.close ();

        return sw.toString ();
    }

    /** Abstract method of sections that invokes right method of the given
    * iterator.
    * @exception Exception if the section should be removed from list of section
    */
    abstract void invokeIterator (Iterator it) throws Exception;

    /** Parses attributes and creates its description.
    * @param name name of the section
    * @param attr attribues
    * @param descr the associated module description
    * @return the section or null if attributes does not represent section
    * @exception IllegalModuleException if the attributes are not valid
    */
    static ManifestSection createSection (String name, Attributes attr, ModuleDescription descr) throws IllegalModuleException {

        // Analyze the section
        String sectionName = attr.getValue (ModuleDescription.TAG_SECTION_CLASS);
        if (sectionName == null) {
            // no section tag
            return null;
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_ACTION)) {
            return new ActionSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_OPTION)) {
            return new OptionSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_LOADER)) {
            return new LoaderSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_FILESYSTEM)) {
            return new FileSystemSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_NODE)) {
            return new NodeSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_SERVICE)) {
            return new ServiceSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_DEBUGGER)) {
            return new DebuggerSection(name, attr, descr);
        } else if (sectionName.equalsIgnoreCase(ModuleDescription.SECTION_CLIPBOARD_CONVERTOR)) {
            return new ClipboardConvertorSection (name, attr, descr);
        } else {
            throw new IllegalModuleException (NbBundle.getMessage (
		    ManifestSection.class, "EXC_IllegalModuleClass",
		    name, sectionName));
        }
    }

    /** Iterator over different types of sections.
     * @see ModuleDescription#forEachSection
    */
    public static interface Iterator {
        /** Process action section.
        * @param as the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processAction (ActionSection as) throws InstantiationException;

        /** Process option section.
        * @param os the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processOption (OptionSection os) throws InstantiationException;

        /** Process loader section.
        * @param ls the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processLoader (LoaderSection ls) throws InstantiationException;

        /** Process debugger section.
        * @param ds the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processDebugger (DebuggerSection ds) throws InstantiationException;

        /** Process service section.
        * @param es the section
        * @exception Instantiationception if there is an error and the section should be ignored
        */
        public void processService (ServiceSection es) throws InstantiationException;

        /** Process file system section.
        * @param fs the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processFileSystem (FileSystemSection fs) throws InstantiationException;

        /** Process node section.
        * @param es the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processNode (NodeSection es) throws InstantiationException;

        /** Process clipboard convertor section.
        * @param ccs the section
        * @exception InstantiationException if there is an error and the section should be ignored
        */
        public void processClipboardConvertor (ClipboardConvertorSection ccs) throws InstantiationException;
    }

    /** Module section for an Action.
     * @see SystemAction
    */
    public static final class ActionSection extends ManifestSection {
        ActionSection(String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super(name, descr);
        }

        /** Get the action object.
         * @return the action
        * @exception InstantiationException if the action cannot be created
        */
        public SystemAction getAction () throws InstantiationException {
            try {
                return (SystemAction)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }


        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processAction (this);
        }
    }

    /** Module section for an Option.
     * @see SystemOption
     */
    public static final class OptionSection extends ManifestSection {
        OptionSection (String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super(name, descr);
        }

        /** Get the system option.
         * @return the option
        * @exception InstantiationException if the action cannot be created
        */
        public SystemOption getOption () throws InstantiationException {
            try {
                return (SystemOption)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processOption (this);
        }
    }

    /** Module section for a Data Loader.
     * @see DataLoader
     */
    public static final class LoaderSection extends ManifestSection {
        /** class name(s) of data object to
        * be inserted after loader that recognizes its
        */
        private String[] installAfter;
        /** class name(s) of data object to be inserted before its recognizing
        * data loader
        */
        private String[] installBefore;

        LoaderSection (String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super (name, descr);
            String val = attrs.getValue (ModuleDescription.TAG_INSTALL_AFTER);
            StringTokenizer tok;
            List res;
            if (val != null) {
                tok = new StringTokenizer (val, ", "); // NOI18N
                res = new LinkedList ();
                while (tok.hasMoreTokens ()) {
                    String clazz = tok.nextToken ();
                    if (! clazz.equals ("")) // NOI18N
                        res.add (clazz);
                }
                installAfter = (String[]) res.toArray (new String[res.size ()]);
            } else {
                installAfter = null;
            }
            val = attrs.getValue (ModuleDescription.TAG_INSTALL_BEFORE);
            if (val != null) {
                tok = new StringTokenizer (val, ", "); // NOI18N
                res = new LinkedList ();
                while (tok.hasMoreTokens ()) {
                    String clazz = tok.nextToken ();
                    if (! clazz.equals ("")) // NOI18N
                        res.add (clazz);
                }
                installBefore = (String[]) res.toArray (new String[res.size ()]);
            } else {
                installBefore = null;
            }
        }

        /** Get the data loader.
         * @throws InstantiationException if the loader could not be created
        * @return the loader
        */
        public DataLoader getLoader () throws InstantiationException {
            try {
                return (DataLoader)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Get the representation class(es) of the loader(s) that this one should be installed after.
        * @return a list of class names, or null
        */
        public String[] getInstallAfter () {
            return installAfter;
        }

        /** Get the representation class(es) of the loader(s) that this one should be installed before.
        * @return a list of class names, or null
        */
        public String[] getInstallBefore () {
            return installBefore;
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processLoader (this);
        }
    }

    /** Module section for a Debugger.
     * @see Debugger
     */
    public static final class DebuggerSection extends ManifestSection {
        DebuggerSection (String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super(name, descr);
        }

        /** Get the debugger object.
         * @return the debugger
         * @throws InstantiationException if the debugger could not be created
        */
        public Debugger getDebugger () throws InstantiationException {
            try {
                return (Debugger)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processDebugger (this);
        }
    }

    /** Module section for a service type.
     * @see ServiceType
     */
    public static final class ServiceSection extends ManifestSection {
        /** attributes associated with this section */
        private Attributes attr;

        ServiceSection(String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super(name, descr);
            this.attr = attrs;
        }

        /** Check if this service type is the default. That means, should it be placed
        * in front of other services?
        *
        * @return true if it is 
        */
        public boolean isDefault () {
            return Boolean.valueOf (attr.getValue (ModuleDescription.TAG_SERVICE_DEFAULT)).booleanValue ();
        }

        /** Getter for the default instance of this service type.
         * @return the service type
         * @throws InstantiationException if the Service could not be created
        */
        public ServiceType getServiceType () throws InstantiationException {
            try {
                return (ServiceType)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Create a new service type of the specified type.
         * @return the service type
         * @throws InstantiationException if the Service could not be created
        */
        public ServiceType createServiceType () throws InstantiationException {
            try {
                return (ServiceType)super.createInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processService (this);
        }
    }

    /** Module section for a File System.
     * @see FileSystem
    */
    public static final class FileSystemSection extends ManifestSection {
        /** attributes associated with this section */
        private Attributes attr;

        /** Constructor */
        FileSystemSection (String name, Attributes attr, ModuleDescription descr) throws IllegalModuleException {
            super (name, descr);
            this.attr = attr;
        }

        /** Get the display name of the file system.
         * This could be used e.g. in a context menu on the Repository.
         * If none was specified, a default name will be created.
        *
        * @return the name
        */
        public String getName() {
            String s = (String)NbBundle.getLocalizedValue (attr, ModuleDescription.TAG_FILESYSTEM_NAME);
            if (s == null) {
                return NbBundle.getMessage (ManifestSection.class,
			"CTL_Repository_Unknown", this.name );
            } else {
                return s;
            }
        }

        /** Get a help context for the file system.
         * If none was specified, a default context will be created.
        * @return the help context
        */
        public HelpCtx getHelpCtx () {
            String s = attr.getValue (ModuleDescription.TAG_FILESYSTEM_HELP);
            return s == null ?
                   org.openide.util.HelpCtx.DEFAULT_HELP :
                   // [PENDING] this constructor looks for a JavaHelp tag, but docs say /com/mycom/index.html!
                   new org.openide.util.HelpCtx(s);
        }

        /** Create a new file system.
        * @return the file system
        * @throws InstantiationException if it could not be created
        */
        public FileSystem createFileSystem () throws InstantiationException {
            try {
                return (FileSystem)super.createInstance ();
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processFileSystem (this);
        }

    }

    /** Module section for a node.
    * @see Node
    */
    public static final class NodeSection extends ManifestSection {
        /** Type to add an entry to the root nodes. */
        public static final String TYPE_ROOTS = "roots"; // NOI18N
        /** Type to add an entry to the Environment (in the Explorer). */
        public static final String TYPE_ENVIRONMENT = "environment"; // NOI18N
        /** Type to add an entry to the Session settings. */
        public static final String TYPE_SESSION = "session"; // NOI18N


        /** type of the node */
        private String type;

        NodeSection (String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super(name, descr);
            type = attrs.getValue (ModuleDescription.TAG_NODE_TYPE);
        }

        /** Get the environment node.
        * @return the node
        * @exception InstantiationException if the node could not be created
        */
        public Node getNode () throws InstantiationException {
            try {
                return (Node)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Get the node type. Determines where the node should be placed in
        * the IDE.
        * @return one of {@link #TYPE_ROOTS}, {@link #TYPE_ENVIRONMENT}, or {@link #TYPE_SESSION}, or null if unspecified
        */
        public String getType () {
            return type;
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processNode (this);
        }
    }

    /** Module section for a Clipboard convertor.
    * @see org.openide.util.datatransfer.ExClipboard.Convertor
    */
    public static final class ClipboardConvertorSection extends ManifestSection {
        ClipboardConvertorSection (String name, Attributes attrs, ModuleDescription descr) throws IllegalModuleException {
            super(name, descr);
        }

        /** Get the convertor.
        * @return the convertor
        * @exception InstantiationException if the object could not be created
        */
        public ExClipboard.Convertor getConvertor () throws InstantiationException {
            try {
                return (ExClipboard.Convertor)super.getInstance ();
            } catch (Exception ex) {
                throw new InstantiationException (ManifestSection.exceptionMessage (ex));
            }
        }

        /** Calls the right method in the iterator.
        */
        void invokeIterator (Iterator it) throws Exception {
            it.processClipboardConvertor (this);
        }
    }

}
... 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.