Sunday, January 25, 2015

Enabling Shared Folder Feature in Virtual Box

To be able to share a folder from host to guest VMs, you need to install Guest Additions. To install it, click on Devices menu and then Install Guest Additions... And then the setup files will be mounted to the CD-ROM drive of the guest OS. Suppose your guest OS is Windows 8 (32 bit) so you can double click on the file VBoxWindowsAddition-x86.exe to install it. After the installation has finished, you'll see your shared folder appear as a new drive if you already configured it.

Please note that if you click on Install Guest Additions... and then nothing happens, it's probably because of your virtual CD-ROM drive is already being mounted to something else. If it is, you should unmount it first.

To unmount it:
1. Go to the settings of the OS guest and click Storage
2. Select the mounted file under the virtual CD-ROM drive for example, Controller IDE.
3. Click on the disc symbol next to CD/DVD Drive combobox and then Remove disk from virtual drive 


Tuesday, January 20, 2015

Ivy HelloWorld example


What is Ivy?

Ivy is additional set of Ant task created by Apache. It's packaged as JAR file and needs to be put in $ANT_HOME/lib, just like other third-party tasks. (To create your own task, you need to create a Java class that extends org.apache.tools.ant.Task)

Ivy contains a set of tasks (resolve, cachepath, retrieve, install, publish) which works like Apache Maven phases. For example, the resolve task downloads all dependencies from Maven's public repository and stores them in ivy cache (~/.ivy2/cache directory).

Why Ivy?

One of the reasons is you are familiar with Ant and you don't want to spend more time to learn Maven.




HelloWorld Example


We are going to show how to use Ivy to compile Java HelloWorld program.

1. Downloading Ivy
Suppose you already installed Ant and then you can download Ivy from http://ant.apache.org/ivy/download.cgi

2. Installing Ivy
cp apache-ivy-2.2.0/ivy-2.2.0.jar $ANT_HOME/lib
3. Creating HelloWorld.java

Suppose we are currently in $WORKSPACE directory then create the file in the directory $WORKSPACE/src/com/vathanakmao/ivytest/ as following:
package com.vathanakmao.ivytest;

import org.apache.commons.lang.StringUtils;

public class HelloWorld {

         public static void main(String[] args) {

                 String string = StringUtils.upperCase("Hello World!");

                 System.out.println(string);

         }

}

4. Creating build.xml file
<project name="test ivy" default="resolve"
                          xmlns:ivy="antlib:org.apache.ivy.ant">

        <target name="resolve" description="resolve dependencies with ivy">
                <ivy:resolve />
                <ivy:cachepath pathid="default.classpath"/>
        </target>

        <target name="compile" depends="resolve" description="Compile">
                <mkdir dir="build/classes" />
                <javac srcdir="src" destdir="build/classes">
                        <classpath refid="default.classpath" />
                </javac>
        </target>
</project>
The resolve task () will fetch the artifacts from Maven's public repository http://repo1.maven.org/maven2 and put them in cache (~/.ivy2/cache).

The cachepath task () creates an Ant path with the given ID default.classpath, pointing to ivy cache directory consisting of the resolved artifacts. Then, the javac task will look for the artifacts in the cache because we declare it as the classpath with refid.

5. Creating ivy.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">

    <info organisation="com/vathanakmao/ivytest" module="helloworld" status="integration"></info>

    <dependencies>
         <dependency org="commons-lang" name="commons-lang" rev="2.6" />
    </dependencies>
</ivy-module>

6. Compiling
ant compile



Monday, January 19, 2015

Embedding Jetty Server: HelloWorld example

Jetty has a slogan, "Don't deploy your application into Jetty. Deploy Jetty into your application". This means that instead of bundling you application as a standard WAR deployed in Jetty, you can use Jetty server as a software component in a Java program just like any POJO.

1. Download the following JAR files:

- Jetty aggregate jar file from http://central.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.3.0.M1/jetty-all-9.3.0.M1.jar Jetty is decomposed into many different dependencies or JAR files. The aggregate JAR file contains all classes of the dependencies. It should not be used for production. All dependencies should be managed by Maven.

- Servlet API 3.0 from http://central.maven.org/maven2/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar Please note that Jetty 9 is compatible with Servlet 3.0 so it won't work with Servlete 2.5.


2. Create handler class, HelloWorld.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler {

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletExce$
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.setHandler(new HelloWorld());
        server.start();
        server.join();
    }
}

3. Compiling

> javac -cp javax.servlet-3.0.0.v201112011016.jar:jetty-all-9.3.0.M1.jar HelloWorld.java

4. Running

> java -cp .:javax.servlet-3.0.0.v201112011016.jar:jetty-all-9.3.0.M1.jar HelloWorld.java
2015-01-04 21:15:28.066:INFO::main: Logging initialized @140ms
2015-01-04 21:15:28.175:INFO:oejs.Server:main: jetty-9.3.0.M1
2015-01-04 21:15:28.248:INFO:oejs.ServerConnector:main: Started ServerConnector@7afc02c8{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2015-01-04 21:15:28.250:INFO:oejs.Server:main: Started @329ms

Open browser and type http://localhost:8080 and then you should see "Hello World".








Saturday, January 17, 2015

Parsing ISO 8601-compliant String to java.util.Date

To parse a string representation of date in ISO 8601 format, for example, 2015-01-18T01:01:01Z, you can use javax.xml.bind.DatatypeConverter class (included in JDK since version 1.6 update 4). The class is part of JAXB API. Please see JAXB for more details.

Please see the code snippet below on how to use it.
System.out.println(DatatypeConverter.parseDateTime("2014-10-15T01:01:01Z").getTime());
The parseDateTime() method returns java.util.Calendar object.



Why not use java.text.SimpleDateFormat class?

The example below works the same.
SimpleDateFormat formatter = new SimplateDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
System.out.println(formatter.parse("2014-10-15T01:01:01Z").getTime());
But the ParseException will be thrown if the date string is in another ISO 8601 format, for example, "2014-10-15T01:01:01+07:00". DatatypeConverter still works for this format and other ISO 8601 formats.





Reference
https://jaxb.java.net/tutorial/section_1_1-Introduction.html#About JAXB
https://jaxb.java.net/guide/Which_JAXB_RI_is_included_in_which_JDK_.html

Thursday, January 8, 2015

Making gray background for code block in Blogger posts

You might want to have gray background for your code block to make it more readable as below.
> echo "Hello World!"

1. Go to your Blogger dashboard and click on Template then Edit HTML.
2. Add the following code above the </head> tag and save.
<style>
pre.codeblock {
   background:#E8E8E8;
}
</style>
3. Create a new post and add the following code.
<pre class="codeblock">
> echo "Hello World!"
</pre>


Downloading and Installing Jetty

Jetty is a web server and servlet container. In 2009, it moved its core components to the projects of Eclipse Foundation, in order to broaden licensing and community of the project.


Installation

1. Download the latest distribution here http://download.eclipse.org/jetty/
2. Extract the distribution file jetty-distribution-9.2.6.v20141205.zip, for example.
3. Start Jetty:
> cd jetty-distribution-9.2.6.v20141205
> java -jar start.jar
2015-01-02 20:51:35.971:INFO::main: Logging initialized @1044ms
2015-01-02 20:51:36.063:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended.  See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html
2015-01-02 20:51:36.351:INFO:oejs.Server:main: jetty-9.2.6.v20141205
2015-01-02 20:51:36.370:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/Users/vathanakmao/workspace/jetty-distribution-9.2.6.v20141205/webapps/] at interval 1
2015-01-02 20:51:36.424:INFO:oejs.ServerConnector:main: Started ServerConnector@69121bc1{HTTP/1.1}{0.0.0.0:8080}
2015-01-02 20:51:36.425:INFO:oejs.Server:main: Started @1497ms
4. Open browser and type http://localhost:8080/ You will see error 404 page below. It means Jetty is running but there is no any default page to serve. You can check in the jetty-distribution-9.2.6.v20141205/webapps directory. It's empty. In previous versions of Jetty, there are default page included.





















Tuesday, January 6, 2015

Searching for specific Blogger posts by label


Searching by a label

In the textbox located on a top navigation bar for the page, you can type label:java to search for posts with label java.

Or you can type one of the following URLs in browser. They are equivalent.

http://myt8chnote.blogspot.com/search?q=label%3Ajava
http://myt8chnote.blogspot.com/search/label/java





Searching by multiple labels

You can search for all posts with label java and all posts with label php by typing in the textbox:

label:java|label:php

Or access the following URL in browser:

http://myt8chnote.blogspot.com/search?q=label:java|label:php








Reference

http://www.mybloggertricks.com/2014/01/17-best-search-techniques-in-blogger.html