git- saving username password - credential.helper cache - how to

Isn't is interesting if there is an option in GIT to save your credentials for short time so that you don't enter your username/password repeatedly each time when you do pull/push? Yes! there is an option introduced in GIT since 1.7.9 (released on January 2012). This method is more secure than permanently saving your username/password in   .git  file of your git clone directory.

So, run the following command at the GIT console (git bash) :
git config --global credential.helper cache
This caches the credentials for 15 minutes ( 900 seconds) by default. If you want a longer timeout period (say3600 seconds), you can do the following :
git config --global credential.helper 'cache --timeout=3600'

Grails - beginners video tutorial


From the Grails site: "Grails aims to bring the 'coding by convention' paradigm to Groovy. It's an open-source web application framework that leverages the Groovy language and complements Java Web development. You can use Grails as a standalone development environment that hides all configuration details or integrate your Java business logic. Grails aims to make development as simple as possible and hence should appeal to a wide range of developers not just those from the Java community."

grails- add jar to lib folder - not working - solution

In grails application you can add jar dependencies by just pasting the .jar file to lib folder. If your code doesn't find the jar dependency at runtime then you can do following :

Run the following grail command (s):
  • clean
  • compile --refresh-dependencies 
In eclipse you can open the Grails Command Prompt by :
Right Click on project -> Grail Tools -> Open Grails Command Prompt

Hope this helps.

nepali english date conversion logic - working java code


My friend Manoj has written about Nepali-English date conversion in this post. Take a look :

http://forjavaprogrammers.blogspot.com/2012/06/how-to-convert-english-date-to-nepali.html

He has explained the algorithm in detail about how to convert English dates into Nepali dates with java code.

android media player - play file http rtsp streams

MediaPlayer class (android.media.MediaPlayer ) can be used to control playback of audio/video files and streams.  

Code:

String url = "http://........"; // your media URL here
//String url = "rtsp://........"; 
//String url = "file:///sdcard/intro.3gp"; //local file

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url); //Sets the data source (file-path or http/rtsp URL) to use

To play audio file, you can simply call mediaPlayer.start();

mediaPlayer.prepare()
mediaPlayer.start();

For the Audio Stream (http, rtsp)

After setting the datasource and the display surface, you need to either call prepare() or prepareAsync().
For streams, you should call prepareAsync(), which returns immediately, rather than blocking until enough data has been buffered.

A MediaPlayer object must first enter the Prepared state before playback can be started. MediaPlayer.OnPreparedListener defines Interfaces  for a callback to be invoked when the media source is ready for playback.


mediaPlayer.prepareAsync();
//You can show progress dialog here untill it prepared to play

mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            //Called when the media file is ready for playback.
            mp.start();
        }
    });
    mediaPlayer.setOnErrorListener(new OnErrorListener() {
        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }
    });


java spring - read properties file variable from xml -PropertyPlaceholderConfigurer

Java springframework xml configuration file - how to read properties file variables from spring xml :
We have to use PropertyPlaceholderConfigurer bean for this.

1).properties file location -

  • src/main/resource @ maven managed project
  • OR at classpath

2)The xml code to initialize/read properties file, 

hibernate annotation inheritance mappedsuperclass - common columns in super class

When you are using annotations for hibernate object relational mapping, there might be the case that we need to abstract out some common columns that goes into all table definitions. These columns might be ID, DFlag, LastModifiedDate etc..
In such case we can take advantage of @MappedSuperclass annotation to achieve inheritance in hibernate annotation.

Example :

Super class BaseTable that contains common column definitions:

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "dflag")
    private int dFlag;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
    
    //other required columns
....
}

Extending it to use in other tables :


@Entity
@Table(name = "LoginUser")
public class LoginUser extends BaseTable  implements Serializable{

    private static final long serialVersionUID = -1920053571118011085L;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "invalidCount")
    private int invalidCount;
    
    //other required tables
   ...
}


It works !

mysql hibernate unicode support - character set, collate

I just did following configurations to achieve Unicode support in my Java+Hibernate+MySQL project.

Configuring MySQL to support Unicode - set Character Set and Collate as :
CREATE TABLE YOUR_DB_NAME
 CHARACTER SET "UTF8"
 COLLATE "utf8_general_ci";

NOTE : You Need to do "ALTER TABLE" instead of "CREATE TABLE", 
      if you are going to modify existing DB.

Hibernate JDBC connection string :
jdbc.url=jdbc:mysql://localhost:3306/YOUR_DB_NAME?useUnicode=true&characterEncoding=UTF-8

Hibernate Configuration:
<hibernate-configuration>
<session-factory>
    ...
    <property name="hibernate.connection.charSet">UTF-8</property>
    <property name="hibernate.connection.characterEncoding">UTF-8</property>
    <property name="hibernate.connection.useUnicode">true</property>
    ...
</session-factory>
</hibernate-configuration>

Java code to find public IP address (servlet and client side code)


Java code to find public IP address :

URL url= new URL("http://gt-tests.appspot.com/ip");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String ip = in.readLine();
System.out.println("IP : "+ip);

I created a simple servlet app on google app engine  and posted at http://gt-tests.appspot.com/ip .

The servlet code returns the public address of client, it looks like :

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  PrintWriter out = resp.getWriter();
  // Get client's IP address
  String addr = req.getRemoteAddr();
  out.println(addr);
  ...

Java Script code that disables right click and prevents selecting the text on web page.

Java Script code that disables right click and selecting the text on web page.
Put the following code in your <body>........</body> tag.
<script>
function disableselect(e){return false;}
function reEnable(){return true;}
document.onselectstart=new Function (){return false;}
if (window.sidebar){
    document.onmousedown=disableselect;
    document.onclick=reEnable;
}
</script>

<script>
document.oncontextmenu = function(){return false;}
if(document.layers) {
    window.captureEvents(Event.MOUSEDOWN);
    window.onmousedown = function(e){
        if(e.target==document)
        return false;
    }
}else {
    document.onmousedown = function(){return false;}
}

For Blogger template,

  • Search for </body> in the template code
  • paste the above script just before </body> tage.
Enjoy !
You are now safe from website article thief.