A Maven configuration for a Java Webstart (JNLP) application

Context

  • codehaus Maven JNLP plugin version 1.0-beta-3
  • Maven shade plugin version 1.6

The Plugin configuration to generate auto-executable Jar

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.6</version>
    <executions>
    <execution>
        <phase>package</phase>
        <goals>
            <goal>shade</goal>
        </goals>
        <configuration>
            <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    <mainClass>com.ichir.projects.MainClass</mainClass>
                </transformer>
            </transformers>
        </configuration>
    </execution>
    </executions>
</plugin>

The Plugin configuration for JNLP (via a profile)

<profiles>
    <profile>
        <id>jnlp</id>
	<build>
	    <plugins>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>webstart-maven-plugin</artifactId>
			<version>1.0-beta-3</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>jnlp</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<codebase>http://localhost/jnlp/</codebase>
				<jnlp>
	    			    <mainClass>com.ichir.projects.MainClass</mainClass>
				</jnlp>
				<pack200>true</pack200>
				<sign>
				    <keystore>${project.basedir}/src/main/jnlp/ichir.jks</keystore>
				    <keypass>mahieddine.ichir</keypass>
				    <storepass>mahieddine.ichir</storepass>
				    <alias>ichir</alias>
				    <verify>false</verify>
				</sign>
				<verbose>true</verbose>
			</configuration>
		</plugin>
	    </plugins>
        </build>
    </profile>
</profiles>

You have to change these configurations according to your application, mainly the codebase, the mainClass (in both plugins configurations) and the keystore parameters to use your own

In order to generate the JNLP repository, run

    mvn clean install -Pjnlp

A jnlp folder is created in the target folder containing all the needed resources and the “launch.jnlp” autostart file.

It is important to put the “jnlp” velocity template.vm file in src/main/jnlp along with the keystore file.

The pom file contains other jnlp configuration properties that you should set accordingly

  • project.name
  • project.description
  • project.url
  • project.organization.name
  • project.organization.url

Downloads

References

Eclipse RCP and P2 Headless Update on Startup

This post is neither a tutorial on how to build Eclipse RCP applications nor how to create Eclipse RCP plugins and features, it is a tutorial on how to make and configure an Eclipse RCP application check for updates on startup.

Context

  • Eclipse 3.7 platform
  • Java 6

Basic example project layout

The plugin project

Add the following dependencies to your plugin

  • org.eclipse.equinox.p2.core
  • org.eclipse.equinox.p2.operations
  • org.eclipse.equinox.p2.repository
  • org.eclipse.equinox.p2.metadata

plugin dependencies

Copy the following classes to your plugin src folder

Override the postWindowOpen method of your ApplicationWorkbenchWindowAdvisor class and add in the following code

@Override
public void postWindowOpen() {
    BundleContext bundleContext = Activator.getDefault().getBundle().getBundleContext();
    ServiceReference<IProvisioningAgent> serviceReference =   bundleContext.getServiceReference(IProvisioningAgent.class);
    IProvisioningAgent agent = bundleContext.getService(serviceReference);
    if (agent == null) {
        System.out.println(">> no agent loaded!");
        return;
    }
    // Adding the repositories to explore
    if (! P2Util.addRepository(agent, "http://localhost/p2/repository")) {
        System.out.println(">> could no add repostory!");
        return;
    }
    // scheduling job for updates
    UpdateJob updateJob = new UpdateJob(agent);
    updateJob.schedule();
}

The feature project

Declare your startup plugin into your feature and add in the following plugins

  • com.ichir.projects.p2update.plugin1 (the plugin we’ve created earlier)
  • org.eclipse.core.net
  • org.eclipse.equinox.ds
  • org.eclipse.equinox.p2.core
  • org.eclipse.equinox.p2.director
  • org.eclipse.equinox.p2.engine
  • org.eclipse.equinox.p2.metadata
  • org.eclipse.equinox.p2.metadata.repository
  • org.eclipse.equinox.p2.publisher
  • org.eclipse.equinox.p2.publisher.eclipse
  • org.eclipse.equinox.p2.repository
  • org.eclipse.equinox.p2.repository.tools
  • org.eclipse.equinox.simpleconfigurator
  • org.eclipse.osgi
  • org.eclipse.ui.forms
  • org.eclipse.swt.win32.win32.x86

The org.eclipse.swt.win32.win32.x86 plugin depends of course on your platform.

feature dependencies

The .product configuration file

Make your product configuration depends on feature and add in the following features

  • org.eclipse.rcp
  • org.eclipse.equinox.p2.rcp.feature
  • com.ichir.projects.p2update.feature1 (the feature we’ve created earlier)

P2 Product dependencies

Specify the following starting levels in your product file

Product start configuration

Versioning

When building Eclipse RCP with auto update features, versioning plugins, features and product is a major part! Each time you want to publish an update on a plugin, its version must be augmented, and so its containing feature and the product file. For simplicity, we can add the “.qualifier” to all our versions to automatically augment the version numbers each build is made.

Exporting application, generating the P2 repository and testing

  1. From the .product file, export the application with caution to check “Generate metadata repository” option. This will generate the executable application aside with a repository folder. Just ignore that repository.
  2. Make any modification to your UI and re-export the application to another folder. This time ignore the generated application and copy the generated repository to your HTTP web server (remember that we added “http://localhost/p2/repository&#8221; as an update repository in ApplicationWorkbenchWindowAdvisor)
  3. Once your application restarted, you should be prompted for an update!

That’s done! Enjoy!

References
To complete

Downloads
To complete

Obtaining a JDBC Connection in Hibernate and OpenJPA

This post is on how to obtain/instantiate a JDBC connection in Hibernate framework or in OpenJPA framework.

Test context

  • Hibernate 3.x
  • OpenJPA 1.2.x

Hibernate

Session session = new Configuration().buildSessionFactory().openSession();
session.doWork(new Work() {
    @Override
    public void execute(Connection connection) throws SQLException {
        // do something with connection
    }
});

OpenJPA

EntityManager manager = getEntityManagerFactory("unit-name").createEntityManager();
Connection connection = (Connection) OpenJPAPersistence.cast(manager).getConnection();
// do something with connection

References