JavaChap Blog Java and Technology musings for the masses

24Apr/130

How to Alias a Host Name on Mac OS X

I Recently moved from Windows to Mac. It wasn't easy. While i'm still getting used to Mac, I decided to document certain things so that it might be useful for other Mac newbies.

In this blog i will explain how to alias a host name on Mac.

All host aliases are configured in a file /etc/hosts. So lets open that file.

  sudo nano /etc/hosts  

Now lets add a new host alias

cvs.javachap.com   192.168.0.100

Save the file by pressing Ctrl + O (^O)

To make sure host is configured properly, we can issue a ping command to the newly configured host.

ping cvs.javachap.com

Just incase if doesn't work, we can flush the cache using dscacheutil

sudo dscacheutil -flushcache

Voila, there you go, you have a new host alias configured.

Tagged as: , No Comments
3Nov/120

How I Fixed Battery Life On my Mac Book Pro with Retina Display

After upgrading to Mountain Lion, Battery life of my Mac Book Pro with Retina Display has gone worst. Max that i'm getting is around 4 hours. So today i have decided to fix it. According to the Apple Support Forums we can do fix few things that we can do on software side to fix the battery life.

Delete Corrupted Settings File

Many people have commented that this method has worked and this is a very method and doesn't need a reboot either. Type in the following commands in your terminal.

cd ~/Library/Preferences/
rm com.apple.desktop.plist
killall Dock

Clear PRAM/NVRAM

  • Shut down your Mac.
  • Locate the following keys on the keyboard: Command (⌘), Option, P, and R. You will need to hold these keys down simultaneously in step 4.
  • Turn on the computer.
  • Press and hold the Command-Option-P-R keys before the gray screen appears.
  • Hold the keys down until the computer restarts and you hear the startup sound for the second time.
  • Release the keys.

Reset SMC

  • Shut down the computer.
  • Plug in the MagSafe power adapter to a power source, connecting it to the Mac if its not already connected.
  • On the built-in keyboard, press the (left side) Shift-Control-Option keys and the power button at the same time.
  • Release all the keys and the power button at the same time.
  • Press the power button to turn on the computer.  
    Note
    : The LED on the MagSafe power adapter may change states or temporarily turn off when you reset the SMC.

I did all the above and now i get 8 hours of battery.

Tagged as: , , No Comments
20Oct/120

Space shuttle Endeavour’s trip through the streets of LA

Mission 26 The Big Endeavour from Givot on Vimeo.

Filed under: General No Comments
15Oct/120

Script to Backup MySQL and CVS to Amazon S3

Here is a simple script that i wrote to backup mysql and cvs root folder to Amazon S3. For the script to work you need to have S3Sync to be in path.

#!/bin/bash

# Database Configurations
BUCKET=backup.javachap.com
DB_USER=backup
DB_PWD=readonly
NOW=$(date +_%m_%d_%y)

S3_BACKUP=/opt/s3backup
CVSROOT=/usr/local/cvsroot
CVS_BACKUP_NAME=jc_cvs_backup$NOW.tar.gz
MYSQL_BACKUP_NAME=jc_mysql_backup$NOW.sql.gz

cd $S3_BACKUP

# tar the cvsroot folder
tar czf $CVS_BACKUP_NAME $CVSROOT

# list MySQL databases and dump each
DB_LIST=`mysql -u $DB_USER -p"$DB_PWD" -e'show databases;'`
DB_LIST=${DB_LIST##Database}
for DB in $DB_LIST;
do
  FILENAME=mysql_${DB}${NOW}.sql.gz
  mysqldump -u $DB_USER -p$DB_PWD --opt --flush-logs $DB | gzip > $FILENAME
done

tar czvf $MYSQL_BACKUP_NAME mysql*
rm -f mysql*

cd /opt/s3sync
ruby s3sync.rb -r -v -s --exclude="archieves$|s3backup.sh" /opt/s3backup/ $BUCKET:

mv /opt/s3backup/*.gz /opt/s3backup/archieves/

15Nov/110

6th Grader and iPhone Application Developer

Not sure which makes me feel like more of a slacker, his mutliple apps in the App Store or his excellent public speaking

Filed under: General No Comments
1Sep/110

The 5 Biggest Ways to Boost MySQL Scalability

Sean Hall with some really good MySQL advice:

  1. Tune those queries. Biggest bang for your buck. Enable the slow query log and watch it. Once you've found a heavy resource intensive query, optimize it! Tune what receives real-world traffic.
  2. Employ Master-Master Replication. Immediately have a read-only slave for your application to hit as well.
  3. Use Your Memory. Set innodb_buffer_pool_size, key_buffer_size and other key options.
  4. RAID Your Disk I/O. Use RAID 10 mirroring and striping. On EC2 striping across a number of EBS volumes using the Linux md software raid.
  5. Tune Key Parameters. speeds up inserts & updates with innodb_flush_log_at_trx_commit=2; create a tablespace and underlying datafile for each table with innodb_file_per_table.

Full details at the original article.

11Jul/116

Enumeration Mapping in Hibernate

In this simple tutorial i will show you, how to map a Java 5 Enumeration (enum) to a varchar column in Hibernate. The use-case that i'm going to demonstrate is very simple, I have a User class which has a reference to an Enumeration Gender.

Gender Enumeration
package com.javachap.domain.user;
/**
 * Male or Female. Used to customize Account messages (e.g. he or she usage) and
 * to choose the default profile picture.
 */
public enum Gender {
    Male, Female;
}
User Domain:
package com.javachap.domain.user;

public class User {

    private String firstName;
    private String lastName;
    private Gender gender;
    private String email

    /**
     * Gets the <code>User</code>'s firstName
     *
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the <code>User</code>'s firstName
     *
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Gets the <code>User</code>'s lastName
     *
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets the <code>User</code>'s lastName
     *
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Gets the <code>User</code>'s email
     *
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets the <code>User</code>'s email
     *
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
}
Hibernate Mapping (HBM):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="user" name="com.javachap.domain.user.User">

    <id name="uid" column="usr_uid">
      <generator class="increment"/>
    </id>

    <property name="firstName" type="java.lang.String" column="usr_first_name"/>

    <property name="lastName" type="java.lang.String" column="usr_last_name" />

    <property name="gender" column="usr_gender" not-null="true">
	  <type name="org.hibernate.type.EnumType">
         <param name="enumClass">com.javaachap.domain.user.Gender</param>
         <param name="type">12</param>
      </type>
    </property>

    <property name="email" type="java.lang.String" column="usr_email"/>

  </class>
</hibernate-mapping>

By default Hibernate persists the enumeration value as integer in the database when type param is not specified, In order to store the enum display name, we use a param type, which should be pointed to value in java.sql.Types. Since we want to store the enum display name in database we have mapped it to 12 which is equivalent to java.sql.Types.VARCHAR.

Hibernate Annotation Based Mapping:

If you prefer convention over configuration, below is the way to map an enum using annotation.

public class User {

   @Enumerated(EnumType.STRING)
   Gender gender

}
10Jul/110

Java 7′s Fork/Join Framework

Eric Bruno:

Fork/Join is an enhancement to the ExecutorService implementation that allows you to more easily break up processing to be executed concurrently, and recursively, with little effort on your part. It's based on the work of Doug Lea, a thought leader on Java concurrency, at SUNY Oswego. Fork/Join deals with the threading hassles; you just indicate to the framework which portions of the work can be broken apart and handled recursively. It employs adivide and conquer algorithm that works like this in pseudocode (as taken from Doug Lea's paper on the subject):

http://drdobbs.com/blogs/java/231000556

4Jul/110

Understanding Java Weak References

Ethan Nicholas :

I'm sure some of you are grumbling by now, as I'm talking about an API which is nearly a decade old and haven't said anything which hasn't been said before. While that's certainly true, in my experience many Java programmers really don't know very much (if anything) about weak references, and I felt that a refresher course was needed. Hopefully you at least learned a little something from this review.

weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself.

http://weblogs.java.net/blog/2006/05/04/understanding-weak-references

16May/110

Learning by Doing

Filed under: General No Comments