OSGI 容器中的 Scala?

发布于 2024-10-18 08:07:06 字数 175 浏览 11 评论 0原文

如何在 Scala 中编码我的包,然后将其部署到 OSGI 容器中?

我是先将它编译成“java”,还是可以将 scala 直接部署到 OSGI 中并使用某种包来识别它?

任何指点都会很棒。 目前,我使用 Apache Felix 作为我的 osgi 容器,但只需对通用概念进行简单的解释就足以让我开始。

How can I code my bundle in Scala and then deploy it into OSGI container?

Do I compile it into "java" first or can i deploy scala straight into OSGI and use some kind of bundles to recognize it?

Any pointers would be great.
Currently I am using Apache Felix as my osgi-container, but just a simple explanation of generic concepts would suffice to get me started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

予囚 2024-10-25 08:07:06

感谢大家的回答,你们引导我找到了解决方案!我将在这里用更简单的术语为更广泛的受众描述它。

目标:用 scala 编写代码,部署到 OSGi。

使用工具:

  1. Equinox OSGi 实现
  2. Eclipse Helios 3.6、
  3. Scala 2.9

流程

  1. 安装Scala IDE< /a> 对于 Eclipse。查找适用于 Scala 2.9 和 Eclipse 3.6 的版本
  2. 在 Eclipse 中创建新的 Scala 项目
  3. 右键单击项目并选择:配置 -> 将项目转换为 OSGi 包转换为插件项目...

    现在,下一部分是我陷入困境的地方。您看,现在我们需要将此捆绑包(我们的项目)部署到 OSGi 环境。然而,我们缺少必须位于 OSGi 容器中的 Scala 类(或包含这些类的包)才能提供我们在包中使用的所有 Scala 包 API。不幸的是,找到“Scala 包”并不那么容易。在环顾四周之后,事实证明,出于某种原因,Scala 包实际上位于 Sonatype Maven 存储库

  4. 下载 scala-library-2.9.1.jarSonatype Maven 存储库,并将其部署(通过最适合您的方式)到您的 OSGi 容器。

  5. 调整你的清单文件以要求 Scala 包(我很确定这是一个包依赖项(即 Require-Bundle)实际上非常安全的地方 - 毕竟,如果没有它,你将永远不会运行你的 Scala 代码) Scala!):

    清单版本:1.0
    捆绑清单版本:2
    捆绑包名称:Scala Hello
    捆绑符号名称:com.test.scala.hello
    捆绑版本:1.0.0.qualifier
    捆绑包供应商: drozy
    导入包:org.osgi.framework;version=“1.5.0”
    捆绑激活器:com.test.scala.hello.Activator
    要求捆绑包:scala-library;bundle-version =“2.9.1”
    
  6. 现在,您可以在 Scala 中编写捆绑激活器(哇哦!):

    //Activator.scala
    com.test.scala.hello 包
    导入java.lang.System
    导入 org.osgi.framework.BundleActivator
    导入 org.osgi.framework.BundleContext
    
    类 Activator 扩展 BundleActivator {
      def 开始(上下文:BundleContext){
          System.out.println("来自 scala 的世界你好!");
      }
      def stop(上下文: BundleContext){}
    }
    
  7. 将您的项目作为捆绑包部署到 OSGi 容器,并留意“Hello world from scala!”

Thanks to everyone for the answers, you led me to the solution! I will describe it here in a little simpler terms for a wider audience.

Goal: Code in scala, deploy to OSGi.

Tools used:

  1. Equinox OSGi implementation
  2. Eclipse Helios 3.6,
  3. Scala 2.9

Procedure

  1. Install Scala IDE for Eclipse. Find version that will work with Scala 2.9 and Eclipse 3.6
  2. Create new Scala Project in Eclipse.
  3. Convert the project to OSGi bundle by right clicking on it and selecting: Configure -> Convert to Plug-in Projects...

    Now, the next part was where I got stuck. You see, now we need to deploy this bundle (our project) to OSGi environment. However we are missing the Scala classes (or bundle that contains those classes) that have to be in OSGi container to provide all the Scala packages API we use in our bundle. Unfortunately finding the "Scala bundle" is not that easy. After looking around it turns out, that for some reason, Scala bundle is actually located in the Sonatype Maven Repository.

  4. Download the scala-library-2.9.1.jar from the appropriate location in the Sonatype Maven Repository, and deploy it (by means most comfortable for you) to your OSGi container.

  5. Adjust your manifest file to require the Scala bundle (I am pretty sure that this is one place where bundle dependency (i.e. Require-Bundle) is actually pretty safe - after all, you will never run your Scala code without Scala!):

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: Scala Hello
    Bundle-SymbolicName: com.test.scala.hello
    Bundle-Version: 1.0.0.qualifier
    Bundle-Vendor: drozzy
    Import-Package: org.osgi.framework;version="1.5.0"
    Bundle-Activator: com.test.scala.hello.Activator
    Require-Bundle: scala-library;bundle-version="2.9.1"
    
  6. Now, you can write your bundle activator in Scala (wooho!):

    //Activator.scala
    package com.test.scala.hello
    import java.lang.System
    import org.osgi.framework.BundleActivator
    import org.osgi.framework.BundleContext
    
    class Activator extends BundleActivator {
      def start(context: BundleContext) {
          System.out.println("Hello world from scala!");
      }
      def stop(context: BundleContext){}
    }
    
  7. Deploy your project as a bundle to OSGi container and look out for the "Hello world from scala!" message.

多情出卖 2024-10-25 08:07:06

ScalaModules

作者的快速介绍视频 2010 年 Scala 日

ScalaModules

A quick intro video by the author here Scala days 2010

诗化ㄋ丶相逢 2024-10-25 08:07:06

OSGi 不关心你用什么语言编写代码:JVM 字节码只是 JVM 字节码。因此:

  • 使用 Scala 编译,
  • 使用 bnd 将生成的类包装在一个包中(就像从 Java 源代码编译的代码一样)。
  • 将捆绑包部署到 OSGi,就像部署“普通”捆绑包一样(因为它是普通捆绑包)。

您会注意到您的包依赖于 Scala 库。同样,这也没什么奇怪的,就像 Java 代码中存在依赖关系一样。为了解决这些依赖关系,您需要从 scala-lang-osgi 安装 Scala 库包

OSGi does not care what language you write your code in: JVM bytecode is just JVM bytecode. So:

  • compile with Scala
  • wrap the resulting classes in a bundle using bnd (just as you would for code compiled from Java sources).
  • deploy the bundle to OSGi, just as you would for a "normal" bundle (because it is a normal bundle).

You'll notice that your bundle has dependencies on the Scala library. Again there is nothing strange about this, it's just like having dependencies in you Java code. In order for those dependencies to resolve, you need to install the Scala library bundle from scala-lang-osgi

×纯※雪 2024-10-25 08:07:06

它没有什么特别之处:用 Scala 编写代码,并通过提供必要的包元数据和服务描述符将其包装为 OSGi 包,就像使用 Java 一样。

Apache Maven 可以在此过程中为您提供帮助。查看 Guggla 项目(Scala 脚本引擎)的工作示例。 maven-bundle-plugin 在 < a href="https://github.com/guggla/guggla/blob/master/pom.xml" rel="noreferrer">pom.xml 负责生成捆绑包元数据并将其包含在最终版本中jar 文件。它指的是服务描述符 您需要提供的 xml 文件。

There is nothing special to it: write your code in Scala and wrap it up as an OSGi bundle by providing the necessary bundle meta data and service descriptors as you would do with Java.

Apache Maven can help you in the process. Have a look at the Guggla Project (a Scala script engine) for a working example. The maven-bundle-plugin in the pom.xml takes care of generating and including the bundle meta data in the final jar file. It refers to the service descriptor xml file which you need to provide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文