https://github.com/kagyuu/OSGiExam
public interface Hello { String sayHello(String name); }
public class HelloImpl { public String sayHello(String name) { return "Hello " + name; } }
public class Client { @Inject private Hello hello; public void doSomething() { System.out.println(hello.sayHello("sachi")); } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>OSGiExam</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>OSGiExam</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>osgi-cdi-api</artifactId>
<version>3.1</version>
<scope>provided</scope>
</dependency>
<!-- It's not javaee-api, because we want to test business logic -->
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<!-- glassfish nexus repo for glassfish dependencies -->
<repository>
<id>glassfish-repo-archive</id>
<name>Nexus repository collection for Glassfish</name>
<url>http://maven.glassfish.org/content/groups/glassfish</url>
<snapshots>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
</project>
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency>でも良いけど、RDB 更新込みの Unit Test をやるためには Java EE の実装が必要。
</repositories> <modules> <module>OSGiExamApi</module> </modules> </project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>OSGiExam</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>OSGiExamApi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>OSGiExamApi OSGi Bundle</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
...
package com.mycompany.osgiexamapi;
public interface Hello {
String sayHello(String name);
}
package com.mycompany.osgiexamapi;
import javax.ejb.Local;
@Local
public interface GoodBye {
String sayGoodBye(String name);
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>OSGiExam</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Activator>com.mycompany.osgiexamapi.Activator</Bundle-Activator>
<Export-Package >com.mycompany.osgiexamapi</Export-Package>
<Private-Package>com.mycompany.osgiexamapi.*</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
Manifest-Version: 1.0
Bnd-LastModified: 1378399068929
Build-Jdk: 1.7.0_21
Built-By: atsushi
Bundle-Activator: com.mycompany.osgiexamapi.Activator
Bundle-ManifestVersion: 2
Bundle-Name: OSGiExamApi OSGi Bundle
Bundle-SymbolicName: com.mycompany.OSGiExamApi
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: com.mycompany.osgiexamapi;uses:="org.osgi.framework,java
x.ejb";version="1.0.0.SNAPSHOT"
Import-Package: javax.ejb,org.osgi.framework;version="[1.5,2)"
Tool: Bnd-1.50.0
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>OSGiExam</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>OSGiExamWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>OSGiExamWeb</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>OSGiExamApi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>osgi-cdi-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
</dependency>
</dependencies>
...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>OSGiExam</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>OSGiExamImpl</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>OSGiExamImpl OSGi Bundle</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>OSGiExamApi</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
...
package com.mycompany.osgiexamimpl;
import com.mycompany.osgiexamapi.Hello;
public class HelloImpl implements Hello {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
package com.mycompany.osgiexamimpl;
import com.mycompany.osgiexamapi.Hello;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implements BundleActivator {
@Override
public void start(final BundleContext context) throws Exception {
System.out.println(String.format("start::%s", Activator.class.getCanonicalName()));
context.registerService(Hello.class.getName(), new HelloImpl(), null);
System.out.println(String.format("activated::%s as %s", HelloImpl.class.getName(), Hello.class.getName()));
}
@Override
public void stop(final BundleContext context) throws Exception {
context.ungetService(context.getServiceReference(Hello.class.getName()));
System.out.println(String.format("deactivated::%s", Hello.class.getName()));
System.out.println(String.format("stop::%s", Activator.class.getCanonicalName()));
}
}
バンドル初期化時には、まだ他のバンドルを参照できないことに注意。Logger も使えないよ (ハマった)、なにか出力したかったら System.out.prinltn() で動かしてみる
package com.mycompany.osgiexamweb;
import com.mycompany.osgiexamapi.Hello;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "LookupPojoFromCDI", urlPatterns = {"/LookupPojoFromCDI"})
public class LookupPojoFromCDI extends HttpServlet {
@Resource
private Hello hello;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>@Resource</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>lookup POJO by @Resource</h1>");
out.println(hello.sayHello("Sachi"));
out.println("<br/><br/><font color=\"gray\">Hello Class is ");
out.println(hello.getClass().getCanonicalName());
out.println("@");
out.println(hello.hashCode());
out.println("</font>");
} catch (Exception ex) {
out.println("<pre>");
ex.printStackTrace(out);
out.println("</pre>");
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
}
... undle:install] Installing com/mycompany/OSGiExamImpl/1.0-SNAPSHOT/OSGiExamImpl-1.0-SNAPSHOT.jar Writing OBR metadata ------------------------------------------------------------------------ Reactor Summary: OSGiExam .......................................... SUCCESS [21.206s] OSGiExamApi OSGi Bundle ........................... SUCCESS [6.011s] OSGiExamWeb ....................................... SUCCESS [4.712s] OSGiExamImpl OSGi Bundle .......................... SUCCESS [0.717s] ------------------------------------------------------------------------ BUILD SUCCESS ------------------------------------------------------------------------ Total time: 34.398s Finished at: Sat Sep 07 22:54:43 JST 2013 Final Memory: 37M/90M ------------------------------------------------------------------------
[~]$ cp ~/NetBeansProjects/OSGiExam/OSGiExamApi/target/OSGiExamApi-1.0-SNAPSHOT.jar \ /Applications/NetBeans/glassfish-3.1.2.2/glassfish/domains/domain1/autodeploy/bundles/ [~]$ cp ~/NetBeansProjects/OSGiExam/OSGiExamImpl/target/OSGiExamImpl-1.0-SNAPSHOT.jar \ /Applications/NetBeans/glassfish-3.1.2.2/glassfish/domains/domain1/autodeploy/bundles/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>OSGiExam</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>OSGiExamEjb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>OSGiExamEjb</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>OSGiExamApi</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<!-- add bundle plugin generated manifest to the war -->
<manifestFile>
${project.build.outputDirectory}/META-INF/MANIFEST.MF
</manifestFile>
</archive>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.2.0</version>
<extensions>true</extensions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>ejb</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>jar</supportedProjectType>
</supportedProjectTypes>
<instructions>
<!-- Specify elements to add to MANIFEST.MF -->
<Export-EJB>ALL</Export-EJB>
<!-- By default, nothing is exported -->
<Export-Package>!*.impl.*, *</Export-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
<execution>
<id>bundle-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: 1.7.0_21 (Oracle Corporation) Built-By: atsushi Build-Jdk: 1.7.0_21 Export-Package: com.mycompany.osgiexamejb;uses:="javax.ejb,com.mycompa ny.osgiexamapi" Export-EJB: ALL Bundle-Version: 1.0.0.SNAPSHOT Tool: Bnd-1.15.0 Bundle-Name: OSGiExamEjb Bnd-LastModified: 1378646187379 Bundle-ManifestVersion: 2 Bundle-SymbolicName: com.mycompany.OSGiExamEjb Import-Package: com.mycompany.osgiexamapi;version="[1.0,2)",javax.ejb
package com.mycompany.osgiexamejb;
import com.mycompany.osgiexamapi.GoodBye;
import javax.ejb.Stateless;
@Stateless
public class GoodByeEjb implements GoodBye {
@Override
public String sayGoodBye(String name) {
return "GoodBye " + name;
}
}
動かしてみる
package com.mycompany.osgiexamweb;
import com.mycompany.osgiexamapi.GoodBye;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "LookupEjbFromJNDI", urlPatterns = {"/LookupEjbFromJNDI"})
public class LookupEjbFromJNDI extends HttpServlet {
@EJB(lookup = "java:global/com.mycompany.OSGiExamEjb_1.0.0.SNAPSHOT/GoodByeEjb")
private GoodBye goodbye;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>@EJB</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>lookup EJB Object by @EJB</h1>");
out.println(goodbye.sayGoodBye("Sachi"));
out.println("<br/><br/><font color=\"gray\">GoodBye Class is ");
out.println(goodbye.getClass().getCanonicalName());
out.println("@");
out.println(goodbye.hashCode());
out.println("</font>");
} catch (Exception ex) {
out.println("<pre>");
ex.printStackTrace(out);
out.println("</pre>");
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
}
[~]$ cp ~/NetBeansProjects/OSGiExam/OSGiExamEjb/target/OSGiExamEjb-1.0-SNAPSHOT.jar \ /Applications/NetBeans/glassfish-3.1.2.2/glassfish/domains/domain1/autodeploy/bundles/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>OSGiExam</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>OSGiExamWeb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>OSGiExamWeb</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>OSGiExamApi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>osgi-cdi-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<!-- add bundle plugin generated manifest to the war -->
<manifestFile>
${project.build.outputDirectory}/META-INF/MANIFEST.MF
</manifestFile>
<!-- For some reason, adding Bundle-ClassPath in maven-bundle-plugin
confuses that plugin and it generates wrong Import-Package, etc.
So, we generate it here.-->
<manifestEntries>
<Bundle-ClassPath>WEB-INF/classes/</Bundle-ClassPath>
</manifestEntries>
</archive>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.2.0</version>
<extensions>true</extensions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>ejb</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>jar</supportedProjectType>
</supportedProjectTypes>
<instructions>
<!-- Specify elements to add to MANIFEST.MF -->
<Web-ContextPath>/mavenhellowebclient</Web-ContextPath>
<!-- By default, nothing is exported -->
<Export-Package>!*.impl.*, *</Export-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
<execution>
<id>bundle-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: 1.7.0_21 (Oracle Corporation) Built-By: atsushi Build-Jdk: 1.7.0_21 Bundle-ClassPath: WEB-INF/classes/ Export-Package: com.mycompany.osgiexamweb;uses:="javax.servlet,javax.e jb,com.mycompany.osgiexamapi,javax.servlet.annotation,javax.servlet.h ttp,org.glassfish.osgicdi,javax.inject,javax.annotation" Bundle-Version: 1.0.0.SNAPSHOT Tool: Bnd-1.15.0 Bundle-Name: OSGiExamWeb Bnd-LastModified: 1378649661814 Bundle-ManifestVersion: 2 Bundle-SymbolicName: com.mycompany.OSGiExamWeb Web-ContextPath: /mavenhellowebclient Import-Package: com.mycompany.osgiexamapi;version="[1.0,2)",javax.anno tation,javax.ejb,javax.inject,javax.servlet,javax.servlet.annotation, javax.servlet.http,org.glassfish.osgicdi;version="[1.0,2)"
動かしてみる
package com.mycompany.osgiexamweb;
import com.mycompany.osgiexamapi.Hello;
import java.io.IOException;
import java.io.PrintWriter;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.glassfish.osgicdi.OSGiService;
@WebServlet(name = "LookupPojoFromOSGi", urlPatterns = {"/LookupPojoFromOSGi"})
public class LookupPojoFromOSGi extends HttpServlet {
@Inject
@OSGiService(dynamic = true)
private Hello hello;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>@OSGi</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>lookup POJO by @Inject @OSGiService</h1>");
out.println(hello.sayHello("Sachi"));
out.println("<br/><br/><font color=\"gray\">Hello Class is ");
out.println(hello.getClass().getCanonicalName());
out.println("@");
out.println(hello.hashCode());
out.println("</font>");
} catch (Exception ex) {
out.println("<pre>");
ex.printStackTrace(out);
out.println("</pre>");
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
}
package com.mycompany.osgiexamweb;
import com.mycompany.osgiexamapi.GoodBye;
import java.io.IOException;
import java.io.PrintWriter;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.glassfish.osgicdi.OSGiService;
@WebServlet(name = "LookupEjbFromOSGi", urlPatterns = {"/LookupEjbFromOSGi"})
public class LookupEjbFromOSGi extends HttpServlet {
@Inject
@OSGiService(dynamic = true)
private GoodBye goodbye;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>@OSGi</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>lookup EJB Object by @Inject @OSGiService</h1>");
out.println(goodbye.sayGoodBye("Sachi"));
out.println("<br/><br/><font color=\"gray\">GoodBye Class is ");
out.println(goodbye.getClass().getCanonicalName());
out.println("@");
out.println(goodbye.hashCode());
out.println("</font>");
} catch (Exception ex) {
out.println("<pre>");
ex.printStackTrace(out);
out.println("</pre>");
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
}
[~]$ cp ~/NetBeansProjects/OSGiExam/OSGiExamWeb/target/OSGiExamWeb-1.0-SNAPSHOT.war \ /Applications/NetBeans/glassfish-3.1.2.2/glassfish/domains/domain1/autodeploy/bundles/
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>PrintBundle</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>PrintBundle OSGi Bundle</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>5.4.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bctsp-jdk16</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.49</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Activator>com.mycompany.printbundle.Activator</Bundle-Activator>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>
!javax.*,
!org.w3c.dom,
!org.xml.sax,
!org.apache.jcp.xml.dsig.internal.dom,
!org.apache.xml.security.utils,
!junit.*,
*
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
Manifest-Version: 1.0
Bnd-LastModified: 1384872395474
Build-Jdk: 1.7.0_21
Built-By: atsushi
Bundle-Activator: com.mycompany.printbundle.Activator
Bundle-ClassPath: .,itextpdf-5.4.4.jar,itext-xtra-5.4.4.jar,itext-asian-
5.2.0.jar,bcmail-jdk16-1.46.jar,bcpg-jdk16-1.46.jar,bcprov-jdk16-1.46.j
ar,bctsp-jdk16-1.46.jar,bcpkix-jdk15on-1.49.jar,bcprov-jdk15on-1.49.jar
Bundle-ManifestVersion: 2
Bundle-Name: PrintBundle OSGi Bundle
Bundle-SymbolicName: com.mycompany.PrintBundle
Bundle-Version: 1.0.0.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Embed-Dependency: *;scope=compile|runtime
Embed-Transitive: true
Embedded-Artifacts: itextpdf-5.4.4.jar;g="com.itextpdf";a="itextpdf";v="
5.4.4",itext-xtra-5.4.4.jar;g="com.itextpdf";a="itext-xtra";v="5.4.4",i
text-asian-5.2.0.jar;g="com.itextpdf";a="itext-asian";v="5.2.0",bcmail-
jdk16-1.46.jar;g="org.bouncycastle";a="bcmail-jdk16";v="1.46",bcpg-jdk1
6-1.46.jar;g="org.bouncycastle";a="bcpg-jdk16";v="1.46",bcprov-jdk16-1.
46.jar;g="org.bouncycastle";a="bcprov-jdk16";v="1.46",bctsp-jdk16-1.46.
jar;g="org.bouncycastle";a="bctsp-jdk16";v="1.46",bcpkix-jdk15on-1.49.j
ar;g="org.bouncycastle";a="bcpkix-jdk15on";v="1.49",bcprov-jdk15on-1.49
.jar;g="org.bouncycastle";a="bcprov-jdk15on";v="1.49"
Export-Package: com.mycompany.printbundle;uses:="org.osgi.framework";ver
sion="1.0.0.SNAPSHOT"
Ignore-Package: javax.crypto.interfaces,javax.mail.internet,javax.imagei
o.stream,javax.xml.crypto.dsig.keyinfo,javax.xml.transform.dom,junit.te
xtui,org.apache.jcp.xml.dsig.internal.dom,org.xml.sax,javax.imageio,jav
ax.naming.directory,org.apache.xml.security.utils,junit.framework,javax
.security.auth.x500,javax.naming,javax.xml.crypto.dsig.dom,javax.xml.pa
rsers,javax.crypto,javax.imageio.plugins.jpeg,org.w3c.dom,javax.activat
ion,javax.imageio.metadata,javax.crypto.spec,javax.xml.transform,javax.
xml.transform.stream,javax.xml.crypto.dsig,javax.xml.crypto,javax.mail,
javax.xml.crypto.dsig.spec,javax.xml.crypto.dom
Import-Package: org.osgi.framework;version="[1.6,2)"
Tool: Bnd-1.50.0
-Dglassfish.osgi.start.level.final=3を追加して再起動