From 16db61cecb52c6bec4afdc12baec7cbef0bf16b6 Mon Sep 17 00:00:00 2001 From: Meudje Date: Wed, 10 Feb 2021 14:16:51 +0100 Subject: [PATCH 1/4] FEAT Update refactor code --- .../illucit/ejbremote/EjbRemoteClient.java | 68 ++----------------- .../illucit/ejbremote/utils/ContextUtils.java | 59 ++++++++++++++++ 2 files changed, 63 insertions(+), 64 deletions(-) create mode 100644 client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java diff --git a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java index 036d4d4..0118094 100644 --- a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java +++ b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java @@ -1,17 +1,13 @@ package com.illucit.ejbremote; -import static javax.naming.Context.INITIAL_CONTEXT_FACTORY; -import static javax.naming.Context.PROVIDER_URL; -import static javax.naming.Context.URL_PKG_PREFIXES; - -import java.util.Hashtable; -import java.util.Map; +import com.illucit.ejbremote.server.ExampleService; import javax.naming.Context; -import javax.naming.InitialContext; import javax.naming.NamingException; +import java.util.Map; -import com.illucit.ejbremote.server.ExampleService; +import static com.illucit.ejbremote.utils.ContextUtils.createEjbProxy; +import static com.illucit.ejbremote.utils.ContextUtils.createRemoteEjbContext; /** * Remote EJB Client. @@ -94,61 +90,5 @@ public static void main(String[] args) { } - /** - * Create Remote EJB Context. - * - * @param host - * host to connect to (e.g. "127.0.0.1") - * @param port - * port to connect to (wildfly HTTP port, e.g. 8080) - * @return remote EJB context - * @throws NamingException - * if creating the context fails - */ - private static Context createRemoteEjbContext(String host, String port) throws NamingException { - - Hashtable props = new Hashtable<>(); - - props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); - props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); - - props.put("jboss.naming.client.ejb.context", false); - props.put("org.jboss.ejb.client.scoped.context", true); - - props.put("endpoint.name", "client-endpoint"); - props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false); - props.put("remote.connections", "default"); - props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false); - - props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port); - props.put("remote.connection.default.host", host); - props.put("remote.connection.default.port", port); - - return new InitialContext(props); - } - - /** - * Get a proxy for a remote EJB. - * - * @param remotingContext - * remote EJB context - * @param ejbUrl - * URL of the EJB - * @param ejbInterfaceClass - * class of the remote interface of the EJB - * @param - * type of the EJB remote interface - * @return EJB proxy - * @throws NamingException - * if the name resolving fails - * @throws ClassCastException - * if the EJB proxy is not of the given type - */ - @SuppressWarnings("unchecked") - private static T createEjbProxy(Context remotingContext, String ejbUrl, Class ejbInterfaceClass) - throws NamingException, ClassCastException { - Object resolvedproxy = remotingContext.lookup(ejbUrl); - return (T) resolvedproxy; - } } diff --git a/client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java b/client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java new file mode 100644 index 0000000..29e0a8c --- /dev/null +++ b/client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java @@ -0,0 +1,59 @@ +package com.illucit.ejbremote.utils; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import java.util.Hashtable; + +import static javax.naming.Context.*; + +public class ContextUtils { + + /** + * Create Remote EJB Context. + * + * @param host host to connect to (e.g. "127.0.0.1") + * @param port port to connect to (wildfly HTTP port, e.g. 8080) + * @return remote EJB context + * @throws NamingException if creating the context fails + */ + public static Context createRemoteEjbContext(String host, String port) throws NamingException { + + Hashtable props = new Hashtable<>(); + + props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); + props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); + + props.put("jboss.naming.client.ejb.context", false); + props.put("org.jboss.ejb.client.scoped.context", true); + + props.put("endpoint.name", "client-endpoint"); + props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false); + props.put("remote.connections", "default"); + props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false); + + props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port); + props.put("remote.connection.default.host", host); + props.put("remote.connection.default.port", port); + + return new InitialContext(props); + } + + /** + * Get a proxy for a remote EJB. + * + * @param remotingContext remote EJB context + * @param ejbUrl URL of the EJB + * @param ejbInterfaceClass class of the remote interface of the EJB + * @param type of the EJB remote interface + * @return EJB proxy + * @throws NamingException if the name resolving fails + * @throws ClassCastException if the EJB proxy is not of the given type + */ + @SuppressWarnings("unchecked") + public static T createEjbProxy(Context remotingContext, String ejbUrl, Class ejbInterfaceClass) + throws NamingException, ClassCastException { + Object resolvedproxy = remotingContext.lookup(ejbUrl); + return (T) resolvedproxy; + } +} From a5f61033bfc4ed19771448abc538ad421fdaa718 Mon Sep 17 00:00:00 2001 From: Meudje Date: Wed, 10 Feb 2021 14:16:51 +0100 Subject: [PATCH 2/4] Refactor update code --- .../illucit/ejbremote/EjbRemoteClient.java | 68 ++----------------- .../illucit/ejbremote/utils/ContextUtils.java | 59 ++++++++++++++++ 2 files changed, 63 insertions(+), 64 deletions(-) create mode 100644 client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java diff --git a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java index 036d4d4..0118094 100644 --- a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java +++ b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java @@ -1,17 +1,13 @@ package com.illucit.ejbremote; -import static javax.naming.Context.INITIAL_CONTEXT_FACTORY; -import static javax.naming.Context.PROVIDER_URL; -import static javax.naming.Context.URL_PKG_PREFIXES; - -import java.util.Hashtable; -import java.util.Map; +import com.illucit.ejbremote.server.ExampleService; import javax.naming.Context; -import javax.naming.InitialContext; import javax.naming.NamingException; +import java.util.Map; -import com.illucit.ejbremote.server.ExampleService; +import static com.illucit.ejbremote.utils.ContextUtils.createEjbProxy; +import static com.illucit.ejbremote.utils.ContextUtils.createRemoteEjbContext; /** * Remote EJB Client. @@ -94,61 +90,5 @@ public static void main(String[] args) { } - /** - * Create Remote EJB Context. - * - * @param host - * host to connect to (e.g. "127.0.0.1") - * @param port - * port to connect to (wildfly HTTP port, e.g. 8080) - * @return remote EJB context - * @throws NamingException - * if creating the context fails - */ - private static Context createRemoteEjbContext(String host, String port) throws NamingException { - - Hashtable props = new Hashtable<>(); - - props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); - props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); - - props.put("jboss.naming.client.ejb.context", false); - props.put("org.jboss.ejb.client.scoped.context", true); - - props.put("endpoint.name", "client-endpoint"); - props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false); - props.put("remote.connections", "default"); - props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false); - - props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port); - props.put("remote.connection.default.host", host); - props.put("remote.connection.default.port", port); - - return new InitialContext(props); - } - - /** - * Get a proxy for a remote EJB. - * - * @param remotingContext - * remote EJB context - * @param ejbUrl - * URL of the EJB - * @param ejbInterfaceClass - * class of the remote interface of the EJB - * @param - * type of the EJB remote interface - * @return EJB proxy - * @throws NamingException - * if the name resolving fails - * @throws ClassCastException - * if the EJB proxy is not of the given type - */ - @SuppressWarnings("unchecked") - private static T createEjbProxy(Context remotingContext, String ejbUrl, Class ejbInterfaceClass) - throws NamingException, ClassCastException { - Object resolvedproxy = remotingContext.lookup(ejbUrl); - return (T) resolvedproxy; - } } diff --git a/client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java b/client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java new file mode 100644 index 0000000..29e0a8c --- /dev/null +++ b/client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java @@ -0,0 +1,59 @@ +package com.illucit.ejbremote.utils; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import java.util.Hashtable; + +import static javax.naming.Context.*; + +public class ContextUtils { + + /** + * Create Remote EJB Context. + * + * @param host host to connect to (e.g. "127.0.0.1") + * @param port port to connect to (wildfly HTTP port, e.g. 8080) + * @return remote EJB context + * @throws NamingException if creating the context fails + */ + public static Context createRemoteEjbContext(String host, String port) throws NamingException { + + Hashtable props = new Hashtable<>(); + + props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); + props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); + + props.put("jboss.naming.client.ejb.context", false); + props.put("org.jboss.ejb.client.scoped.context", true); + + props.put("endpoint.name", "client-endpoint"); + props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false); + props.put("remote.connections", "default"); + props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false); + + props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port); + props.put("remote.connection.default.host", host); + props.put("remote.connection.default.port", port); + + return new InitialContext(props); + } + + /** + * Get a proxy for a remote EJB. + * + * @param remotingContext remote EJB context + * @param ejbUrl URL of the EJB + * @param ejbInterfaceClass class of the remote interface of the EJB + * @param type of the EJB remote interface + * @return EJB proxy + * @throws NamingException if the name resolving fails + * @throws ClassCastException if the EJB proxy is not of the given type + */ + @SuppressWarnings("unchecked") + public static T createEjbProxy(Context remotingContext, String ejbUrl, Class ejbInterfaceClass) + throws NamingException, ClassCastException { + Object resolvedproxy = remotingContext.lookup(ejbUrl); + return (T) resolvedproxy; + } +} From acd372720001132b98222cd35776016af953044c Mon Sep 17 00:00:00 2001 From: Meudje Date: Wed, 10 Feb 2021 23:25:46 +0100 Subject: [PATCH 3/4] Refactor update code style --- README.md | 6 +- client/pom.xml | 14 +- .../illucit/ejbremote/EjbRemoteClient.java | 145 +++++++++--------- .../illucit/ejbremote/utils/EjbUrlUtils.java | 27 ++++ 4 files changed, 115 insertions(+), 77 deletions(-) create mode 100644 client/src/main/java/com/illucit/ejbremote/utils/EjbUrlUtils.java diff --git a/README.md b/README.md index 85039a6..d56012a 100644 --- a/README.md +++ b/README.md @@ -36,4 +36,8 @@ To compile and run the example you can simply use a maven call: cd client mvn clean compile package exec:java -You should see the example result "Hello World" and the the remotely queried Wildfly Home Dir on the output. \ No newline at end of file +You should see the example result "Hello World" and the the remotely queried Wildfly Home Dir on the output. + +#Contributor + +- @ryankarl65 \ No newline at end of file diff --git a/client/pom.xml b/client/pom.xml index ae1e950..c46469e 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -33,8 +33,8 @@ 1.2.1 - 1.7 - 1.7 + 8 + 8 @@ -127,8 +127,18 @@ jboss-marshalling-river runtime + + + + org.apache.logging.log4j + log4j-core + 2.12.1 + + + + ${project.artifactId} diff --git a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java index 0118094..c5c69d3 100644 --- a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java +++ b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java @@ -1,6 +1,9 @@ package com.illucit.ejbremote; import com.illucit.ejbremote.server.ExampleService; +import com.illucit.ejbremote.utils.EjbUrlUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import javax.naming.Context; import javax.naming.NamingException; @@ -11,84 +14,78 @@ /** * Remote EJB Client. - * - * @author Christian Simon * + * @author Christian Simon */ public class EjbRemoteClient { - /** - * Run example. - * - * @param args - * (not used) - */ - public static void main(String[] args) { - - // Connection to Wildfly Server instance - String host = "127.0.0.1"; - String port = "8080"; // Wildfly HTTP port - - Context remotingContext; - try { - remotingContext = createRemoteEjbContext(host, port); - } catch (NamingException e) { - System.err.println("Error setting up remoting context"); - e.printStackTrace(); - return; - } - - // Syntax: ejb:${appName}/${moduleName}/${beanName}!${remoteView} - // appName = name of EAR deployment (or empty for single EJB/WAR - // deployments) - // moduleName = name of EJB/WAR deployment - // beanName = name of the EJB (Simple name of EJB class) - // remoteView = fully qualified remote interface class - String ejbUrl = "ejb:/ejb-remote-server/ExampleServiceImpl!com.illucit.ejbremote.server.ExampleService"; - - ExampleService service; - try { - service = createEjbProxy(remotingContext, ejbUrl, ExampleService.class); - } catch (NamingException e) { - System.err.println("Error resolving bean"); - e.printStackTrace(); - return; - } catch (ClassCastException e) { - System.err.println("Resolved EJB is of wrong type"); - e.printStackTrace(); - return; - } - - // Call remote method with parameter - - String toGreet = "World"; - - String exampleResult; - try { - exampleResult = service.greet(toGreet); - } catch (Exception e) { - System.err.println("Error accessing remote bean"); - e.printStackTrace(); - return; - } - - // Hello World! - System.out.println("Example result: " + exampleResult); - - // Retrieve result from EJB call - - Map systemProperties; - try { - systemProperties = service.getSystemProperties(); - } catch (Exception e) { - System.err.println("Error accessing remote bean"); - e.printStackTrace(); - return; - } - - System.out.println("Wildfly Home Dir: " + systemProperties.get("jboss.home.dir")); - - } + /** + * Run example. + * + * @param args (not used) + */ + public static void main(String[] args) { + + Logger log = LogManager.getLogger(EjbRemoteClient.class); + + // Connection to Wildfly Server instance + String host = "127.0.0.1"; + String port = "8080"; // Wildfly HTTP port + + Context remotingContext; + try { + remotingContext = createRemoteEjbContext(host, port); + } catch (NamingException e) { + log.error("Error setting up remoting context"); + return; + } + + final String appName = "ejb-remote"; + final String moduleName = "PersonServiceImpl"; + final String beanName = "ejb-remote"; + final String remoteInterface = "PersonInterface"; + + String ejbUrl = EjbUrlUtils.getEjbUrl(appName, moduleName, beanName, remoteInterface); + + ExampleService service = null; + try { + service = createEjbProxy(remotingContext, ejbUrl, ExampleService.class); + log.info(" Connexion to Remote is Done !"); + } catch (NamingException e) { + log.error("Error resolving bean"); + } catch (ClassCastException e) { + log.error("Resolved EJB is of wrong type"); + } + + // Call remote method with parameter + + String toGreet = "World"; + + String exampleResult; + try { + assert service != null; + exampleResult = service.greet(toGreet); + } catch (Exception e) { + log.error("Error accessing remote bean"); + return; + } + + // Hello World! + System.out.println("Example result: " + exampleResult); + + // Retrieve result from EJB call + + Map systemProperties; + try { + systemProperties = service.getSystemProperties(); + } catch (Exception e) { + log.error("Error accessing remote bean"); + return; + } + + System.out.println("Wildfly Home Dir: " + systemProperties.get("jboss.home.dir")); + + } } diff --git a/client/src/main/java/com/illucit/ejbremote/utils/EjbUrlUtils.java b/client/src/main/java/com/illucit/ejbremote/utils/EjbUrlUtils.java new file mode 100644 index 0000000..dd25d1e --- /dev/null +++ b/client/src/main/java/com/illucit/ejbremote/utils/EjbUrlUtils.java @@ -0,0 +1,27 @@ +package com.illucit.ejbremote.utils; + +public class EjbUrlUtils { + + /** + * Configure EJB Url + * Syntax: ejb:${appName}/${moduleName}/${beanName}!${remoteView} + * + * @param appName name of EAR deployment (or empty for single EJB/WAR deployments) + * @param moduleName name of EJB/WAR deployment + * @param beanName name of the EJB (Simple name of EJB class) + * @param remoteInterface fully qualified remote interface class + * @return String + * @author Meudje Karl + */ + public static String getEjbUrl(String appName, String moduleName, String beanName, String remoteInterface) { + + return "ejb:/" + + appName + + "/" + + moduleName + + "/" + + beanName + + "!" + + remoteInterface; + } +} From 543b3862c835a23569c41f3d206eb00f7d8b9a79 Mon Sep 17 00:00:00 2001 From: Meudje Date: Thu, 11 Feb 2021 09:44:14 +0100 Subject: [PATCH 4/4] Refactor update error on refact ejbUrl --- .../main/java/com/illucit/ejbremote/EjbRemoteClient.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java index c5c69d3..bc1db3c 100644 --- a/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java +++ b/client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java @@ -40,10 +40,11 @@ public static void main(String[] args) { return; } - final String appName = "ejb-remote"; - final String moduleName = "PersonServiceImpl"; - final String beanName = "ejb-remote"; - final String remoteInterface = "PersonInterface"; + + final String appName = "ejb-remote-server"; + final String moduleName = ""; + final String beanName = "ExampleServiceImpl"; + final String remoteInterface = "com.illucit.ejbremote.server.ExampleService"; String ejbUrl = EjbUrlUtils.getEjbUrl(appName, moduleName, beanName, remoteInterface);