250 likes | 466 Views
Modularity…. ‘Modularity’ includes…. packaging/bundles, access and visibility discovery classloading instantiation (including deferred or lazy) managing dependencies - dependency injection vs. service locator declarative approaches (i.e. using annotations) life cycle management
E N D
‘Modularity’ includes… • packaging/bundles, access and visibility • discovery • classloading • instantiation (including deferred or lazy) • managing dependencies - dependency injection vs. service locator • declarative approaches (i.e. using annotations) • life cycle management • registration • deployment and execution • resource loading
Modules • Aggregates of classes • Contain one or more packages • Have their own visibility rules • Have a version tag • Declare versioned dependencies • No more global classpath with modules!
Modular Applications • Discover their components at runtime • Modules (bundles) • Handles the lifecycle • Dynamic loading / reloading • May add/remove/reload components at runtime • Service Registry • Must satisfy dependencies between components • Have API contracts between components • Light container / framework • Run inside a runtime container
Modularity Approaches/Options • Classloader • JDK ServiceLoader • NetBeans Lookup API • OSGi
Lookup – NetBeans Modularity Solution • Small, NetBeans independent library • Part of NetBeans org-openide-util.jar • org.openide.util.Lookup • Works with any version of Java (unlike JDK's ServiceLoader) • A Lookup is dynamic • Can fire changes • A Lookup is instantiable • You can make one and use it • Lookups are composable • ProxyLookup can combine and switch between other lookups and fire changes
Similarities between Lookup and OSGi • Runtime container manages lifecycle and dependencies of modules • Both are packaged as JAR, metadata stored in META-INF/MANIFEST.MF and some other information • Runtime starts up, read meta information and sets up dependencies • Every module has its own classloader loading explicitly declared dependencies only • Dynamic
OSGi & NetBeans • http://wiki.apidesign.org/wiki/OSGiAndNetBeans • As of NetBeans 6.9, much support for OSGi. • A Service Registry for OSGi Using NetBeans Lookup API • http://kenai.com/projects/osgilookups ServiceLookupForPath BundleWrapperClassLoader
Looking at Lookup • Specific uses of this API: • As a Service loader • Lookup.getDefault() • Communication among components... • LookupListener • Global Selection Management • Utilities.actionsGlobalContext() • Selections, Actions, enablement
Service Registry - Separating API and SPI public interface MyService { ... } MyService s = Lookup.getDefault().lookup(MyService.class); • Would return an implementation class • Possibly from a different module Collection<? extends MyService> s = Lookup.getDefault().lookupAll(MyService.class);
META-INF/services mechanism • (Since JDK 1.3) • Somewhere in classpath the file: META-INF/services/mypackage.MyClass • with a single line containing the name of the implementation class; for instance: myotherpackage.MyClassImpl
@ServiceProvider Annotation package my.module; import org.netbeans.spi.whatever.Thing; import org.openide.util.lookup.ServiceProvider; @ServiceProvider(service=Thing.class) public class MyThing implements Thing {...} would result in a resource file META-INF/services/org.netbeans.spi.whatever.Thing containing the single line of text: my.module.MyThing • Declarative Registration Using Annotations • See: http://wiki.netbeans.org/DeclarativeRegistrationUsingAnnotations