concept Declarative Services in category osgi

appears as: Declarative Services, Declarative Services
OSGi in Action: Creating Modular Applications in Java

This is an excerpt from Manning's book OSGi in Action: Creating Modular Applications in Java.

The simplest way to protect against dangling services is to let a component framework such as Declarative Services manage services for you. Component frameworks are discussed in detail in chapters 11 and 12; for now, you can think of them as watchful parents that protect their children from the harsh realities of the world. But even component frameworks may not be able to help against rogue clients that stubbornly refuse to relinquish references to your service. You somehow need to give these bundles a reference that you can clear yourself, without requiring their cooperation.

  • Declarative Services is an OSGi standard component framework that manages service publication, service dependencies, and configuration dependencies on behalf of components.
  • Component orientation in general and Declarative Services in particular are worthwhile approaches when you’re working with OSGi. In the next chapter, we’ll push even further by looking at two more advanced component frameworks, in case Declarative Services doesn’t address all your needs.

    Listing 11.2. Circle class used in the Declarative Services paint example
    public class Circle implements SimpleShape { public void draw(Graphics2D g2, Point p) { int x = p.x - 25; int y = p.y - 25; GradientPaint gradient = new GradientPaint(x, y, Color.RED, x + 50, y, Color.WHITE); g2.setPaint(gradient); g2.fill(new Ellipse2D.Double(x, y, 50, 50)); BasicStroke wideStroke = new BasicStroke(2.0f); g2.setColor(Color.black); g2.setStroke(wideStroke); g2.draw(new Ellipse2D.Double(x, y, 50, 50)); } }