| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| |
| = DevFaqModuleLoadUnloadNotification |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqModuleLoadUnloadNotification |
| :description: Apache NetBeans wiki DevFaqModuleLoadUnloadNotification |
| :toc: left |
| :toc-title: |
| :syntax: true |
| |
| === How can code in one module be notified when other modules are loaded or unloaded? |
| |
| Let's begin by stating that you probably do not _need_ to know when other modules are loaded or unloaded. |
| The module system takes care of dependency management for you, |
| so your module should never be loaded unless all of its stated dependencies are loaded too. |
| |
| The normal means of communicating between modules about available services |
| (which you could consider _indirect dependencies_) is using the Lookup API. |
| If what you really wanted was to know when a _service_ became available, |
| you do not need to listen to module load or unload events as such; |
| which module hosts the service is not of direct interest. |
| Instead: |
| |
| 1. Define a service `some.Interface` in some module. |
| 2. Implement that interface using `some.Implementation` in some other module. Register it with `@ServiceProvider(service=Interface.class)`. |
| 3. In some module consuming the service, use |
| [source,java] |
| ---- |
| |
| import some.Interface; |
| // ... |
| private static final Lookup.Result<Interface> services = |
| Lookup.getDefault().lookupResult(Interface.class); |
| static { |
| services.addLookupListener(new LookupListener() { |
| public void resultChanged(LookupEvent ev) { |
| // some module was loaded or unloaded! |
| } |
| }); |
| } |
| // ... |
| for (Interface service : services.allInstances()) { |
| // use a currently available service |
| } |
| ---- |
| |
| If you simply want to run some code when _your_ module loads or unloads, |
| registering an instance of |
| link:http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/ModuleInstall.html[ModuleInstall] |
| will do the trick. |
| There is even a wizard in the IDE for setting this up. |
| |
| But in the very rare case that you want to be notified when _other_ modules are loaded or unloaded, |
| it is possible because: |
| |
| 1. You can use link:http://bits.netbeans.org/dev/javadoc/org-openide-util/org/openide/util/Lookup.Result.html[Lookup.Result] to listen to changes in the contents of a `Lookup`, even the global lookup. |
| 2. Every module installs an instance of link:http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/ModuleInfo.html[ModuleInfo] into the default `Lookup` so the module system (or other code) can find out details about the module including its code name base, version numbers and display name. |
| |
| By using these two facts together, it is possible to listen to changes in the installed modules by running code like this at some point in the application's lifecycle (link:DevFaqAppLifecycleHooks.asciidoc[DevFaqAppLifecycleHooks]): |
| |
| [source,java] |
| ---- |
| |
| final Lookup.Result<ModuleInfo> result = |
| Lookup.getDefault().lookupResult(ModuleInfo.class); |
| result.addLookupListener(new LookupListener() { |
| public void resultChanged(LookupEvent event) { |
| // it seems a module was installed or removed |
| } |
| }); |
| ---- |
| |
| Once you detect that a module has been created you may also want to register a `PropertyChangeListener` and listen to `ModuleInfo.PROP_ENABLED`. |
| (A module present in the installation will provide a `ModuleInfo` but `isEnabled` might be false if it is not currently loaded.) |
| |
| <hr/> |
| Applies to: NetBeans 6.7 and later |
| |
| === Apache Migration Information |
| |
| The content in this page was kindly donated by Oracle Corp. to the |
| Apache Software Foundation. |
| |
| This page was exported from link:http://wiki.netbeans.org/DevFaqModuleLoadUnloadNotification[http://wiki.netbeans.org/DevFaqModuleLoadUnloadNotification] , |
| that was last modified by NetBeans user Jglick |
| on 2009-12-03T14:18:59Z. |
| |
| |
| *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed. |