Click here to close now.


Microservices Expo Authors: Pat Romanski, Jason Bloomberg, Plutora Blog, Elizabeth White, Carmen Gonzalez

Related Topics: Java IoT, Microservices Expo

Java IoT: Article

Component Development and Assembly Using OSGi Services

Using components to build software systems will provide many architectural advantages apart from promoting ease of reuse

This article introduces the concepts of Component Oriented Development and Assembly (CODA) using the OSGi Service platform with an example application. The article starts with an introduction to software components, elaborates with an example application, followed by an overview of the OSGi Service platform, and an implementation of the example application using this platform.

Introduction to Software Components
Components are parts that can be assembled to form a larger system. Electronic components such as ICs (Integrated Circuits) are assembled together to build an electronic system; similarly software components are assembled together to build a software system. Software systems have a static form as well as a dynamic runtime form. Software components can be assembled either in static form or dynamic form. In either case, the software component is an independent unit of development, deployment, and assembly. Using components to build software systems will provide many architectural advantages apart from promoting ease of reuse.

Age Calculation - An Example Application
Consider an application meant for calculating the age of people. Assume this application prompts the user to provide her personal details such as name and date of birth. Once the user provides the details, the application calculates the age of the person as of today and displays it with a personal greeting. Such an application can be built from the following software components:

  • UserProfile Component: Responsible for collecting the personal details of the user such as name and date of birth
  • Age Component: Responsible for calculating the age, given the date of birth
  • Greeting Component: Responsible for displaying the greeting message along with age of the person; needs to make use of the UserProfile component to obtain input from the user and Age component to calculate the age
  • AgeCalculationApp Component: Responsible for invoking the Greeting service provided by Greeting component

The components can be represented in a UML component diagram as shown in Figure 1. The dotted arrow lines in the diagram denote dependency.

Figure 1: Components in the Age Calculation application

Interfaces of Software Components
A software interface is a collection of software operations. Software components interact with one another by invoking the software operations. To facilitate structured interactions across components, each component exposes one or more software interfaces through which other components can interact. The exposed interfaces of a component are called as its "provided interfaces." Internals of the component implement the service contracts exposed by the provided interfaces.

Apart from providing services, a component needs to consume services provided by other components. The set of services that a component wants to consume is specified through "required interfaces". A component can work with any other component that provides the required interfaces. Hence there is no tight coupling between one component and another; the dependency of a component is only with interfaces and not with other components.

Interfaces in the Age Calculation Application
Let us identify the interfaces provided and required by each of the four components in the Age Calculation application.

Age Component
The Age component is responsible for calculating the age, given the date of birth. Hence it needs to provide a service for calculating the age. Let's expose this service through a provided interface IAgeCalculation. The Java code for this interface is given in Figure 2.

Figure 2: AgeCalculation Java interface code

In the IAgeCalculation interface definition, Calendar is a data type defined in java.util package to represent the date. IAge is a custom-defined data type to hold the age. The Java code for IAge is shown in Figure 3.

Figure 3: Java code for data type IAge

The Age Component can be represented in a UML component diagram as shown in Figure 4. The lollipop connector from the component denotes provided interface.

Figure 4: UML representation of Age component

UserProfile Component
The UserProfile component is responsible for collecting the personal information from end user and passing it back to the consuming component. Let us define an interface called as IUserProfileCollection to expose this service. The Java code for this interface is shown in Figure 5.

Figure 5: Java code for IUserProfileCollection interface

When the method collectUserProfile() is called, the component starts an interactive session with the end user to collect her personal information. During the user interaction session, the method isProfileCollected() will return false; after the user interaction session has finished and the user profile has been collected, this method will return true. At this time, the user profile is ready for pick-up by the consuming component and it can do so by invoking giveUserProfile() method.

giveUserProfile() method returns the user profile information through a custom defined data type IUserProfile. Java code for IUserProfile data type is given in Figure 6.

Figure 6: Java code for IUserProfile data type

The UserProfile component in a UML component diagram notation is shown in Figure 7.

Figure 7: UML representation of UserProfile component

Greeting Component
The Greeting component is responsible for displaying the greeting message along with the age. We'll expose this service through the IGreeting interface. The Java code for IGreeting interface is shown in Figure 8.

Figure 8: Java code for interface IGreeting

The Greeting component provides the IGreeting service, and it requires the IAgeCalculation and IUserProfileCollection services. The required and provided interfaces of the Greeting component can be represented using the UML component diagram as shown in Figure 9. The receptacle sockets in the component diagram denote required interfaces.

Figure 9: UML representation of Greeting component

AgeCalculationApp Component
The AgeCalculationApp component is responsible for invoking the IGreeting service of Greeting component. The AgeCalculationApp itself does not provide any service. The UML diagram for this component is shown in Figure 10.

Figure 10: UML representation of AgeCalculationApp

Application Assembly
Components are assembled together to build a software system. Components for an assembly should be chosen such a way that, amongst the chosen components the set of required interfaces is fulfilled by the set of provided interfaces. The four components of the example application can be assembled to result in Age Calculation application. UML component diagram denoting this assembly is shown in Figure 11. Connectors with a lollipop and a receptacle together show how two components assemble over a common interface (one component provides the interface and another component requires it).

Figure 11: UML representation of Age Calculation application assembly

Once an application is assembled, specific components from the assembly can be replaced with compatible newer components. Let us say we have two new components - GUIUserProfile and GUIGreeting, which have same interfaces as UserProfile and Greeting components, respectively. Then these new components can replace the old components to provide us with a GUI application. The application assembly would then look like Figure 12.

Figure 12: Age Calculation GUI Application Assembly

Introduction to the OSGi Service Platform
OSGi Service is a standards based software development platform. The platform standards are specified by the OSGi Alliance (, formerly known as the Open Services Gateway initiative.  OSGi alliance is an industry backed nonprofit organization.

OSGi platform provides a modularity and component model on top of Java. Figure 13 illustrates how OSGi logically fits in a development / deployment stack.

Figure 13: OSGi From Development / Deployment Perspective

OSGi Framework
At the core of the OSGi platform is the OSGi framework defined by standards. OSGi framework is the runtime environment on which OSGi applications can be executed. OSGi framework specifies a common API using which application developers can develop OSGi applications. The OSGi framework specifications is used by OSGi framework developers also, who build implementations of the framework as per the specifications.

There are a number of framework implementations available from different developers. A few well known framework implementations are listed below:

The functionality of OSGi framework is divided into many layers. For our purposes, we shall focus on Modules Layer and Services Layer.  Figure 14 illustrates the layers we would like to focus.

The modules layer is the bottom most layer interacting with the runtime framework. The modules layer defines a modularity framework on top of standard Java so that modules expose and interact via well-defined APIs. Individual module is called as a "bundle" in OSGi. OSGi bundle is the atomic unit of deployment on an OSGi framework. An OSGi bundle is nothing but a Java Archive (JAR) File with some special manifest information that would be used by the OSGi framework when the bundle is deployed. Unlike a normal JAR file, which exposes all its code when it is deployed on a Java Virtual Machine (JVM), the bundle does not expose all the code contained within. Only code (Java packages) marked for explicit export is exposed outside the bundle.

Figure 14: OSGi Framework Functional Layers

Above the Module layer is the Services layer. An OSGi Service is defined by a Java interface. Any bundle can implement the service, and register the service with the OSGi Service Registry. OSGi Service Registry, defined as part of framework standards, supports a publish, subscribe, and look-up mechanism of exchanging services across components. A bundle can register the service that it has implemented with the OSGi Service Registry. A bundle requiring a service can query the OSGi Service Registry to look-up any registered service implementation. Instead of looking-up, a bundle can also subscribe to be notified on service registration / unregistration events. These mechanisms make the bundles that provide service and bundles that consume service unaware of each other, leading to independent development and deployment of these components. Once deployed, they can be  assembled dynamically using the OSGi Service Registry.

Implementing Age Calculation Using OSGi
The Age Calculation application has one bundle for each of the components. Each bundle has a Java package that is exposed for access from outside and an internal Java package. The exposed package defines the interfaces and the internal package contains implementation classes. The package diagram for all the bundles is shown in Figure 15.

Figure 15: Age Calculation Application bundles

The service provided by each bundle is exposed by using the OSGi Declarative Services framework. Using the same framework, each bundle also specifies the list of services it requires. The OSGi Declarative Services framework (also known as Service Component Runtime), will perform the work of registering the provided service with the OSGi Service Registry and obtaining the required service through look-up into OSGi Service Registry. Thus, the Declarative Services framework provides automatic assembly of deployed components.

As an example, let us consider the Greeting bundle. The Declarative Services specification of the components in this bundle is done through an XML file, which is given in Figure 16.

Figure 16: Greeting component Declarative Service Definition

The XML file contains following metadata about the Greeting component:

  • A component by name com.demo.greeting is defined
  • The component is implemented by the class com.demo.greeting.internal.Greeting
  • The component requires two interfaces - IuserProfileCollection and IAgeCalculation. These required interfaces should be dynamically wired to this component using setUserProfileCollection() and setAgeCalculation() methods on the component implementation class.
  • The component provides a service through the provided interface com.demo.greeting.IGreeting.

To understand how the Greeting component works, some portions of the implementation class is presented in Figure 17. The Greeting class has two field variables referring to the services required by this component. In the component Declarative Service metadata setter methods of these two variables are linked to the service reference (required interface). Hence when the Service Component Runtime (SCR) activates this component, it would call the setter methods to point these variables to objects which provide the service. For example, an instance of AgeCalculator object from Age component would be passed to ageCalculation_ member variable.

Figure 17: Implementation of Greeting component

The Greeting class's  implementation of greet() method obtains the user profile by invoking IUserProfileCollection service. From the obtained profile, date of birth is extracted and IAgeCalculation service is used to calculate the age. Finally the method displays the age along with a greeting to the user.

Output from the Example Application
The Age Calculation application, with console mode user interaction components, has the following output.

osgi> What is your first name?


What is your last name?


What is your title (Mr./Ms./Mrs./Prof./Dr.)?


What is your year of birth?


What is your month of birth (1-12)?


What is your date of birth (1-31)?


Hello Mr. Albert Einstein, you are 132 years, 10 months, and 3 days old.

If we replace the Greeting and UserProfile components in the application assembly with GUIGreeting and GUIUserProfile components, the application output is as below:

The source code of the application can be downloaded as a zip file containing an Eclipse workspace folder. To open the source code and run the application simply unzip the folder and switch the Eclipse workspace to point to the unzipped folder. Choose the appropriate components in the Run Configuration to run either console version or the GUI version of the assembly. For example, for the GUI version, the following run configuration will be applicable:

More Stories By Piram Manickam

Piram Manickam works at Infosys Limited. He would like to acknowledge and thank Sangeetha S, a beloved colleague and friend, for her invaluable contributions in this work.

More Stories By Subrahmanya SV

Subrahmanya SV works at Infosys Limited. He would like to acknowledge and thank Sangeetha S, a beloved colleague and friend, for her invaluable contributions in this work.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
newuniverse 01/31/12 03:10:00 AM EST

This is a very simple and descriptive example. Good job.

@MicroservicesExpo Stories
Enterprises are turning to the hybrid cloud to drive greater scalability and cost-effectiveness. But enterprises should beware as the definition of “policy” varies wildly. Some say it’s the ability to control the resources apps’ use or where the apps run. Others view policy as governing the permissions and delivering security. Policy is all of that and more. In his session at 16th Cloud Expo, Derek Collison, founder and CEO of Apcera, explained what policy is, he showed how policy should be arch...
The causality question behind Conway’s Law is less about how changing software organizations can lead to better software, but rather how companies can best leverage changing technology in order to transform their organizations. Hints at how to answer this question surprisingly come from the world of devops – surprising because the focus of devops is ostensibly on building and deploying better software more quickly. Be that as it may, there’s no question that technology change is a primary fac...
The last decade was about virtual machines, but the next one is about containers. Containers enable a service to run on any host at any time. Traditional tools are starting to show cracks because they were not designed for this level of application portability. Now is the time to look at new ways to deploy and manage applications at scale. In his session at 16th Cloud Expo, Jake Moshenko, Product Manager at CoreOS, examined how CoreOS + fit into the development lifecycle from pushing gi...
Internet of Things (IoT) will be a hybrid ecosystem of diverse devices and sensors collaborating with operational and enterprise systems to create the next big application. In their session at @ThingsExpo, Bramh Gupta, founder and CEO of, and Fred Yatzeck, principal architect leading product development at, discussed how choosing the right middleware and integration strategy from the get-go will enable IoT solution developers to adapt and grow with the industry, while at th...
This week we're attending SYS-CON Event's DevOps Summit in New York City. It's a great conference and energy behind DevOps is enormous. Thousands of attendees from every company you can imagine are focused on automation, the challenges of DevOps, and how to bring greater agility to software delivery. But, even with the energy behind DevOps there's something missing from the movement. For all the talk of deployment automation, continuous integration, and cloud infrastructure I'm still not se...
Countless business models have spawned from the IaaS industry. Resell Web hosting, blogs, public cloud, and on and on. With the overwhelming amount of tools available to us, it's sometimes easy to overlook that many of them are just new skins of resources we've had for a long time. In his General Session at 16th Cloud Expo, Phil Jackson, Lead Technology Evangelist at SoftLayer, broke down what we've got to work with and discuss the benefits and pitfalls to discover how we can best use them to d...
SYS-CON Events announced today that Harbinger Systems will exhibit at SYS-CON's 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. Harbinger Systems is a global company providing software technology services. Since 1990, Harbinger has developed a strong customer base worldwide. Its customers include software product companies ranging from hi-tech start-ups in Silicon Valley to leading product companies in the US a...
The 4th International Internet of @ThingsExpo, co-located with the 17th International Cloud Expo - to be held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA - announces that its Call for Papers is open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than
SYS-CON Events announced today that ProfitBricks, the provider of painless cloud infrastructure, will exhibit at SYS-CON's 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. ProfitBricks is the IaaS provider that offers a painless cloud experience for all IT users, with no learning curve. ProfitBricks boasts flexible cloud servers and networking, an integrated Data Center Designer tool for visual control over the...
"We got started as search consultants. On the services side of the business we have help organizations save time and save money when they hit issues that everyone more or less hits when their data grows," noted Otis Gospodnetić, Founder of Sematext, in this interview at @DevOpsSummit, held June 9-11, 2015, at the Javits Center in New York City.
17th Cloud Expo, taking place Nov 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy. Meanwhile, 94% of enterprises ar...
Microservices are individual units of executable code that work within a limited framework. They are extremely useful when placed within an architecture of numerous microservices. On June 24th, 2015 I attended a webinar titled “How to Share Share-Nothing Microservices,” hosted by Jason Bloomberg, the President of Intellyx, and Scott Edwards, Director Product Marketing for Service Virtualization at CA Technologies. The webinar explained how to use microservices to your advantage in order to deliv...
Software is eating the world. The more it eats, the bigger the mountain of data and wealth of valuable insights to digest and act on. Forward facing customer-centric IT organizations, leaders and professionals are looking to answer questions like how much revenue was lost today from platinum users not converting because they experienced poor mobile app performance. This requires a single, real-time pane of glass for end-to-end analytics covering business, customer, and IT operational data.
In the midst of the widespread popularity and adoption of cloud computing, it seems like everything is being offered “as a Service” these days: Infrastructure? Check. Platform? You bet. Software? Absolutely. Toaster? It’s only a matter of time. With service providers positioning vastly differing offerings under a generic “cloud” umbrella, it’s all too easy to get confused about what’s actually being offered. In his session at 16th Cloud Expo, Kevin Hazard, Director of Digital Content for SoftL...
Discussions about cloud computing are evolving into discussions about enterprise IT in general. As enterprises increasingly migrate toward their own unique clouds, new issues such as the use of containers and microservices emerge to keep things interesting. In this Power Panel at 16th Cloud Expo, moderated by Conference Chair Roger Strukhoff, panelists addressed the state of cloud computing today, and what enterprise IT professionals need to know about how the latest topics and trends affect t...
Public Cloud IaaS started its life in the developer and startup communities and has grown rapidly to a $20B+ industry, but it still pales in comparison to how much is spent worldwide on IT: $3.6 trillion. In fact, there are 8.6 million data centers worldwide, the reality is many small and medium sized business have server closets and colocation footprints filled with servers and storage gear. While on-premise environment virtualization may have peaked at 75%, the Public Cloud has lagged in adop...
DevOps Summit, taking place Nov 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 17th Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development...
One of the hottest new terms in the world of enterprise computing is the microservice. Starting with the seminal 2014 article by James Lewis and Martin Fowler of ThoughtWorks, microservices have taken on a life of their own – and as with any other overhyped term, they have generated their fair share of confusion as well. Perhaps the best definition of microservices comes from Janakiram MSV, Principal at Janakiram & Associates. “Microservices are fine-grained units of execution. They are designe...
Agile, which started in the development organization, has gradually expanded into other areas downstream - namely IT and Operations. Teams – then teams of teams – have streamlined processes, improved feedback loops and driven a much faster pace into IT departments which have had profound effects on the entire organization. In his session at DevOps Summit, Anders Wallgren, Chief Technology Officer of Electric Cloud, will discuss how DevOps and Continuous Delivery have emerged to help connect dev...
In their general session at 16th Cloud Expo, Michael Piccininni, Global Account Manager - Cloud SP at EMC Corporation, and Mike Dietze, Regional Director at Windstream Hosted Solutions, reviewed next generation cloud services, including the Windstream-EMC Tier Storage solutions, and discussed how to increase efficiencies, improve service delivery and enhance corporate cloud solution development. Michael Piccininni is Global Account Manager – Cloud SP at EMC Corporation. He has been engaged in t...