Seam Glassfish Slidecast

  • 3,078 views
Uploaded on

A SlideCast on how to use GlassFish and Seam together

A SlideCast on how to use GlassFish and Seam together

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,078
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
146
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. SEAM ON GLASSFISH A SLIDECAST DAN ALLEN JBOSS, A DIVISION OF RED HAT
  • 2. package org.example.vehicles.action; import javax.ejb.Remove;... @Stateful @Name(quot;vehicleTradequot;) public class VehicleTradeBean implements VehicleTrade { @Logger private Log log; @In FacesMessages facesMessages; private String value; public void trade() { log.info(quot;vehicleTrade.trade() action called with: #{vehicleTrade.value}quot;); facesMessages.add(quot;trade #{vehicleTrade.value}quot;); } @Length(max = 10) public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Destroy @Remove public void destroy() {} }
  • 3. package org.example.vehicles.action; import javax.ejb.Stateless;... @Stateless @Name(quot;authenticatorquot;) public class AuthenticatorBean implements Authenticator { @Logger private Log log; @In Identity identity; @In Credentials credentials; public boolean authenticate() { log.info(quot;authenticating {0}quot;, credentials.getUsername()); //write your authentication logic here, //return true if the authentication was //successful, false otherwise if (quot;adminquot;.equals(credentials.getUsername())) { identity.addRole(quot;adminquot;); return true; } return false; } }
  • 4. /build.properties jboss.home = /home/dallen/opt/jboss-as jboss.domain = default glassfish.home = /home/dallen/opt/glassfish-v2 glassfish.domain = domain1 Tells script which GlassFish installation to target.
  • 5. 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=quot;${basedir}/glassfish.build.xmlquot;/>
  • 6. asadmin macro <macrodef name=quot;asadminquot;> <attribute name=quot;cmdquot;/> <attribute name=quot;argsquot; default=quot;quot;/> <attribute name=quot;logquot; default=quot;truequot;/> <element name=quot;pre-conditionsquot; optional=quot;truequot;/> <sequential> <fail unless=quot;glassfish.homequot;>glassfish.home not set</fail> <pre-conditions/> <exec executable=quot;${glassfish.home}/bin/asadminquot;> <arg value=quot;@{cmd}quot;/> <arg line=quot;@{args}quot;/> <redirector outputproperty=quot;gf.cmd.outputquot; alwayslog=quot;@{log}quot;/> </exec> </sequential> </macrodef>
  • 7. Using the asadmin macro Starting the server <asadmin cmd=quot;start-domainquot; args=quot;${glassfish.domain}quot;> <pre-conditions> <fail unless=quot;glassfish.domainquot;>glassfish.domain not set</fail> </pre-conditions> </asadmin> Stopping the server <asadmin cmd=quot;stop-domainquot; args=quot;${glassfish.domain}quot;> <pre-conditions> <fail unless=quot;glassfish.domainquot;>glassfish.domain not set</fail> </pre-conditions> </asadmin> Registering a data source <asadmin cmd=quot;add-resourcesquot; args=quot;${basedir}/resources/glassfish-resources-${profile}.xmlquot;/>
  • 8. <target name=quot;gf-deploy-hibernatequot; description=quot;Deploys Hibernate to be a JPA provider on GlassFishquot;> <fail unless=quot;glassfish.homequot;>glassfish.home not set</fail> <fail unless=quot;glassfish.domainquot;>glassfish.domain not set</fail> <copy todir=quot;${glassfish.home}/domains/${glassfish.domain}/lib/extquot;> <fileset dir=quot;${basedir}/libquot;> <include name=quot;antlr.jarquot;/> <include name=quot;asm.jarquot;/> <include name=quot;asm-attrs.jarquot;/> <include name=quot;cglib.jarquot;/> <include name=quot;commons-collections.jarquot;/> <include name=quot;commons-logging.jarquot;/> <include name=quot;concurrent.jarquot;/> <include name=quot;dom4j.jarquot;/> <include name=quot;hibernate.jarquot;/> <include name=quot;hibernate-*.jarquot;/> <exclude name=quot;hibernate-search.jarquot;/> <include name=quot;javassist.jarquot;/> <include name=quot;jboss-common-core.jarquot;/> <include name=quot;jta.jarquot;/> <include name=quot;persistence-api.jarquot;/> <include name=quot;mysql-connector-java-5.1.6.jarquot;/> </fileset> </copy> </target>
  • 9. /resources/META-INF/persistence-dev.xml <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <persistence> <persistence-unit name=quot;vehicleseequot;> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>vehicleseeDatasource</jta-data-source> <properties> ... <property name=quot;hibernate.transaction.manager_lookup_classquot; value=quot;@transactionManagerLookupClass@quot; /> </properties> </persistence-unit> </persistence> Strip proprietary JNDI prefix java:/ /resources/vehiclesee-dev-ds.xml <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <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>
  • 10. /resources/glassfish-resources-dev.xml <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <resources> <jdbc-connection-pool name=quot;vehicleseePoolquot; datasource-classname=quot;com.mysql.jdbc.jdbc2.optional.MysqlDataSourcequot; res-type=quot;javax.sql.DataSourcequot;> <property name=quot;userquot; value=quot;dallenquot;/> <property name=quot;passwordquot; value=quot;dallenquot;/> <property name=quot;urlquot; value=quot;jdbc:mysql://localhost/vehiclesquot;/> </jdbc-connection-pool> <jdbc-resource Uses DataSource rather jndi-name=quot;vehicleseeDatasourcequot; than JDBC driver pool-name=quot;vehicleseePoolquot; enabled=quot;truequot; object-type=quot;userquot;/> </resources> JNDI name referenced GlassFish Admin Console in persistence.xml
  • 11. /build.xml (top of jar target) <!-- defaults, can be overridden in preceding target or from commandline flag --> <property name=quot;ejbJndiPatternquot; value=quot;${project.name}/#{ejbName}/localquot;/> <property name=quot;transactionManagerLookupClassquot; value=quot;org.hibernate.transaction.SunONETransactionManagerLookupquot;/> Token replacements for components.properties <filterset id=quot;seamquot;> <filter token=quot;ejbJndiPatternquot; value=quot;${ejbJndiPattern}quot;/> <filter token=quot;seamBootstrapPuquot; value=quot;${seamBootstrapPu}quot;/> <filter token=quot;seamEmfquot; value=quot;${seamEmf}quot;/> <filter token=quot;puJndiNamequot; value=quot;${puJndiName}quot;/> </filterset> Token replacements for persistence.xml <filterset id=quot;persistencequot;> <filter token=quot;transactionManagerLookupClassquot; value=quot;${transactionManagerLookupClass}quot;/> </filterset>
  • 12. /build.xml (jar target) <copy tofile=quot;${jar.dir}/META-INF/persistence.xmlquot; file=quot;${basedir}/resources/META-INF/persistence-${profile}.xmlquot; overwrite=quot;truequot;> <filterset refid=quot;persistencequot;/> </copy> Apply token replacements /build.xml (war target) <copy tofile=quot;${war.dir}/WEB-INF/classes/components.propertiesquot; file=quot;${basedir}/resources/components-${profile}.propertiesquot; overwrite=quot;truequot;> <filterset refid=quot;seamquot;/> </copy>
  • 13. /resources/WEB-INF/components.xml <components xmlns=quot;http://jboss.com/products/seam/componentsquot; ... xmlns:persistence=quot;http://jboss.com/products/seam/persistencequot; xsi:schemaLocation=quot; ... http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsdquot;> <persistence:entity-manager-factory name=quot;entityManagerFactoryquot; persistence-unit-name=quot;vehicleseequot;/> <persistence:managed-persistence-context name=quot;entityManagerquot; entity-manager-factory=quot;#{entityManagerFactory}quot; auto-create=quot;truequot;/> </components> Seam bootstraps persistence unit == application-managed persistence
  • 14. /resources/WEB-INF/components.xml <components xmlns=quot;http://jboss.com/products/seam/componentsquot; ... xmlns:tx=quot;http://jboss.com/products/seam/transactionquot; xsi:schemaLocation=quot; ... http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsdquot;> <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)
  • 15. /build.xml <copy tofile=quot;${war.dir}/WEB-INF/classes/components.propertiesquot; file=quot;${basedir}/resources/components-${profile}.propertiesquot;> <filterset refid=quot;seamquot;/> </copy> + /resources/components-dev.properties jndiPattern=@ejbJndiPattern@ /WEB-INF/classes/components.properties jndiPattern=java:comp/env/vehiclesee/#{ejbName}/local /WEB-INF/components.xml <components> <core:init jndiPattern=quot;@jndiPattern@quot;/> </compoennts>
  • 16. /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>
  • 17. /build.xml (war target) <target name=quot;warquot; ...> ... <fileset dir=quot;${basedir}/viewquot;> <include name=quot;**/*.xcssquot;/> </fileset> <fileset dir=quot;${basedir}/resourcesquot;> Add fileset to copy the provided theme <include name=quot;**/*.xcssquot;/> resource (theme.xcss) to the classpath </fileset> ... to workaround bug in RichFaces with </target> GlassFish.
  • 18. 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
  • 19. 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
  • 20. 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