[Tips] #1 – Firefox, Cloud Storage, Eclipse and SVN

December 29, 2013 Leave a comment

1. We can easily view a list of “about” pages of Firefox browser by entering “about:about” on address bar. (Google Chrome also)

2. I usually backup my data in “cloud” as Google Drive, Drop box and Ubuntu One. We install and use them on local computer conveniently!

3. I have a problem with workspace of Eclipse, I try to delete my workspace from external folder (for example, the folder is named “myworkspace” ) but I restart Eclipse the folder also is created automatically. We can solve problem by setting shutdown workspace on Eclipse.

Window > Preferences > General > Startup and Shutdown. Remove it!

4. It is nice way by using Google Code to storage our code. With my google account, I set up a SVN server to manage code of my projects.

See video tutorial here: http://www.youtube.com/watch?v=FwGJZcCj1Yo

 

Categories: Tips

[Java] Initial size for the arraylist

December 5, 2013 Leave a comment

For example:


ArrayList<String> row = new ArrayList<String>(numberOfColum);

for (int j = 0; j < numberOfColum; j++) {
row.add("");

}

Categories: Java

[Solved] No device drivers were found – Windows 7 install from USB Boot

November 25, 2013 Leave a comment

The solution was as simple as switching the external hard drive from a USB 3.0 port to a USB 2.0 port, rebooting, and restarting the installation.

Categories: Windows

Step by step – Spring Hibernate JSF and Primefaces Integration

November 25, 2013 4 comments

Please download the attached document and source code.

Document: Spring_Hibernate_JSF_Primefaces_Intergration

Source code: https://www.dropbox.com/s/ypkfbdi6pmi50to/myteam.zip

Categories: Web App

Redirect HTTP to HTTPS using Servlet Filter

November 25, 2013 Leave a comment

1. Write a filter to redirect from http to https


package vn.nvanhuong.servlet;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HTTPSFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws java.io.IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

String uri = req.getRequestURI();
String getProtocol = req.getScheme();
String getDomain = req.getServerName();
String getPort = Integer.toString(req.getServerPort());

if (getProtocol.toLowerCase().equals("http")) {

// Set response content type
response.setContentType("text/html");

// New location to be redirected
String httpsPath = "https" + "://" + getDomain + ":" + getPort
+ uri;

String site = new String(httpsPath);
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
res.setHeader("Location", site);
}

// Pass request back down the filter chain
chain.doFilter(req, res);

}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

2. Compile above filter and create following entries in web.xml


<!-- HTTPS Redirect -->
<filter>
<filter-name>HTTPS</filter-name>
<filter-class>vn.nvanhuong.servlet.HTTPSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HTTPS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

References
[1]. http://www.tutorialspoint.com/servlets/servlets-page-redirect.htm
[2]. http://www.easywayserver.com/blog/jsp-get-url/

Categories: Servlet Tags:

[Java] Compare two list

November 15, 2013 Leave a comment

Problem:

I have two classes are Employee and Department


class Department{
private Long deptId;
private String deptName;

public Long getDeptId() {
return deptId;
}
public void setDeptId(Long deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}

class Employee{
private Long empId;
private String empName;
private Department dept;

public Employee() {
super();
}

public Employee(Long empId, String empName, Department dept) {
super();
this.empId = empId;
this.empName = empName;
this.dept = dept;
}
public Long getEmpId() {
return empId;
}
public void setEmpId(Long empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}

}

And, I write  a class for compare two list with data from Employee class: ListComparisonTest


package tdd.sprint10;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.*;

public class ListComparisonTest {

private Department dept;

public ListComparisonTest() {
}

@Before
public void setUp() throws Exception {
dept=new Department();
}

@After
public void tearDown() throws Exception {
}

@Test
public void testCompareList() {
Employee emp1 = new Employee();
emp1.setEmpId(1L);
emp1.setEmpName(&quot;Van Huong&quot;);
emp1.setDept(dept);

Employee emp2 = new Employee();
emp2.setEmpId(2L);
emp2.setEmpName(&quot;Xuan Vinh&quot;);
emp2.setDept(dept);

List&lt;Employee&gt; firstList = new ArrayList&lt;Employee&gt;();
firstList.add(emp1);
firstList.add(emp2);

List&lt;Employee&gt; secondList = new ArrayList&lt;Employee&gt;();
secondList.add(new Employee(1L,&quot;Van Huong&quot;,dept));
secondList.add(new Employee(2L,&quot;Xuan Vinh&quot;,dept));

assertEquals(firstList.size(), secondList.size());
assertTrue(firstList.containsAll(secondList));
}

}

It’s fail when I  run the test class with JUnit Test!

The solution: create hashCode() and equals() functions on Employee class

On Eclipse IDE, press Alt + Shift + S, chose  generate and hashCode() and equals() functions.


@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dept == null) ? 0 : dept.hashCode());
result = prime * result + ((empId == null) ? 0 : empId.hashCode());
result = prime * result + ((empName == null) ? 0 : empName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (dept == null) {
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (empId == null) {
if (other.empId != null)
return false;
} else if (!empId.equals(other.empId))
return false;
if (empName == null) {
if (other.empName != null)
return false;
} else if (!empName.equals(other.empName))
return false;
return true;
}

Categories: Java

[Spring + Hibernate] Configure connection pool with Spring based – c3p0, Apache DBCP and BoneCP

November 11, 2013 1 comment

Application Context

1. Without connection pool


<!-- Data Source Declaration -->
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>

2. c3p0


<!-- C3P0 Connection Pool -->
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />

<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />
</bean>

3. Apache DBCP


<!-- DBCP configuration -->
<bean id="dbcp" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="removeAbandoned" value="true" />
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>

4. BoneCP


<!-- BoneCP configuration -->
<bean id="boneCP" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="60" />
<property name="idleMaxAgeInMinutes" value="240" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
</bean>

and session factory declaration…


<!-- Session Factory Declaration -->
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" /> <!-- without connection pool -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.default_schema">myagile</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

Reference:
[1]. http://jolbox.com/
[2]. http://www.javacodegeeks.com/2012/04/jsf-2-primefaces-3-spring-3-hibernate-4.html
[3]. http://www.mchange.com/projects/c3p0/
[4]. http://www.mkyong.com/hibernate/how-to-configure-the-c3p0-connection-pool-in-hibernate/

Categories: Web App