www.it-ebooks.info www.it-ebooks.info OSGi and Apache Felix 3.0 Beginner’s Guide Copyright © 2010 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmied in any form or by any means, without the prior wrien permission of the publisher, except in the case of brief quotaons embedded in crical arcles or reviews. Every eort has been made in the preparaon of this book to ensure the accuracy of the informaon presented. However, the informaon contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark informaon about all of the companies and products menoned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this informaon. First published: November 2010 Producon Reference: 1291010 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-849511-38-4 www.packtpub.com Cover Image by John M. Quick (john.m.quick@gmail.com) www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Quick Intro to Felix and OSGi 7 What is OSGi? 8 The framework layout 10 The funconal layers 10 The bundle lifecycle states 12 Bundle wiring 13 The shared service registry 14 Working with bundles 15 Anatomy of a bundle 15 The OSGi headers 16 Mandatory headers 16 Funconal headers 17 Informaon headers 18 Start levels 18 The Start Level Service 18 The acve start level 18 Using start levels 20 Apache Felix and sub-projects 21 Summary 24 Chapter 2: Seng Up the Environment 25 Seng up the Felix framework 25 Checking that a JDK is installed 26 Download and unpack the Felix distribuon 27 Time for acon – downloading and installing Felix 27 What's in the box? 28 Time for acon – starng Felix 28 www.it-ebooks.info Table of Contents [ ii ] Maven2 and Felix 30 Installing Maven2 30 Life-cycles and phases 30 Maven plugins 32 The POM 32 The Felix Maven Plugins 32 Summary 33 Chapter 3: Felix Gogo 35 The Tiny Shell Language 36 Chained execuon 36 Variable assignment and referencing 37 Value types 37 Object properes and operaons 38 Execuon quotes 38 Commands and scopes 39 felix scope commands 39 Lisng installed bundles: lb 40 help 40 install 43 update 44 resolve 45 stop and start 45 uninstall 46 refresh 46 headers and inspect 47 which 49 log 50 cd and ls 50 frameworklevel and bundlelevel 52 gogo scope commands 54 echo 54 grep 54 cat 55 tac 56 set 57 Summary 58 Chapter 4: Let's Get Started: The Bookshelf Project 59 A simple Bookshelf project 60 The data inventory er 61 www.it-ebooks.info Table of Contents [ iii ] The business logic er 62 The user interacon er 63 OSGi, Felix, and 65 Taking it step-by-step 65 Some convenons 67 Summary 69 Chapter 5: The Book Inventory Bundle 71 Set up the Book Inventory API Bundle project 72 Time for acon – seng up the project skeleton 72 Time for acon – creang the project POM 73 The Bundle identy 73 More on bundle versions 74 Dependencies 76 Customizing the build 77 Dening the distribuon parameters 78 The Book bean interface 79 The Book bean aributes 79 Time for acon – creang the Book bean interface 80 The Book Inventory interface 81 Time for acon – wring the BookInventory interface 82 Build and deploy the bundle 83 Time for acon – building and deploying the bundle 84 Let's implement those interfaces 85 Time for acon – creang the POM 85 Time for acon – implemenng a mutable book 87 Time for acon – implemenng the mock (memory-stored) Book Inventory 88 The factory method 88 Implemenng a mock getGoups() 89 Storing a book 89 Removing a stored book 90 Loading a stored book 90 Implemenng the book search 91 Wring the Bundle Acvator 94 Time for acon – add a dependency to the OSGi Core library 94 Time for acon – creang the Acvator 95 More on Bundle Contexts 96 Time for acon – declaring Bundle-Acvator 97 Build and deploy the bundle 97 Summary 98 www.it-ebooks.info Table of Contents [ iv ] Chapter 6: Using the OSGi Bundle Repository 99 OBR, the OSGi Bundle Repository 99 The repository XML Descriptor 101 Updang the OBR repository 103 Using the OBR scope commands 103 obr:repos 103 obr:list 104 obr:info 105 obr:deploy 106 obr:source and obr:javadoc 107 Updang bundles in the repository 107 Installing the Book Inventory bundles to Felix 108 Time for acon – install the book inventory bundles 108 On dependency management 110 Summary 111 Chapter 7: The Bookshelf: First Stab 113 The Bookshelf Service bundle 113 Dene the main Bookshelf Service interfaces 115 Time for acon – wring the APIs 116 The Authencaon interface 116 The BookshelfService interface 116 Implemenng the service 119 Time for acon – wring BookshelfServiceImpl 119 Time for acon – implemenng the service acvator 122 Framework service lookup 123 Trying the BookshelfService 126 Time for acon – building the bundle 126 Time for acon – installing and tesng the service 126 Time for acon – fullling the missing dependency 129 On class visibility 131 Summary 132 Chapter 8: Adding a Command-Line Interface 133 The Apache Felix Gogo Shell Service 134 Time for acon – creang the Bookshelf Service TUI bundle 134 Implemenng a Gogo Shell Service command 135 Implemenng the book:search command 136 Time for acon – adding the required dependencies 136 Time for acon – wring the BookshelfServiceProxy 137 On Converters 141 www.it-ebooks.info Table of Contents [ v ] Time for acon – implemenng a bundle acvator 142 Time for acon – packaging and installing 143 Time for acon – trying out the book:search command 144 Time for acon – cleaning up the bookshelf-service acvator 146 Implemenng the book:add command 147 Time for acon – implemenng the book-add command 147 Updang an installed bundle 148 Trying the commands 151 Sourcing scripts 153 Time for acon – creang a book populaon script 153 Summary 156 Chapter 9: Improve the Bookshelf Service with iPOJO 157 What is Inversion of Control? 158 The Service Locator paern 159 The Dependency Injecon paern 159 The Whiteboard paern 161 The Extender Paern 161 The iPOJO Felix sub-project 162 Components and instances 163 iPOJO Maven plugin 164 The metadata le 164 Using the plugin 167 Injecng iPOJOs 168 Install the iPOJO service bundle 169 Let iPOJO register the inventory implementaon 169 Time for acon – creang the iPOJO metadata 170 Update the POM 170 Congure bundle for iPOJO 171 Build and test it 172 The Felix iPOJO Gogo Command bundle 173 ipojo scope commands usage 174 Migrate the bookshelf service 175 Time for acon – removing lookups in the service implementaon 175 Time for acon – wring the bookshelf service iPOJO conguraon 177 Update the POM 177 Deploy and check 179 iPOJO using annotaons 180 Overview 180 Beginner's annotaons 180 @Component 181 @Provides 181 www.it-ebooks.info Table of Contents [ vi ] @Requires 181 @ServiceProperty 182 @Property 182 @Instanate 182 Instanang annotated components 182 Update the text UI bundle 183 Time for acon – updang the BookshelfServiceProxyImpl 183 Time for acon – wring the iPOJO meta.xml 184 Time for acon – updang the POM 185 Summary 187 Chapter 10: Improving the Logging 189 On logging 189 Logging levels 190 Who's listening? 191 The OSGi Log Service 192 The Service end 193 Usage of the Log Service 194 The service provider end 195 Apache Felix Log Service 196 The log command 197 Creang the log helper bundles 198 Time for acon – creang the bookshelf-log-api bundle 198 Time for acon – creang the log helper implementaon 199 Implemenng the BookshelfLogHelper service 200 Add logging to the bookshelf-service 201 Time for acon – updang the bundle POM 201 Time for acon – updang the bookshelf service logging calls 202 Time for acon – logging to BookshelfLogHelper 203 Update bookshelf-service-tui dependency 204 Trying it out 205 Using other Log Service implementaons 208 Summary 208 Chapter 11: How About a Graphical Interface? 209 The OSGi HTTP Service 209 Component structure 210 Registraon of servlets 211 iPOJO and the Whiteboard Extender 212 Hp Service implementaons 213 The Apache Felix Hp Service 213 Time for acon – installing the Apache Felix Hp Service 214 www.it-ebooks.info Table of Contents [ vii ] A simple bookshelf web applicaon 214 Time for acon – implemenng the servlet 215 The iPOJO conguraon 216 Implemenng the operaons 217 Time for acon – declaring the parameter constants 217 Time for acon – implemenng the operaons 218 Trying it out 224 Summary 226 Chapter 12: The Web Management Console 227 Geng started 228 Installing the Web Console 228 Time for acon – installing commons-leupload and commons-io 229 Time for acon – installing json 229 Time for acon – installing and starng the Web Console 231 A quick overview 231 Bundles 232 Log Service 233 OSGi Repository 234 Services 234 Shell 235 System Informaon 236 Apache Felix iPOJO WebConsole Plugin 236 Summary 238 Chapter 13: Improving the Graphics 239 OSGi Web Containers 240 Pax Web 240 Time for acon – installing the Pax Web bundles 241 Uninstall previous hp support 241 Install PAX Web bundles 242 Double-check the hp service implementaon 242 Our bookshelf-webapp 243 Time for acon – creang the bookshelf-webapp bundle 244 Web applicaon registraon 245 Time for acon – seng up the web applicaon bundle 245 Time for acon – specifying dependencies 246 Geng a service reference in JSP 247 Time for acon – wring the session bean 248 Complete the authencaon pages 249 www.it-ebooks.info [...]... Felix implementation and how it fits into the OSGi world www.it-ebooks.info Quick Intro to Felix and OSGi You will: Take a quick overview of OSGi Understand the OSGi service platform, its functional layers, and their interaction Take a deep dive into OSGi bundles, their manifest headers Understand how bundles are activated on an OSGi platform Learn about bundle start levels and how they can be... complete and dynamic component model Wasn't that a complicated definition! So how would you really use it to 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 expect The OSGi standards... covers Chapter 1, Quick intro to OSGi and Felix gives an overview of OSGi and introduces Felix Chapter 2, Setting up the Environment walks the reader through the pre-requisites needed for developing as they read Chapter 3, Felix Gogo covers the Felix Gogo command-line shell and syntax Chapter 4, Let's Get Started: The Bookshelf Project sets the scope of work for the case study and describes the chapter-by-chapter... Smart Home, E-Health, and Mobile, 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 the reader from the development environment setup to the troubleshooting of potential issues, walking them through the development of an OSGi- based application and explaining relevant... http://www .osgi. org/Markets/EHealth In short, the applications of OSGi are limitless and with a wide involvement from many disparate parties Do you think you can contributing? If yes, you can consider following one of the previous groups and contribute your experience! Let's take a look at the layout of an OSGi service platform and understand the way it works [] www.it-ebooks.info Quick Intro to Felix and OSGi. .. specifications Among the many adopters of OSGi as an application framework in the Open Source community are Knopflerfish, Equinox (Eclipse), and Felix We will be working with the Apache Felix OSGi service platform implementation The OSGi Service Platform's Core Specification documents the framework's expected behavior and also specifies the way its different parts interact, and react to external requests in... modular and extensible applications This book uses the Felix framework 3.0 as an OSGi service platform implementation, and covers its usage to a level where it makes you comfortable enough to write your own enterprise-level applications This book is aimed at Java developers looking to learn about writing reusable and network distributable software following the OSGi standards using the famous Felix framework... This book starts with an introduction to the OSGi Service Platform, its parts and its bundle structure It then walks the reader through the Felix framework's setup and their development environment It describes the Felix Framework and how to operate it using Gogo This book will teach you everything possible about the practical implementation of OSGi using the Felix Framework as a launch pad The book then... JSP Web Application Bundle and explaining Web Containers in the context of OSGi Chapter 14, Pitfalls and Troubleshooting includes a few tips on common issues faced when writing a bundle and describes a few means to troubleshoot them Appendix A, Eclipse, Maven, and Felix is an introduction to some of the productivity tools available for a Java developer in general and to an OSGi developer in specific... Intro to Felix and OSGi The optional OSGI- OPT/ directory can be used to store additional resources that are not needed for the proper functioning of the bundle: resources such as source code and additional documentation The framework may choose to throw this content away, if it needs to save storage space Another standard OSGi directory that can also be present in a bundle archive is the OSGI- INF/ . ] Maven2 and Felix 30 Installing Maven2 30 Life-cycles and phases 30 Maven plugins 32 The POM 32 The Felix Maven Plugins 32 Summary 33 Chapter 3: Felix Gogo 35 The. C: Pop Quiz Answers 30 3 Chapter 1: Quick intro to Felix and OSGi 30 3 Chapter 2: Seng up the Environment 30 3 Chapter 3: Felix Gogo 30 4 Chapter 5: The Book