March 23, 2015

Invoke Java Applet from JSF page

I have a Java Applet that will handle some operations on the client machine.
This Applet should be called upon client's request from a JSF page.

Development steps:
1. Managed Bean

1.1 boolean value to enable/disable calling Java Applet

private boolean showApplet = false;

1.2 return the Java Applet HTML invoker

public String getAppletHtml() {
    if (showApplet) {
        return "<object type=\"application/x-java-applet\" id=\"the applet\""
                + "code=\"ics.jvm.setup.JVMSetupApplet.class\" codebase=\"./\""
                + "archive=\"./the-applet.jar\" width=\"0\" height=\"0\">" + "<param name=\"force\" value=\"true\" />";
    }
    return "";
}

1.3 enable/disable action method

public String showApplet() {
    showApplet = true;
    return null;
}

2. JSF page
2.1 Write the calling tags
Note: You have to set the escape attribute of your tag to false. Because as default it's set to true and the HTML related character are escaped.

<h:commandLink value="Reset Configuration" action="#{bean.showApplet}" style="color: white; text-decoration: blink;">
    <h:outputText value="#{bean.appletHtml}" escape="false" />
</h:commandLink>


Cannot Un-deploy a web-app completely in Tomcat

Hi,

I have been using Maven for deploy and redeploy to Tomcat, and it was great cause you don't need always to do it manually.
But in Windows environment I have encounter an issue that is "I cannot undeploy the web application completely from the Tomcat".
I have found a solution and its easy:

Open context.xml from /tomcat/conf folder and modify the context to match this :

<Context antiJARLocking="true" antiResourceLocking="true">

March 10, 2015

Maven: Create "version.properties" file inside the generated Jar file

Sometimes when you create a Jar file and send it to the Client, you need to keep tracking it.

You can append the version with the Jar file name, but what if the client change it?
So, I have found that the best way is to inject a file inside the Jar file contains all necessary attributes as Version Number, Build Number and Build date.

How to do that?
Simply, create a file inside /src/main/resources/ (I called it version.properties).
In this file write the following:

version = ${project.version}
build.no = ${build.version}
build.date = ${timestamp}

Go to pom.xml and set the following properties:


<properties>
  <build.version>2.1.91</build.version>
  <timestamp>${maven.build.timestamp}</timestamp>
  <maven.build.timestamp.format>HH:mm dd.MM.yyyy</maven.build.timestamp.format>
</properties>


in the same file go to and add the following lines:


<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>

Finished ?! Now when you generate the Jar file you will find the version.properties file in the root of the jar file as follow:

version = project version
build.no = 1.2.3
build.date = 10:20 1.2.2015

Maven: Copy file to Linux from Windows and Execute Remote Bash file

I have been working on a project that requires to run on a remote Linux machine.
Every time I make changes, I build the jar -> sign it -> rename the jar -> copy to Linux Server -> run Bash file.

It was exhausting steps, so I have search for I way that Maven can handle all these steps.
What I have are the followings:

1. Add custom build number


1
2
3
4
5
<properties>
  <build.version>2.1.90</build.version>
  <timestamp>${maven.build.timestamp}</timestamp>
  <maven.build.timestamp.format>HH:mm dd.MM.yyyy</maven.build.timestamp.format>
</properties>

2. Building a Jar file contains all dependencies


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<artifactId>maven-assembly-plugin</artifactId>
  <configuration>
   <finalName>jarfileName-${build.version}</finalName>
   <archive>
     <manifest>
       <addClasspath>false</addClasspath>
       <mainClass>sawalha.MainClassName</mainClass>
     </manifest>
     <manifestEntries>
       <Implementation-Version>${build.version}</Implementation-Version>
       <Permissions>all-permissions</Permissions>
       <Codebase>*</Codebase>
       <Implementation-Build>${project.version}</Implementation-Build>
       <Build-Date>${maven.build.timestamp}</Build-Date>
     </manifestEntries>
   </archive>
   <descriptorRefs>
     <descriptorRef>jar-with-dependencies</descriptorRef>
   </descriptorRefs>
   <appendAssemblyId>false</appendAssemblyId>
   <finalName>jarfileName-${build.version}-dep</finalName>
  </configuration>
  <executions>
   <execution>
     <id>make-assembly</id>
     <phase>package</phase>
     <goals>
       <goal>single</goal>
     </goals>
   </execution>
  </executions>
</plugin>

3. Sign the Jar file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jarsigner-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
      <id>sign</id>
      <goals>
        <goal>sign</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <keystore>sign-dir\file.p12</keystore>
    <alias>"signature alias"</alias>
    <storepass>storepass</storepass>
    <keypass>keypass</keypass>
    <storetype>pkcs12</storetype>
  </configuration>
</plugin>

4. Setup Apache Ant

    1. Download and Install ANT
    2. Download the optional Ant libraries for FTP (ant-commons-net.jar & commons-net-3.3.jar)
    3. Download the optional Ant libraries for SSH (ant-jsch-1.8.1.jar & jsch-0.1.44.jar)
    4. Create directory under the maven project (I called it ant-lib)

5. Copy the Jar file into Linux machine & Execute the Bash file


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.5</version>
  <configuration>
    <target>
      <echo message="hello ant, from Maven!" />
      <echo>Maybe this will work?</echo>
      <taskdef name="ftp"
        classname="org.apache.tools.ant.taskdefs.optional.net.FTP">
        <classpath>
          <pathelement
            location="/ant-lib/ant-commons-net.jar" />
          <pathelement
            location="/ant-lib/commons-net-3.3.jar" />
        </classpath>
      </taskdef>
      <taskdef name="sshexec"
        classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
        <classpath>
          <pathelement
            location="/ant-lib/ant-jsch-1.8.1.jar" />
          <pathelement
            location="/ant-lib/jsch-0.1.44.jar" />
        </classpath>
      </taskdef>
      <ftp action="send" server="10.20.30.40" remotedir="/deployment/java"
        userid="userName" password="password" dir="${project.build.directory}">
          <include name="jarfileName-${build.version}-dep.jar" />
        </fileset>
      </ftp>
      <sshexec host="10.20.30.40" username="userName" password="password"
        command="/home/scripts/update.fp.sh ${build.version}"
        trust="true" />
    </target>
  </configuration>
</plugin>

To execute the goal, run the following command:
>mvn antrun:run

Waiting for your questions... Have a nice day.

March 4, 2015

Java Swing Designer for eclipse

I have found a great tool for designing and developing Java Swing Applications using eclipse.