Skip to content

Commit de406c4

Browse files
committed
Remote EJB sample of EE 8.
1 parent 89f0149 commit de406c4

File tree

16 files changed

+375
-0
lines changed

16 files changed

+375
-0
lines changed

ejb/pom.xml

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+
<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.javaee8</groupId>
6+
<artifactId>samples-parent</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>ejb</artifactId>
11+
<packaging>pom</packaging>
12+
13+
<name>Java EE 8 Sample: ejb</name>
14+
15+
<modules>
16+
<module>remote</module>
17+
</modules>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.javaee8</groupId>
22+
<artifactId>test-utils</artifactId>
23+
<version>${project.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>fish.payara.ejb.rest</groupId>
28+
<artifactId>ejb-rest-client</artifactId>
29+
<version>5.191-SNAPSHOT</version>
30+
</dependency>
31+
</dependencies>
32+
</project>

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.javaee8</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 8 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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.javaee8</groupId>
6+
<artifactId>ejb-remote</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>ejb-remote-roles-allowed</artifactId>
11+
<packaging>war</packaging>
12+
13+
<name>Java EE 8 Sample: ejb - remote - Roles Allowed</name>
14+
15+
<profiles>
16+
<profile>
17+
<id>payara-ci-managed</id>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.javaee8.ejb.remote.vendor</groupId>
21+
<artifactId>ejb.remote.vendor.payara-rest</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
</dependencies>
25+
</profile>
26+
27+
<profile>
28+
<id>payara-remote</id>
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.javaee8.ejb.remote.vendor</groupId>
32+
<artifactId>ejb.remote.vendor.payara-rest</artifactId>
33+
<version>1.0-SNAPSHOT</version>
34+
</dependency>
35+
</dependencies>
36+
</profile>
37+
38+
<profile>
39+
<id>glassfish-remote</id>
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.javaee8.ejb.remote.vendor</groupId>
43+
<artifactId>ejb.remote.vendor.payara-rest</artifactId>
44+
<version>1.0-SNAPSHOT</version>
45+
</dependency>
46+
</dependencies>
47+
</profile>
48+
</profiles>
49+
50+
51+
</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.javaee8.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.javaee8.ejb.remote.remote;
3+
4+
import javax.ejb.Remote;
5+
6+
@Remote
7+
public interface BeanRemote {
8+
String method();
9+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee8.ejb.remote;
3+
4+
import static org.javaee8.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.javaee8.RemoteEJBContextFactory;
13+
import org.javaee8.RemoteEJBContextProvider;
14+
import org.javaee8.ejb.remote.remote.Bean;
15+
import org.javaee8.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+
try {
71+
72+
// Obtain the JNDI naming context in a vendor specific way.
73+
Context ejbRemoteContext = remoteEJBContextProvider.getContextWithCredentialsSet("u1", "p1");
74+
75+
BeanRemote beanRemote = (BeanRemote) ejbRemoteContext.lookup("java:global/test/Bean");
76+
77+
assertEquals("method", beanRemote.method());
78+
} catch (Exception e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
83+
}
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 8 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+

ejb/remote/vendor/payara-rest/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.javaee8</groupId>
9+
<artifactId>ejb-remote-vendor</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<groupId>org.javaee8.ejb.remote.vendor</groupId>
14+
<artifactId>ejb.remote.vendor.payara-rest</artifactId>
15+
16+
<name>Java EE 8 Sample: ejb - remote - vendor - Payara REST Remote EJB Provider</name>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.javaee8</groupId>
21+
<artifactId>test-utils</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee8;
3+
4+
import static javax.naming.Context.INITIAL_CONTEXT_FACTORY;
5+
import static javax.naming.Context.PROVIDER_URL;
6+
import static javax.naming.Context.SECURITY_CREDENTIALS;
7+
import static javax.naming.Context.SECURITY_PRINCIPAL;
8+
9+
import java.util.Hashtable;
10+
11+
import javax.naming.Context;
12+
import javax.naming.InitialContext;
13+
import javax.naming.NamingException;
14+
15+
/**
16+
* This class returns a JNDI context suitable for remote lookups against the default URL
17+
* for a remote Payara or GlassFish server (localhost). It sets the provided credentials
18+
* in a Payara/GlassFish specific way.
19+
*
20+
* @author Arjan Tijms
21+
*
22+
*/
23+
public class PayaraEJBContextProvider implements RemoteEJBContextProvider {
24+
public Context getContextWithCredentialsSet(String username, String password) {
25+
Hashtable<String, String> environment = new Hashtable<String, String>();
26+
environment.put(INITIAL_CONTEXT_FACTORY, "fish.payara.ejb.rest.client.RemoteEJBContextFactory");
27+
environment.put(PROVIDER_URL, "http://localhost:8080/ejb-invoker");
28+
environment.put(SECURITY_PRINCIPAL, "u1");
29+
environment.put(SECURITY_CREDENTIALS, "p1");
30+
31+
try {
32+
return new InitialContext(environment);
33+
} catch (NamingException e) {
34+
throw new IllegalStateException(e);
35+
}
36+
37+
}
38+
39+
public void releaseContext() {
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.javaee8.PayaraEJBContextProvider

ejb/remote/vendor/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
<groupId>org.javaee8</groupId>
8+
<artifactId>ejb-remote-vendor</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>pom</packaging>
11+
12+
<name>Java EE 8 Sample: ejb - remote - vendor</name>
13+
14+
<profiles>
15+
<profile>
16+
<id>payara-ci-managed</id>
17+
<modules>
18+
<module>payara-rest</module>
19+
</modules>
20+
</profile>
21+
22+
<profile>
23+
<id>payara-remote</id>
24+
<modules>
25+
<module>payara-rest</module>
26+
</modules>
27+
</profile>
28+
29+
<profile>
30+
<id>glassfish-remote</id>
31+
<modules>
32+
<module>payara-rest</module>
33+
</modules>
34+
</profile>
35+
</profiles>
36+
37+
</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.javaee8;
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+
}
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.javaee8;
3+
4+
import javax.naming.Context;
5+
6+
public interface RemoteEJBContextProvider {
7+
Context getContextWithCredentialsSet(String username, String password);
8+
void releaseContext();
9+
}

0 commit comments

Comments
 (0)