Secure SQL connection – SSH Tunnel

Having a central database can be really useful for team projects, developers can develop their local code pointing directly to the same data, this way if the database is refreshed or if the schema is changed they would see the changes instantly.
One problem with a central database is having a secure connection to the clients and the server, specially when the remote database server is hosted outside the local network. To solve the security problem we can use a SSH tunnel between the clients(developer machines) and the remote database server. Another advantage of SSH tunnel is that you don’t need to allow remote access to the database which makes the database access more restricted and secure.
When this is done your local development environment will access the remote database similar to accessing a local database.
Modifying startup services in Ubuntu
By default when services like Mysql or Apache are installed in Ubuntu by default they are being set to autostart when the operating system starts. If you are installing the new service on a sever then you would probably want it to start on system boot, but you may not want to start all of the services when you start up your machine specially if you are using an old laptop.
The way that the startup services are managed in Debian based operating systems is that during the boot system searches for the start-up scripts in the /etc/rcX.d (X is the run level number). Depending on the current run level it looks for service links in /etc/rcX.d folders. Every entry in rcX.d points to another script in the /etc/init.d which is the script starting up that service.
To disable a start-up service you would have to remove the links in the /etc/rcX.d folders pointing to the /etc/init.d folder. To do this you have Two options, you can:
- Either do it the hard way, which is removing the links manually.
- Use a tool called update-rc.d that does the job automatically.
Java Generics
Generics were added to Java in J2SE5.0. They allow “a type or method to operate on objects of various types while providing compile-time type safety.”
The initial Idea behind Java generics was to reduce run-time type casting exceptions. The following is an example of how Generics were added to the ArrayList class to avoid such exceptions.
List v = new ArrayList();
v.add("test");
Integer i = (Integer)v.get(0); // Run time error
The Class Cast Exception is thrown when this block of code is accessed during the run-time. As you can see the code declares an ArrayList, String “test” is added to the arrayList, but since the type of objects stored in the ArrayList is not declared the compiler doesn’t see any problem with it. That is why the run-time exception is thrown when the retrieved object from the list is being cast to an Integer.
To avoid such such run-time exceptions Generics were added to the arrayList class. Using Generics when developers declare an ArrayList they have the option of declaring the type of objects being stored in the list.
Using Java Generics the above code would become:
List<String> v = new ArrayList<String>();
v.add("test");
Integer i = v.get(0); // (type error) Compile time error
As you can see in the second example since we are clearly defining the type of objects inside the array list the compiler can detect the casting exception in the code when the code is being compiled. There are many other benefits in using generics, it simplify the code, it makes it easier to read and to understand.
Today Generics are being used widely by Java developers, you may have been using them without noticing them.
Internet Explorer 9

IE9 test driver is out! I was so excited to read this new today. This is a great news for IE users, because it should provide a better security and speed comparing to previous versions of IE and because it does have more features and a better looking interface.
I believe that the people who would benefit from this most of all are developers like me! This should force more users to upgrade and hopefully get rid of damn IE6 once and for all! This would eventually mean less hacks on the front-end trying to make the pages in IE6 look like Google Chrome and Firefox.
Read MoreXubuntu – Time Synchronization
In xubuntu (XFCE) you can run the following command to synchronize your system clock with time servers:
sudo ntpdate pool.ntp.org
After running this command you would get a response similar to the following:
21 Aug 04:08:20 ntpdate[4824]: adjust time server 209.167.68.100 offset 0.017300 secRead More
Struts2 Iterating through a List of List
This is a simple example demonstrating how to use the iterator tag in struts2 to iterate through a list of objects which contains another list of objects.
Assume that we have a class called City which contains a list of phone numbers:
public class City {
private String name;
private List<PhoneNumber> phoneNumbers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}
Read More
Plain-text encryption/decryption in Java
There could be cases in which you are planning to keep some sensitive data in your database but you do not wish to have this information available to anyone (not even the DBA should be able to see the content) except the specific user. To do this you can encrypt the sensitive plan-text provided by the user. Save the encrypted message in the database and decrypt it anytime the user would like to view/edit the data. Doing this is quite easy with java. You would just need to add a few jar files to your project and a few lines of code to encrypt and decrypt the data.
The libraries you need to download are available at: http://www.jasypt.org/
The following sample code encrypts and decrypts a String. Please not that I have created an object of BasicTextEncryptor twice the reason for that is to show that you would need to set the correct password used in the encryption process to decrypt the plain-text message.
import org.jasypt.util.text.BasicTextEncryptor;
public class Secret {
public static void main(String[] args) {
String userPassword="PassWord";
String message = "Some text which is to be encrypted by the user password. Later it is decrypted to the original plain-text using the same password.";
//Encrypt
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(userPassword);
String myEncryptedText = textEncryptor.encrypt(message);
System.out.println("Encrypted text:\n"+myEncryptedText);
//Decrypt
BasicTextEncryptor textDecryptor = new BasicTextEncryptor();
textDecryptor.setPassword(userPassword);
String plainText = textDecryptor.decrypt(myEncryptedText);
System.out.println("Decrypted text:\n"+plainText);
}
}
Read More