Archive

Archive for the ‘Java’ Category

Dependencies for creating a JSF (PrimeFaces) Portlet in Liferay

October 28, 2014 Leave a comment

Following are the dependencies if you are trying to create a JSF (PrimeFaces) Portlet in Liferay:

– commons-fileupload-1.2.2.jar
– commons-io-1.3.2.jar
– jboss-el-2.0.0.GA.jar
– jsf-api-2.1.21.jar
– jsf-impl-2.1.21.jar
– liferay-faces-alloy-3.2.4-ga5-SNAPSHOT.jar
– liferay-faces-bridge-api-3.2.4-ga5-SNAPSHOT.jar
– liferay-faces-bridge-impl-3.2.4-ga5-SNAPSHOT.jar
– liferay-faces-util-3.2.4-ga5-SNAPSHOT.jar
– primefaces-3.5.jar (primefaces-5.0.jar also working)

Its recommended to use exactly the same versions of these jars because of conflicting classes/issues in other versions (other versions too would be stable but i got a conflict when using different versions). This combination of JARs have been testing for creating JSF (PrimeFaces) portlets with Liferay version 6.2.10.1-ee-ga1.

Cheers and have a nice day 🙂

Advertisements
Categories: Java, JSF, PrimeFaces

How to override default PrimeFaces CSS

October 13, 2014 Leave a comment

For JSF/PrimeFaces beginners overriding the default primefaces CSS is often an issue and there are also not much answers to this question on the internet. Okay why do you want to override the default CSS..? Because of a number of reasons.. For example the default PrimeFaces components are too big and i wanted them to look like those shown in PrimeFaces showcase. So all i have to do is to override the CSS with my custom CSS.
Create a CSS file in the WebContent\resources\css folder (create the css folder). Put the following code in the CSS file:

.ui-widget,
.ui-widget .ui-widget
{
    font-size: 90% !important;
}

Now all you have to do is to include this CSS file in your JSF page by adding the following code.

<h:outputStylesheet name="css/custom-style.css" />

And thats all, now you can add any styles, classes to this CSS file and make your JSF/PrimeFaces components fancy and the way you want them to be. Cheers.

Categories: Java, JSF, PrimeFaces

Installing OpenJDK on Ubuntu

Hi All! Today i tried to install oracle jdk on ubuntu but as i hadn’t previously worked on linux/ubuntu i didn’t knew how to do it. Then i found that OpenJDK is the default implementation of JVM on Ubuntu and it could be installed with this simple command:

 sudo apt-get install openjdk-6-jdk 

after this run the following command to see if the jdk is successfully installed:

 java -version 

Cheers 🙂

Categories: Java

Generating an overly Complex String :)

February 13, 2013 Leave a comment

This is how i generated an overly “complex” string. There would be surely better implementations but this one works fine for me. 🙂 

package com.intuira.test;

import java.util.Random;

/**
 * @author Hassan Ali
 */
public class OverlyComplexString {
   
    private static String[] alphabets = {"a", "B", "c", "D", "e", "F", "G", "h", "i", "j", "k", "L", "M", "N", "O", "p", "Q", "r", "S", "T", "u", "v", "W", "x", "Y", "z"};
    private static String[] numerals = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    private static String[] specialChars = {".", ",", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", "=", "_", ":", ";", "'", "\"", "/", "<", ">"};
    private static Random random = new Random();

    
/**
 * returns an overly complex randomly generated String 😉
 */
    private static String getOverlyComplexString(int length) {
        
        StringBuilder overlyComplexString = new StringBuilder();
        
        for(int i=0; i<length; i++) {

            
            switch(random.nextInt(3)) { 
            
                case 0:
                    overlyComplexString.append(alphabets[random.nextInt(alphabets.length)]);
                    break;
                
                case 1:
                    overlyComplexString.append(numerals[random.nextInt(numerals.length)]);
                    break;
                    
                case 2:
                    overlyComplexString.append(specialChars[random.nextInt(specialChars.length)]);
                    break;
                
            }
        
        }
        
        return overlyComplexString.toString();
    }
    
    public static void main(String[] args) { 
    
        System.out.println(getOverlyComplexString(20));
        
    }
    
}
Categories: Java

System.getenv would not work until you restart the IDE (Netbeans)

September 27, 2012 Leave a comment

Today i tried to get the value of a newly created environment variable, I was using Netbeans 7.1 (windows 7), it returned null, however the variable was added. I got to console and executed the program from there, it worked fine. It took me an hour to find out the problem. The problem was that while netbeans is running and you add a new Environment Variable and then try to get it by using

System.getnv() or System.getenv(String name)

it will return null until you restart your Netbeans. Probably there might be some issue that values are not updated for the Netbeans process, so you have to restart your Netbeans (i’m not sure about Eclipse and others :/). After that you’ll get the value of the newly added environment variable. A quick tip if you are facing the same, restart your netbeans..! 🙂

Categories: Java

Connecting to Ms SQL Server 2005 using JDBC in Windows Authentication Mode

April 16, 2012 2 comments

Hi All! There are 2 modes of connecting to the Ms SQL Server
1) Windows Authentication Mode
2) SqlServer Authentication Mode

I will be discussing the Windows authentication mode here. Actually i was first using SqlServer authentication mode but that wasn’t required at the deployment site so had to work with Windows Authentication. I am discussing only from implementation point of view.

First you have to download the SqlJdbc 4.0 jar libraries (google it and you’ll find it from Microsoft). You are concerned with following two files in that downloaded SqlJdbc 4.0 folder:

a) sqljdbc4.jar
b) sqljdbc_auth.dll (please note that you will find two sqljdbc_auth.dlls for x86 and x64 so carefully choose according to your platform)

Add both of these in your class path. An important thing is here that if you are using an IDE (like eclipse which i have used here) it may not provide you with an option to add a dll to the path so a Simple Solution for that is to place the dll (sqljdbc_auth.dll) in the your JRE’s bin (i placed it at C:\Program Files\Java\jre6\bin).

Now when the configurations are done lets come to the code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class SqlServerConnect {

	/**
	 * @author Hassan Ali
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */

	public SqlServerConnect() throws ClassNotFoundException, SQLException {
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		Connection connection = DriverManager
				.getConnection("jdbc:sqlserver://localhost:1433;database=BioStar;integratedSecurity=true;");
		System.out
				.println("Connected to MS SQL 2005 using windows authentication");
	}

	public static void main(String[] args) throws ClassNotFoundException, SQLException {

		new SqlServerConnect();
	}

}

having this working example now i will explain it a bit, i’ll highlight the important points:

a) The Micrsoft JDBC driver for MS SQL server is: com.microsoft.sqlserver.jdbc.SQLServerDriver so loaded it using Class.formName()
b) The Connection String for connecting to MS Sql Server using Windows Authentication Mode is: jdbc:sqlserver://localhost:1433;database=BioStar;integratedSecurity=true;

(Note that using windows authentication you have to give no username or password simply have to mention this additional integratedSecurity=true.)

Simply copying this connection string and using it in you application as far as you change the “database=BioStar” to “database=yourDbName” would be working.
In windows authentication you don’t need to mention user name and password for connecting is because you are connecting as a windows user to your MS Sql Server instance.

I only tried to mention some important points relating the connection string and sqljdbc_auth.dll etc things because people (i too) get more confused and irritated on such issues. Any questions or comments are always welcomed. Cheers 🙂

Categories: Java

Working with System Tray in Java

November 29, 2011 Leave a comment

Just thought that this is a useful handy piece of code, so am posting it. Its a demo of how to work with the System Tray in Java. The classes of interest are

java.awt.SystemTray

and

java.awt.TrayIcon

.

package com.hassan.javasystemtray;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class SystemTrayDemo {
	
   /**
    * 
    * @author Hassan Ali
    * https://jaspirations.wordpress.com
    * http://about.me/hassan.malik
    * hassan.ali.cs@gmail.com
    */
	
	private static PopupMenu getPopMenu() { 
		
		PopupMenu popUpMenu = new PopupMenu();
		MenuItem menuItem = new MenuItem("Hello User!");
		popUpMenu.add(menuItem);
		
		return popUpMenu;
	}
	
	public static void main(String[] args) { 
		
		SystemTray sysTray = SystemTray.getSystemTray();
		
		if(!SystemTray.isSupported())
			JOptionPane.showMessageDialog(null, "System Tray is not supported on this platform");
		else { 
			
			try {
			
				sysTray.add(new TrayIcon(new ImageIcon("C:\\faces_png.png").getImage(), 
						"Java System Tray Demo", SystemTrayDemo.getPopMenu()));
			}
			catch(AWTException awtException) { 
				
				System.out.println(awtException.toString());
			}
		}
		
	}

}
Categories: Java