blob: 4a18910b8f026d75fd983699f172e889b0fa2bb5 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= NetBeans 平台中使用 OSGi 的快速入门
:jbake-type: platform_tutorial
:jbake-tags: tutorials
:jbake-status: published
:syntax: true
:source-highlighter: pygments
:toc: left
:toc-title:
:icons: font
:experimental:
:description: NetBeans 平台中使用 OSGi 的快速入门 - Apache NetBeans
:keywords: Apache NetBeans Platform, Platform Tutorials, NetBeans 平台中使用 OSGi 的快速入门
本文档快速演示了如何将 OSGi 包集成到 NetBeans 平台应用程序中。
本教程重点介绍一个基于 link:http://www.aqute.biz/Code/Download#sudoku[由 Peter Kriens 开发的 Sudoku 游戏]的 "hello world" 方案。您需要导入构成 Sudoku 游戏的 OSGi 包,以及所需的相关 Equinox 包。在部署该应用程序之后,Web 浏览器中会显示以下应用程序:
image::http://netbeans.dzone.com/sites/all/files/sudokugame-4.png[]
在学完此简单方案后,本教程结尾的“深入阅读”部分还列出了更多有用的方案。
*注意:*为开发适用于 NetBeans 平台的应用程序,您无需下载该平台的独立版本。通常,您是在 NetBeans IDE 中开发应用程序和模块,并且只包含运行 NetBeans 平台和您的应用程序所需的模块。
== 设置环境
除了下载上面列出的软件以外,您还需要执行一些步骤来准备您的环境,如下所述。
[start=1]
1. 在您磁盘上的文件系统中创建一个新文件夹。
[start=2]
1. Equinox 分发中的以下文件存放在该文件夹中:
* javax.servlet_xxx.jar
* org.eclipse.equinox.ds_xxx.jar
* org.eclipse.equinox.http_xxx.jar
* org.eclipse.equinox.util_xxx.jar
* org.eclipse.osgi.services_xxx.jar
* org.eclipse.osgi_xxx.jar
[start=3]
1. 将下面两个 Sudoku JAR 也存放在上述 JAR 所在的文件夹中:
* aQute.sudoku.jar
* aQute.webrpc.jar
现在,您已将要导入的 OSGi 包集中存放在一个位置,从该位置可以将这些包集成到应用程序中。
== 创建应用程序
在准备好所有必需的 OSGi 包之后,请按照下面所述创建新的 NetBeans 平台应用程序。
[start=1]
1. 选择“文件”|“新建项目”,然后选择通过 Equinox 模板创建基于 Equinox NetBeans 平台应用程序:
image::http://netbeans.dzone.com/sites/all/files/sudokugame-5.png[]
单击“下一步”。
[start=2]
1. 为该应用程序指定名称,如下所示。
image::http://netbeans.dzone.com/sites/all/files/sudokugame-3.png[]
单击“完成”。
[start=3]
1. 您可以运行通过上述模板生成的应用程序,随即您会看到包含一个窗口的应用程序,其中显示了构成应用程序的所有 NetBeans 模块和 OSGi 包。
== 导入 OSGi
除了下载上面列出的软件以外,您还需要执行一些步骤来准备您的环境,如下所述。
[start=1]
1. 在“项目”窗口中右键单击该应用程序,选择“属性”,转至“库”标签,单击“添加群集”,然后浏览到上述 JAR 所在的文件夹。随即会打开一个向导,以便您将这些 JAR 转换为一个“群集”(即一组将被合并到应用程序中的相关模块/包)。
image::http://netbeans.dzone.com/sites/all/files/sudokugame-1.png[]
[start=2]
1. 如上所示,将所有导入的 OSGi 包设置为“启用”,以便在应用程序启动时可以装入并使用这些包。
[start=3]
1. 单击“完成”后,将会更新 "platform.properties" 配置文件。它应该与以下内容类似:
[source,java]
----
cluster.path.extra=netbinox:\../../path/to/my/nbsudokugame/folder
disabled.modules.extra=org.netbeans.libs.felix
cluster.path=\
${nbplatform.active.dir}/harness:\
${nbplatform.active.dir}/platform:\
${cluster.path.extra}
disabled.modules=\
org.jdesktop.layout,\
org.netbeans.api.visual,\
org.netbeans.core.execution,\
org.netbeans.core.multiview,\
org.netbeans.insane,\
org.netbeans.libs.felix,\
org.netbeans.libs.jsr223,\
org.netbeans.modules.apisupport.harness,\
org.netbeans.modules.apisupport.tc.cobertura,\
org.netbeans.modules.autoupdate.services,\
org.netbeans.modules.autoupdate.ui,\
org.netbeans.modules.core.kit,\
org.netbeans.modules.favorites,\
org.netbeans.modules.jellytools.platform,\
org.netbeans.modules.jemmy,\
org.openide.compat,\
org.openide.execution,\
org.openide.options,\
org.openide.util.enumerations
nbplatform.active=default
----
*注意:*上面的第一行指向您之前创建的文件夹。
[start=4]
1. 然后,将以下代码行添加到应用程序的 "project.properties" 配置文件中,该代码指定将不显示闪屏,并且还指定了用于部署应用程序的端口:
[source,java]
----
run.args.extra=--nosplash -J-Dorg.osgi.service.http.port=8080
----
== 运行应用程序
现在,该应用程序已准备就绪,可以按照如下所述进行部署。
[start=1]
1. 运行应用程序!随即会部署应用程序中的所有 OSGi 包和 NetBeans 模块。同时,还会部署用于查看已部署的 OSGi 包和 NetBeans 模块的应用程序,最终提供的桌面应用程序可使您非常方便地监视当前已部署的项目:
image::http://netbeans.dzone.com/sites/all/files/sudokugame-6_0.png[]
或者,也可以删除提供上述窗口的整个模块。然后,删除上述窗口需要的所有模块,即删除窗口系统、操作系统及其他所有内容,但 OSGi 集成所需的一些 JAR 除外,这些 JAR 包括:引导、启动、文件系统、模块系统、实用程序和查找。
[source,java]
----
cluster.path.extra=netbinox:\../../path/to/my/nbsudokugame/folder
disabled.modules.extra=org.netbeans.libs.felix
cluster.path=\
${nbplatform.active.dir}/harness:\
${nbplatform.active.dir}/platform:\
${cluster.path.extra}
disabled.modules=\
org.jdesktop.layout,\
org.netbeans.api.annotations.common,\
org.netbeans.api.progress,\
org.netbeans.api.visual,\
org.netbeans.core,\
org.netbeans.core.execution,\
org.netbeans.core.io.ui,\
org.netbeans.core.multiview,\
org.netbeans.core.nativeaccess,\
org.netbeans.core.output2,\
org.netbeans.core.ui,\
org.netbeans.core.windows,\
org.netbeans.insane,\
org.netbeans.libs.felix,\
org.netbeans.libs.jna,\
org.netbeans.libs.jsr223,\
org.netbeans.libs.junit4,\
org.netbeans.modules.apisupport.harness,\
org.netbeans.modules.apisupport.tc.cobertura,\
org.netbeans.modules.applemenu,\
org.netbeans.modules.autoupdate.services,\
org.netbeans.modules.autoupdate.ui,\
org.netbeans.modules.core.kit,\
org.netbeans.modules.editor.mimelookup,\
org.netbeans.modules.editor.mimelookup.impl,\
org.netbeans.modules.favorites,\
org.netbeans.modules.javahelp,\
org.netbeans.modules.jellytools.platform,\
org.netbeans.modules.jemmy,\
org.netbeans.modules.keyring,\
org.netbeans.modules.masterfs,\
org.netbeans.modules.nbjunit,\
org.netbeans.modules.options.api,\
org.netbeans.modules.options.keymap,\
org.netbeans.modules.print,\
org.netbeans.modules.progress.ui,\
org.netbeans.modules.queries,\
org.netbeans.modules.sendopts,\
org.netbeans.modules.settings,\
org.netbeans.modules.spi.actions,\
org.netbeans.spi.quicksearch,\
org.netbeans.swing.outline,\
org.netbeans.swing.plaf,\
org.netbeans.swing.tabcontrol,\
org.openide.actions,\
org.openide.awt,\
org.openide.compat,\
org.openide.dialogs,\
org.openide.execution,\
org.openide.explorer,\
org.openide.io,\
org.openide.loaders,\
org.openide.nodes,\
org.openide.options,\
org.openide.text,\
org.openide.util.enumerations,\
org.openide.windows
nbplatform.active=default
----
然后,您将获得一个非常适合进行模块化 Web 开发的非 GUI 应用程序(即服务器应用程序)。
[start=2]
1. 现在,在浏览器中输入以下 URL(可选,使用 NetBeans UI 实用程序 API 中的 URLDisplayer.getDefault() 以编程方式在所需的位置打开浏览器),随即会部署 Sudoku 游戏中定义的 "index.html" 并将其显示在浏览器中,接下来您便可以开始玩 Suduko 游戏:
[source,java]
----
http://localhost:8080/rpc/sudoku/index.html
----
恭喜!您已将第一批 OSGi 包集成到了 NetBeans 平台应用程序中。
== 深入阅读
现在,您已经学完了本教程并了解了在 NetBeans 平台应用程序中重用 OSGi 包所需执行的步骤,接下来可以参阅以下相关文档和更高级的方案:
* link:http://www.osgi.org/blog/2006_09_01_archive.html[Peter Kriens 和 Sudoku 游戏]
* link:http://wiki.apidesign.org/wiki/NetbinoxTutorial[Jaroslav Tulach 和 Netbinox]
* Toni Epple OSGi/NetBeans link:http://eppleton.sharedhost.de/blog/?p=662[博客文章]和 link:http://eppleton.sharedhost.de/blog/?s=Frankenstein%27s+IDE[演示]
* Gunnar Reinseth NetBeans EMF 集成( link:http://eclipse.dzone.com/emf-on-netbeans-rcp[第 1 部分], link:http://eclipse.dzone.com/emf-on-netbeans-rcp-2[第 2 部分])
* 还可以阅读 link:http://java.dzone.com/news/new-cool-tools-osgi-developers[OSGi 开发者的新炫酷工具]