Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use eclipse to develop OSGI plug-ins for felix

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use eclipse to develop OSGI plug-ins for felix". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use eclipse to develop felix OSGI plug-ins".

1. Download http://felix.apache.org/downloads.cgi on the official website

Current latest version 6.0.3

There are two ways to run a felix window

(1) you can download the release version directly.

Felix Framework Distribution (org.apache.felix.main.distribution-6.0.3.zip)

Decompressed directory

Enter the project directory and execute it under the command line

Java-jar. / bin/felix.jar

By default, felix.jar loads environment variables in config.propeties and system.properties under the jar,config directory of plug-ins required for startup in the bundle folder of the current running directory. If you use other directories as the startup root directory, there is no information required for felix startup in this directory, and there will be problems with startup. When the project starts for the first time, it will create a felix-cache directory under the startup directory to store the data generated by the framework running process. Of course, this directory can be configured at startup time, using java-jar. / bin/felix.jar.

(2) Import source code into eclipse and run

Download subproject Main (org.apache.felix.main-6.0.3-project.zip)

Extract org.apache.felix.main-6.0.3-project.zip, enter the directory, and go to the command line.

Run the mvn eclipse:eclipse project file

Type mvn package again, package and compile

File- > Import Import to eclipse

Right-click on the org.apache.felix.main.Main class of eclipse to run Run As Java Application

Common command

Lb: view installed plug-in package start: run plug-in package stop: stop running plug-in package install: install plug-in package uninstall: delete plug-in package other commands can be viewed through help

2. Felix development plug-in

The reference is from the official example code apache-felix-osgi-tutorial of felix

Apache-felix-tutorial-example-2

Apache-felix-tutorial-example-2b

Apache-felix-tutorial-example-3

This is a check for input words (English, Chinese.) Whether the correct example function

First define a service class:

Package test.penngo.service; / * simply defines a dictionary service interface that simply verifies whether a word is correct. * * / public interface DictionaryService {/ * check if the word exists * * / public boolean checkWord (String word);}

Development of plug-in package for English Dictionary

Package tutorial.example2;import java.util.Hashtable;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import test.penngo.service.DictionaryService;/** * English dictionary service plug-in * * / public class Activator implements BundleActivator {/ * service startup * * @ param context the framework context for the bundle. * * / public void start (BundleContext context) {Hashtable props = new Hashtable (); props.put ("Language", "English"); context.registerService (DictionaryService.class.getName (), new DictionaryImpl (), props);} / * * Service stop * * @ param context the framework context for the bundle. * / public void stop (BundleContext context) {/ / NOTE: The service is automatically unregistered. } / * English dictionary service implementation * * / private static class DictionaryImpl implements DictionaryService {String [] m_dictionary = {"welcome", "to", "the", "osgi", "tutorial"} / * * English word detection * * / public boolean checkWord (String word) {word = word.toLowerCase (); for (int I = 0; I < m_dictionary.length) Return true; +) {if (m _ dictionary.equals (word)) {return true;}} return false;}

META-INF.MF

Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: EnglishBundle-Name: English dictionaryBundle-Description: A bundle that registers an English dictionary serviceBundle-Vendor: penngoBundle-Version: 1.0.0Bundle-Activator: tutorial.example2.ActivatorImport-Package: org.osgi.framework, test.penngo.service

English dictionary plug-ins are packaged and exported, but there will be problems with the export package that comes with eclipse. This article uses the fatjar tool to package.

Development of plug-in package for Chinese Dictionary

Package tutorial.example2b; import java.util.Hashtable;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import test.penngo.service.DictionaryService;/** * Chinese dictionary package * * / public class Activator implements BundleActivator {public void start (BundleContext context) {Hashtable props = new Hashtable (); props.put ("Language", "Chinese"); context.registerService (DictionaryService.class.getName (), new DictionaryImpl (), props) } public void stop (BundleContext context) {/ / NOTE: The service is automatically unregistered. } / * Chinese dictionary service implementation * * / private static class DictionaryImpl implements DictionaryService {/ / The set of words contained in the dictionary. String [] m_dictionary = {"Welcome", "Hello", "tutorial", "Module"}; public boolean checkWord (String word) {word = word.toLowerCase (); for (int I = 0; I < m_dictionary.length) ITunes +) {if (m _ dictionary.equals (word)) {return true;}} return false } Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: ChineseBundle-Name: Chinese dictionaryBundle-Description: A bundle that registers a Chinese dictionary serviceBundle-Vendor: penngoBundle-Version: 1.1.0Bundle-Activator: tutorial.example2b.ActivatorImport-Package: org.osgi.framework, test.penngo.service

Plug-in packaging

Client call dictionary development, also as a felix plug-in

Package tutorial.example3;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOException;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import test.penngo.service.DictionaryService / * * when the client starts, it receives the input from the console, checks whether the entered words are correct, and prints the initial corresponding test results. * Note: when we do not enter any information, the word detection will end. If we need to re-enter the word detection program, we need to restart the plug-in. * * / public class Activator implements BundleActivator {public void start (BundleContext context) throws Exception {/ / get all dictionary plug-ins ServiceReference [] refs = context.getServiceReferences (DictionaryService.class.getName (), "(Language=*)"); if (refs! = null) {System.out.println ("= currently available dictionary plug-ins:" + refs.length) Try {System.out.println ("Enter a blank line to exit."); BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); String word = ""; while (true) {System.out.print ("Enter word:") Word = in.readLine (); if (word.length () = = 0) {break;} boolean isCorrect = false For (ServiceReference service:refs) {DictionaryService dictionary = (DictionaryService) context.getService (service); if (dictionary.checkWord (word)) {isCorrect = true System.out.println ("Correct.");}} if (isCorrect = = false) {System.out.println ("Incorrect.") } catch (IOException ex) {ex.printStackTrace ();}} else {System.out.println ("Couldn't find any dictionary service...");}} / * * Implements BundleActivator.stop (). Does nothing since * the framework will automatically unget any used services. * @ param context the framework context for the bundle. * / public void stop (BundleContext context) {/ / NOTE: The service is automatically released. }} Manifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: clientBundle-Name: Dictionary clientBundle-Description: A bundle that uses the dictionary service if it finds it at startupBundle-Vendor: penngoBundle-Version: 1.3.0Bundle-Activator: tutorial.example3.ActivatorExport-Package: test.penngo.serviceImport-Package: org.osgi.framework#// Note: the package test.penngo.service is in the same jar as tutorial.example3. If example3.jar is updated, other test.penngo.service-dependent users also need to update or restart felix.

Install the plug-in

Put the plug-in package example2.jar,example2b.jar,example3.jar generated above into the plugins folder of the org.apache.felix.main-6.0.3 project

Installation and running effect:

Through plug-in development, continue to expand to add other language dictionaries (Japanese, Russian, Korean), do not need to restart the service, nor affect the existing language dictionary functions (English, Chinese).

3. Develop http service plug-in

Package test.penngo.http.example;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter () .write ("hello penngo");} package test.penngo.http.example;import org.osgi.framework.BundleActivator Import org.osgi.framework.BundleContext;import org.osgi.framework.ServiceReference;import org.osgi.service.http.HttpService;import org.osgi.util.tracker.ServiceTracker;public class Activator implements BundleActivator {private ServiceTracker httpTracker Public void start (BundleContext context) throws Exception {httpTracker = new ServiceTracker (context, HttpService.class.getName (), null) {public void removedService (ServiceReference reference, Object service) {/ / HTTP service is no longer available, unregister our servlet... Try {((HttpService) service) .unregister ("/ hello");} catch (IllegalArgumentException exception) {/ / Ignore; servlet registration probably failed earlier on... }} public Object addingService (ServiceReference reference) {/ / HTTP service is available, register our servlet... HttpService httpService = (HttpService) this.context.getService (reference); try {httpService.registerServlet ("/ hello", new TestServlet (), null, null);} catch (Exception exception) {exception.printStackTrace () } return httpService;}}; / / start tracking all HTTP services... HttpTracker.open ();} public void stop (BundleContext context) throws Exception {/ / stop tracking all HTTP services... HttpTracker.close ();}} # MANIFEST.MFManifest-Version: 1.0Bundle-ManifestVersion: 2Bundle-SymbolicName: helloBundle-Name: hello penngoBundle-Description: Http Hello WorldBundle-Vendor: penngoBundle-Version: 1.0.0Bundle-Activator: test.penngo.http.example.ActivatorImport-Package: org.osgi.framework, org.osgi.service.http, org.osgi.util.tracker, javax.servlet.http, javax.servlet

Note that the htpp plug-in package depends on:

Org.apache.felix.http.base-4.0.6.jar,org.apache.felix.http.bridge-4.0.8.jar,org.apache.felix.http.jetty-4.0.10.jar,org.apache.felix.http.servlet-api-1.1.2.jar,org.apache.felix.http.whiteboard-4.0.0.jar

You need to install the plug-in package above before you can run the http function properly in felix.

Installation and operation effect

Browser access:

Thank you for reading, the above is "how to use eclipse to develop felix OSGI plug-in" content, after the study of this article, I believe you on how to use eclipse to develop felix OSGI plug-in this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report