I recently ran across a problem with the Embedded Glassfish 4 maven problem.. as soon as I tried to activate any CDI specific stuff, I got the following exception:

Sep 08, 2013 11:05:37 PM org.glassfish.deployment.admin.DeployCommand execute
SEVERE: Exception while loading the app : CDI deployment failure:WELD-001524 Unable to load proxy class for bean Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default] with class interface javax.enterprise.inject.Instance using classloader WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
org.jboss.weld.exceptions.WeldException: WELD-001524 Unable to load proxy class for bean Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default] with class interface javax.enterprise.inject.Instance using classloader WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
at org.jboss.weld.bean.proxy.ProxyFactory.getProxyClass(ProxyFactory.java:306)
at org.jboss.weld.bean.builtin.AbstractFacadeBean.initializeAfterBeanDiscovery(AbstractFacadeBean.java:60)
at org.jboss.weld.bootstrap.BeanDeployer.doAfterBeanDiscovery(BeanDeployer.java:354)
at org.jboss.weld.bootstrap.BeanDeployment.afterBeanDiscovery(BeanDeployment.java:280)
at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:522)
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:213)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:131)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:328)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:493)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:356)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
at com.sun.enterprise.admin.cli.embeddable.DeployerImpl.deploy(DeployerImpl.java:133)
at com.sun.enterprise.admin.cli.embeddable.DeployerImpl.deploy(DeployerImpl.java:109)
at org.glassfish.maven.PluginUtil.doDeploy(PluginUtil.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.maven.AbstractDeployMojo.doDeploy(AbstractDeployMojo.java:239)
at org.glassfish.maven.RunMojo.execute(RunMojo.java:68)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:106)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:318)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:153)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:555)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:414)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:357)
Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/codehaus/plexus/classworlds/realm/ClassRealm) previously initiated loading for a different type with name "javax/enterprise/util/TypeLiteral"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:386)
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
at java.lang.Class.privateGetPublicMethods(Class.java:2641)
at java.lang.Class.getMethods(Class.java:1457)
at org.jboss.weld.bean.proxy.ProxyFactory.addMethodsFromClass(ProxyFactory.java:513)
at org.jboss.weld.bean.proxy.ProxyFactory.addMethods(ProxyFactory.java:468)
at org.jboss.weld.bean.proxy.ProxyFactory.createProxyClass(ProxyFactory.java:402)
at org.jboss.weld.bean.proxy.ProxyFactory.getProxyClass(ProxyFactory.java:299)
... 50 more

Sep 08, 2013 11:05:37 PM PluginUtil doDeploy
INFO: Deployed null

As it turned out, this is a problem with Maven 3.1 which now bundles Plexus without correctly isolating the classpath.
If you have this problem as well, use Maven 3.0.5 instead.

This issue is reported on the Glassfish JIRA.

I’ve messed around a lot to get JRebel to work with the Embedded-Glassfish 4 maven plugin. Especially, as I thought maven would spawn an own JVM and I had to alter the domain.xml for it to work… I was soooo wrong.

Simply adding the JRebel Plugin to my pom.xml and adjusting my MAVEN_OPTS was enough.

Thats my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>com.dominikdorn.vaadin7ee</groupId>
    <artifactId>javaee7demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>javaee7demo</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- don't use version 4.0 of the plugin, its broken -->
        <embedded-glassfish.version>3.1.2.2</embedded-glassfish.version>
        <glassfish.version>4.0</glassfish.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
 
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.glassfish.embedded</groupId>
                <artifactId>maven-embedded-glassfish-plugin</artifactId>
                <version>${embedded-glassfish.version}</version>
                <configuration>
                    <app>target/${project.artifactId}-${project.version}/</app>
                    <ports>
                        <http-listener>8282</http-listener>
                        <https-listener>8383</https-listener>
                    </ports>
                    <contextRoot>${project.artifactId}</contextRoot>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.glassfish.main.common</groupId>
                        <artifactId>simple-glassfish-api</artifactId>
                        <version>${glassfish.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.glassfish.main.extras</groupId>
                        <artifactId>glassfish-embedded-all</artifactId>
                        <version>${glassfish.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.zeroturnaround</groupId>
                <artifactId>jrebel-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <id>generate-rebel-xml</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and I simply start the app with

mvn clean package
MAVEN_OPTS="-noverify -javaagent:/home/domdorn/.jrebel/jrebel.jar" mvn embedded-glassfish:run

While JRebel is usually the tool of choice when it comes to fast redeploy cycles, its not always required to get your app redeployed really fast and keep development iterations short.

This example shows you how you can use the embedded-glassfish maven plugin to develop apps with Vaadin7 that redeploy in under a second.

The basic idea is to keep the generated war file respectively the exploded directory as small as possible.
We can achieve this by packaging all the required libraries with the application server itself and only including them as dependency with “provided” scope.
The only thing left is to fix some classloader issues that prevent loading of UIs and Themes.

So lets get started:

Here’s the pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>parent</artifactId>
        <groupId>com.dominikdorn.vaadin7ee</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>javaee7demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>javaee7demo</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- don't use version 4.0 of the plugin, its broken -->
        <embedded-glassfish.version>3.1.2.2</embedded-glassfish.version>
        <glassfish.version>4.0</glassfish.version>
        <vaadin.version>7.1.3</vaadin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-server</artifactId>
            <version>${vaadin.version}</version>
            <!-- add the vaadin dependencies to the glassfish embedded plugin deps to keep war size small -->
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client</artifactId>
            <version>${vaadin.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client-compiled</artifactId>
            <version>${vaadin.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-themes</artifactId>
            <version>${vaadin.version}</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.glassfish.embedded</groupId>
                <artifactId>maven-embedded-glassfish-plugin</artifactId>
                <version>${embedded-glassfish.version}</version>
                <configuration>
                    <app>target/${project.artifactId}-${project.version}/</app>
                    <ports>
                        <http-listener>8282</http-listener>
                        <https-listener>8383</https-listener>
                    </ports>
                    <contextRoot>${project.artifactId}</contextRoot>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.glassfish.main.common</groupId>
                        <artifactId>simple-glassfish-api</artifactId>
                        <version>${glassfish.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.glassfish.main.extras</groupId>
                        <artifactId>glassfish-embedded-all</artifactId>
                        <version>${glassfish.version}</version>
                    </dependency>


                    <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-client-compiled</artifactId>
                    <version>${vaadin.version}</version>
                    </dependency>
                    <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-client</artifactId>
                    <version>${vaadin.version}</version>
                    </dependency>
                    <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-themes</artifactId>
                    <version>${vaadin.version}</version>
                    </dependency>
                    <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-client-compiler</artifactId>
                    <version>${vaadin.version}</version>
                    </dependency>

                    <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-server</artifactId>
                    <version>${vaadin.version}</version>
                    </dependency>
                    <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-client</artifactId>
                    <version>${vaadin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Note that we’re declaring the vaadin dependencies in provided scoped and additionally as plugin-dependencies for the maven embedded-glassfish plugin.
Also note, that at the time of this post, there’s an version 4.0 of the embedded-glassfish maven plugin, but it was broken (at least on my computer) resulting in non working glassfish instances.

Next we’ll need to create a subclass of the Vaadin Servlet to prevent issues with the classloader not seeing classes of our webapp. Create one like this

package com.dominikdorn.vaadin7ee.demo.example_one;

import com.vaadin.server.VaadinServlet;

/**
 * Subclass of the VaadinServlet to fix issues with the Classloader 
 * not finding our UIs and Themes.
 * 
 * @author Dominik Dorn <dominik -at- dominikdorn -dot- com>
 *         http://dominikdorn.com/
 */
public class ClassloaderFixVaadinServlet extends VaadinServlet {
}

Then register it in your web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
        version="3.0"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    
    <servlet>
        <servlet-name>Vaadin7Example1Servlet</servlet-name>
        <!-- we're using our own servlet to fix classloading issues -->
        <!--<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>-->
        <servlet-class>com.dominikdorn.vaadin7ee.demo.example_one.ClassloaderFixVaadinServlet</servlet-class>
        <init-param>
            <param-name>UI</param-name>
            <param-value>com.dominikdorn.vaadin7ee.demo.example_one.ExampleOneUI</param-value>
        </init-param>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>Vaadin7Example1Servlet</servlet-name>
        <url-pattern>/example1/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Vaadin7Example1Servlet</servlet-name>
        <url-pattern>/VAADIN/*</url-pattern>
    </servlet-mapping>
    
</web-app>

That’s it!

Compile your app with

mvn clean package

, then start the server with

mvn embedded-glassfish:run.

When changing classes, either recompile with

mvn compile package
or better configure your IDE to deploy classes to target/${project.artifactId}-${project.version}/WEB-INF/classes.
Then simply press ENTER in the window where you started the embedded-glassfish instance.

This should get you redeploys in under one second. If you’re app grows large and deployment wait time rises, I highly recommend to use JRebel.

I also wrote a post on what to do if embedded glassfish is complaining about expired certificates. It might come handy.

Was this article useful? Do you have other tips to improve the developer productivity with Vaadin ? Share them in the comments below!

I’m messing around with the Embedded Glassfish 4 Maven plugin and came across this error:


Sep 07, 2013 10:40:11 PM com.sun.enterprise.security.ssl.impl.SecuritySupportImpl checkCertificateDates
SEVERE: SEC5054: Certificate has expired: [
[
Version: V3
Subject: CN=GTE CyberTrust Root 5, OU="GTE CyberTrust Solutions, Inc.", O=GTE Corporation, C=US
Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5

Key: Sun RSA public key, 2048 bits
modulus: 23741889829347261660812437366387754385443431973861114865490414153884050331745811968523116847625570146592736935209718565296053386842135985534863157983128812774162998053673746470782252407673402238146869994438729551246768368782318393878374421033907597162218758024581735139682087126982809511479059100617027892880227587855877479432885604404402435662802390484099065871430585284534529627347717530352189612077130606642676951640071336717026459037542552927905851171460589361570392199748753414855675665635003335769915908187224347232807336022456537328962095005323382940080676931822787496212635993279098588863972868266229522169377
public exponent: 65537
Validity: [From: Fri Aug 14 16:50:00 CEST 1998,
To: Thu Aug 15 01:59:00 CEST 2013]
Issuer: CN=GTE CyberTrust Root 5, OU="GTE CyberTrust Solutions, Inc.", O=GTE Corporation, C=US
SerialNumber: [ 01b6]

Certificate Extensions: 4
[1]: ObjectId: 2.5.29.19 Criticality=true
BasicConstraints:[
CA:true
PathLen:5
]

[2]: ObjectId: 2.5.29.32 Criticality=false
CertificatePolicies [
[CertificatePolicyId: [1.2.840.113763.1.2.1.3]
[] ]
]

[3]: ObjectId: 2.5.29.15 Criticality=true
KeyUsage [
Key_CertSign
Crl_Sign
]

[4]: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 76 0A 49 21 38 4C 9F DE F8 C4 49 C7 71 71 91 9D v.I!8L....I.qq..
]
]

]
Algorithm: [SHA1withRSA]
Signature:
0000: 41 3A D4 18 5B DA B8 DE 21 1C E1 8E 09 E5 F1 68 A:..[...!......h
0010: 34 FF DE 96 F4 07 F5 A7 3C F3 AC 4A B1 9B FA 92 4.......<..J....
0020: FA 9B ED E6 32 21 AA 4A 76 C5 DC 4F 38 E5 DF D5 ....2!.Jv..O8...
0030: 86 E4 D5 C8 76 7D 98 D7 B1 CD 8F 4D B5 91 23 6C ....v......M..#l
0040: 8B 8A EB EA 7C EF 14 94 C4 C6 F0 1F 4A 2D 32 71 ............J-2q
0050: 63 2B 63 91 26 02 09 B6 80 1D ED E2 CC B8 7F DB c+c.&...........
0060: 87 63 C8 E1 D0 6C 26 B1 35 1D 40 66 10 1B CD 95 .c...l&.5.@f....
0070: 54 18 33 61 EC 13 4F DA 13 F7 99 AF 3E D0 CF 8E T.3a..O.....>...
0080: A6 72 A2 B3 C3 05 9A C9 27 7D 92 CC 7E 52 8D B3 .r......'....R..
0090: AB 70 6D 9E 89 9F 4D EB 1A 75 C2 98 AA D5 02 16 .pm...M..u......
00A0: D7 0C 8A BF 25 E4 EB 2D BC 98 E9 58 38 19 7C B9 ....%..-...X8...
00B0: 37 FE DB E2 99 08 73 06 C7 97 83 6A 7D 10 01 2F 7.....s....j.../
00C0: 32 B9 17 05 4A 65 E6 2F CE BE 5E 53 A6 82 E9 9A 2...Je./..^S....
00D0: 53 0A 84 74 2D 83 CA C8 94 16 76 5F 94 61 28 F0 S..t-.....v_.a(.
00E0: 85 A7 39 BB D7 8B D9 A8 B2 13 1D 54 09 34 24 7D ..9........T.4$.
00F0: 20 81 7D 66 7E A2 90 74 5C 10 C6 BD EC AB 1B C2 ..f...t\.......

]
Sep 07, 2013 10:40:11 PM com.sun.enterprise.v3.server.AppServerStartup postStartupJob
INFO: Undefined Product Name - define product and version info in config/branding 0.0.0 (0) startup time : Embedded (1,472ms), startup services(1,467ms), total(2,939ms)
Sep 07, 2013 10:40:11 PM org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread run
INFO: JMXStartupService has disabled JMXConnector system
Sep 07, 2013 10:40:11 PM com.sun.enterprise.connectors.jms.util.JmsRaUtil getInstalledMqVersion
WARNING: RAR7000 : Check for a new version of MQ installation failed : /tmp/gfembed2546515913036538019tmp/lib/install/applications/jmsra/../imqjmsra.rar (No such file or directory):/tmp/gfembed2546515913036538019tmp/lib/install/applications/jmsra/imqjmsra.rar
Sep 07, 2013 10:40:12 PM PluginUtil startGlassFish
INFO: Started GlassFish ServerId = maven, GlassFish = com.sun.enterprise.glassfish.bootstrap.StaticGlassFishRuntime$1@4bdb635a, TimeTaken = 1,887 ms

A quick search revealed some hints on Stackoverflow. Unfortunately, they all refer to Glassfish running as standalone, not in an embedded fashion.

Long story short, you’ll have to update the jar file used by the glassfish-embedded maven plugin like this:


mkdir -p /tmp/gf
cd /tmp/gf
unzip /home/domdorn/.m2/repository/org/glassfish/main/extras/glassfish-embedded-all/4.0/glassfish-embedded-all-4.0.jar
chmod +rwx * -Rf
# default keystore password is "changeit"
keytool -delete -keystore config/cacerts.jks -alias gtecybertrust5ca
zip -r ../glassfish-embedded-all-4.0.jar .
mv /home/domdorn/.m2/repository/org/glassfish/main/extras/glassfish-embedded-all/4.0/glassfish-embedded-all-4.0.jar /home/domdorn/.m2/repository/org/glassfish/main/extras/glassfish-embedded-all/4.0/glassfish-embedded-all-4.0.jar_backup
cp ../glassfish-embedded-all-4.0.jar /home/domdorn/.m2/repository/org/glassfish/main/extras/glassfish-embedded-all/4.0/glassfish-embedded-all-4.0.jar

Of course, you’ll have to replace “/home/domdorn/.m2/repository” with the path to your local maven repository.

Recently I had to work with a Oracle instance and had to use a query that uses a subquery like (the IN … statement) this


SELECT * FROM table_a WHERE id IN (...)

The method I had to implement/fix takes an arbitrary number of ids as arguments.. if you get
more than 1000 ids, Oracle will spill a message like this into your fa.. aehm.. onto your console:

"ORA-01795 maximum number of expressions in a list is 1000"

To “fix” this problem, I wrote a small utility class that creates sub-collections of a specific maximum size out of a given collection.

Here it is:

As I keep finding myself searching for orm.xml templates as well as persistence.xml templates for JPA 1.0 and JPA 2.0, I decided to post them here for my own reference and others to find ;)

JPA 1.0 orm.xml and persistence.xml templates

orm.xml template for JPA 1.0

<?xml version="1.0" ?>
<entity-mappings
        xmlns="http://java.sun.com/xml/ns/persistence/orm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
        http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
                 version="1.0">

</entity-mappings>

persistence.xml template for JPA 1.0

<?xml version="1.0" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

</persistence>

JPA 2.0 orm.xml and persistence.xml templates

orm.xml template for JPA 2.0

<?xml version="1.0" ?>
<entity-mappings
        xmlns="http://java.sun.com/xml/ns/persistence/orm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
        http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
                 version="2.0">

</entity-mappings>

persistence.xml template for jpa 2.0

<?xml version="1.0" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">

</persistence>

With these templates your favorite IDE should be able to autocomplete the allowed tags on its own.

While migrating a project of mine from JPA2 to Avaje Ebean, I encountered a issue, I wasn’t expecting.

Avaje Ebean does not support the JPA EntityListener Annotations, like

@PrePersist,
@PostPersist,
@PreUpdate,
@PostUpdate,
@PostLoad

Some nice folks on the Ebean Mailinglist directed me to some Documentation about EntityListeners in Ebean including a helpful forum link which finally pointed me to the BeanPersistController Interface in the Ebean Java API .
With that Information, I was able to create a EntityListener that enables the use of the JPA EntityListener Annotations with Ebean.

This Gist shows how I’ve done it:

package models.sgcore;

import com.avaje.ebean.event.BeanPersistAdapter;
import com.avaje.ebean.event.BeanPersistRequest;

import javax.annotation.PreDestroy;
import javax.persistence.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * This is a <code>BeanPersistController</code> that looks for methods annotated with the JPA Annotations
 * <code>@PrePersist</code>
 * <code>@PostPersist</code>
 * <code>@PreUpdate</code>
 * <code>@PostUpdate</code>
 * <code>@PreDestroy</code>
 * <code>@PostLoad</code>
 *
 * registers those methods with this Listener and calls them when necessary.
 */
public class SGBeanPersistController extends BeanPersistAdapter {
    
    private Map<String,Method> prePersistMap = new TreeMap<String, Method>();
    private Map<String,Method> postPersistMap = new TreeMap<String, Method>();

    private Map<String,Method> preUpdateMap = new TreeMap<String, Method>();
    private Map<String,Method> postUpdateMap = new TreeMap<String, Method>();

    private Map<String,Method> preDestroyMap = new TreeMap<String, Method>();

    private Map<String,Method> postLoadMap = new TreeMap<String, Method>();




    
    @Override
    public boolean isRegisterFor(Class<?> aClass) {
        if(aClass.getAnnotation(Entity.class) != null){
            System.out.println("Registering a Entity; Type is " + aClass.toString());
            Method[] methods = aClass.getMethods();
            boolean hasListener = false;
            for(Method m : methods)
            {
//                System.out.println("looking if method " + m.toString() + " has Annotation on it. ");
                if(m.isAnnotationPresent(PrePersist.class))
                {
                    prePersistMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PostPersist.class))
                {
                    postPersistMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PreDestroy.class))
                {
                    preDestroyMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PreUpdate.class))
                {
                    preUpdateMap.put(aClass.getName(), m);
                    hasListener = true;
                }
                
                if(m.isAnnotationPresent(PostUpdate.class))
                {
                    postUpdateMap.put(aClass.getName(), m);
                    hasListener = true;
                }

                if(m.isAnnotationPresent(PostLoad.class))
                {
                    postLoadMap.put(aClass.getName(), m);
                    hasListener = true;
                }


            }
            return hasListener;
        }
        return false;
    }

    
    private void getAndInvokeMethod(Map<String,Method> map, Object o)
    {
        Method m = map.get(o.getClass().getName());
        if(m != null)
            try {
                m.invoke(o);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    }

    @Override
    public boolean preInsert(BeanPersistRequest<?> request) {
        getAndInvokeMethod(prePersistMap, request.getBean());
        return super.preInsert(request);
    }

    @Override
    public boolean preDelete(BeanPersistRequest<?> request) {
        getAndInvokeMethod(preDestroyMap, request.getBean());
        return super.preDelete(request);
    }

    @Override
    public boolean preUpdate(BeanPersistRequest<?> request) {
        getAndInvokeMethod(preUpdateMap, request.getBean());
        return super.preUpdate(request);
    }

    @Override
    public void postDelete(BeanPersistRequest<?> request) {
        // there is no @PostDestroy annotation in JPA 2
        super.postDelete(request);
    }

    @Override
    public void postInsert(BeanPersistRequest<?> request) {
        getAndInvokeMethod(postPersistMap, request.getBean());
        super.postInsert(request);
    }

    @Override
    public void postUpdate(BeanPersistRequest<?> request) {
        getAndInvokeMethod(postUpdateMap, request.getBean());
        super.postUpdate(request);
    }

    @Override
    public void postLoad(Object bean, Set<String> includedProperties) {
        getAndInvokeMethod(postLoadMap, bean);
        super.postLoad(bean, includedProperties);
    }
}

Questions? Comments? Forks?

I appreciate any kind of feedback! :)

Recently I hold a presentation about Avaje Ebean on my local Java User Group – The Java Student User Group.

Ebean is a alternative to the established Java Persistence API (JPA) implementations like Hibernate, EclipseLink etc.
It uses the JPA Annotations like
@Table
@Entity
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
@Column
@Enumerated
@Temporal

etc.
to map your Java Objects to your database tables, but thats all that it has in common with JPA and its implementations.

You can download the ebean presentation.

Today I created a custom JSF 2 Composite Component, but Mojarra threw an error on me, when I tried to use a German Umlaut like &uuml; in my markup, like this

HTTP Status 500 -


type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.servlet.ServletException: javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.

root cause

javax.servlet.ServletException: javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.

root cause

java.util.concurrent.ExecutionException: javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.

root cause

javax.faces.view.facelets.FaceletException: Error Parsing /resources/sg/uploadedDoc.xhtml: Error Traced[line: 45] The entity "uuml" was referenced, but not declared.

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.


GlassFish Server Open Source Edition 3.0.1


The reason this happened is, that I referenced an entity “uuml;” in a xml document where it is not defined.
XML basically just supports a few build-in entities, like “amp;” “quot;”, “apos;”, “lt;” and “gt;”.

To let the SAX-Parser know which additional entities I wanted to use, I simply added the XHTML 1.1 DOCTYPE to the head of the document.
My Component now looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:composite="http://java.sun.com/jsf/composite">
<body>
<composite:interface>
....
</composite:interface>
 
<composite:implementation>
.... text &Uuml;bermorgen ist auch noch ein Tag ... text
</composite:implementation>
</body>
</html>

Hope this helps some of you out there.

top