O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

OSGi and Apache Felix 3.0

Book Description

Get a firm grasp on OSGi concepts with this superbly written guide to building your own applications using the Felix framework. Totally practical and with lots of handholding, it’s the perfect primer for absolute beginners.

  • Build a completely operational real-life application composed of multiple bundles and a web front end using Felix

  • Get yourself acquainted with the OSGi concepts, in an easy-to-follow progressive manner

  • Learn everything needed about the Felix Framework and get familiar with Gogo, its command-line shell to start developing your OSGi applications

  • Simplify your OSGi development experience by learning about Felix iPOJO

  • A relentlessly practical beginner's guide that will walk you through making real-life OSGi applications while showing you the development tools (Maven, Eclipse, and so on) that will make the journey more enjoyable

  • In Detail

    The OSGi specification is a module system and service platform that implements a complete and dynamic component model. Wasn't that a complicated definition! So how would you really use it in practical modular applications? Let this book break down the seemingly overwhelming OSGi standards for you by explaining Apache Felix's powerful architecture in a simple and easy-to-understand manner using Apache Felix framework to get you up and running sooner than you could expect.

    The OSGi standards have found a wide range of applications in the context of the Enterprise, Telecommunications, Telematics, Smart Home, E-Health, and Mobile applications, to name just a few. Apache Felix is one of the most famous implementations of the OSGi framework specification. This book introduces OSGi on the simple and extensible Felix framework and guides you from the development environment setup to the troubleshooting of potential issues, walking you through the development of an OSGi-based application and explaining relevant software design concepts.

    The book starts with an introduction to the OSGi Service Platform, its parts, and its bundle structure. It then walks you through setting up the Felix framework and your development environment. It describes the Felix Framework and how to operate it using Gogo. It will teach you everything possible about the practical implementation of OSGi using the Felix Framework as a launch pad.

    The book then kicks off the Bookshelf project, a case study that will be used to progressively explain the important concepts around OSGi using the Felix framework. The Bookshelf project feature trail will set the context to explain OSGi headers, the bundle activator, the bundle context, and so on.

    As you implement the bookshelf step by step, you learn about OBR repositories, dependency management, and bundle version management with Felix.

    Moving ahead, a few more advanced topics are covered, such as using iPOJO for dependency injection and service registration; then the book moves on to the implementation of a web-based graphical interface, first using a simple Servlet, and then building a JSP-based Web Application Bundle.

    OSGi service specifications such as the Log Service, Http Service, and Web Container are explained. Finally, the book describes some of the common pitfalls during bundle development, and gives hints on troubleshooting them in Felix.

    A step-by-step beginner's guide to implementing a real-life OSGi application, with the Apache Felix 3.0 framework

    Table of Contents

    1. OSGi and Apache Felix 3.0
      1. OSGi and Apache Felix 3.0
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      6. 1. Quick Intro to Felix and OSGi
        1. What is OSGi?
        2. The framework layout
          1. The functional layers
          2. The bundle lifecycle states
          3. Bundle wiring
        3. The shared service registry
        4. Working with bundles
          1. Anatomy of a bundle
          2. The OSGi headers
            1. Mandatory headers
            2. Functional headers
            3. Information headers
        5. Start levels
          1. The Start Level Service
          2. The active start level
          3. Using start levels
        6. Apache Felix and sub-projects
          1. Pop Quiz
        7. Summary
      7. 2. Setting Up the Environment
        1. Setting up the Felix framework
          1. Checking that a JDK is installed
          2. Download and unpack the Felix distribution
        2. Time for action - downloading and installing Felix
          1. What's in the box?
        3. Time for action - starting Felix
          1. What just happened?
        4. Maven2 and Felix
          1. Installing Maven2
          2. Life-cycles and phases
          3. Maven plugins
          4. The POM
          5. The Felix Maven Plugins
          6. Pop quiz
        5. Summary
      8. 3. Felix Gogo
        1. The Tiny Shell Language
          1. Chained execution
          2. Variable assignment and referencing
          3. Value types
          4. Object properties and operations
          5. Execution quotes
          6. Commands and scopes
        2. felix scope commands
          1. Listing installed bundles: lb
          2. help
          3. install
          4. update
          5. resolve
          6. stop and start
          7. uninstall
          8. refresh
          9. headers and inspect
          10. which
          11. log
          12. cd and ls
          13. frameworklevel and bundlelevel
        3. gogo scope commands
          1. echo
          2. grep
          3. cat
          4. tac
          5. set
          6. Pop Quiz
        4. Summary
      9. 4. Let's Get Started: The Bookshelf Project
        1. A simple Bookshelf project
          1. The data inventory tier
          2. What's CRUD?
          3. The business logic tier
          4. The user interaction tier
        2. OSGi, Felix, and...
        3. Taking it step-by-step
        4. Some conventions
        5. Summary
      10. 5. The Book Inventory Bundle
        1. Set up the Book Inventory API Bundle project
        2. Time for action - setting up the project skeleton
        3. Time for action - creating the project POM
          1. The Bundle identity
          2. More on bundle versions
          3. Dependencies
          4. Customizing the build
          5. Defining the distribution parameters
        4. The Book bean interface
          1. The Book bean attributes
        5. Time for action - creating the Book bean interface
          1. Have a go hero personalize the Book Bean API
        6. The Book Inventory interface
        7. Time for action - writing the BookInventory interface
        8. Build and deploy the bundle
        9. Time for action - building and deploying the bundle
          1. What just happened?
        10. Let's implement those interfaces
        11. Time for action - creating the POM
        12. Time for action - implementing a mutable book
        13. Time for action - implementing the mock (memory-stored) Book Inventory
          1. The factory method
          2. Implementing a mock getGoups()
          3. Storing a book
          4. Removing a stored book
          5. Loading a stored book
          6. Implementing the book search
        14. Writing the Bundle Activator
        15. Time for action - add a dependency to the OSGi Core library
        16. Time for action - creating the Activator
          1. More on Bundle Contexts
        17. Time for action - declaring Bundle-Activator
        18. Build and deploy the bundle
          1. Pop quiz
        19. Summary
      11. 6. Using the OSGi Bundle Repository
        1. OBR, the OSGi Bundle Repository
          1. The repository XML Descriptor
          2. Updating the OBR repository
        2. Using the OBR scope commands
          1. obr:repos
          2. obr:list
          3. obr:info
          4. obr:deploy
          5. obr:source and obr:javadoc
          6. Updating bundles in the repository
        3. Installing the Book Inventory bundles to Felix
        4. Time for action - install the book inventory bundles
          1. What just happened?
        5. On dependency management
          1. Pop Quiz
        6. Summary
      12. 7. The Bookshelf: First Stab
        1. The Bookshelf Service bundle
          1. Have a go hero preparing the bookshelf-service project
          2. Define the main Bookshelf Service interfaces
        2. Time for action - writing the APIs
          1. The Authentication interface
          2. The BookshelfService interface
          3. Have a go hero tailor the bundle to your liking
          4. Implementing the service
        3. Time for action - writing BookshelfServiceImpl
          1. Have a go hero -complete service implementation
        4. Time for action - implementing the service activator
          1. Framework service lookup
        5. Trying the BookshelfService
        6. Time for action - building the bundle
        7. Time for action - installing and testing the service
          1. What just happened?
        8. Time for action - fulfilling the missing dependency
          1. What Just Happened?
        9. On class visibility
          1. Pop quiz
        10. Summary
      13. 8. Adding a Command-Line Interface
        1. The Apache Felix Gogo Shell Service
        2. Time for action - creating the Bookshelf Service TUI bundle
        3. Implementing a Gogo Shell Service command
        4. Implementing the book:search command
        5. Time for action - adding the required dependencies
        6. Time for action - writing the BookshelfServiceProxy
          1. On Converters
        7. Time for action - implementing a bundle activator
        8. Time for action - packaging and installing
        9. Time for action - trying out the book:search command
        10. Time for action - cleaning up the bookshelf-service activator
          1. Implementing the book:add command
        11. Time for action - implementing the book-add command
          1. Go ahead hero- building and deploying the changes
        12. Updating an installed bundle
          1. What just happened?
        13. Trying the commands
          1. Go ahead hero- implementing the other commands
        14. Sourcing scripts
        15. Time for action - creating a book population script
        16. Summary
      14. 9. Improve the Bookshelf Service with iPOJO
        1. What is Inversion of Control?
          1. The Service Locator pattern
          2. The Dependency Injection pattern
          3. The Whiteboard pattern
          4. The Extender Pattern
        2. The iPOJO Felix sub-project
          1. Components and instances
          2. iPOJO Maven plugin
            1. The metadata file
              1. Components
              2. Instances
            2. Using the plugin
        3. Injecting iPOJOs
          1. Install the iPOJO service bundle
        4. Let iPOJO register the inventory implementation
        5. Time for action - creating the iPOJO metadata
          1. Update the POM
          2. Configure bundle for iPOJO
          3. Build and test it
          4. What just happened?
        6. The Felix iPOJO Gogo Command bundle
          1. ipojo scope commands usage
        7. Migrate the bookshelf service
        8. Time for action - removing lookups in the service implementation
        9. Time for action - writing the bookshelf service iPOJO configuration
          1. Update the POM
          2. Deploy and check
        10. iPOJO using annotations
          1. Overview
          2. Beginner's annotations
            1. @Component
            2. @Provides
            3. @Requires
            4. @ServiceProperty
            5. @Property
            6. @Instantiate
          3. Instantiating annotated components
        11. Update the text UI bundle
        12. Time for action - updating the BookshelfServiceProxyImpl
        13. Time for action - writing the iPOJO meta.xml
        14. Time for action - updating the POM
          1. Have a go hero- updating the bundles to use annotations
          2. Have a go hero- implementing a file-based bookshelf-inventory
        15. Summary
      15. 10. Improving the Logging
        1. On logging
          1. Logging levels
          2. Who's listening?
        2. The OSGi Log Service
          1. The Service end
          2. Usage of the Log Service
          3. The service provider end
        3. Apache Felix Log Service
          1. The log command
        4. Creating the log helper bundles
        5. Time for action - creating the bookshelf-log-api bundle
        6. Time for action creating the log helper implementation
          1. Implementing the BookshelfLogHelper service
        7. Add logging to the bookshelf-service
        8. Time for action updating the bundle POM
        9. Time for action - updating the bookshelf service logging calls
        10. Time for action - logging to BookshelfLogHelper
          1. Update bookshelf-service-tui dependency
          2. Have a go hero- adding the remaining logs
        11. Trying it out
        12. Using other Log Service implementations
        13. Summary
      16. 11. How About a Graphical Interface?
        1. The OSGi HTTP Service
          1. Component structure
          2. Registration of servlets
        2. iPOJO and the Whiteboard Extender
        3. Http Service implementations
          1. The Apache Felix Http Service
        4. Time for action - installing the Apache Felix Http Service
        5. A simple bookshelf web application
        6. Time for action - implementing the servlet
          1. The iPOJO configuration
          2. Implementing the operations
        7. Time for action - declaring the parameter constants
        8. Time for action - implementing the operations
          1. Have a go hero- implementing the remaining operations
        9. Trying it out
          1. What just happened?
          2. Pop Quiz
        10. Summary
      17. 12. The Web Management Console
        1. Getting started
          1. Installing the Web Console
        2. Time for action - installing commons-fileupload and commons-io
        3. Time for action - installing json
        4. Time for action - installing and starting the Web Console
        5. A quick overview
          1. Bundles
          2. Log Service
          3. OSGi Repository
          4. Services
          5. Shell
          6. System Information
        6. Apache Felix iPOJO WebConsole Plugin
        7. Summary
      18. 13. Improving the Graphics
        1. OSGi Web Containers
        2. Pax Web
        3. Time for action - installing the Pax Web bundles
          1. Uninstall previous http support
          2. Install PAX Web bundles
          3. Double-check the http service implementation
          4. What just happened?
        4. Our bookshelf-webapp
        5. Time for action - creating the bookshelf-webapp bundle
        6. Web application registration
        7. Time for action - setting up the web application bundle
          1. What just happened?
        8. Time for action - specifying dependencies
        9. Getting a service reference in JSP
        10. Time for action - ­ writing the session bean
          1. What just happened?
          2. Complete the authentication pages
        11. Time for action - using the service
        12. A first smoke test
        13. Implement the remaining pages
        14. Time for action - ­ implementing the list books by category page
          1. A note on JSP imports
        15. Time for action - explicit package imports
          1. Have a go hero implementing the remaining pages
          2. Search with authors
          3. Add book
          4. Have a go hero- extending the application
          5. Pop Quiz
        16. Summary
      19. 14. Pitfalls and Troubleshooting
        1. Common pitfalls
          1. I start my bundle but nothing happens
            1. Have you declared your bundle activator in the manifest?
            2. Do you have the correct logging level set?
          2. I update my bundle, but I can't see any change
            1. Are you updating the right bundle?
            2. Are you updating the right bundle location?
            3. Have you refreshed the bundle dependencies?
          3. I refresh my OBR but the bundles don't get updated
            1. Is the remote OBR being updated?
          4. The artifact JAR I need doesn't have OSGi entries in its manifest
            1. Creating the bundle manually
            2. Using the BND tool
          5. I get a "No impl service available" error with any shell command
            1. Re-initialize the environment
          6. I get a "No LogReaderService available" error with the log command
            1. Do you have a Log Service installed?
          7. I can't add/connect to an OBR
            1. Is that URL valid?
            2. Does the OBR have the right format?
            3. Do you need a proxy to access the Internet?
          8. I'm getting a "Jsp support is not enabled" error message
            1. Did you install JSP support?
          9. My JSP can't find a class that it needs
            1. Is that class on an exported package?
            2. Does the web application bundle import the required class package?
        2. Troubleshooting tips
          1. How to get debug logs from the Felix Log Service
          2. How can remote debugging help
          3. Where to get answers online
        3. Summary
      20. A. Eclipse, Maven, and Felix
        1. Productivity tools
          1. An Integrated Development Environment
          2. A build management system
        2. Setting up Eclipse and plugins
          1. Maven integration plugin
          2. OSGi framework container plugin
          3. Choosing the workspace
          4. Installing the Eclipse plugins
            1. Installing Pax Runner
            2. Installing m2clipse
        3. Setting up a new Maven project in Eclipse
          1. Creating the Maven project
          2. What just happened?
          3. Customizing the build process
        4. Time for action - completing the project
        5. Importing a Maven project into Eclipse
        6. Debugging with Eclipse
          1. Remote debugging configuration
            1. Setting up the remote application
        7. Time for action - editing the Felix run script
            1. Configuring the IDE for remote debugging
          1. Connecting to Felix remotely
            1. Set a breakpoint
            2. Start the remote debugger
          2. Running embedded Felix
            1. Configuring embedded Felix
            2. Taking it for a ride
            3. Adding OBR repositories
            4. Starting a test bundle
          3. Debugging embedded
        8. Summary
      21. B. Where to Go from Here?
        1. On declarative services
        2. On persistent storage
        3. On web services
        4. On Java Management Extensions (JMX)
        5. Additional topics
        6. Summary
      22. C. Pop Quiz Answers
        1. Chapter 1: Quick intro to Felix and OSGi
        2. Chapter 2: Setting up the Environment
        3. Chapter 3: Felix Gogo
        4. Chapter 5: The Book Inventory Bundle
        5. Chapter 6: Using the OSGi Bundle Repository
        6. Chapter 7: The Bookshelf: First Stab
        7. Chapter 11: How About a Graphical Interface?
        8. Chapter 13: Improving the Graphics