Skip to content

Commit 28bd017

Browse files
Gregor Pfeifergpfeifer
authored andcommitted
Add an example of a Jersey OSGi bundle for Jetty 12 (ee8)
Signed-off-by: Gregor Pfeifer <[email protected]>
1 parent 604cf2e commit 28bd017

File tree

9 files changed

+345
-0
lines changed

9 files changed

+345
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[//]: # " Copyright (c) 2015, 2018 Oracle and/or its affiliates. All rights reserved. "
2+
[//]: # " "
3+
[//]: # " This program and the accompanying materials are made available under the "
4+
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
5+
[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. "
6+
[//]: # " "
7+
[//]: # " SPDX-License-Identifier: BSD-3-Clause "
8+
9+
HelloWorld OSGi Example
10+
=======================
11+
12+
This example demonstrates how to develop a simple OSGi WAR bundle
13+
containing a RESTful hello world web service
14+
15+
Contents
16+
--------
17+
18+
The example WAR (see the `war-bundle` module) consists of two Jersey
19+
resources:
20+
21+
`org.glassfish.jersey.examples.osgi.helloworld.resource.HelloWorldResource`
22+
23+
that produces a textual response to an HTTP GET
24+
25+
`org.glassfish.jersey.examples.osgi.helloworld.resource.AnotherResource`
26+
27+
that produces a different textual response to an HTTP GET. The
28+
purpose of this resource is to show how to define multiple web
29+
resources within a web application.
30+
31+
The mapping of the URI path space is presented in the following table:
32+
33+
URI path | Resource class | HTTP method
34+
------------------ | -------------------- | -------------
35+
**_/helloworld_** | HelloWorldResource | GET
36+
**_/another_** | AnotherResource | GET
37+
38+
Running the Example
39+
-------------------
40+
41+
To run the example, you would need to build the WAR file and install it
42+
to an OSGi runtime (e.g. Apache Felix) together with other OSGi modules.
43+
Look at the attached `functional-test` module for details on the
44+
programatical runtime configuration. To build the war archive and run
45+
the tests, you can just launch
46+
47+
> mvn clean install
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
This program and the accompanying materials are made available under the
4+
terms of the Eclipse Distribution License v. 1.0, which is available at
5+
http://www.eclipse.org/org/documents/edl-v10.php.
6+
7+
SPDX-License-Identifier: BSD-3-Clause
8+
9+
-->
10+
11+
<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">
12+
13+
<modelVersion>4.0.0</modelVersion>
14+
15+
<parent>
16+
<groupId>org.glassfish.jersey.examples</groupId>
17+
<artifactId>project</artifactId>
18+
<version>2.42-SNAPSHOT</version>
19+
</parent>
20+
21+
<artifactId>osgi-helloworld-webapp-jetty-12</artifactId>
22+
<name>jersey-examples-osgi-helloworld-webapp-jetty-12</name>
23+
<packaging>pom</packaging>
24+
25+
<modules>
26+
<module>war-bundle</module>
27+
<!--
28+
TBD: Write functional tests.
29+
<module>functional-test</module>
30+
-->
31+
</modules>
32+
33+
<profiles>
34+
<profile>
35+
<id>pre-release</id>
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-assembly-plugin</artifactId>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
</profile>
45+
</profiles>
46+
47+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
.vscode/
3+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# A minimalistic Hello World OSGi Bundle for Jetty 12 e88
2+
3+
The Resource will be available under:
4+
5+
{{base_url}}/helloworld/webresources/hello
6+
7+
**helloworld** is configured in the MANIFEST.MF
8+
9+
**webresources** is configured in web.xml
10+
11+
**hello** is path to the resource
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This program and the accompanying materials are made available under the
5+
terms of the Eclipse Distribution License v. 1.0, which is available at
6+
http://www.eclipse.org/org/documents/edl-v10.php.
7+
8+
SPDX-License-Identifier: BSD-3-Clause
9+
10+
-->
11+
12+
<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">
13+
14+
<modelVersion>4.0.0</modelVersion>
15+
16+
<parent>
17+
<groupId>org.glassfish.jersey.examples</groupId>
18+
<artifactId>osgi-helloworld-webapp-jetty-12</artifactId>
19+
<version>2.42-SNAPSHOT</version>
20+
</parent>
21+
22+
<groupId>org.glassfish.jersey.examples.osgi-helloworld-jetty-12-webapp</groupId>
23+
<artifactId>war-bundle</artifactId>
24+
<name>jersey-examples-osgi-helloworld-jetty-12-webapp-wab</name>
25+
<packaging>war</packaging>
26+
27+
<description>OSGi Helloworld Webapp WAR bundle for Jetty 12</description>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.glassfish.jersey.containers</groupId>
32+
<artifactId>jersey-container-servlet-core</artifactId>
33+
<scope>provided</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>jakarta.ws.rs</groupId>
37+
<artifactId>jakarta.ws.rs-api</artifactId>
38+
<scope>provided</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>javax.servlet</groupId>
42+
<artifactId>servlet-api</artifactId>
43+
<version>${servlet2.version}</version>
44+
<scope>provided</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.apache.felix</groupId>
49+
<artifactId>org.apache.felix.eventadmin</artifactId>
50+
<scope>provided</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.apache.felix</groupId>
54+
<artifactId>org.apache.felix.framework</artifactId>
55+
<scope>provided</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<!-- to generate the MANIFEST-FILE required by the bundle -->
62+
<plugin>
63+
<groupId>org.apache.felix</groupId>
64+
<artifactId>maven-bundle-plugin</artifactId>
65+
<extensions>true</extensions>
66+
<executions>
67+
<execution>
68+
<id>bundle-manifest</id>
69+
<phase>process-classes</phase>
70+
<goals>
71+
<goal>manifest</goal>
72+
</goals>
73+
</execution>
74+
</executions>
75+
<configuration>
76+
<manifestLocation>${project.build.directory}/META-INF</manifestLocation>
77+
<supportedProjectTypes>
78+
<supportedProjectType>bundle</supportedProjectType>
79+
<supportedProjectType>war</supportedProjectType>
80+
</supportedProjectTypes>
81+
<instructions>
82+
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
83+
<Import-Package>
84+
org.glassfish.jersey.servlet,
85+
*
86+
</Import-Package>
87+
<Export-Package>
88+
org.glassfish.jersey.examples.osgi.helloworld
89+
</Export-Package>
90+
<Webapp-Context>helloworld</Webapp-Context>
91+
<Web-ContextPath>helloworld</Web-ContextPath>
92+
<Jetty-Environment>ee8</Jetty-Environment>
93+
<_wab>src/main/webapp</_wab>
94+
</instructions>
95+
</configuration>
96+
</plugin>
97+
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-war-plugin</artifactId>
101+
<configuration>
102+
<attachClasses>true</attachClasses>
103+
<archive>
104+
<manifestFile>${project.build.directory}/META-INF/MANIFEST.MF</manifestFile>
105+
</archive>
106+
</configuration>
107+
</plugin>
108+
</plugins>
109+
</build>
110+
111+
<profiles>
112+
<profile>
113+
<id>release</id>
114+
<!-- do not create source zip bundles -->
115+
<build>
116+
<plugins>
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-assembly-plugin</artifactId>
120+
<configuration>
121+
<skipAssembly>true</skipAssembly>
122+
</configuration>
123+
</plugin>
124+
</plugins>
125+
</build>
126+
</profile>
127+
<profile>
128+
<id>moxy</id>
129+
<activation>
130+
<property>
131+
<name>moxy</name>
132+
</property>
133+
</activation>
134+
<dependencies>
135+
<dependency>
136+
<groupId>org.eclipse.persistence</groupId>
137+
<artifactId>org.eclipse.persistence.moxy</artifactId>
138+
<version>${moxy.version}</version>
139+
<scope>provided</scope>
140+
</dependency>
141+
</dependencies>
142+
</profile>
143+
</profiles>
144+
145+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2024. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.osgi.helloworld;
12+
13+
import javax.ws.rs.GET;
14+
import javax.ws.rs.Path;
15+
16+
/**
17+
* The Hello resource
18+
*
19+
* @author Gregor Pfeifer
20+
*/
21+
@Path("/hello")
22+
public class HelloResource {
23+
24+
@GET
25+
public String sayHello() {
26+
return "Hello world :-)";
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2024. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
package org.glassfish.jersey.examples.osgi.helloworld;
12+
13+
import org.glassfish.jersey.server.ResourceConfig;
14+
15+
/**
16+
* The Hello ResourceConfig
17+
*
18+
* @author Gregor Pfeifer
19+
*/
20+
public class HelloResourceConfig extends ResourceConfig {
21+
22+
public static Class<?>[] CLASSES = new Class[] {
23+
HelloResource.class
24+
};
25+
26+
public HelloResourceConfig() {
27+
super(CLASSES);
28+
}
29+
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
5+
6+
This program and the accompanying materials are made available under the
7+
terms of the Eclipse Distribution License v. 1.0, which is available at
8+
http://www.eclipse.org/org/documents/edl-v10.php.
9+
10+
SPDX-License-Identifier: BSD-3-Clause
11+
12+
-->
13+
14+
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
15+
16+
<!-- Jersey APP with a ResourceConfig-->
17+
<servlet>
18+
<servlet-name>Jersey Web Application</servlet-name>
19+
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
20+
<init-param>
21+
<param-name>javax.ws.rs.Application</param-name>
22+
<param-value>org.glassfish.jersey.examples.osgi.helloworld.HelloResourceConfig</param-value>
23+
</init-param>
24+
<load-on-startup>1</load-on-startup>
25+
</servlet>
26+
<servlet-mapping>
27+
<servlet-name>Jersey Web Application</servlet-name>
28+
<url-pattern>/webresources/*</url-pattern>
29+
</servlet-mapping>
30+
31+
</web-app>

0 commit comments

Comments
 (0)