29
SEAM ON GLASSFISH A SLIDECAST DAN ALLEN JBOSS, A DIVISION OF RED HAT

Seam Glassfish Slidecast

Embed Size (px)

DESCRIPTION

A SlideCast on how to use GlassFish and Seam together

Citation preview

Page 1: Seam Glassfish Slidecast

SEAM ON GLASSFISHA SLIDECAST

DAN ALLENJBOSS, A DIVISION OF RED HAT

Page 2: Seam Glassfish Slidecast
Page 3: Seam Glassfish Slidecast
Page 4: Seam Glassfish Slidecast
Page 5: Seam Glassfish Slidecast
Page 6: Seam Glassfish Slidecast

package org.example.vehicles.action;

import javax.ejb.Remove;...

@Stateful@Name("vehicleTrade")public class VehicleTradeBean implements VehicleTrade{ @Logger private Log log;

@In FacesMessages facesMessages;

private String value;

public void trade() { log.info("vehicleTrade.trade() action called with: #{vehicleTrade.value}"); facesMessages.add("trade #{vehicleTrade.value}"); }

@Length(max = 10) public String getValue() { return value; }

public void setValue(String value) { this.value = value; }

@Destroy @Remove public void destroy() {}

}

Page 7: Seam Glassfish Slidecast

package org.example.vehicles.action;

import javax.ejb.Stateless;...

@Stateless@Name("authenticator")public class AuthenticatorBean implements Authenticator{ @Logger private Log log;

@In Identity identity; @In Credentials credentials;

public boolean authenticate() { log.info("authenticating {0}", credentials.getUsername()); //write your authentication logic here, //return true if the authentication was //successful, false otherwise if ("admin".equals(credentials.getUsername())) { identity.addRole("admin"); return true; } return false; }

}

Page 8: Seam Glassfish Slidecast
Page 9: Seam Glassfish Slidecast
Page 10: Seam Glassfish Slidecast

/build.properties

jboss.home = /home/dallen/opt/jboss-asjboss.domain = defaultglassfish.home = /home/dallen/opt/glassfish-v2glassfish.domain = domain1

Tells script which GlassFish installation to target.

Page 11: Seam Glassfish Slidecast

Adding GlassFish targets

Define in separate Ant build file– glassfish.build.xml

Prefix targets to avoid naming conflict– prefix: “gf-”

Import into build.xml (before first target)<import file="${basedir}/glassfish.build.xml"/>

Page 12: Seam Glassfish Slidecast

asadmin macro

<macrodef name="asadmin"> <attribute name="cmd"/> <attribute name="args" default=""/> <attribute name="log" default="true"/> <element name="pre-conditions" optional="true"/> <sequential> <fail unless="glassfish.home">glassfish.home not set</fail> <pre-conditions/> <exec executable="${glassfish.home}/bin/asadmin"> <arg value="@{cmd}"/> <arg line="@{args}"/> <redirector outputproperty="gf.cmd.output" alwayslog="@{log}"/> </exec> </sequential></macrodef>

Page 13: Seam Glassfish Slidecast

Using the asadmin macro

<asadmin cmd="start-domain" args="${glassfish.domain}"> <pre-conditions> <fail unless="glassfish.domain">glassfish.domain not set</fail> </pre-conditions></asadmin>

Starting the server

Stopping the server

Registering a data source

<asadmin cmd="stop-domain" args="${glassfish.domain}"> <pre-conditions> <fail unless="glassfish.domain">glassfish.domain not set</fail> </pre-conditions></asadmin>

<asadmin cmd="add-resources" args="${basedir}/resources/glassfish-resources-${profile}.xml"/>

Page 14: Seam Glassfish Slidecast

<target name="gf-deploy-hibernate" description="Deploys Hibernate to be a JPA provider on GlassFish"> <fail unless="glassfish.home">glassfish.home not set</fail> <fail unless="glassfish.domain">glassfish.domain not set</fail> <copy todir="${glassfish.home}/domains/${glassfish.domain}/lib/ext"> <fileset dir="${basedir}/lib"> <include name="antlr.jar"/> <include name="asm.jar"/> <include name="asm-attrs.jar"/> <include name="cglib.jar"/> <include name="commons-collections.jar"/> <include name="commons-logging.jar"/> <include name="concurrent.jar"/> <include name="dom4j.jar"/> <include name="hibernate.jar"/> <include name="hibernate-*.jar"/> <exclude name="hibernate-search.jar"/> <include name="javassist.jar"/> <include name="jboss-common-core.jar"/> <include name="jta.jar"/> <include name="persistence-api.jar"/> <include name="mysql-connector-java-5.1.6.jar"/> </fileset> </copy></target>

Page 15: Seam Glassfish Slidecast

/resources/META-INF/persistence-dev.xml

<?xml version="1.0" encoding="UTF-8"?><persistence> <persistence-unit name="vehiclesee"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>vehicleseeDatasource</jta-data-source> <properties> ... <property name="hibernate.transaction.manager_lookup_class" value="@transactionManagerLookupClass@" /> </properties> </persistence-unit></persistence>

/resources/vehiclesee-dev-ds.xml

<?xml version="1.0" encoding="UTF-8"?><datasources> <local-tx-datasource> <jndi-name>vehicleseeDatasource</jndi-name> <use-java-context>false</use-java-context> <connection-url>jdbc:mysql://localhost/vehicles</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>dallen</user-name> <password>dallen</password> </local-tx-datasource></datasources>

Strip proprietary JNDI prefix java:/

Page 16: Seam Glassfish Slidecast

/resources/glassfish-resources-dev.xml

<?xml version="1.0" encoding="UTF-8"?><resources>

<jdbc-connection-pool name="vehicleseePool" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" res-type="javax.sql.DataSource"> <property name="user" value="dallen"/> <property name="password" value="dallen"/> <property name="url" value="jdbc:mysql://localhost/vehicles"/> </jdbc-connection-pool>

<jdbc-resource jndi-name="vehicleseeDatasource" pool-name="vehicleseePool" enabled="true" object-type="user"/>

</resources>

Uses DataSource rather than JDBC driver

JNDI name referenced in persistence.xml

GlassFish Admin Console

Page 17: Seam Glassfish Slidecast

/build.xml (top of jar target)

<!-- defaults, can be overridden in preceding target or from commandline flag --><property name="ejbJndiPattern" value="${project.name}/#{ejbName}/local"/><property name="transactionManagerLookupClass" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>

<filterset id="seam"> <filter token="ejbJndiPattern" value="${ejbJndiPattern}"/> <filter token="seamBootstrapPu" value="${seamBootstrapPu}"/> <filter token="seamEmf" value="${seamEmf}"/> <filter token="puJndiName" value="${puJndiName}"/></filterset>

<filterset id="persistence"> <filter token="transactionManagerLookupClass" value="${transactionManagerLookupClass}"/></filterset>

Token replacements for persistence.xml

Token replacements for components.properties

Page 18: Seam Glassfish Slidecast

/build.xml (war target)

<copy tofile="${war.dir}/WEB-INF/classes/components.properties" file="${basedir}/resources/components-${profile}.properties" overwrite="true"> <filterset refid="seam"/></copy>

/build.xml (jar target)

<copy tofile="${jar.dir}/META-INF/persistence.xml" file="${basedir}/resources/META-INF/persistence-${profile}.xml" overwrite="true"> <filterset refid="persistence"/></copy>

Apply token replacements

Page 19: Seam Glassfish Slidecast

/resources/WEB-INF/components.xml

<components xmlns="http://jboss.com/products/seam/components" ... xmlns:persistence="http://jboss.com/products/seam/persistence" xsi:schemaLocation=" ... http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd">

<persistence:entity-manager-factory name="entityManagerFactory" persistence-unit-name="vehiclesee"/> <persistence:managed-persistence-context name="entityManager" entity-manager-factory="#{entityManagerFactory}" auto-create="true"/>

</components>

Seam bootstraps persistence unit == application-managed persistence

Page 20: Seam Glassfish Slidecast

/resources/WEB-INF/components.xml

<components xmlns="http://jboss.com/products/seam/components" ... xmlns:tx="http://jboss.com/products/seam/transaction" xsi:schemaLocation=" ... http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd">

<tx:ejb-transaction/>

</components>

Allows Seam to pass along transaction synchronization events to other Seam components.

Before completion:

- org.jboss.seam.beforeTransactionCompletion

After successful completion:

- org.jboss.seam.afterTransactionCompletion(true)

After transaction failure:

- org.jboss.seam.afterTransactionCompletion(false)

Page 21: Seam Glassfish Slidecast

/WEB-INF/classes/components.properties

jndiPattern=java:comp/env/vehiclesee/#{ejbName}/local

/WEB-INF/components.xml

<components> <core:init jndiPattern="@jndiPattern@"/></compoennts>

/build.xml

<copy tofile="${war.dir}/WEB-INF/classes/components.properties" file="${basedir}/resources/components-${profile}.properties"> <filterset refid="seam"/></copy>

/resources/components-dev.properties

jndiPattern=@ejbJndiPattern@

+

Page 22: Seam Glassfish Slidecast

/resources/WEB-INF/web.xml (end of file)

<ejb-local-ref> <ejb-ref-name>vehiclesee/EjbSynchronizations/local</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local-home/> <local>org.jboss.seam.transaction.LocalEjbSynchronizations</local></ejb-local-ref>

<ejb-local-ref> <ejb-ref-name>vehiclesee/AuthenticatorBean/local</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local-home/> <local>org.example.vehicles.action.Authenticator</local></ejb-local-ref>

<ejb-local-ref> <ejb-ref-name>vehiclesee/VehicleTradeBean/local</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <local-home/> <local>org.example.vehicles.action.VehicleTrade</local></ejb-local-ref>

Page 23: Seam Glassfish Slidecast

/build.xml (war target)

<target name="war" ...> ... <fileset dir="${basedir}/view"> <include name="**/*.xcss"/> </fileset> <fileset dir="${basedir}/resources"> <include name="**/*.xcss"/> </fileset> ...</target>

Add fileset to copy the provided theme resource (theme.xcss) to the classpath to workaround bug in RichFaces with GlassFish.

Page 24: Seam Glassfish Slidecast
Page 25: Seam Glassfish Slidecast
Page 26: Seam Glassfish Slidecast
Page 27: Seam Glassfish Slidecast

Achieving hot deploy

jboss-seam.jar must be exploded since it contains an EJB– Deploy exploded EAR from staging area

– gf-explode uses “adadmin deploydir”

Run staging target– gf-hotdeploy runs “ant stage”

Seam's hot deploy classloader works!– The catch: only works with a WAR, not an EAR

Page 28: Seam Glassfish Slidecast

Container-managed persistence

Persistence unit must be deployed in a separate JAR

JAR must be in EAR lib directory and cannot be exploded

Requires additional configuration in components.xml, persistence.xml, and web.xml

Allows you to use @PersistenceContext

Page 29: Seam Glassfish Slidecast

Commands to develop by

gf-start - Starts GlassFish

gf-stop - Stops GlassFish

gf-restart - Restarts GlassFish

gf-datasource - Registers the datasource and connection pool for the profile

gf-explode - Deploys the exploded archive to GlassFish (initial)

gf-hotdeploy - Hot deploys Java classes and components (exploded only)

gf-deploy - Deploys the packaged archive to GlassFish

gf-undeploy - Undeploys the exploded or packaged archive from GlassFish

gf-deploy-hibernate - Deploys Hibernate as a JPA provider to GlassFish