Overview

# NetBeans Architecture Answers for editor/mimelookup module

- Author: mroskanin@netbeans.org
- Answers as of: Dec 7, 2006
- Answers for questions version: 1.25
- Latest available version of questions: 1.26

WARNING: answering questions version 1.25 rather than the current 1.26.

## Interfaces table

##### Group of java interfaces

Interface Name | In/Out | Stability | Specified in What Document?
MimeLookupAPI | Exported | Official |
LoadersAPI | Imported | Official | .../overview-summary.html The module is needed for compilation. The module is used during runtime. Specification version 4.11 is required.
ModulesAPI | Imported | Official | .../overview-summary.html The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.
UtilitiesAPI | Imported | Official | ../org-openide-util/overview-summary.html The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.
NodesAPI | Imported | Official | .../overview-summary.html The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.
FilesystemsAPI | Imported | Official | .../overview-summary.html The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.

##### Group of lookup interfaces

Interface Name | In/Out | Stability | Specified in What Document?
org.netbeans.api.editor.mimelookup.MimeLookup | Imported | Official | in API extends Lookup and it searches the default lookup for instances of `MimeLookupInitializer`.
org.netbeans.modules.editor.mimelookup.LayerMimeLookupInitializer | Imported | Official | searches default lookup for instances of `Class2LayerFolder`.
org.netbeans.spi.editor.mimelookup.MimeLookupInitializer | Exported | Official | is registered via META-INF/services and is looked up by `MimeLookup`.
org.netbeans.spi.editor.mimelookup.Class2LayerFolder | Exported | Official | is registered via META-INF/services and is looked up by `LayerMimeLookupInitializer`.

##### Group of layer interfaces

Interface Name | In/Out | Stability | Specified in What Document?
Editors | Exported | Official | `MimeLookup` module creates folder "Editors" in default layer filesystem since all mime types folders are expected to be located in this folder.
EditorsMimeTypes | Imported | Official | `LayerMimeLookupInitializer` gathers all mime type sensitive lookupable objects from Editors[/<mime-type>][/<specific-subfolder>] and expose them over layer MimeLookup.

## General Information

Question (arch-what): What is this project good for?

Answer: Each editor provides an EditorKit which controls the policy of specific MIME content type. The policy of content type should be easily registered and found via some lookup mechanism, that will provide convenient way of using it either for kit provider or base editor infrastructure. In addition to this, the policy can be inherited, (i.e. in case of embeded kits like JSP) and the content types need to be merged in this case. MIME Lookup API should provide all mentioned requierements via easy lookup query, so content type policy user need not to solve this searching and merging on its own side.

Question (arch-overall): Describe the overall architecture.

Answer: It consists of

- API in `org.netbeans.api.editor.mimelookup`
- SPI in `org.netbeans.spi.editor.mimelookup`

MIME Lookup defines MimeLookupAPI.

API contains only one class `MimeLookup` with public methods:

- `static MimeLookup getMimeLookup(String mime)` - gets mime specific lookup
- `MimeLookup childLookup(String mime)` - gets mime specific child (embeded) lookup
- `Object lookup(Class clazz)` - Look up an object matching a given interface.
- `Result lookup(Lookup.Template template)` - The general lookup method. Callers can get list of all instances and classes that match the given `template` and attach a listener to this be notified about changes.

`MimeLookup` is represented via ProxyLookup that collects registered lookups. Particular lookups, responsible for looking up their objects can be registered using interface `MimeLookupInitializer` into default lookup by META-INF/services registration.

In addition to this basic registration, xml layer folder registration is also available. It is provided by registering implemented interface `Class2LayerFolder` into default lookup via META-INF/services registration. This approach provides a mapping of class to specific subfolder. Using this mapping one can achieve the convenient way of using `MimeLookup` i.e.

`MimeLookup.getMimeLookup("text/x-java").lookup(FoldManagerFactory.class);`

Using this, an instance of FoldManagerFactory is retrieved from the folder with path "Editors/text/x-java/FoldManager" provided that FoldManagerFactory.class is registered to a subfolder "FoldManager" via `Class2LayerFolder` registration.

There `InstanceProvider` can be used if there are files of various types in the layer folder that need additional handling before becoming and instance of certain class. For more details look at use case of PopupActions creation.

The Javadoc documentation can be generated by using

        cd /cvs/editor/mimelookup
        ant javadoc

Question (arch-usecases): Describe the main use cases of the new API. Who will use it under what circumstances? What kind of code would typically need to be written to use the module?

Answer:

#### Per mime-type operation

Operation of the editor module must be parametrized by the type of the file being edited. In the past the operation was parametrized by the class of the editor kit but that did not show up as being useful enough.
It is more practical to use a string-based parametrization concretely the mime-type. Anyone can then easily register an additional functionality for the editor because it's just enough to know the right mime-type and the type of the functionality class to be implemented and the xml layer folder where the class should be registered.
For example the editor/fold module expects to find the registered fold manager factories (`org.netbeans.spi.editor.fold.FoldManagerFactory` classes) in the Editors/<mime-type>/FoldManager layer folder.
The editor/completion module expects to find the registered completion providers (`org.netbeans.spi.editor.completion.CompletionProvider` classes) in the Editors/<mime-type>/CompletionProviders layer folder.

#### Provide list of instances as lookup result

On the modules' implementation side the registered functionality must be retrieved somehow. It's necessary to instantiate the registered objects and react to module enablding/disabling which can affect validity of the registered objects.
As the most convenient solution appears to use `org.openide.util.Lookup` allowing to provide the registered instances as a `Lookup.Result` allowing to listen for changes (e.g. caused by the module enabling/disabling).
This resulted into creation of `class MimeLookup extends Lookup` containing `static MimeLookup getMimeLookup(String mimeType)`.

#### Nested mime-types

On the lexical level the document can contain nested languages.
For example JSP document can contain pieces of java code which can further contain javadoc comment tokens with nested javadoc language.
The nested languages should allow for special settings such as fonts and colors of nested syntax coloring but even things like actions that would be active in the nested document section.
This resulted into creation of `MimeLookup childLookup(String mimeType)` method in `MimeLookup`.

## API Use Cases

#### Find class instances for the given mime-type

An API method

`MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");`

can be used for getting the mime specific lookup. Having this we can lookup class or template:

`Object obj = lookup.lookup(LookedUpClass.class);`

or

`Lookup.Result result = lookup.lookup(new Lookup.Template(LookedUpClass.class));`

#### Getting mime-type specific child (embeded) MimeLookup

Child mime content can be embeded into parent mime content in various embeded languages. In this case mime lookup child is specified as subelement of parent lookup i.e.: MimeLookup("text/x-jsp") can have a child MimeLookup("text/x-java") in a case of a jsp scriplet.

`MimeLookup lookup = MimeLookup.getMimeLookup("text/x-jsp").childLookup("text/x-java");`

## SPI Use Cases

#### Declare the mime specific object via xml layer

This is the simpliest way of declaring, it is suitable for objects stored directly in mime specific folders:

    <filesystem>
        <folder name="Editors">
            <folder name="text">
                <folder name="x-java">
                    <file name="org-netbeans-modules-editor-mimelookuptest-MimeSpecificObject.instance"/>
                </folder>
            </folder>
        </folder>
    </filesystem>

Lookup of this object will look like:

    MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");
    MimeSpecificObject mso = (MimeSpecificObject) lookup.lookup(MimeSpecificObject.class);

#### Declare the object via xml layer in some specific folder using Class2LayerFolder.

Some objects are connected to some specific folder in mime specific layer folders, i.e. FoldManagerFactory.class is placed in FoldManager subfolder in the Editors/<mime-type>/. Such objects can be registered to the specific folder using interface `Class2LayerFolder`. Let's register FoldManagerFactory.class to FoldManager folder. First we need to implement the interface:

    public class FoldManagerClass2LayerFolder implements Class2LayerFolder{

        public FoldManagerClass2LayerFolder {
        }

        /* declaring the class */
        public Class getClazz(){
            return FoldManagerFactory.class;
        }

        /* assigning the declared class to folder */
        public String getLayerFolderName(){
            return "FoldManager";
        }

        /* we will not support InstanceProvider */
        public org.netbeans.spi.editor.mimelookup.InstanceProvider getInstanceProvider() {
            return null;
        }

    }

Then we need to register it to default lookup via META-INF/services registration. We need to create a folder structure `META-INF/services` and place there a file `org.netbeans.spi.editor.mimelookup.Class2LayerFolder` with the content `FoldManagerClass2LayerFolder`

having this we can register appropriate object to specific folder:

    <filesystem>
        <folder name="Editors">
            <folder name="text">
                <folder name="x-java">
                    <folder name="FoldManager">
                        <file name="org-netbeans-modules-editor-MyFoldManagerFactory.instance"/>
                    </folder>
                </folder>
            </folder>
        </folder>
    </filesystem>

Lookup of this object will look like:

    MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");
    FoldManagerFactory foldManagerFactory =  (FoldManagerFactory) lookup.lookup(FoldManagerFactory.class);

or, if there should be more instances of the FoldManagerFactory:

    MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java");
    Collection foldManagerFactories = lookup.lookup(new Lookup.Template(FoldManagerFactory.class)).allInstances();

Notice, that the FoldManagerFactory object is found in "FoldManager" folder. It is not necessary for client of the API to know about some folder structure.

#### Providing implemented MimeLookupInitializer

It is the general way of adding mime specific object into the `MimeLookup`. Implementation of `MimeLookupInitializer` should be created and registered to default lookup via `META-INF/services` registration. For details, please look at the simplified `TestMimeLookupInitializer` in `mimelookup/test/unit` or `LayerMimeLookupInitializer`.

#### Using InstanceProvider for declaration of compound folder objects and inheritance

Demonstration of `InstanceProvider` and inheritance will be used together in one use case. Example of editor context menu construction will be used. Each module can register its actions to editor context menu via xml layer. As context menu is mime type sensitive (java editor has different menu items than plain editor) the actions are registered into mime specific layer folders to subfolder "Popup". When the context menu is constructing, the xml layer is scanned for the action items located in the "Popup" subfolder of specific mime folder. In addition to this there is inheritance mechanism used to share global actions (like Cut, Copy, Paste) over all mime types context menus, thus not only the action items from actual mime type are considered. All underlaying mime types are scanned also and the result is context menu with merged action items.

For example JSP scriplet context menu should be merged from action items gathered over:

1. Editors/Popup - this is base level for global actions like Cut, Copy, Paste
2. Editors/text/x-jsp/Popup - items from jsp mime type
3. Editors/text/x-jsp/text/x-java/Popup - items specific to java scriplet in JSP document

For now, this construction mechanism is implemented in editor module in NbEditorKit class. The code responsible for construction is quite large, and it is not very generic, now it supports only base level + mime specific level merging. Embeded languages like example in JSP scriplet are not supported. `MimeLookup` will solve also this embeding and the gathering of the context menu items will looks such simply as:

    MimeLookup lookup = MimeLookup.getMimeLookup("text/x-jsp").childLookup("text/x-java");
    PopupActions actions = (PopupActions) lookup.lookup(PopupActions.class);
    List popupActions = actions.getPopupActions();

where PopupActions is implementation of `InstanceProvider` and PopupActions.class needs to be registered to "Popup" subfolder using `Class2LayerFolder` implementation. Let's register this step by step.

Because action items are instances of various objects like:

1. javax.swing.Action
2. javax.swing.JSeparator
3. org.openide.util.actions.SystemAction
4. java.lang.String

the `InstanceProvider` needs to be created for this:

    public class PopupActions implements InstanceProvider{

        List ordered;

        public PopupActions(){
        }

        public PopupActions(List ordered){
            this.ordered = ordered;
        }

        public List getPopupActions(){
            List retList = new ArrayList();
            for (int i = 0; i<ordered.size(); i++){
                DataObject dob = (DataObject) ordered.get(i);
                InstanceCookie ic = (InstanceCookie)dob.getCookie(InstanceCookie.class);
                if (ic!=null){
                    try{
                        if (String.class.isAssignableFrom(ic.instanceClass()) ||
                            Action.class.isAssignableFrom(ic.instanceClass()) ||
                            SystemAction.class.isAssignableFrom(ic.instanceClass()) ||
                            JSeparator.class.isAssignableFrom(ic.instanceClass())){
                            Object instance = ic.instanceCreate();
                            retList.add(instance);
                        }
                    }catch(IOException ioe){
                        ioe.printStackTrace();
                    }catch(ClassNotFoundException cnfe){
                        cnfe.printStackTrace();
                    }
                } else{
                    retList.add(dob.getName());
                }
            }
            return retList;
        }

        public Object createInstance(List ordered) {
            return new PopupActions(ordered);
        }
    }

This `InstanceProvider` needs to be declared in `Class2LayerFolder` implementation:

    public class PopupInitializer implements Class2LayerFolder{

        public PopupInitializer() {
        }

        public Class getClazz(){
            return PopupActions.class;
        }

        public String getLayerFolderName(){
            return "Popup"; //NOI18N
        }

        public InstanceProvider getInstanceProvider() {
            return new PopupActions();
        }

    }

Now, we just need to register PopupInitializer into default lookup via META-INF/services registration and the initialization is done.

Question (arch-time): What are the time estimates of the work?

Answer: Done.

Question (arch-quality): How will the quality of your code be tested and how are future regressions going to be prevented?

Answer: Unit tests are available.

There are several testing areas covered:

- `MimeLookupTest.java`
  - Looking up the class that has not registered subfolder via `Class2LayerFolder`. It should be found in the appropriate mime-type specific folder
  - Looking up the class that has registered subfolder via `Class2LayerFolder`
  - Testing if the `MimeLookup` is not recursive (see issue #58991 for more details)
  - Testing lazy lookup object creation. Object is instantiated only if it is directly looked up
  - Testing `MimeLookupInitializer` creation, registration and performing a lookup
- `MimeLookupInheritanceTest.java`
  - Testing the inheritance and instance provider functionality as well as sorting of merged elements
- `MimeLookupPopupItemsChangeTest.java`
  - Testing the dynamic change (addition or removal of a file [looked-up object] from xml layer folder) in inheritance tree, merging, sorting

After fixing the issue #58941 unit tests of template lookup should be added.

Question (arch-where): Where one can find sources for your module?

WARNING: Question with id="arch-where" has not been answered!

## Project and platform dependencies

Question (dep-nb): What other NetBeans projects and modules does this one depend on?

Answer:

These modules are required in project.xml file:

- LoadersAPI - The module is needed for compilation. The module is used during runtime. Specification version 4.11 is required.
- ModulesAPI - The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.
- UtilitiesAPI - The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.
- NodesAPI - The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.
- FilesystemsAPI - The module is needed for compilation. The module is used during runtime. Specification version 6.2 is required.

Question (dep-non-nb): What other projects outside NetBeans does this one depend on?

Answer: No other projects.

Question (dep-platform): On which platforms does your module run? Does it run in the same way on each?

Answer: All platforms.

Question (dep-jre): Which version of JRE do you need (1.2, 1.3, 1.4, etc.)?

Answer: JDK1.4 and higher can be used.

Question (dep-jrejdk): Do you require the JDK or is the JRE enough?

Answer: JRE is sufficient.

## Deployment

Question (deploy-jar): Do you deploy just module JAR file(s) or other files as well?

Answer: No additional files.

Question (deploy-nbm): Can you deploy an NBM via the Update Center?

Answer: Yes.

Question (deploy-shared): Do you need to be installed in the shared location only, or in the user directory only, or can your module be installed anywhere?

Answer: Anywhere.

Question (deploy-packages): Are packages of your module made inaccessible by not declaring them public?

Answer: Yes, only the API and SPI are public. The implementation is not public.

Question (deploy-dependencies): What do other modules need to do to declare a dependency on this one?

Answer:

    OpenIDE-Module-Module-Dependencies: org.netbeans.modules.editor.mimelookup/1 > 1.3.22

## Compatibility with environment

Question (compat-i18n): Is your module correctly internationalized?

Answer: Yes.

Question (compat-standards): Does the module implement or define any standards? Is the implementation exact or does it deviate somehow?

Answer: Compatible with standards.

Question (compat-version): Can your module coexist with earlier and future versions of itself? Can you correctly read all old settings? Will future versions be able to read your current settings? Can you read or politely ignore settings stored by a future version?

Answer: No persistence is used for `MimeLookup`

## Access to resources

Question (resources-file): Does your module use `java.io.File` directly?

Answer: Only in tests.

Question (resources-layer): Does your module provide own layer? Does it create any files or folders in it? What it is trying to communicate by that and with which components?

Answer: Yes. Editors - `MimeLookup` module creates folder "Editors" in default layer filesystem since all mime types folders are expected to be located in this folder.

Question (resources-read): Does your module read any resources from layers? For what purpose?

Answer: Yes. EditorsMimeTypes - `LayerMimeLookupInitializer` gathers all mime type sensitive lookupable objects from Editors[/<mime-type>][/<specific-subfolder>] and expose them over layer MimeLookup.

Question (resources-mask): Does your module mask/hide/override any resources provided by other modules in their layers?

Answer: No.

## Lookup of components

Question (lookup-lookup): Does your module use `org.openide.util.Lookup` or any similar technology to find any components to communicate with? Which ones?

Answer: Yes. org.netbeans.api.editor.mimelookup.MimeLookup - in API extends Lookup and it searches the default lookup for instances of `MimeLookupInitializer`. org.netbeans.modules.editor.mimelookup.LayerMimeLookupInitializer - searches default lookup for instances of `Class2LayerFolder`.

Question (lookup-register): Do you register anything into lookup for other code to find?

Answer: Yes. org.netbeans.spi.editor.mimelookup.MimeLookupInitializer - is registered via META-INF/services and is looked up by `MimeLookup`. org.netbeans.spi.editor.mimelookup.Class2LayerFolder - is registered via META-INF/services and is looked up by `LayerMimeLookupInitializer`.

Question (lookup-remove): Do you remove entries of other modules from lookup?

Answer: No.

## Execution Environment

Question (exec-property): Is execution of your code influenced by any environment or Java system (`System.getProperty`) property?

Answer: No.

Question (exec-component): Is execution of your code influenced by any (string) property of any of your components?

Answer: No.

Question (exec-ant-tasks): Do you define or register any ant tasks that other can use?

Answer: No.

Question (exec-classloader): Does your code create its own class loader(s)?

Answer: No.

Question (exec-reflection): Does your code use Java Reflection to execute other code?

Answer: No.

Question (exec-privateaccess): Are you aware of any other parts of the system calling some of your methods by reflection?

Answer: No.

Question (exec-process): Do you execute an external process from your module? How do you ensure that the result is the same on different platforms? Do you parse output? Do you depend on result code?

Answer: No.

Question (exec-introspection): Does your module use any kind of runtime type information (`instanceof`, work with `java.lang.Class`, etc.)?

Answer: No.

Question (exec-threading): What threading models, if any, does your module adhere to?

Answer: No special threading models used.

Question (security-policy): Does your functionality require modifications to the standard policy file?

Answer: No.

Question (security-grant): Does your code grant additional rights to some other code?

Answer: No.

## Format of files and protocols

Question (format-types): Which protocols and file formats (if any) does your module read or write on disk, or transmit or receive over the network? Do you generate an ant build script? Can it be edited and modified?

Answer: No files read or written to the disk.

Question (format-dnd): Which protocols (if any) does your code understand during Drag & Drop?

Answer: No D&D.

Question (format-clipboard): Which data flavors (if any) does your code read from or insert to the clipboard (by access to clipboard on means calling methods on `java.awt.datatransfer.Transferable`?

Answer: No clipboard support.

## Performance and Scalability

Question (perf-startup): Does your module run any code on startup?

Answer: No.

Question (perf-exit): Does your module run any code on exit?

Answer: No.

Question (perf-scale): Which external criteria influence the performance of your program (size of file in editor, number of files in menu, in source directory, etc.) and how well your code scales?

Answer: Number of initialized MimeLookups. Since the `MimeLookup` delegates to ProxyLookup performance during lookup depends on the performance of ProxyLookup. `LayerMimeLookupInitializer` instantiates the object found in the layer only during direct lookup of the particular class using LazyLookup (inner class defined in `LayerMimeLookupInitializer`).

Question (perf-limit): Are there any hard-coded or practical limits in the number or size of elements your code can handle?

Answer: No limits.

Question (perf-mem): How much memory does your component consume? Estimate with a relation to the number of windows, etc.

Answer: `MimeLookup` caches instances of mime sensitive MimeLookups in static Map, instances of children MimeLookups in Map, InitializerListeners and Initializers in Lists. `LayerMimeLookupInitializer` also caches mime sensitive LayerMimeLookupInitializers and LazyLookups.

Question (perf-wakeup): Does any piece of your code wake up periodically and do something even when the system is otherwise idle (no user interaction)?

Answer: No.

Question (perf-progress): Does your module execute any long-running tasks?

Answer: No.

Question (perf-huge_dialogs): Does your module contain any dialogs or wizards with a large number of GUI controls such as combo boxes, lists, trees, or text areas?

Answer: No.

Question (perf-menus): Does your module use dynamically updated context menus, or context-sensitive actions with complicated and slow enablement logic?

Answer: No.

Question (perf-spi): How the performance of the plugged in code will be enforced?

Answer: Pluggins are just clients lookups that are installed into `MimeLookup`. The performance should be influenced by the lookupable object gathering by clients lookups. `LayerMimeLookupInitializer` (client lookup provided by mimelookup module) provides a LazyLookup, it instantiates the object found in the layer only during direct lookup of the particular class. Otherwise performance should be similar as ProxyLookup.

Built on December 7 2006. | Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.