Skip to content

Commit 383a070

Browse files
committed
Added remote EJB sample with Payara/GlassFish concrete impl
1 parent 97e615e commit 383a070

File tree

17 files changed

+358
-3
lines changed

17 files changed

+358
-3
lines changed

ejb/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The [JSR 345](https://jcp.org/en/jsr/detail?id=345) is an architecture for the d
66

77
- embeddable
88
- lifecycle
9+
- remote
910
- singleton
1011
- stateful
1112
- stateless

ejb/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
<groupId>org.javaee7</groupId>
77
<artifactId>samples-parent</artifactId>
88
<version>1.0-SNAPSHOT</version>
9-
<relativePath>../pom.xml</relativePath>
109
</parent>
11-
<groupId>org.javaee7</groupId>
10+
1211
<artifactId>ejb</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1412
<packaging>pom</packaging>
13+
1514
<name>Java EE 7 Sample: ejb</name>
1615

1716
<modules>
1817
<module>embeddable</module>
1918
<module>lifecycle</module>
19+
<module>remote</module>
2020
<module>singleton</module>
2121
<module>stateful</module>
2222
<module>stateless</module>

ejb/remote/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.javaee7</groupId>
6+
<artifactId>ejb</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>ejb-remote</artifactId>
11+
<packaging>pom</packaging>
12+
13+
<name>Java EE 7 Sample: ejb - remote</name>
14+
15+
<modules>
16+
<module>vendor</module>
17+
<module>roles-allowed</module>
18+
</modules>
19+
20+
</project>

ejb/remote/roles-allowed/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- Copyright Payara Services Limited -->
4+
5+
<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>
6+
7+
<parent>
8+
<groupId>org.javaee7</groupId>
9+
<artifactId>ejb-remote</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>ejb-remote-roles-allowed</artifactId>
14+
<packaging>war</packaging>
15+
16+
<name>Java EE 7 Sample: ejb - remote - Roles Allowed</name>
17+
18+
<profiles>
19+
<profile>
20+
<id>payara-ci-managed</id>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.javaee7.ejb.remote</groupId>
24+
<artifactId>ejb.remote.payara-glassfish</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
</dependency>
27+
</dependencies>
28+
</profile>
29+
30+
<profile>
31+
<id>payara-remote</id>
32+
<dependencies>
33+
<dependency>
34+
<groupId>org.javaee7.ejb.remote</groupId>
35+
<artifactId>ejb.remote.payara-glassfish</artifactId>
36+
<version>1.0-SNAPSHOT</version>
37+
</dependency>
38+
</dependencies>
39+
</profile>
40+
41+
<profile>
42+
<dependencies>
43+
<dependency>
44+
<groupId>org.javaee7.ejb.remote</groupId>
45+
<artifactId>ejb.remote.payara-glassfish</artifactId>
46+
<version>1.0-SNAPSHOT</version>
47+
</dependency>
48+
</dependencies>
49+
</profile>
50+
</profiles>
51+
52+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7.ejb.remote.remote;
3+
4+
import java.io.Serializable;
5+
6+
import javax.annotation.security.RolesAllowed;
7+
import javax.ejb.Stateless;
8+
9+
@Stateless
10+
public class Bean implements BeanRemote, Serializable {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@Override
15+
@RolesAllowed("g1")
16+
public String method() {
17+
return "method";
18+
}
19+
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7.ejb.remote.remote;
3+
4+
import javax.ejb.Remote;
5+
6+
@Remote
7+
public interface BeanRemote {
8+
String method();
9+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7.ejb.remote;
3+
4+
import static org.javaee7.ServerOperations.addUsersToContainerIdentityStore;
5+
import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE;
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assume.assumeTrue;
8+
9+
import javax.naming.Context;
10+
import javax.naming.NamingException;
11+
12+
import org.javaee7.RemoteEJBContextFactory;
13+
import org.javaee7.RemoteEJBContextProvider;
14+
import org.javaee7.ejb.remote.remote.Bean;
15+
import org.javaee7.ejb.remote.remote.BeanRemote;
16+
import org.jboss.arquillian.container.test.api.Deployment;
17+
import org.jboss.arquillian.container.test.api.RunAsClient;
18+
import org.jboss.arquillian.junit.Arquillian;
19+
import org.jboss.shrinkwrap.api.Archive;
20+
import org.jboss.shrinkwrap.api.ShrinkWrap;
21+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
22+
import org.junit.After;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
/**
28+
* This class demonstrates and tests how to request an EJB bean from a remote server.
29+
*
30+
* <p>
31+
* {@link RemoteEJBContextProvider} is used, which is a test artifact abstracting the different
32+
* ways this is done for different servers.
33+
*
34+
* @author Arjan Tijms
35+
*
36+
*/
37+
@RunWith(Arquillian.class)
38+
public class RemoteBeanTest {
39+
40+
private RemoteEJBContextProvider remoteEJBContextProvider;
41+
42+
@Deployment
43+
public static Archive<?> deployment() {
44+
45+
// Add user u1 with password p1 and group g1 to the container's native identity store
46+
addUsersToContainerIdentityStore();
47+
48+
return ShrinkWrap.create(JavaArchive.class)
49+
.addClasses(Bean.class, BeanRemote.class)
50+
.addAsManifestResource(INSTANCE, "beans.xml");
51+
}
52+
53+
@Before
54+
public void before() {
55+
remoteEJBContextProvider = RemoteEJBContextFactory.getProvider();
56+
assumeTrue(
57+
"No RemoteEJBContextProvider available in current profile",
58+
remoteEJBContextProvider != null);
59+
}
60+
61+
@After
62+
public void after() {
63+
remoteEJBContextProvider.releaseContext();
64+
}
65+
66+
@Test
67+
@RunAsClient
68+
public void callProtectedRemoteBean() throws NamingException {
69+
70+
// Obtain the JNDI naming context in a vendor specific way.
71+
Context ejbRemoteContext = remoteEJBContextProvider.getContextWithCredentialsSet("u1", "p1");
72+
73+
BeanRemote beanRemote = (BeanRemote) ejbRemoteContext.lookup("java:global/test/Bean");
74+
75+
assertEquals("method", beanRemote.method());
76+
}
77+
78+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
create-file-user --groups g1 --passwordfile ${project.build.directory}/test-classes/password.txt u1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AS_ADMIN_USERPASSWORD=p1

ejb/remote/vendor/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Java EE 7 Samples: EJB - Remote - Vendor #
2+
3+
This module contains vendor specific implementations to obtain the JNDI context from where remote EJB beans can be requested
4+
from with a username/password credential.
5+
6+
## Implementations ##
7+
8+
- payara-glassfish - An implementation that works for both Payara and GlassFish
9+
10+
11+
12+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Java EE 7 Samples: EJB - Remote - Vendor - Payara and GlassFish #
2+
3+
This modules contains a class that returns a JNDI context suitable for remote lookups against the default URL
4+
for a remote Payara or GlassFish server (localhost). It sets the provided credentials
5+
in a Payara/GlassFish specific way and puts the required client jar on the classpath.
6+
7+
8+
9+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- Copyright Payara Services Limited -->
4+
5+
<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>
6+
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
10+
<maven.compiler.source>1.7</maven.compiler.source>
11+
<maven.compiler.target>1.7</maven.compiler.target>
12+
</properties>
13+
14+
<groupId>org.javaee7.ejb.remote</groupId>
15+
<artifactId>ejb.remote.payara-glassfish</artifactId>
16+
<version>1.0-SNAPSHOT</version>
17+
18+
<name>Java EE 7 Sample: EJB - remote - vendor - Payara and GlassFish Remote EJB Provider</name>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.javaee7</groupId>
23+
<artifactId>test-utils</artifactId>
24+
<version>1.0-SNAPSHOT</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.glassfish.main.appclient</groupId>
28+
<artifactId>gf-client</artifactId>
29+
<version>5.0</version>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7;
3+
4+
import javax.naming.Context;
5+
import javax.naming.InitialContext;
6+
import javax.naming.NamingException;
7+
import javax.security.auth.Subject;
8+
9+
import com.sun.enterprise.security.auth.login.common.PasswordCredential;
10+
import com.sun.enterprise.security.common.ClientSecurityContext;
11+
12+
/**
13+
* This class returns a JNDI context suitable for remote lookups against the default URL
14+
* for a remote Payara or GlassFish server (localhost). It sets the provided credentials
15+
* in a Payara/GlassFish specific way.
16+
*
17+
* @author Arjan Tijms
18+
*
19+
*/
20+
public class PayaraEJBContextProvider implements RemoteEJBContextProvider {
21+
22+
@Override
23+
public Context getContextWithCredentialsSet(String username, String password) {
24+
25+
// Create a new subject with a password credential
26+
Subject subject = new Subject();
27+
subject.getPrivateCredentials().add(new PasswordCredential(username, password.toCharArray(), "default"));
28+
29+
// Store this subject into a global variable where the CORBA/IIOP code will pick it up.
30+
ClientSecurityContext.setCurrent(new ClientSecurityContext(username, subject));
31+
32+
// Note: no need for setting "java.naming.factory.initial", since this is already defined
33+
// by jndi.properties in the glassfish-naming.jar on the classpath.
34+
try {
35+
return new InitialContext();
36+
} catch (NamingException e) {
37+
throw new IllegalStateException(e);
38+
}
39+
}
40+
41+
@Override
42+
public void releaseContext() {
43+
ClientSecurityContext.setCurrent(null);
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.javaee7.PayaraEJBContextProvider

ejb/remote/vendor/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- Copyright Payara Services Limited -->
4+
5+
<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>
6+
7+
<parent>
8+
<groupId>org.javaee7</groupId>
9+
<artifactId>ejb</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>remote</artifactId>
14+
<packaging>pom</packaging>
15+
16+
<name>Java EE 7 Sample: ejb - remote - vendor</name>
17+
18+
<profiles>
19+
<profile>
20+
<id>payara-ci-managed</id>
21+
<modules>
22+
<module>payara-glassfish</module>
23+
</modules>
24+
</profile>
25+
26+
<profile>
27+
<id>payara-remote</id>
28+
<modules>
29+
<module>payara-glassfish</module>
30+
</modules>
31+
</profile>
32+
33+
<profile>
34+
<id>glassfish-remote</id>
35+
<modules>
36+
<module>payara-glassfish</module>
37+
</modules>
38+
</profile>
39+
</profiles>
40+
41+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7;
3+
4+
import java.util.Iterator;
5+
import java.util.ServiceLoader;
6+
7+
public class RemoteEJBContextFactory {
8+
9+
public static RemoteEJBContextProvider getProvider() {
10+
11+
ServiceLoader<RemoteEJBContextProvider> loader = ServiceLoader.load(RemoteEJBContextProvider.class);
12+
13+
Iterator<RemoteEJBContextProvider> providers = loader.iterator();
14+
15+
if (!providers.hasNext()) {
16+
return null;
17+
}
18+
19+
return providers.next();
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)