2. Agenda
› How to make my Java more modular
› Discover and exploit OSGI
› Tools, tools, tools…
› Output
3. About me
Who: Andrii Krokhmalnyi
Domain: Java/Java EE
Company: EPAM Systems
Experience in OSGI: about 2 years
4.
5. Modular design
› Separates functionality into distinct isolated sections (modules)
› Defines a standard interface for modules
› Adds ability to replace modules transparently for users of the system
› Improves scalability and robustness of the system
› Makes functional parts of the system reusable and easily updatable
› …But adds some extent of complexity in overall system orchestration
6. Modularity in Java: Service Provider Interface
META-INF/services/com.foo.logging.LogRenderer
com.foo.logging.impl.ConsoleLogRenderer
com.foo.service.MyService.java
public static void main(String[] args) {
ServiceLoader<LogRenderer> renderers =
ServiceLoader.load(LogRenderer.class);
for (LogRenderer renderer : renderers) {
...
}
7. Modularity in Java: OSGI
MANIFEST.MF
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Bundle-SymbolicName: foo-service
Bundle-Activator: com.foo.service.Activator
Export-Package: com.foo.dao,com.foo.domain,com.foo.service
Import-Package: com.foo.logging;version="[2.0,3)"
image from http://wallpapersus.com/
12. Core :: Container and bundles
image from http://www.osgi.org
OSGI Container
Class Loader #1
Bundle #1
Class Loader #2
Bundle #2
Class Loader #N
Bundle #N
(modules)
a regular jar archive with tweaked manifest
26. Overview of Apache Felix
Apache License 2.0
OSGI R4.2 compatible
Provides a web console
Provides a Maven plugin
Active development
Proved solution
Good documentation
27. Overview of Apache Karaf
Apache License 2.0
Supports both Felix and Equinox
Lightweight and extensible
Interactive shell console
Hot deployment
Native OS integration
Extra management tools
Active development
permits = friendly modules list (current module is accessible only for those)class = entry class
Framework API:org.osgi.framework packageBundle Wiring API: for wiring bundles (requirements and capabilities), classes BundleRevision, BundleWiring, FrameworkWiringStart Level API:describes how to enable a management agent to control the relative starting and stopping order of bundles in an OSGi Service Platform (setting start level, in Karafosgi:bundle-level, osgi:start-level).Remote Services: setup of remote access for the services
Class loading: “uses” marks packages that exported API use (method signatures, parent classes), so the framework can check if those don’t conflict with imported packagesif java.* => parentif in Import-Package => exportersearch in Require-Bundlesearch in Bundle-ClassPathif in DynamicImport-Package => wiring
Web-ContextPath: OSGI 4.2 specWebapp-Context: PAX Web specific
Device Access specification supports the coordination of automatic detection and attachment ofexisting devices on an OSGi Service Platform, facilitates hot-plugging and -unplugging of newdevices, and downloads and installs device drivers on demand.Preferences = system or user-specific data storageUser Admin = authentication/authorizationMonitor Admin = status variables get/set/reset from administrative bundles