一、官網下載http://felix.apache.org/downloads.cgi,
當前最新版本6.0.3
運行felix窗口有兩種方式
(1) 能夠直接下載發佈版本
Felix Framework Distribution(org.apache.felix.main.distribution-6.0.3.zip)html
解壓後目錄java
進入項目目錄,命令行下執行apache
java -jar ./bin/felix.jar
felix.jar 默認會在當前運行目錄的 bundle 文件夾內加載啓動所需的插件jar,config 目錄下的 config.propeties 和 system.properties 裏面加載環境變量,若是將其餘目錄做爲啓動根目錄,該目錄下不存在 felix 啓動所需信息,啓動就會有問題。項目第一次啓動的時候會在啓動目錄下建立一個felix-cache的目錄,用於存放框架運行過程產生的數據,固然該目錄能夠在啓動的時候配置,使用 java -jar ./bin/felix.jar <felix-dir> 便可。api
(2) 源碼導入eclipse運行
下載子項目Main(org.apache.felix.main-6.0.3-project.zip)瀏覽器
解壓org.apache.felix.main-6.0.3-project.zip,進入目錄,命令行下
運行mvn eclipse:eclipse工程文件,
再輸入mvn package,打包編譯
File->Import導入到eclipsebash
在eclipse的org.apache.felix.main.Main類右鍵運行Run As Java Application框架
經常使用命令eclipse
lb:查看已裝插件包 start:運行插件包 stop:中止運行插件包 install:安裝插件包 uninstall:刪除插件包 其它命令能夠經過help查看
二、felix開發插件
參考是felix官方例子代碼apache-felix-osgi-tutorial裏的
apache-felix-tutorial-example-2
apache-felix-tutorial-example-2b
apache-felix-tutorial-example-3
這是一個檢查輸入單詞(英文、中文....)是否正確的例子功能ide
先定義一個服務類:工具
package test.penngo.service; /** * 簡單的定義了一個字典服務接口,該接口的功能只是簡單的驗證一個詞是否正確。 **/ public interface DictionaryService { /** * 檢查 單詞是否存在 **/ public boolean checkWord(String word); }
英文詞典插件包開發
package tutorial.example2; import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import test.penngo.service.DictionaryService; /** * 英文字典服務插件 **/ public class Activator implements BundleActivator { /** * 服務啓動 * * @param context the framework context for the bundle. **/ public void start(BundleContext context) { Hashtable<String, String> props = new Hashtable<String, String>(); props.put("Language", "English"); context.registerService(DictionaryService.class.getName(), new DictionaryImpl(), props); } /** * 服務中止 * * @param context the framework context for the bundle. **/ public void stop(BundleContext context) { // NOTE: The service is automatically unregistered. } /** * 英文詞典服務實現 **/ private static class DictionaryImpl implements DictionaryService { String[] m_dictionary = { "welcome", "to", "the", "osgi", "tutorial" }; /** * 英文單詞檢測 **/ public boolean checkWord(String word) { word = word.toLowerCase(); for (int i = 0; i < m_dictionary.length; i++) { if (m_dictionary[i].equals(word)) { return true; } } return false; } } }
META-INF.MF
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: English Bundle-Name: English dictionary Bundle-Description: A bundle that registers an English dictionary service Bundle-Vendor: penngo Bundle-Version: 1.0.0 Bundle-Activator: tutorial.example2.Activator Import-Package: org.osgi.framework, test.penngo.service
英文詞典插件打包導出,eclipse自帶的export打包會有問題,本文使用fatjar工具打包,
中文詞典插件包開發
package tutorial.example2b; import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import test.penngo.service.DictionaryService; /** * 中文詞典包 **/ public class Activator implements BundleActivator { public void start(BundleContext context) { Hashtable<String, String> props = new Hashtable<String, String>(); props.put("Language", "Chinese"); context.registerService( DictionaryService.class.getName(), new DictionaryImpl(), props); } public void stop(BundleContext context) { // NOTE: The service is automatically unregistered. } /** * 中文詞典服務實現 **/ private static class DictionaryImpl implements DictionaryService { // The set of words contained in the dictionary. String[] m_dictionary = { "歡迎", "你好", "教程", "模塊" }; public boolean checkWord(String word) { word = word.toLowerCase(); for (int i = 0; i < m_dictionary.length; i++) { if (m_dictionary[i].equals(word)) { return true; } } return false; } } }
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: Chinese Bundle-Name: Chinese dictionary Bundle-Description: A bundle that registers a Chinese dictionary service Bundle-Vendor: penngo Bundle-Version: 1.1.0 Bundle-Activator: tutorial.example2b.Activator Import-Package: org.osgi.framework, test.penngo.service
插件打包
客戶端調用詞典開發,也是做爲一個felix插件
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; /** * 此客戶端在啓動時,接收控制檯輸入,檢查輸入的單詞是否正確,而且打印初相應的檢測結果, * 注意:當咱們沒有輸入任何信息的時候,單詞檢測便會結束,若是須要從新進入單詞檢測程序,咱們須要從新啓動插件。 **/ public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { // 獲取全部詞典插件 ServiceReference[] refs = context.getServiceReferences( DictionaryService.class.getName(), "(Language=*)"); if (refs != null) { System.out.println("=======當前可用詞典插件:" + 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.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: client Bundle-Name: Dictionary client Bundle-Description: A bundle that uses the dictionary service if it finds it at startup Bundle-Vendor: penngo Bundle-Version: 1.3.0 Bundle-Activator: tutorial.example3.Activator Export-Package: test.penngo.service Import-Package: org.osgi.framework #//注意:test.penngo.service這個包與tutorial.example3打在同一個jar中,若是example3.jar更新,其它依賴test.penngo.service的也須要更新或重啓felix.
安裝插件
把上邊生成的插件包example2.jar,example2b.jar,example3.jar入到org.apache.felix.main-6.0.3項目的plugins文件夾中
安裝和運行效果:
經過插件方式開發,後期繼續擴展增長其它語言詞典(日語,俄語,韓語),不須要重啓服務,也不影響已存在的語言詞典功能(英文、中文)。
三、開發http服務插件
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.MF Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: hello Bundle-Name: hello penngo Bundle-Description: Http Hello World Bundle-Vendor: penngo Bundle-Version: 1.0.0 Bundle-Activator: test.penngo.http.example.Activator Import-Package: org.osgi.framework, org.osgi.service.http, org.osgi.util.tracker, javax.servlet.http, javax.servlet
注意htpp插件包依賴:
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
須要先安裝上邊的插件包,才能正常在felix中運行http功能。
安裝運行效果
瀏覽器訪問:
附件源碼: