Using OSGi & Equinox Effectively: An Interview With Jeff McAffer
Join the DZone community and get the full member experience.Join For Free
after releasing his latest book covering osgi & equinox, i took the opportunity to catch up with jeff mcaffer. in this interview we discuss the basics of osgi, the equinox implementation, benefits of modularity and some best practices around using equinox.
james: can you introduce yourself and your associations with eclipse?
jeff: i'm jeff mcaffer, the cto and co-founder of eclipsesource. i've been involved in eclipse since developing its first prototypes while working at ibm more than 10 years ago. since that time i've focused mainly on runtime work and have led various efforts from the runtime to equinox to the evolution of eclipse as a rich client platform and now the eclipsert effort to drive eclipse runtime technologies. along the way i've been on the board of directors, several pmcs and the architecture council.
james: what audiences are you targetting with the book - it's more than just the eclipse crowd?
jeff: oh yes. the title is osgi and equinox -- that is, osgi with some detail on equinox. as you know osgi is at the heart of eclipse but the technology is applicable in a much broader scope. we see osgi being used in embedded environments (its original target audience), desktop apps (with eclipse rcp and other application infrastructure), a wide range of server side infrastructure and most of the major java application servers. many people in the enterprise java space are look at and talking about osgi. this book addresses all of these readers. it seeks to make clear the basic and some advanced topics in a generic osgi way. of course we use equinox as the example framework and eclipse for tooling but the vast majority of the material and samples apply to any osgi system.
james: could you give a really simple explanation of osgi, and then what equinox is?
jeff: at its simplest, osgi is the specification for a module runtime for java. equinox is the reference implementation for the core osgi of that specification. of course, both are quite a bit more than that. osgi has many add-on services from the embedded to enterprise domains. equinox covers many topics that are not dealt with in the specs (e.g., provisioning, native launching, non-osgi-based code library integration, dynamic aspect weaving, ...).
james: do you see equinox & osgi as the core for any modular system, or can it be overkill in some cases?
jeff: osgi is elegantly minimal -- just 27 java types are defined in the core specification. if you are building a java application and are interested in what modularity has to offer, i don't see any viable alternative to osgi. implementations such as concierge can be quite small (e.g., <100k). other implementations are larger but more performant or more fully featured. as always, use the right runtime for the job.
james: what are the key benefits of osgi?
jeff: osgi is a simple but powerful modularity framework for java. using osgi means that you can write self-contained modules with clear api boundaries and strong runtime enforcement of those boundaries. this reduces the amount of code you have to write to create your system because you can reuse other people's off-the-shelf modules. this sounds great to geeks but the appeal is wider than that.
as we say in chapter 1 of the book, "modularity ... is the lubricant of collaboration." modularity enables freedom of action for both producers and consumers of technology. this fundamentally changes the way you develope and deploy software solutions at both a personal and organizational level. the nasa guys are a great example of this. they have vastly diverse teams of people collaborating in space missions. osgi, initially in the form of eclipse rcp but now on the server as well, empowered all members of the team participate in creating the solution. in some ways the power of osgi extends beyond systems engineering and into social engineering.
james: what has put equinox ahead of other osgi implementations?
jeff: i would not say "ahead" but rather "different". there are a handful of open source osgi implementations and each has focused on a different set of users. equinox has benefitted from years of widespread use as the runtime for the eclipse ide and rcp-based applications. each day, literally millions of people are running equinox-based systems on their desktop. the requirements of those applications have pressed the equinox team to drive scaleability and robustness as well as paying attention to edge and corner cases that are part of real software systems. in the last few years equinox has come to be the framework-of-choice for server implementors from websphere to spring dm to sap netweaver. this has further enhanced the state of equinox as an industrial-strength osgi framework.
james: how difficult was it to write the book? did it take long?
jeff: this is the second book project in which i have been involved. this go around was a little more drawn out (it took about 1.5 years) but was actually easier. that is partially because of lessons learned but also because we started from some existing material. we were able to seed a few chapters from the rcp book, toast, the sample application we develop through out the book, was largely already implemented, we could reuse the samples manager to manage the code, and many of the production processes were well understood. all in all it was also a huge amount of fun. paul and simon are awesome to work with and toast is very motivating. part of the challenge was stopping from adding more cool stuff to toast!
probably the biggest challenge was hitting the different levels of detail for the different readers. we wanted to create a book that would ease people into osgi but then not leave them hanging. so many tutorials make everything look easy but when you try for yourself there are many unanswered questions. osgi is actually very simple on the surface--create a bundle, describe its dependencies, drop it in a system and shazaam. that is, until you build a real system. then all the gorpy details come to play--third-party non-osgi code, api definition and evolution, dynamic behaviour, and the like. in the book we give readers the fundamental understanding they need to craft their own systems. envisioning and creating that content was hard work.
james: what is the key tip that you would give to anyone thinking about, or already, using osgi?
jeff: good fences make good neighbors. i love my house but we have eight direct neighbors. i've got hours of stories involving police, ambulances, building inspectors, heavy equipment, lawyers, courts, you name it. on reflection, the vast majority of those stories revolve around ill-defined boundaries and behaviours. and so it is with software systems. osgi gives you the tools you need to define the boundaries between elements of the system--modules. it then enforces those boundaries at runtime. so the key tip is to pay attention to your api definition and evolution--maintain your fences.
james: what is the trickiest part to osgi?
jeff: the things that frustrate people the most about osgi are not osgi at all but what it enables--modularity and dynamism. defining a module requires you to think about the boundaries and behaviours you want the module to have. many systems are only loosely defined and evolve quickly. this in turn requires you to think harder about your modules and their interaction and refactor frequently. osgi enables you to capture and enforce these interactions. of course, you can still write big wads of spaghetti code if you like but that is a choice independent of osgi. similarly, if you want to have a system where function can come and go spontaneously, you have to account for that. again, osgi enables the creation of such systems but, well, there's no free lunch, you still have to code accordingly.
of course, there are some quirks in the way osgi works and the impedance mismatch with other extensibility mechanisms like context classloaders and the like cause people no end of grief. for the most part these issues are dealt with once and progress continues.
james: it seems to me that following your examples, you get some best practices for programming in general. could you list out some of your best practices for working with osgi applications?
jeff: don't program osgi -- in my osgi talks and training courses i like to start off saying "the best practice for programming osgi is to not use osgi". what i mean by that is, write pojos and use dependency injection style. too many people have code that reaches all over the place and has osgi api calls littered throughout. for the most part this makes the code hard to read, hard to test and difficult to reuse in other contexts. full up toast has about 80 bundles and only a handful reference osgi core framework apis!
use declarative services -- go ahead and put down the servicetracker rocks and sticks and make the move to declarative services. ds is a key part to eliminating the widespread use of osgi apis i mentioned earlier. it greatly simplifies the collaboration between bundles using osgi services by eliminating all of the boilerplate and brittle/complicated code otherwise needed.
understand the bundle food chain -- put another way, minimize and manage your dependencies. be realistic about the bundles you are using and why. i've never been a fan of duplication but have also seen cases where many megabytes of code was sucked in just to get a simple constant!
don't use bundle activators -- sure, sometimes the function in a bundle needs to be setup but this should be the exception. this is especially true if you are using ds. the downside of activators is that they take time on startup and they generally capture some sort of singleton-like behaviour. for example, the typical httpservice bundle has an activator that starts the http server listening on a configured port. what if you want two? and want them started at different times? and, ...
james: what is the most common mistake you see with osgi use?
jeff: based on the best practices i mentioned earlier, the biggest problem i see is people using too much osgi api too often, especially around services. seriously, look in your bundle manifests for "org.osgi" and justify each occurrence. likely most cases are where you are reaching out to the rest of the world. that forces you to make assumptions about your surroundings and, as a result, limits the situations in which your bundle can be used. use ds or some other injection mechanism to have these things injected into your bundle.
james: what's your opinion on modularity: design it in up front, or refactor it in as you go?
jeff: great question! with just about everything it is better if you design and implement for your actual requirements. i cringe at the thought that not all teams have the benefits of modularity at the top of their priority list, but hey, that's reality. if modularity is something that factors into your thinking, then absolutely, design it in as soon as possible. i've seen some pretty major modularity retrofitting projects and they are a struggle. in eclipse itself we have done some of these and even with our inherent modularity, it can be a struggle. by far the most grief came from places where things were tightly coupled and a jumble of different concerns (i.e., not cohesive).
actually, in the book we talk about this and point to high cohesion and loose coupling explicitly as best programming practices. with or without explicit modularity, if you do these well, your software is more modular and the approach brings clarity to the design process. it also makes refactoring easier (and less necessary). so, spending time designing and coding for loose coupling is a little more effort up front but has huge payoffs in the medium to long term.
in the end, reality is that requirements are unclear and changing. you have to be ready, willing and able to refactor. then refactor more. and do it again. getting modularity in earlier helps with that but good design processes are more important.
james: where is equinox going in the future? does it remain at the core of the e4 effort?
jeff: equinox, the framework, will continue to evolve with the osgi specifications and be a powerful and robust implementation. it will take cues from projects like e4, end users and real-world usecases, and evolve to meet those needs. the equinox project itself is expanding to address broader modularity needs. last year we added dynamic code weaving capabilities to the release. this year we are excited to see the scalamodules work moving to equinox. in addition, we have been enhancing the server side and provisioning efforts in equinox.
the real story however is bigger than just equinox, it is the community and ecosystem that it enables. a couple years ago at eclipse we kicked off the eclipsert effort to drive the use of eclipse technology in the runtime space. since then eclipsert has grown to include many well-known runtime technologies such as eclipselink (formerly toplink from oracle) and jetty. the gemini and virgo projects were recently announced and represent vast swaths of oracle and springsource runtime technology being contributed to eclipsert. in particular, the spring dm server is moving to eclipse as part of virgo. so equinox and eclipsert have a huge and very exciting future.
james: how does p2 fit in to all this? and what alternatives are their to provisioning?
jeff: p2 is part of equinox and is the latest generation provisioning technology from eclipse. while it is designed as a general provisioning infrastructure, it excels at managing osgi-based systems big and small. one of the reasons p2 exists is that everyone's provisioning problem is different. it is extremely flexible, extensible and configurable. the book covers p2 in considerable detail and the code samples show how to integrate p2 at a deep level in support of dynamic and remote provisioning.
of course, wherever there is diverse need, there is diverse supply. there are a number of alternatives to p2. they all have their strengths and weaknesses so teams should pick the one that best suits their needs. the osgi specifications have a couple related services in initial provisioning and deployment admin, apache ace is relatively new on the scene, there are commercial options from traditional osgi vendors. perhaps the most pervasive though is home-grown solutions. it is amazing how inventive people get in the provisioning space.
james: since you started working for eclipse almost a decade ago, what have been the most exciting changes you've seen?
jeff: now that you mention it, i've been working on eclipse since early 1999. wow, that's a long time! over that time i've seen three major transitions. the first was from an unknown, proprietary, niche effort to an open source phenomenon and domination of the tooling business. that was exciting and gratifying. i learned a lot about the technology, modularity, community and open source. the next transition was the evolution of eclipse as a rich client platform. very cool. this was a community driven affair with people taking our work and using it in ways that we never dreamed of. to me, that validated the whole concept behind modularity. the third transition, we are witnessing now--eclipse in the runtime or eclipsert. the rate and pace of adoption in the runtime space is staggering. every day someone comes out of the woodwork looking to use or contribute something new. we ran a series of eclipsert days last fall and the range of attendees and was they were doing was very exciting. i don't know what we'll do to top this. guess we'll find out in a year or two...
Opinions expressed by DZone contributors are their own.