JAVA EE
Keycloak – 1 – UserAuth
EJB Complete Sample 2 – Client
Client Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
package com.kutay.zorlu.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 javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.kutay.zorlu.ejbremote.server.ExampleService; /** * Remote EJB Client. * @author Kutay ZORLU * */ 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-kutay/ExampleServiceImpl!com.kutay.zorlu.ejbremote.server.ExampleService"; # Module NAME! 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 // java.lang.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<Object, Object> 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")); } /** * 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<Object, Object> 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 <T> * 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> T createEjbProxy(Context remotingContext, String ejbUrl, Class<T> ejbInterfaceClass) throws NamingException, ClassCastException { Object resolvedproxy = remotingContext.lookup(ejbUrl); return (T) resolvedproxy; } } |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<?xml version="1.0"?> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kutay.zorlu</groupId> <artifactId>ejb-remote-client-kutay</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>WildFly Example: EJB Remote Client</name> <description>Java client for remote EJB example on Wildfly</description> <url>http://www.kutayzorlu.com</url> <licenses> <license> <name>Microsoft License</name> <distribution>repo</distribution> <url>http://www.Microsoft.com/Licenses</url> </license> </licenses> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- JBoss dependency versions --> <version.wildfly>8.1.0.Final</version.wildfly> <version.jboss.spec.javaee.7.0>1.0.0.Final</version.jboss.spec.javaee.7.0> <!-- other plugin versions --> <version.compiler.plugin>3.1</version.compiler.plugin> <version.exec.plugin>1.2.1</version.exec.plugin> <!-- maven-compiler-plugin --> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.source>1.7</maven.compiler.source> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-7.0</artifactId> <version>${version.jboss.spec.javaee.7.0}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.wildfly</groupId> <artifactId>wildfly-ejb-client-bom</artifactId> <version>${version.wildfly}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Business Interfaces of the server EJB. --> <dependency> <groupId>com.kutay.zorlu</groupId> <artifactId>ejb-remote-server-kutay</artifactId> <type>ejb</type> <version>1.0.0</version> </dependency> <!-- Import the transaction spec API, we use runtime scope because we aren't using any direct reference to the spec API in our client code --> <dependency> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.2_spec</artifactId> <scope>runtime</scope> </dependency> <!-- Import the EJB 3.1 API, we use runtime scope because we aren't using any direct reference to EJB spec API in our client code --> <dependency> <groupId>org.jboss.spec.javax.ejb</groupId> <artifactId>jboss-ejb-api_3.2_spec</artifactId> <scope>runtime</scope> </dependency> <!-- JBoss EJB client API jar. We use runtime scope because the EJB client API isn't directly used in this example. We just need it in our runtime classpath --> <dependency> <groupId>org.jboss</groupId> <artifactId>jboss-ejb-client</artifactId> <scope>runtime</scope> </dependency> <!-- client communications with the server use XNIO --> <dependency> <groupId>org.jboss.xnio</groupId> <artifactId>xnio-api</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.jboss.xnio</groupId> <artifactId>xnio-nio</artifactId> <scope>runtime</scope> </dependency> <!-- The client needs JBoss remoting to access the server --> <dependency> <groupId>org.jboss.remoting</groupId> <artifactId>jboss-remoting</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.jboss</groupId> <artifactId>jboss-remote-naming</artifactId> <scope>runtime</scope> </dependency> <!-- Remote EJB accesses can be secured --> <dependency> <groupId>org.jboss.sasl</groupId> <artifactId>jboss-sasl</artifactId> <scope>runtime</scope> </dependency> <!-- data serialization for invoking remote EJBs --> <dependency> <groupId>org.jboss.marshalling</groupId> <artifactId>jboss-marshalling-river</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> </plugins> </build> </project> |
# Controlling the result.
If you choose RMI, you don’t need a Java EE EJB app server. You have to keep client and server JVMs in synch; you can’t upgrade the client without upgrading the server. You have to write all the services that the EJB app server provides for you (e.g., connection pooling, naming and directory services, pooling, request queuing, transactions, etc.).
Agains the errors : You need to use JDK 8 –
1 2 |
No standard field found for reverse order comparator! at org.jboss.marshalling.river.Protocol.<clinit>(Protocol.java:220) |
Wildfy
Creating user / add-user.bat
1 2 3 4 5 6 7 |
wildfly-25.0.0.Final\bin>add-user.bat JAVA_HOME is not set. Unexpected results may occur. Set JAVA_HOME to the directory of your local JDK to avoid this message. What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) |
JSF & JSF IMPL & javaserverfaces-spec – 1
JavaServer Faces technology simplifies building user interfaces for JavaServer applications. Developers can build web applications by assembling reuseable UI components in a page; connecting these components to an application data source; and wiring client-generated events to server-side event handlers. This project provides information on the continued development of the JavaServer Faces specification.
- Eclipse Mojarra
- Oracle Mojarra
- Apache MyFaces
Example Maven Dependency – 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <junit.version>5.7.1</junit.version> </properties> <dependencies> <dependency> <groupId>javax.faces</groupId> <artifactId>javax.faces-api</artifactId> <version>2.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.enterprise</groupId> <artifactId>cdi-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.29.Final</version> </dependency> </dependencies> |
Example Maven Dependency – 2
Exclude Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<dependency> <groupId>opt.generic.tools</groupId> <artifactId>genericTools</artifactId> <version>1.1</version> <exclusions> <exclusion> <groupId>org.apache.myfaces.core</groupId> <artifactId>myfaces-api</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </exclusion> <exclusion> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> </exclusion> <!-- <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> </exclusion> <exclusion> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </exclusion> <exclusion> <groupId>javax.faces</groupId> <artifactId>javax.faces-api</artifactId> </exclusion>^ --> </exclusions> </dependency> |
Example Maven Dependency – 3
Hibernate SQL Dialects (hibernate.dialect)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Hibernate SQL Dialects (hibernate.dialect) RDBMS Dialect DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS390 org.hibernate.dialect.DB2390Dialect PostgreSQL org.hibernate.dialect.PostgreSQLDialect MySQL5 org.hibernate.dialect.MySQL5Dialect MySQL5 with InnoDB org.hibernate.dialect.MySQL5InnoDBDialect MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect Oracle (any version) org.hibernate.dialect.OracleDialect Oracle 9i org.hibernate.dialect.Oracle9iDialect Oracle 10g org.hibernate.dialect.Oracle10gDialect Oracle 11g org.hibernate.dialect.Oracle10gDialect Sybase org.hibernate.dialect.SybaseASE15Dialect Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect Microsoft SQL Server 2000 org.hibernate.dialect.SQLServerDialect Microsoft SQL Server 2005 org.hibernate.dialect.SQLServer2005Dialect Microsoft SQL Server 2008 org.hibernate.dialect.SQLServer2008Dialect SAP DB org.hibernate.dialect.SAPDBDialect Informix org.hibernate.dialect.InformixDialect HypersonicSQL org.hibernate.dialect.HSQLDialect H2 Database org.hibernate.dialect.H2Dialect Ingres org.hibernate.dialect.IngresDialect Progress org.hibernate.dialect.ProgressDialect Mckoi SQL org.hibernate.dialect.MckoiDialect Interbase org.hibernate.dialect.InterbaseDialect Pointbase org.hibernate.dialect.PointbaseDialect FrontBase org.hibernate.dialect.FrontbaseDialect Firebird org.hibernate.dialect.FirebirdDialect |
1 2 3 4 5 |
<attribute name="JndiName">java:/hibernate/SessionFactory</attribute> <!-- Datasource settings --> <attribute name="Datasource">java:HsqlDS</attribute> <attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute> |
Reference : https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/configuration.html
1 2 3 4 5 6 7 8 9 10 11 |
Thread.currentThread().setContextClassLoader(new ClassLoader() { @Override public Enumeration<URL> getResources(String name) throws IOException { if (name.equals("META-INF/persistence.xml")) { return Collections.enumeration(Arrays.asList(new File("conf/persistence.xml") .toURI().toURL())); } return super.getResources(name); } }); Persistence.createEntityManagerFactory("test"); |
1 2 3 4 5 |
IDE PRoject Location of the XML file persistence.xml files within the META-INF directory of any CLASSPATH element. EntityManagerFactory definition in conf/META-INF/persistence.xml |
Example Hibernate Mapping files
Example 1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“> <hibernate-mapping> <class name=”com.kutayzorlu.entity.Adres” table=”adres”> <id name=”id” column=”id”> <generator class=”increment”/> </id> <property name=”adres1″ type=”string” length=”50″ column=”adres1″ /> <property name=”adres2″ type=”string” length=”50″ column=”adres2″ /> <property name=”zipCode” type=”string” length=”5″ column=”zip_code” /> <many-to-one name=”kisi” column=”kisi_id” not-null=”true” class=”com.kutayzorlu.entity.Kisi” /> </class> </hibernate-mapping> |
Example 2;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd“> <hibernate-mapping> <class name=”com.kutayzorlu.entity.Kisi” table=”kisi”> <id name=”id” column=”id”> <generator class=”increment”/> </id> <property name=”adi” type=”string” column=”adi” length=”25″ /> <property name=”soyadi” type=”string” column=”soyadi” length=”25″ /> <property name=”evTelefonu” type=”string” column=”ev_telefonu” length=”15″ /> <property name=”cepTelefonu” type=”string” column=”cep_telefonu” length=”15″ /> <property name=”mail” type=”string” column=”mail” length=”50″ /> <set name=”adresler” cascade=”all” lazy=”true”> <key column=”kisi_id”/> <one-to-many class=”com.kutayzorlu.entity.Adres” /> </set> </class> </hibernate-mapping> |
Example 3;