Browsed by
Category: english

Dynamic switching dev and prod datasource with maven profiles

Dynamic switching dev and prod datasource with maven profiles

maven logo
Most of the time when you develop an application that uses a database you are likely to use another database for your local development work than what you will use later in production. The main reason for this is that there are databases like H2 which by design are fitting the development situation while they are not very well suited for production usage. H2 i.e. has very fast startup times, can run completely in memory -this is an advantage because every time it is restarted its state is reset, too-, uses only few system resources and has a good mySQL compatibility. On the other hand it is not that popular in production as it lacks many functionalities like custom functions, an SQL interpreter etc.. Following this it is a very common scenario to use mySQL in production while using H2 for development. This way the developer can use the same database dialect for development while the local installation of a mySQL DB is not needed.
The problem with this scenario is that you need to switch the datasource to production when you build a new release and switch back to development when you want to use your local H2 for testing. This is something which can easily be forgotten and become annoying over time. Therefore it is a step which could and should be automated.
When you are using JavaEE you define your datasource depending on the application server either via an interactive tool (i.e. glassfish or payara via the asadmin command) or in a xml file (i.e. wildfly/jboss standalone.xml or TomEE context.xml). As an example I show you here a valid datasource definition in the wildly standalone.xml

<subsystem xmlns="urn:jboss:domain:datasources:4.0">
  <datasources>
    <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
      <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
        <driver>h2</driver>
        <security>
          <user-name>sa</user-name>
        </security>
      </datasource>

      <datasource jndi-name="java:jboss/datasources/ExampleMySQLDS" pool-name="ExampleMySQLDS" enabled="true" use-java-context="true">
        <connection-url>jdbc:mysql://localhost:3306/brachub</connection-url>
        <driver>mysql</driver>
        <security>
            <user-name>root</user-name>
            <password>1234</password>
        </security>
        <statement>
            <prepared-statement-cache-size>100</prepared-statement-cache-size>
            <share-prepared-statements>true</share-prepared-statements>
        </statement>
    </datasource>

    <drivers>
      <driver name="h2" module="com.h2database.h2">
        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
      </driver>
      <driver name="mysql" module="com.sql.mysql">
        <driver-class>com.mysql.jdbc.Driver</driver-class>
      </driver>
    </drivers>
  </datasources>
</subsystem>

Read More Read More

Apache Shiro part 2 – securing a JSF Java EE 7 application

Apache Shiro part 2 – securing a JSF Java EE 7 application

apache shiro logo

In the first part I described why I chose Apache Shiro as an Authentication framework. In this part I will describe the simplest working solution to secure a Java EE7 application with JSF/Primefaces frontend. To be honest I wont use much of Primefaces in this sample but the application for which I did this research uses it so I added the dependency here and added an Primefaces component to this demo project.

Bootstrapping

I created an empty Java EE project via maven and added the needed Shiro dependencies as well as the Primefaces dependency to

the pom file.

<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>de.are_you_ready</groupId>
    <artifactId>shirotest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>

        <apache-shiro.version>1.3.2</apache-shiro.version>
        <primefaces.version>6.0</primefaces.version>
        <junit.version>4.12</junit.version>
    </properties>

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

        <!-- Security -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${apache-shiro.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>${apache-shiro.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>${apache-shiro.version}</version>
        </dependency>

        <!-- PrimeFaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${primefaces.version}</version>
        </dependency>

        <!-- Testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>shirotest</finalName>
    </build>
</project>

Read More Read More

Don’t rely on Java finalizers

Don’t rely on Java finalizers

java-logo
Recently I was facing a problem where a Java application should persist a part of its state when it was closed. This was already implemented but seemed to work just unreliably and it was not clearly reproducible when it worked properly and when it failed. After analyzing the existing source code I figured out that finalizers were used to implement the functionality by delegating the call of the persist logic to the garbage collector. This seems like a very good idea at first but lacks in reliability. As a short note up front don’t use finalizers for important things.

A finalizer is a method which represents the opposite of a constructor. It is a method with the purpose to do some cleanup work, free up some memory and so on which gets called by the garbage collector before the object gets collected. A simple example would look like this
[java]
protected void finalize() {
logger.info(“start important work before GC”);
fooBar.persist(this.getDataMap());
}
[/java]

Read More Read More

Apple MacBook Pro late 2016

Apple MacBook Pro late 2016

apple.com

This blog should not transform to yet another Apple blog but as a Java Developer the Mac is my preferred platform which I use daily. This is the reason why I tend to take a closer look when Apple shows the next generation of macOS or Mac hardware even if I’m not buying a new one immediately. I want to get a feeling were the platform is going to. Last week Apple showed its last iteration of the MacBook Pro to the public and I want to share my thoughts as a developer about it.

At first I like the new look, the lower weight is really great for me as I often travel around with the MacBook being in my backpack and every grams you could save there is a win. The first thing that got me thinking was if it really is a good idea to start the 13″ model with just 8GB of RAM. From a developer standpoint I think that this is the absolute minimum and you should get an upgrade here if possible. When you are a Java developer and start your app server (sometimes multiple servers at once), a database in a VM, your IDE, Browser, Mail etc this will lead very fast to eating up all the RAM and swapping out to the SSD. SSDs are very fast these days but they are no match to the RAM and it will slow down your system. 16GB will be a better solution here and that Apple doesn’t offer 32GB for the 13″ model is a shame. That they commented afterwards that this decision was made because 32GB would have used to much of the battery is a reason but not one that I like. I would much more like to work a shorter time with a faster system then work longer with a slow system that has used up all its memory because this often makes effective working impossible and I guess in the future we will need more RAM than today instead of less.

Read More Read More

Java Forum Nord 2016

Java Forum Nord 2016

Java Forum Nord
Last week (October 20th) I attended the second “Java Forum Nord” in Hannover. The Java Forum is now in its second year but is the evolution of an older and smaller conference in Göttingen (Source Talk Tage). The relocation and renaming of the conference was necessary because it wasn’t possible to attract an audience huge enough to get top speakers to Göttingen. To make the transfer possible the JUG Deutschland which is located in Göttingen and hosted the Source Talk cooperates now with the JUG Bremen, JUG Hamburg, JUG Hessen (Kassel), JUG Ostfalen, SUN User Group Deutschland and the local JUG Hannover.
As a result the conference professionalized very quickly and the audience grew by a huge number. After the first edition in 2015 took place in a small Hotel at the Hannover main train station and was quickly sold out they relocated to the bigger Hotel Dormero in Hannover which had space for 400 participants and was sold out, too. After I would consider last year as a transition year I had this year the feeling to be on a very serious one day conference which had definitively left the amateur status. What makes this conference so special is that it is organized by the JUGs (Java User Groups) and aims for no profit which makes it possible to give very cheap tickets away.

Read More Read More