Overview

# NetBeans Architecture Answers for Utilities module

- Author: jtulach@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?
UtilitiesAPI | Exported | Official | .../org/openide/util/doc-files/api.html
LookupAPI | Exported | Official | .../util/lookup/doc-files/lookup-api.html allows the discovery
LookupSPI | Exported | Official | .../util/lookup/doc-files/lookup-spi.html simplifies creation and registration of own lookup objects
ProviderRegistrationMechanism | Imported | Standard | ...//java.sun.com/j2se/1.3/docs/guide/jar/jar.html
ProviderRegistrationRemoval | Exported | Under Development | .../org/openide/util/doc-files/api.html
AWTBridge | Exported | Private | .../modules/openide/util/AWTBridge.java a class that is looked up in Lookup.getDefault() and if registered can provide better UI elements for Actions.
ParserFactory | Imported | Private | The XMLUtil class is loading a class from `core/core.jar` to initialize the correct parser factory. This is a fix for issue 68942.
SharedClassObject.setAccessible | Exported | Under Development | .../org/openide/util/SharedClassObject.html used to instantiate subclasses.
WeakListener.setAccessible | Exported | Under Development | .../org/openide/util/WeakListeners.html used to call the remove method using reflection
Lookups.metaInfServices | Exported | Under Development | .../org/openide/util/lookup/Lookups.html calls constructor of registered classes using reflection

##### Group of property interfaces

Interface Name | In/Out | Stability | Specified in What Document?
BrandingSupport | Exported | Official | org/openide/util/NbBundle.html which is similar to localization but also supports changes of resorces for application build on top of NetBeans. This is handled by our class NbBundle which reimplements the JDK's standard ResourceBundle to to take branding into the consideration.
HelpID | Exported | Standard | org/openide/util/HelpCtx.html read from JComponent.getClientProperty to simulate the standard javahelp behaviour and extract the help id for given component.
iconBase | Imported | Standard | SystemAction reacts to expected requests from UI Utilities module for `iconBase` property by returning value based on its result of its `iconResource()` method.
org.openide.util.SharedClassObject.initialize | Exported | Private | For purposes of SystemOption the SharedClassObject handles `getProperty ("org.openide.util.SharedClassObject.initialize")` in a special way, by returning `null` if initialization is not running and `Boolean.TRUE` if it is.
OpenIDE-Transmodal-Action | Exported | Under Development | CallbackSystemAction checks whether `action.getValue("OpenIDE-Transmodal-Action")` returns `Boolean.TRUE` to enable it in dialogs, otherwise the action is disabled when there is an open dialog.
netbeans.screen.insets | Exported | Private | Influences results of Utilities.getUsableScreenBounds
netbeans.taskbar.height | Exported | Private | Influences results of Utilities.getUsableScreenBounds
line.separator | Imported | Standard | used on few places
org.openide.util.Lookup | Exported | Under Development | checked by the initialization of the Lookup.getDefault() and can contain name of a class that extends `org.openide.util.Lookup` and has public constructor, that should be instantiated and returned from Lookup.getDefault() the class will be loaded by Thread.currentThread().getContextClassLoader() classloader the first time `Lookup.getDefault` is invoked. The property can also contain value `"-"` which means to completely disable the lookup instantiation and return Lookup.EMPTY from Lookup.getDefault(). If the property is unspecified, the default `MetaInfServicesLookup` is constructed for `Thread.currentThread().getContextclassLoader()` that implements the JDK's standard. If, by a chance an instance of Lookup.Provider is found in there, its lookup is returned as result. Otherwise the `MetaInfServicesLookup` is the result of Lookup.getDefault().

##### Group of java.io.File interfaces

Interface Name | In/Out | Stability | Specified in What Document?
FileLocation | Exported | Under Development | the JAR file is located in platform cluster under `lib/org-openide-util.jar`
TranslateNames | Exported | Official | org/openide/util/Utilities.html Utilities.translate reads `META-INF/netbeans/translate.names` files from JARs

##### Group of lookup interfaces

Interface Name | In/Out | Stability | Specified in What Document?
ActionManagerInvocation | Exported | Private | because of the API separation, CallableSystemAction uses lookup for `ActionsBridge` provided by `org-openide-actions` module when looking for org.openide.actions.ActionManager implementation.
LookupInitializationLookup | Exported | Under Development | #property-org.openide.util.Lookup during initialization of the Lookup.getDefault() the Lookup.Provider is being searched
LookupSharedClassObject | Exported | Under Development | singleton subclasses of SharedClassObject are searched for using Lookup.
LookupContextGlobalProvider | Exported | Stable | actionsGlobalContext searches for ContextGlobalProvider in Lookup.getDefault(). The provider is usually provided by window system implementation.
LookupEntityCatalog | Exported | Under Development | EntityCatalog delegates its methods to all instances of EntityCatalogs found in Lookup
LookupErrorManager | Exported | Under Development | ErrorManager delegates its methods to all instances of ErrorManagers found in Lookup
LookupClassLoader | Exported | Under Development | Nearly all resource looking functions and reflective code uses ClassLoader obtained from Lookup.getDefault() for loading system wide resources.

## General Information

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

Answer:

Described in the overall answer.

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

Answer:

This module contains general classes needed in NetBeans, extensions to basic JRE contepts, useful methods and other UtilitiesAPI classes.

Also this module defines the Lookup which the NetBeans way for dynamic registration and lookup of components in our modularized component system. It allows lookup and discovery of features by description of their interfaces. The classes are devided into two parts. The LookupAPI - allows the discovery and the LookupSPI - simplifies creation and registration of own lookup objects.

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: There is a great introduction to Lookup and its usage in its javadoc. Here is just a list of frequently asked or interesting questions slowly expanding as people ask them:

### Lookup faq:

#### How to specify that a service in Lookup should be available only on Windows?

Q: Most of the time I specify interfaces that I want to add to the Lookup class in the layer.xml file. But, let's say I have a platform-specific interface (something on Windows only, for instance).

How can I specify (in the xml, or programmatically) that this service should only be added to the Lookup if the platform is Windows? >

In general there are three ways to achieve this.

- It is possible to write a specific module and enable it only on windows. See os specific modules documentation. Then you can put a registration of your instance into your module's META-INF/services directory and it will be available only on Windows.

- Another possibility that does not require new module, but which executes a code on startup (which may have performance implications) is to use `methodvalue` attribute. Register your instance in layer using `your-Object.instance` file as described at services documentation and in your factory method either return the instance your want or `null` depending on result of Utilities.isWindows() call.

- In some cases, the interface for which you will register an implementation permits a no-operation semantics. For example, `InstalledFileLocator.locate(...)` can return a valid `File`, or null. You could always register an `InstalledFileLocator` instance yet disable it on non-Windows platforms (always returning null).

#### How shall I write an extension point for my module?

Q: I have more modules one of them providing the core functionality and few more that wish to extend it. What is the right way to do it? How does the Netbeans platform declare such extension point?

Start with declaring an extension interface in your core module and put it into the module's public packages. Imagine for example that the core module is in JAR file `org-my-netbeans-coremodule.jar` and already contains in manifests line like `OpenIDE-Module: org.my.netbeans.coremodule/1` and wants to display various tips of the day provided by other modules and thus defines:

    package org.my.netbeans.coremodule;

    public interface TipsOfTheDayProvider {
        public String provideTipOfTheDay ();
    }

And in its manifest adds line `OpenIDE-Module-Public-Packages: org.my.netbeans.coremodule.*` to specify that this package contains exported API and shall be accessible to other modules.

When the core module is about to display the tip of the day it can ask the system for all registered instances of the `TipsOfTheDayProvider`, randomly select one of them:

    import java.util.Collection;
    import java.util.Collections;
    import org.openide.util.Lookup;

    Lookup.Result result = Lookup.getDefault ().lookup (new Lookup.Template (TipsOfTheDayProvider.class));
    Collection c = result.allInstances ();
    Collections.shuffle (c);
    TipsOfTheDayProvider selected = (TipsOfTheDayProvider)c.iterator ().next ();

and then display the tip. Simple, trivial, just by the usage of Lookup interface once creates a registry that other modules can enhance. But such enhancing of course requires work on the other side. Each module that would like to register its `TipsOfTheDayProvider` needs to depend on the core module - add `OpenIDE-Module-Module-Dependencies: org.my.netbeans.coremodule/1` into its manifest and write a class with its own implementation of the provider:

    package org.my.netbeans.extramodule;

    class ExtraTip implements TipsOfTheDayProvider {
        public String provideTipOfTheDay () {
            return "Do you know that in order to write extension point you should use Lookup?";
        }
    }

Then, the only necessary thing is to register such class by using the J2SE standard ProviderRegistrationMechanism into plain text file `META-INF/services/org.my.netbeans.coremodule.TipsOfTheDayProvider` in the module JAR containing just one line:

    org.my.netbeans.extramodule.ExtraTip

and your modules are now ready to communicate using your own extension point.

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

Answer:

The module has been around since 1997 and is stilly improved from time to time.

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

Answer:

There is a lot of unit tests in CVS.

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:

This module is independent of other NetBeans modules and can be used independently. For better interaction with UI parts of NetBeans it however indirectly communicates with UI Utilities module using AWTBridge - a class that is looked up in Lookup.getDefault() and if registered can provide better UI elements for Actions.

Default answer to this question is:

These modules are required in project.xml file:

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

Answer:

Reexports XML APIs so needs some XML parser implementation, but as one is provided in any 1.4 java, it in fact has no dependencies except on JRE.

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

Answer:

Platform independent.

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

Answer:

Currently JRE 1.4 is needed.

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

Answer:

JRE is enough.

## Deployment

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

Answer:

FileLocation - the JAR file is located in platform cluster under `lib/org-openide-util.jar` .

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

Answer:

No reason to not deploy nbm.

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:

Module is on real java classpath and as such it has to be in the shared directory.

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

Answer:

Yes, public packages declared.

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

Answer:

OpenIDE-Module-Module-Dependencies: org.openide.util > 6.8.22

## Compatibility with environment

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

Answer:

There is not much things to localize in this module, so it is properly localized. But it is good to note that the module adds BrandingSupport - which is similar to localization but also supports changes of resorces for application build on top of NetBeans. This is handled by our class NbBundle which reimplements the JDK's standard ResourceBundle to to take branding into the consideration. .

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

Answer:

The default lookup registration follows the JDK's ProviderRegistrationMechanism but enhances it to also support the ProviderRegistrationRemoval.

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:

This module has no settings.

## Access to resources

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

Answer:

No.

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:

No.

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

Answer:

No.

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:

- LookupInitializationLookup - during initialization of the Lookup.getDefault() the Lookup.Provider is being searched.
- LookupSharedClassObject - singleton subclasses of SharedClassObject are searched for using Lookup. .
- LookupContextGlobalProvider - actionsGlobalContext searches for ContextGlobalProvider in Lookup.getDefault(). The provider is usually provided by window system implementation. .
- LookupEntityCatalog - EntityCatalog delegates its methods to all instances of EntityCatalogs found in Lookup .
- LookupErrorManager - ErrorManager delegates its methods to all instances of ErrorManagers found in Lookup .
- LookupClassLoader - Nearly all resource looking functions and reflective code uses ClassLoader obtained from Lookup.getDefault() for loading system wide resources. .

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

Answer:

No.

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:

- netbeans.screen.insets - Influences results of Utilities.getUsableScreenBounds.
- netbeans.taskbar.height - Influences results of Utilities.getUsableScreenBounds.
- line.separator - used on few places .
- org.openide.util.Lookup - checked by the initialization of the Lookup.getDefault() and can contain name of a class that extends `org.openide.util.Lookup` and has public constructor, that should be instantiated and returned from Lookup.getDefault() the class will be loaded by Thread.currentThread().getContextClassLoader() classloader the first time `Lookup.getDefault` is invoked.

  The property can also contain value `"-"` which means to completely disable the lookup instantiation and return Lookup.EMPTY from Lookup.getDefault().

  If the property is unspecified, the default `MetaInfServicesLookup` is constructed for `Thread.currentThread().getContextclassLoader()` that implements the JDK's standard. If, by a chance an instance of Lookup.Provider is found in there, its lookup is returned as result. Otherwise the `MetaInfServicesLookup` is the result of Lookup.getDefault().

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

Answer:

- HelpID - read from JComponent.getClientProperty to simulate the standard javahelp behaviour and extract the help id for given component.
- iconBase - SystemAction reacts to expected requests from UI Utilities module for `iconBase` property by returning value based on its result of its `iconResource()` method.
- org.openide.util.SharedClassObject.initialize - For purposes of SystemOption the SharedClassObject handles `getProperty ("org.openide.util.SharedClassObject.initialize")` in a special way, by returning `null` if initialization is not running and `Boolean.TRUE` if it is.
- OpenIDE-Transmodal-Action - CallbackSystemAction checks whether `action.getValue("OpenIDE-Transmodal-Action")` returns `Boolean.TRUE` to enable it in dialogs, otherwise the action is disabled when there is an open dialog.

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, we do not create own classloader.

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

Answer:

SharedClassObject.setAccessible - used to instantiate subclasses. . WeakListener.setAccessible - used to call the remove method using reflection . Lookups.metaInfServices - calls constructor of registered classes using reflection . ActionManagerInvocation - because of the API separation, CallableSystemAction uses lookup for `ActionsBridge` provided by `org-openide-actions` module when looking for org.openide.actions.ActionManager implementation. .

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 external processes executed.

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

Answer:

Utilities provide wrapper for java beans introspection. ParserFactory - The XMLUtil class is loading a class from `core/core.jar` to initialize the correct parser factory. This is a fix for issue 68942.

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

Answer:

XXX no answer for exec-threading

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

Answer:

No security permissions manipulated.

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

Answer:

No security permitions manipulated.

## 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:

The META-INF/services/... files. TranslateNames - Utilities.translate reads `META-INF/netbeans/translate.names` files from JARs.

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

Answer:

The same as for clipboard.

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:

MultiTransferObject can be used in Transferable to represent content composed of multiple independent Transferables.

## 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:

Nothing.

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:

Lookup code scales linearily.

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

Answer:

The default implementation of the `MetaInfServicesLookup` just keeps hashmap between queried classes and their implementations. The amount of memory is linear to amount of registered classes, but of course we are not counting the memory occupied by the instances which the lookup creates, that can be arbitrary.

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

Answer:

There are no big data structures. The amount of memory occupied by instances of AbstractLookup is measured by unit tests.

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:

Actions declared as CallableSystemAction.asynchronous() are executed outside of AWT thread on a dedicated request processor one.

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:

There are no menus.

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

Answer:

No enforcing is done.

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