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.

facebook security bug - change password of a active user - without knowing original password

change password of a active user -without knowing original password - security bug - Facebook allows to change password in active login without entering current password

As of May 2012, Facebook has over 900 million active users. Security and privacy should be the number one concern of Facebook Inc. But I just found one BUG in Facebook security system.

This might (not) be a security bug in Facebook. And probably be fixed by Facebook when you tried to do the same, because I am going to report this to Facebook.

All the steps below that I am going to share - deals with changing someone else’s password without entering their previous/current password. I have never seen or write code for “login preference change” that allows to change password without entering previous password or other information.  I was shocked to know that Facebook allows it. I was just playing with Security option in Facebook’s Account setting https://www.facebook.com/settings. And found that.

Steps that I followed :

eclipse proguard maven project configuration - java obfuscate

I am going to describe how can can configure proguard and maven to obfuscate a java project. If you need help on how to configure maven project in eclipse see my earlier post.

A)Project configs
    <!-- Project configs -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.gt</groupId>
    <artifactId>maven-proguard-test</artifactId>
    <packaging>jar</packaging>
    <version>-ver.01</version>
    <name>maven-proguard-test-application</name>

    <properties>
        <project.build.mainClass>com.gt.App</project.build.mainClass>
    </properties>