MarceloLeite2604/osgi-example

β˜… 0Forks 0JavaGitHub β†—Compare

README

OSGI Example - Pure OSGi Implementation

A comprehensive, multi-module OSGi workspace demonstrating "pure" OSGi concepts using the programmatic approach: BundleActivators and ServiceTrackers, without Declarative Services or modern frameworks.

🎯 Project Philosophy

This project teaches OSGi fundamentals "the hard way" to build deep understanding before adopting convenience frameworks.

What This Means:

  • βœ… Manual BundleActivator for lifecycle management
  • βœ… Explicit context.registerService() for service registration
  • βœ… ServiceTracker with ServiceTrackerCustomizer for dynamic service consumption
  • βœ… Hand-written MANIFEST.MF configuration
  • βœ… Raw JDBC (no JPA), raw Servlets (no JAX-RS), manual JSON parsing
  • ❌ NO Declarative Services (@Component, @Reference)
  • ❌ NO Blueprint, Spring DM, or OSGi Annotations

πŸ—οΈ Architecture

Module Structure

osgi-example/
β”œβ”€β”€ api     β†’ Service contract (SensorService interface, POJOs)
β”œβ”€β”€ core    β†’ Business logic (JDBC PostgreSQL implementation)
β”œβ”€β”€ web     β†’ HTTP REST API (Raw HttpServlet, ServiceTrackers)
β”œβ”€β”€ target  β†’ P2 dependency resolution (Eclipse Equinox, Jetty, JDBC)
└── releng  β†’ Product packaging (Start levels, launcher configuration)

Technology Stack

Layer Technology Version
Language Java 17 (LTS)
OSGi Runtime Eclipse Equinox 2024-03
Build System Maven + Eclipse Tycho 4.0.4
HTTP Server Eclipse Jetty (Equinox HTTP Service) Latest
Database PostgreSQL + JDBC Driver 15 / 42.7.2
Deployment Docker + Docker Compose Latest

πŸš€ Quick Start

Build and Run

cd osgi-example
docker-compose up --build

Test the API

# GET all readings
curl http://localhost:8080/api/sensors

# POST new reading
curl -X POST http://localhost:8080/api/sensors \
  -H 'Content-Type: application/json' \
  -d '{"sensorId":"sensor-001","temperature":23.5,"humidity":45.2}'

Access OSGi Console

docker logs -f osgi-application
# Type 'ss' to see bundle status
# Type 'services' to see registered services

πŸ“š Complete Documentation

This project includes comprehensive documentation (100+ pages):

Document Purpose
DOCUMENTATION_INDEX.md πŸ“‘ Master index of all documentation
QUICK_REFERENCE.md πŸ“Œ One-page cheat sheet
BUILD_GUIDE.md πŸ”¨ Complete build and setup instructions
ARCHITECTURE.md πŸŽ“ Deep technical dive and design rationale
DIAGRAMS.md πŸ“Š Visual architecture diagrams
PROJECT_STRUCTURE.md πŸ—ΊοΈ File tree and navigation guide
OSGI_CONSOLE_REFERENCE.md πŸ› Console commands and troubleshooting
DELIVERY_SUMMARY.md βœ… Project completion checklist

β†’ Start with DOCUMENTATION_INDEX.md for guided learning paths

πŸŽ“ What You'll Learn

Core OSGi Concepts

  1. BundleActivator Pattern - Manual service registration and lifecycle
  2. ServiceTracker Pattern - Dynamic service discovery and consumption
  3. Service Registry - Understanding OSGi's runtime service model
  4. Bundle Dependencies - Import-Package vs Export-Package
  5. Start Levels - Controlling bundle activation order
  6. Eclipse Tycho - Building OSGi with Maven

Demonstrated "Hard Way" Techniques

  • Manual service registration: context.registerService()
  • ServiceTrackerCustomizer for service lifecycle events
  • Raw servlet registration with HttpService
  • Direct JDBC without connection pooling frameworks
  • Manual JSON serialization/deserialization
  • Explicit MANIFEST.MF configuration

πŸ’» Build from Source

# Maven build only
mvn clean verify

# Run the built product
cd com.github.marceloleite2604.osgiexample.releng/target/products/.../
./osgi-example -console -consoleLog

Note: Requires Java 17 and Maven 3.6+

πŸ”§ Import into Eclipse IDE

  1. File β†’ Import β†’ Maven β†’ Existing Maven Projects
  2. Select osgi-example directory β†’ Import all modules
  3. Open com.github.marceloleite2604.osgiexample.target/osgi-example.target
  4. Click "Set as Active Target Platform"
  5. Open com.github.marceloleite2604.osgiexample.releng/osgi-example.product
  6. Click "Launch an Eclipse application"

πŸ› Troubleshooting

Quick diagnostics:

# Check bundle status
osgi> ss

# Diagnose bundle issues
osgi> diag <bundle-id>

# List registered services
osgi> services

# View logs
docker logs -f osgi-application

β†’ See OSGI_CONSOLE_REFERENCE.md for comprehensive troubleshooting

🎯 Project Highlights

βœ… Pure OSGi - No shortcuts, no frameworks masking concepts
βœ… Production-Ready - Docker deployment with PostgreSQL
βœ… Comprehensive Documentation - 8 detailed guides
βœ… Real Integration - Database + HTTP + JSON
βœ… Eclipse Compatible - Import directly into Eclipse IDE
βœ… Educational Focus - Learn fundamentals deeply

πŸ”„ Service Lifecycle Example

This project demonstrates OSGi's dynamic nature:

// Core Bundle: Manual Registration
public void start(BundleContext context) {
    SensorServiceImpl impl = new SensorServiceImpl();
    context.registerService(SensorService.class, impl, null);
}

// Web Bundle: Dynamic Tracking
ServiceTracker<SensorService, SensorService> tracker = 
    new ServiceTracker<>(context, SensorService.class, 
        new ServiceTrackerCustomizer<>() {
            public SensorService addingService(ServiceReference ref) {
                // React to service appearing
            }
            public void removedService(ServiceReference ref, SensorService svc) {
                // React to service disappearing
            }
        });
tracker.open();

β†’ See ARCHITECTURE.md for complete flow diagrams

🚦 Next Steps

  1. Run the project - Follow Quick Start above
  2. Explore the console - Use OSGi commands to see dynamic behavior
  3. Read the architecture - Understand the "why" behind design decisions
  4. Modify the code - Add features, experiment with bundles
  5. Compare with DS - Appreciate modern conveniences after mastering fundamentals

πŸ‘€ Author

Marcelo Leite
GitHub: @MarceloLeite2604

πŸ“„ License

Educational example demonstrating OSGi fundamentals.


Master OSGi the right way. Understand the fundamentals before using frameworks. πŸŽ“

Contributors

MarceloLeite2604

Issues