Posts

Showing posts from 2014

Error while creating hibernate session factory bean

Error Text: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream There are two probable reasons for this situation in my experience. Some of them are as follows 1. The "id" is not present in one or more mapping files (hbm.xml) 2. The DTD definition may be wrong, try replacing following with later. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

Java get human understandable time in seconds, minutes, hours & days from milliseconds.

This function returns human readable time from milliseconds. Use this in logging utilities to make sense of time as a common sense. :)  public static final String getHumanRedableTime(long timeInMs) {         String r;         long time = (int) (timeInMs / 1000);         long secs = time % 60 > 0 ? time % 60 : 0;         time = time / 60;         long mins = time % 60 > 0 ? time % 60 : 0;         time = time / 60;         long hours = time % 24 > 0 ? time % 24 : 0;         long days = time / 24;         r = String.valueOf(secs);         String s = secs > 1 ? " seconds " : " second ";         r = r + s;         if (mins > 0) {             String m = (mins > 1) ? " minutes " : " minute ";             r = mins + m + r;         }         if (hours > 0) {             String h = (hours > 1) ? " hours " : " hour ";             r = hours + h + r;         }         if (days > 0) {    

Arrange music for iTunes, build albums automatically for music files that miss album name, artist name etc.

I did come across a problem with my Iphone and other Apple devices. Songs were not grouping properly in albums, and as all know apple group songs by all other tags nut the folder you keep them in. I have large music library, and I wanted to change album name to name of folder in which the songs were contained. Below is small Java program that will help you achieving that. Just change the directory paths and enjoy. # Main Class package com.gvt; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.ArrayList; import java.util. List ; import javax.swing.filechooser.FileNameExtensionFilter; import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.AudioFileIO; import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.CannotWriteException; import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException; import org.jaudiotagger.audio.exceptions.ReadOnlyFileExcep

Get the Recurrence of Days Randomally

I faced a problem where I had to generate random picks in a week so that it  can include permutations of all days in a week, in every possible manner. Computationally it creates following possibilities, plus one more fora none condition P = 7C1 + 7C2 + 7C3 + 7c4 + 7C5 + 7C6 + 7C7    = 7 + 21 + 35 + 35 + 21 + 7 + 1    = 127 So 128 conditions including 1 none of all condition. Where nCk is computed as   n!/((n-k)!*k!) or (n (n-1) (n-2) .... (n-k+1))/ (k (k-1) (k-2)........1) So going by traditional programming it will take 128 if else conditions or switch cases to generate a random sample. This will grow exponentially if the value of 'n' increases in case the requirement changes to some thing. The following method returns a string of a possible random sample out of these 128 probabilities. Here days are marked as follows. Sunday as 0 Monday as 1 Tuesday as 2 Wednesday as 3 Thursday as 4 Friday as 5 Saturday as 6 The output would come as a rando

Eclipse - Maven project dependencies not in classpath

Image
While configuring a new maven project one may face problem "Maven project dependencies, not in classpath". There are some steps one need to take care while configuring maven project in their workspace. This error is related to workspaces abiding by JAVA 1.4, so it will be very rare that somebody finds this issue. 1. Check out the project from the repository like svn, cvs, git etc. 2. Right click on project and go to Configure > Convert to Maven Project 3. If the project is still not initialized, right-click on the project and go to project properties and go to Project Facets . 4. Apply the facets as per project, don't forget to click on Java facet. 5. Make sure that version is 1.5+ for Java. By default, many eclipse flavours take it default as Java 1.4. This causes Maven dependencies not to move in application classpath, and hence many libraries will be missing, and your project will keep throwing the compilation error. 6. If still project is not showing maven

The security hole left with JNDI for server resources

In Java world, JNDI (Java Naming and Directory Interface) is one very common method for applications to access server resources like data sources, EJB's, JMS queues, file stores etc. The security risk is that: Not only JNDI can be called by applications hosted in the same container, but also remotely. In an organization, application server admins do not take care of security risks unknowingly or neglect by assuming they or on a secure LAN. This exposes the resources to grave risk as anyone within LAN can access unauthorized data without being detected and abuse the system. ctx = null ; Properties env = new Properties (); env . put( Context . INITIAL_CONTEXT_FACTORY , "CONTEXT_FACTORY" ); env . put( Context . PROVIDER_URL , "CONTEXT_PROVIDER_URL" ); DataSource datasource = ( DataSource )initialContext . lookup( "DATASOURCE_CONTEXT_NAME" ); try { ctx = new InitialContext (env); Connection conn = datasource . getConnection(); //