Skip to content

Commit 4e1084f

Browse files
author
Gregor Pfeifer
committed
Add an example of a Jersey OSGi bundle for Jetty 12 (ee8)
1 parent 604cf2e commit 4e1084f

File tree

9 files changed

+350
-0
lines changed

9 files changed

+350
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2010, 2023 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+
<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">
15+
16+
<modelVersion>4.0.0</modelVersion>
17+
18+
<parent>
19+
<groupId>org.glassfish.jersey.examples</groupId>
20+
<artifactId>project</artifactId>
21+
<version>2.42-SNAPSHOT</version>
22+
</parent>
23+
24+
<artifactId>osgi-helloworld-webapp-jetty-12</artifactId>
25+
<name>jersey-examples-osgi-helloworld-webapp-jetty-12</name>
26+
<packaging>pom</packaging>
27+
28+
<modules>
29+
<module>war-bundle</module>
30+
<!--
31+
TBD: Write functional tests.
32+
<module>functional-test</module>
33+
-->
34+
</modules>
35+
36+
<profiles>
37+
<profile>
38+
<id>pre-release</id>
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-assembly-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</profile>
48+
</profiles>
49+
50+
</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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (c) 2010, 2023 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+
<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">
15+
16+
<modelVersion>4.0.0</modelVersion>
17+
18+
<parent>
19+
<groupId>org.glassfish.jersey.examples</groupId>
20+
<artifactId>osgi-helloworld-webapp-jetty-12</artifactId>
21+
<version>2.42-SNAPSHOT</version>
22+
</parent>
23+
24+
<groupId>org.glassfish.jersey.examples.osgi-helloworld-jetty-12-webapp</groupId>
25+
<artifactId>war-bundle</artifactId>
26+
<name>jersey-examples-osgi-helloworld-jetty-12-webapp-wab</name>
27+
<packaging>war</packaging>
28+
29+
<description>OSGi Helloworld Webapp WAR bundle for Jetty 12</description>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.glassfish.jersey.containers</groupId>
34+
<artifactId>jersey-container-servlet-core</artifactId>
35+
<scope>provided</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>jakarta.ws.rs</groupId>
39+
<artifactId>jakarta.ws.rs-api</artifactId>
40+
<scope>provided</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>servlet-api</artifactId>
45+
<version>${servlet2.version}</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.apache.felix</groupId>
51+
<artifactId>org.apache.felix.eventadmin</artifactId>
52+
<scope>provided</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.apache.felix</groupId>
56+
<artifactId>org.apache.felix.framework</artifactId>
57+
<scope>provided</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<!-- to generate the MANIFEST-FILE required by the bundle -->
64+
<plugin>
65+
<groupId>org.apache.felix</groupId>
66+
<artifactId>maven-bundle-plugin</artifactId>
67+
<extensions>true</extensions>
68+
<executions>
69+
<execution>
70+
<id>bundle-manifest</id>
71+
<phase>process-classes</phase>
72+
<goals>
73+
<goal>manifest</goal>
74+
</goals>
75+
</execution>
76+
</executions>
77+
<configuration>
78+
<manifestLocation>${project.build.directory}/META-INF</manifestLocation>
79+
<supportedProjectTypes>
80+
<supportedProjectType>bundle</supportedProjectType>
81+
<supportedProjectType>war</supportedProjectType>
82+
</supportedProjectTypes>
83+
<instructions>
84+
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
85+
<Import-Package>
86+
org.glassfish.jersey.servlet,
87+
*
88+
</Import-Package>
89+
<Export-Package>
90+
org.glassfish.jersey.examples.osgi.helloworld
91+
</Export-Package>
92+
<Webapp-Context>helloworld</Webapp-Context>
93+
<Web-ContextPath>helloworld</Web-ContextPath>
94+
<Jetty-Environment>ee8</Jetty-Environment>
95+
<_wab>src/main/webapp</_wab>
96+
</instructions>
97+
</configuration>
98+
</plugin>
99+
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-war-plugin</artifactId>
103+
<configuration>
104+
<attachClasses>true</attachClasses>
105+
<archive>
106+
<manifestFile>${project.build.directory}/META-INF/MANIFEST.MF</manifestFile>
107+
</archive>
108+
</configuration>
109+
</plugin>
110+
</plugins>
111+
</build>
112+
113+
<profiles>
114+
<profile>
115+
<id>release</id>
116+
<!-- do not create source zip bundles -->
117+
<build>
118+
<plugins>
119+
<plugin>
120+
<groupId>org.apache.maven.plugins</groupId>
121+
<artifactId>maven-assembly-plugin</artifactId>
122+
<configuration>
123+
<skipAssembly>true</skipAssembly>
124+
</configuration>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</profile>
129+
<profile>
130+
<id>moxy</id>
131+
<activation>
132+
<property>
133+
<name>moxy</name>
134+
</property>
135+
</activation>
136+
<dependencies>
137+
<dependency>
138+
<groupId>org.eclipse.persistence</groupId>
139+
<artifactId>org.eclipse.persistence.moxy</artifactId>
140+
<version>${moxy.version}</version>
141+
<scope>provided</scope>
142+
</dependency>
143+
</dependencies>
144+
</profile>
145+
</profiles>
146+
147+
</project>
Lines changed: 29 additions & 0 deletions
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+
}
Lines changed: 31 additions & 0 deletions
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)