Skip to main content

Create Custom CountDownLatch In Java

· One min read
Sandeep Bhardwaj
CustomCountDownLatch.java
/**
* Custom CoundDownLach implementation
* @author sandeep
*
*/
public class CustomCountDownLatch
{
int counter;

public CustomCountDownLatch(int counter)
{
this.counter = counter;
}

public synchronized void await() throws InterruptedException
{
if (counter > 0)
{
wait();
}
}

/**
* method will decrement the counter by 1 each time
*/
public synchronized void countDown()
{
counter--;
if (counter == 0)
{
notifyAll();
}
}
}

Integrate Thunderbird on Elementary OS Loki

· One min read
Sandeep Bhardwaj

Install Thunderbird using below command.

sandeep@koko:~$ sudo apt install thunderbird 

then install

sandeep@koko:~$ sudo apt install thunderbird-gnome-support  ttf-lyx

After successful installation of thunderbird install below add-ons to make Thunderbird more integrated and beautiful.

Add-ons

  1. GNotifier :- GNotifier integrates thunderbird's notifications with the native notification system.

  2. MinimizeToTray revived :- Minimizes windows into the system trayMinTrayR.

  3. Stylish :- Restyle the thunderbird css with Stylish, a user styles manager. and then install the below theme.

Change in thunderbird config

Thunderbird Preferences > Advanced > General > Config Editor then set mail.tabs.autoHide to true.

Missing Dropbox icons on Elementary OS Loki

· One min read
Sandeep Bhardwaj

After installing the Dropbox on latest Elementary OS Loki icons of dropbox is missing in wingpanel.

For fixing this issue you just need modify dropbox.desktop file.Open the dropbox.desktop file in your favourite editor.

sandeep@koko:~$ sudo gedit /usr/share/applications/dropbox.desktop 

then replace the line with Exec with below line and then restart you machine.

Exec=env XDG_CURRENT_DESKTOP=Unity QT_STYLE_OVERRIDE='' dropbox start -i

This solution worked for me and i hope it will work for you as well.

Install Gradle on Ubuntu

· One min read
Sandeep Bhardwaj
  1. Download the latest Gradle version from Gradle Download page.

  2. Extract the binaries to the desired location.

  3. Set the GRADLE_HOME environment variable.

Setting the environment variable in Ubuntu in really easy, just need to edit the /etc/profile. Just add your gradle installation directory as below.

Open the /etc/profile file in your favorite editor.

sudo gedit /etc/profile

Add below lines in end.

GRADLE_HOME=/data/dev/tools/gradle-2.10
PATH=$PATH:$GRADLE_HOME/bin
export GRADLE_HOME
export PATH
Verify the GRADLE_HOME
echo $GRADLE_HOME
Verify the Gradle version
gradle -v
Output
------------------------------------------------------------
Gradle 2.10
------------------------------------------------------------

Build time: 2015-12-21 21:15:04 UTC
Build number: none
Revision: 276bdcded730f53aa8c11b479986aafa58e124a6

Groovy: 2.4.4
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_60 (Oracle Corporation 25.60-b23)
OS: Linux 3.19.0-47-generic amd64

Set GRADLE_USER_HOME on Ubuntu

Next if you wants to override the default location of gradle local repository.

Again edit the /etc/profile like we did it above and add below line in the end.

export GRADLE_USER_HOME=/data/dev/repository/gradle

Set Maven Home on Ubuntu

· One min read

Setting the environment variable in Ubuntu in really easy, just need to edit the /etc/profile. Just add your maven installation directory as below.

Open the /etc/profile file in your favorite editor.

sudo gedit /etc/profile

Add following lines in end

M2_HOME=/data/dev/tools/apache-maven-3.3.9
PATH=$PATH:$M2_HOME/bin
export M2_HOME
export PATH

Verify the M2_HOME

echo $M2_HOME

Check the maven version

mvn -v

Output

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T22:11:47+05:30)
Maven home: /data/dev/tools/apache-maven-3.3.9
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: /usr/local/java/jdk1.8.0_60/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.19.0-47-generic", arch: "amd64", family: "unix"

Create SessionFactory in Hibernate

· One min read
Sandeep Bhardwaj

Hibernate SessionFactory in 4.x

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class Hibernate4Util {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
try {
Configuration configuration = new Configuration().configure();

serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();

// pass ServiceRegistry in buildSessionFactory method.
// buildSessionFactory() method without any argument is deprecated
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e) {
System.err.println("Error creating Session: " + e);
throw new ExceptionInInitializerError(e);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

Hibernate SessionFactory in 3.x

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Hibernate3Util {
private static SessionFactory sessionFactory;

static {
try {
Configuration configuration = new Configuration().configure();
sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException e) {
System.err.println("Error creating Session: " + e);
throw new ExceptionInInitializerError(e);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

The entity name must immediately follow the '&' in the entity reference. Blogspot template

· One min read
Sandeep Bhardwaj

Some times when we write some JavaScript on blogspot template we got this error
**"The entity name must immediately follow the '&' in the entity reference."**This error come if we have some and condition in our JavaScript like

if(document.getElementById && !document.all){  
// do something here

}

Here is the simple solution of this just replace the & with &.

if(document.getElementById && !document.all){  
// do something here

}

Post you valuable comment if this solution works for you.

BlockingQueue In Java

· One min read
Sandeep Bhardwaj
public interface BlockingQueue extends Queue

A BlockingQueue provides additionally functionality

  • wait for the queue to become non-empty when retrieving an element.
  • wait for space to become available in the queue when storing an element.

Implementing Classes:

There are 7 implemented classed of BlockingQueue

  1. ArrayBlockingQueue
  2. DelayQueue
  3. ArrayBlockingQueue
  4. LinkedBlockingDeque
  5. LinkedTransferQueue
  6. PriorityBlockingQueue
  7. SynchronousQueue

Usage of LanguageUtil class in Liferay

· One min read
Sandeep Bhardwaj

For fetching properties based on locale from properties file in liferay is easy. Use get() method of com.liferay.portal.kernel.language.LanguageUtil class of liferay. It will automatically pick the correct locale file and return the property value.

There are 6 overloaded get methods in LanguageUtil class.

get(Locale locale, String key): StringLanguageUtil  
get(Locale locale, String key, String defaultValue) StringLanguageUtil
get(PageContext pageContext, String key) StringLanguageUtil
get(PageContext pageContext, String key, String defaultValue) StringLanguageUtil
get(PortletConfig portletConfig, Locale locale, String key) StringLanguageUtil
get(PortletConfig portletConfig, Locale locale, String key, String defaultValue) StringLanguageUtil

Usage

<%@page import="com.liferay.portal.kernel.language.LanguageUtil"%>  
<%
String propertyValue=LanguageUtil.get(pageContext, "property_name", new String("NOT_FOUND"));
%>

Argument of get method :-

1st: page context.
2nd: property name whose value to be fetched.
3rd: default value return by get method, if property not found.

Custom Annotation for validating a Bean

· 2 min read
Sandeep Bhardwaj

Custom annotation for validating all the field of a class. If fields are null then display a message containing field name with error message.

Annotation

package com;  

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNullAndNotEmpty {

}

Validator class

package com;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class NotNullAndNotEmptyValidator {
public static List<String> validate(Object obj) {
List<String> errors = new ArrayList<String>();

NotNullAndNotEmpty annotations = (NotNullAndNotEmpty) obj.getClass().getAnnotation(NotNullAndNotEmpty.class);

// if annotation not found on class then return empty list
if (annotations != null) {

Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
if (field.get(obj) == null) {
errors.add(field.getName() + " is null or empty");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return errors;
}
}

Usage :- Model

package com;

@NotNullAndNotEmpty
public class Employee {
private String name;
private String address;

public Employee() {

}

public Employee(String name, String address) {
super();
this.name = name;
this.address = address;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

}

Run the test

package com;

import java.util.List;

public class Test {

public static void main(String[] args) {
Employee emp = new Employee("Sandeep", null);

List<String> errors = NotNullAndNotEmptyValidator.validate(emp);

for (String error : errors) {
System.out.println(error);
}

}
}

Output

address is null or empty