Getting Started with Equinox & Osgi

Getting Started with Equinox & Osgi

Brought to you by... #37 m CONTENTS INCLUDE: n What is Equinox n Developing Your First Bundle n Launching an Equinox System Getting Started with Equinox & OSGi n Programming Model n Key Equinox Execution Options Visit refcardz.co n Hot Tips and more... By Jeff McAffer Developing your first bundle, continued WHAT IS EQUINOX Next. For the project name, choose one you like (see the tip Equinox is a highly modular, dynamic Java runtime environment on naming). For the remainder of the settings, match them to based on the OSGi framework specifications. It is small, the wizard shown in Figure 1 and click Next. On the next page performant and highly customizable. Equinox forms the basis ensure that the Generate an activator option in the Plug-in of all Eclipse systems from embedded airline check-in kiosks options section is checked. and ski lift gates to rich client applications to IDEs to high- Get More Refcardz! Get More performance application servers such as WebSphere and the Spring dm server. Bundle vs Plug-in. Bundles and plug-ins are the Note same thing. Bundle is the traditional OSGi term This reference card gives you a quick tour of the technology, how whereas plug-in is the original Eclipse term. In it works and how to use it. We touch on modularity basics, key metadata markup and some best-practices for creating modules. Eclipse 3.0 when the Equinox project started and OSGi was m We then look at runtime elements of Equinox and OSGi – adopted, these terms became synonyms. lifecycle, classloading, key APIs and strategies for inter-bundle e.co collaboration (e.g., services and extensions). zon d . GETTING STARTED w As part of the Eclipse ecosystem, the Equinox project also ww produces a number of downloads. Of course, much of Equinox is included in the regular Eclipse SDK and RCP downloads, so if you have Eclipse, you can start right away. That is what we will do here. Hot To get all the Equinox bundles, go to the Equinox Tip download site – http://download.eclipse.org/ equinox and choose a build. Choose a "Release" or "Stable" build for best results. Figure 1 Hot Equinox is 99% pure Java and runs on JREs as Tip low as J2ME Foundation 1.1. So it runs on just about anything you have that runs Java. The Equinox & OSGi Equinox launchers (e.g., eclipse.exe) depend on OS and chip architecture so look for platform-specific downloads. Ok, fire up Eclipse and choose a new, empty workspace location and let’s create your first bundle. DEVELOPING YOUR FIRST BUNDLE OSGi defines modules as bundles. We’ll go into details in a minute but first, let’s create and run one. Start by creating a new plug-in project for the bundle. Select File > New > Project… From the resulting dialog, select Plug-in Project and click Getting Started with DZone, Inc. | www.dzone.com 2 Getting Started with Equinox & OSGi tech facts at your fingertips Developing your first bundle, continued Launching an Equinox system, continued you like. Each time it gets the proper lifecycle events. With Equinox it is most common to use the Hot osgi> stop 1 reverse domain name convention (i.e., Java Goodbye, World Tip package naming) for bundle names. Bundles are likely to end up grouped together so they need The console is started by adding the -console to have unique names. Since every bundle is developed in a Hot command line argument when starting Equinox. separate project, it is convenient to match a project’s name Tip The console has many useful commands. For ex- with that of the bundle it contains. In the wizard screenshot ample, you can use the "diag" command to show we used org.equinoxosgi.helloworld. Of course, you should the missing prerequisites for bundles that are not resolved. ensure that you own the rights to the related domain (e.g., equinoxosgi.org in this case). Now that your bundle project is created the bundle’s manifest BUNDLES editor will be opened. On the Overview tab, click the Activator link and check out the Activator class that was created. It should Having created and run a bundle, let’s take a look inside and see look like this. what’s going on. A bundle is basically a JAR file or directory with public class Activator implements BundleActivator { some extra headers in the MANIFEST.MF. Looking at a typical public void start(BundleContext context) throws bundle from an Eclipse install (Figure 2) you can see that there is Exception { a description of the bundle in the manifest, various class files in a } standard configuration and additional support files such as legal public void stop(BundleContext context) throws Exception { information, translations and extension contributions. } Bundle The Activator is the entry point for your bundle’s code—sort JAR of like the standard main() method but specific to a bundle. Change the start() and stop() methods to print a message (e.g., System.out.println(“Hello/Goodbye World”);) Execution specification To add these lines select in the body of a method Hot and type ‘sysout’ then Ctrl-Space. The Java editor Tip will auto-complete that to System.out.println(); Code and position the cursor inside the parentheses. Extension LAUNCHING AN EQUINOX SYSTEM specification Figure 2 Now that you have a bundle, lets run it. Open the launch The manifest for a bundle specifies identity, lifecycle and configuration dialog using Run > Run Configurations… menu dependency information. entry. Double click on OSGi Framework. On the Bundles tab, uncheck the Target Platform box in the list of bundles and then Bundle-RequiredExecutionEnvironment: J2SE-1.5 click Add Required Bundles. You should now have two bundles Bundle-SymbolicName: org.equinoxosgi.helloworld Bundle-Version: 1.0.0 selected, yours and org.eclipse.osgi. Click Run. The OSGi Bundle-Name: Hello World console will appear and your println message should appear. Bundle-ManifestVersion: 2 osgi> Hello, World Bundle-ClassPath: . Bundle-Activator: org.equinoxosgi.helloworld.Activator To get a sense of what’s happening and the kind of dynamic Export-Package: org.equinoxosgi.helloworld behavior that is inherent in Equinox, type ss in the console. This Import-Package: org.osgi.framework; version="1.3.0" shows a “short status” of the system. Each bundle is listed along with its numeric id and current state. Notice that your bundle is Important headers ACTIVE. That means its start() method has been called. Header Description/Use osgi> ss Bundle-SymbolicName ([a..zA..Z] | [0..9] | ’_’ | ’-’ | ’.’)* sequence that distinguishes this bundle from other bundles. Typically Framework is launched. Java package naming conventions are used. id State Bundle Bundle-Version Four part numeric version number where the fourth 0 ACTIVE org.eclipse.osgi_3.5.0.v20081027-1700 segment is alphanumeric. The combination of Bundle- 1 ACTIVE org.equinoxosgi.helloworld_1.0.0 SymbolicName and Bundle-Version uniquely identify a set of bundle content. Stop your bundle by typing stop 1. The bundle stopped and the Bundle-Name Human-readable name for this bundle. message from stop() is printed. You can restart the bundle using Bundle-ManifestVersion What version of OSGi markup is being used. Typical start 1. A bundle can be started and stopped as many times as value is 2. DZone, Inc. | www.dzone.com 3 Getting Started with Equinox & OSGi tech facts at your fingertips Bundles, continued Dependencies, continued Important headers Require-Bundle Header Description/Use Advantages Disadvantages Bundle-ClassPath Comma-separated list of JAR entries (directories or • Can be used for non-code • Tight coupling – can be brittle since it JAR files) in the bundle in which to find classes and dependencies: e.g. Help doc requires the presence of a specific bundle resources. ’.’ (dot) is the default and signifies the contributions • Split packages – Completeness, ordering, bundle’s root directory (i.e., the bundle itself.) • Convenient for depending on all performance exports from a bundle • Allows one bundle to shadow/override Bundle- Comma-separated list of execution environments in • Joins packages split over bundles packages from another RequiredExecutionEnvironment which this bundle can run. For example, CDC-1.0/ • Useful when refactoring bundle code • Can result in unexpected signature Foundation-1.0,J2SE-1.3. or introducing OSGi changes Bundle-Activator The class used to manage the lifecycle (e.g., start and stop) of this bundle. Export-Package Comma-separated list of Java packages made Historically, Eclipse has used Require-Bundle to available to others by this bundle. Each package may Hot be individually version numbered. Tip specify prerequisites, as that was the mechanism Import-Package Comma-separated list of Java packages this bundle first put into place. Since the introduction of Equi- requires. Each package can be qualified with a version range. nox, however, Import-Package has been the recommended way Require-Bundle Comma-separated list of bundles that this bundle of specifying dependencies. requires. Each bundle can be qualified with a version range. Bundle-NativeCode Description of the native code libraries contained in this bundle. Eclipse includes comprehensive tooling for Equi- Bundle-ActivationPolicy “lazy” to indicate that this bundle should be activated Hot when its code is first referenced. Tip nox and OSGi. The Plug-in Development Environ- ment (PDE) includes tools for defining, navigating and launching bundles. For example, the manifest editor we DEPENDENCIES saw earlier is part of PDE. It includes a Dependencies tab that supports the analysis of the code in a bundle and the auto- In OSGi, all bundles explicitly declare the packages they expose matic addition of dependencies found. to others and the packages they require from others – their dependencies. This yields two main benefits: n Creating valid configurations is easier.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us