• Avoiding typing password in sudo - passwordless sudo!
    Dec 7, 2009

    Edit the /etc/sudoers file with your favorite editor or just use visudo command, and then edit the line containing your username from something like

    your_username ALL=(ALL) ALL

    to

    your_username ALL=NOPASSWD: ALL

    That is it! Next time you use sudo, you should not be prompted for a password.

    Read more...
  • Using netstat to see listening ports.
    Dec 7, 2009

    Sometimes it comes handy for an application developer to see which ports are being listened to in his/her machine. To me it came handy when my GWT hosted mode was crashing and leaving ports to be still listened by some processes, so other GWT hosted mode could not start complaining that port is still in use. So here it is:

    sudo netstat --tcp --listening --programs --numeric

    or shorter

    sudo netstat -tlpn

    --numeric flag comes handy in order to not resolve host names and port number, and --programs flag makes netstat show which program exactly is listening to a given port and what PID does it have. Certain processes run in privileged mode, so this in order to see them you need to call this with sudo

    To do less typing, I made an alias for it, and put it into my ~/.bashrc file:

    alias lports='sudo netstat --tcp --listening --programs --numeric'

    Read more...
  • Using mysqldump to dump database without data and without table definitions.
    Dec 4, 2009

    mysqldump is a utility that allows us to dump mysql databases to sql files. While invoking it with default parameters, it dumps both table definitions and the content of the tables, thus data inside them. However sometime we do not with to have either of those, so here are few parameters you can pass to mysqldump in order to tell it to not dump either table definitions, or data inside them:

    mysqldump --no-create-info db_name < only_data.sql

    or

    mysqldump -t db_name < only_data.sql

    The command above will dump only the content of database db_name to the file only_data.sql without table definitions. This is useful when we already have a table definitions somewhere, and we only want to insert data later. New table definitions may also include changes like additions to columns, which will not break the procedure of importing old data.

    mysqldump --no-data db_name < only_data.sql or

    mysqldump -d db_name < only_data.sql

    The command above will dump only the table definitions, without data inside them.

    Read more...
  • Working with MySQL DATE, DATETIME, TIME and TIMESTAMP in Java
    Dec 3, 2009

    MySQL has following data types for dates and times, with their short description as defined in mysql manual

    DATE - contains an information about a date in YYYY-MM-DD format, ranging from 1000-01-01 to 9999-12-31

    TIME - represents time of the day or elapsed time. It can be in HH:MM:SS or HHH:MM:SS format.

    DATETIME - represents both date and time in YYYY-MM-DD HH:MM:SS format. It ranges from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

    TIMESTAMP - is same as DATETIME, but ranges from '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC

    On contrary, Java library for MySQL has only following classes for these datatypes: java.sql.Date, java.sql.Time and java.sql.Timestamp, and as we can see from conversion table of Java connector, both DATETIME and TIMESTAMP types are converted to java.sql.Timestamp, so java.sql.ResultSet interface has getDate(), getTime() and getTimestamp() methods.

    I also noticed that if you try to retrieve or set year, month or day from java.sql.Time, or hours and minute from java.sql.Time, you will get an IllegalArgumentException. Here are bits from Java source code:

    java.sql.Date:

    /**
     * This method is deprecated and should not be used because SQL Date 
     * values do not have a time component.
     *
     * @deprecated
     * @exception java.lang.IllegalArgumentException if this method is invoked
     * @see #setSeconds
     */
    public int getSeconds() {
        throw new java.lang.IllegalArgumentException();
    }
    
    
    /**
     * This method is deprecated and should not be used because SQL Date 
     * values do not have a time component.
     *
     * @deprecated
     * @exception java.lang.IllegalArgumentException if this method is invoked
     * @see #getHours
     */
    public void setHours(int i) {
        throw new java.lang.IllegalArgumentException();
    }

    java.sql.Time:

    /**
     * This method is deprecated and should not be used because SQL &lt;code&gt;TIME&lt;/code&gt; 
     * values do not have a date component.
     *
     * @deprecated
     * @exception java.lang.IllegalArgumentException if this
     *           method is invoked
     * @see #setDate
     */
    @Deprecated
    public int getDate() {
        throw new java.lang.IllegalArgumentException();
    }
    
    
    /**
     * This method is deprecated and should not be used because SQL &lt;code&gt;TIME&lt;/code&gt; 
     * values do not have a year component.
     *
     * @deprecated
     * @exception java.lang.IllegalArgumentException if this
     *           method is invoked
     * @see #getYear
     */
    @Deprecated
    public void setYear(int i) {
        throw new java.lang.IllegalArgumentException();
    }

    Good use of OO concepts from Sun developers I think. java.sql.Timestamp on contrary has all these methods legal, and has nanoseconds field defined in addition.

    If you want to retrieve both date and time from DATETIME data type, and it is not in the range of java.sql.Timestamp, you will have to separately retrieve java.sql.Time and java.sql.Date fields, and use them separately:

    java.sql.Date date = rs.getDate("date_time_field");
    java.sql.Time time = rs.getTime("date_time_field");
    Read more...
  • Calculating MD5 hash in Java
    Dec 2, 2009

    Searching through internet, I found many webpages pointing out the simple way of calculating an MD5 hash in a following way :

    public static String hashPassword(String password) {
        BigInteger hash = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(password.getBytes());
            hash = new BigInteger(1, md5.digest());
        } catch (NoSuchAlgorithmException nsae) {}
        return hash.toString(16);
    }

    The problem with the following code is that sometimes (when hash starts with 0) it does not output the 32 character sequence, but omits leading zeros, which can be a problem in certain applications. As an example of such has, I found a string "tintiri" (without quotes).

    Looking around a bit more, I found the following snippet which seems to work correct:

    public static String md5hash(String input){
        byte[] inputBytes = input.getBytes();
        String output = null;
             
        try{
            MessageDigest algorithm = 
                MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(inputBytes);
            byte messageDigest[] = algorithm.digest();
                   
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                hexString.append(Integer.toHexString(
                   (messageDigest[i] & 0xFF) | 0x100)
                   .toLowerCase().substring(1, 3));
            }
            output = hexString.toString();
        } catch(NoSuchAlgorithmException nsae){
            //ignore
    
        }
        return output;
    }
    Read more...
  • Setting up Blogger to run on your Google Apps administered subdomain and/or domain.
    Nov 18, 2009

    I will not go through set-up procedure of Blogger blog or Google Apps domain, because there is a lot of information about it around, and Help systems of both got plenty of resources. What I would like to go through it the procedure of linking your blog to a subdomain in your Google Apps administered domain. In order to accomplish our objective, we need make changes on both sides, so we will first configure Blogger, and then we will configure Google Apps.

    1. Configuring Blogger

      After entering you blog from Blogger dashboard, click the "Settings" tab, and then "Publishing". There you can see a domain name you can enter for your blog. Enter your full domain name there, and optionally click the "Redirect YOURDOMAIN to blog YOURBLOG". For me the blog name was blog.asgarli.net and I did not choose asgarli.net to be directed to my blog, so I left the checkbox unchecked.

    2. Configuring Google Apps

      Log in into Google https://www.google.com/a/YOUR_DOMAIN_NAME, and then go to Domain Settings menu from the menu bar above. Switch to Domain Names tab, in order to be able to log on into Godaddy's DNS console. Note that this procedure is different if you did not buy the domain name from Google itself, so this step may vary for you.

      After following link "Advanced DNS Setting", we will get the necessary credentials and an address to be able to log in to DNS configuration panel. Then we follow the "Sign in to DNS console" link, and then use the credentials from previous page in order to log in. After a successful log in, we will get the list of domains we control.

      Then we click on the domain name, we will get into control panel for that domain. From there we need to go into Total DNS control in order to be able to add a CNAME record.

      After we click "Add CNAME Record", we write our alias name in "Enter an Alias Name" field ("blog" in my case) and ghs.google.com in "Points to Host Name" field. We leave the default value of 1 hour to TTL.

    That is it! It could be possible that it will take up to 24 hours for DNS record to be saved, and website to start working, but in practice it may take only minutes.

    Here you can find additional info about DNS from Google: http://www.google.com/support/a/bin/answer.py?hl=en&answer=48090

    Read more...
  • Eclipse, Java and GWT on 64 bit Ubuntu 9.10.
    Nov 10, 2009

    I recently installed Ubuntu 9.10 an and as a Java developers had a necessity to install JVM and Eclipse on my system. I also have a need to run Google Web Toolkit (GWT) in hosted mode, so I need a 32 bit runtime, because there is no 64 bit version of GWT.

    First step would be installing 32 bit JRE, which can be done via command

    sudo apt-get install ia32-sun-java6-bin

    After that I had the problem that 64 bit Java applications, including 64 bit Eclipse did not run. By writing a small java program which would display system properties, I figured out that when 32 bit JRE is being installed, it is set as default JRE. After a bit of research I found a way to set default programs in Ubuntu, just like in Windows:

    sudo update-alternatives

    We need to set the default Java VM, so we will use command

    sudo update-alternatives --config java

    After choosing 64 bit virtual machine by default, Eclipse successfully runs. Another way would be writing an eclipse launch script, if you want to have 32 bit JVM as default, but have Eclipse running 64 bit JVM. Script would be something like this:

    #!/bin/bash
    
    PATH=/usr/lib/jvm/ia32-java-1.5.0-sun/bin:$PATH/home/username/programs/eclipse32/eclipse

    You will need to change the path to Eclipse and then chmod +x the script file, and after that you should be able to run it.

    Note that process can also be done in reverse order, means if we want to run 32 bit version of Eclipse and want to have 64 bit VM by default.

    Read more...

subscribe via RSS