import javax.naming.InitialContext;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
/**
* @author hennebrueder This class garanties that only one single SessionFactory
* is instanciated and that the configuration is done thread safe as
* singleton. Actually it only wraps the Hibernate SessionFactory.
* When a JNDI name is configured the session is bound to to JNDI,
* else it is only saved locally.
* You are free to use any kind of JTA or Thread transactionFactories.
*/
public class InitSessionFactory {
/**
* Default constructor.
*/
private InitSessionFactory() {
}
/**
* Location of hibernate.cfg.xml file. NOTICE: Location should be on the
* classpath as Hibernate uses #resourceAsStream style lookup for its
* configuration file. That is place the config file in a Java package - the
* default location is the default Java package.
*
* Examples:
*CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* initialises the configuration if not yet done and returns the current
* instance
*
* @return
*/
public static SessionFactory getInstance() {
if (sessionFactory == null)
initSessionFactory();
return sessionFactory;
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
*SessionFactoryif needed.
*
* @return Session
* @throws HibernateException
*/
public Session openSession() {
return sessionFactory.getCurrentSession();
}
/**
* The behaviour of this method depends on the session context you have
* configured. This factory is intended to be used with a hibernate.cfg.xml
* including the following propertyThis would return
* name="current_session_context_class">thread
* the current open session or if this does not exist, will create a new
* session
*
* @return
*/
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
/**
* initializes the sessionfactory in a safe way even if more than one thread
* tries to build a sessionFactory
*/
private static synchronized void initSessionFactory() {
/*
* [laliluna] check again for null because sessionFactory may have been
* initialized between the last check and now
*
*/
Logger log = Logger.getLogger(InitSessionFactory.class);
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
String sessionFactoryJndiName = cfg
.getProperty(Environment.SESSION_FACTORY_NAME);
if (sessionFactoryJndiName != null) {
cfg.buildSessionFactory();
log.debug("get a jndi session factory");
sessionFactory = (SessionFactory) (new InitialContext())
.lookup(sessionFactoryJndiName);
} else{
log.debug("classic factory");
sessionFactory = cfg.buildSessionFactory();
}
} catch (Exception e) {
System.err
.println("%%%% Error Creating HibernateSessionFactory %%%%");
e.printStackTrace();
throw new HibernateException(
"Could not initialize the Hibernate configuration");
}
}
}
public static void close(){
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;
}
}
Wednesday, January 30, 2008
Hibernate Context Class File
Monday, January 21, 2008
MP3 Tag lib (JID3 0.5.4) Saving ID3V2 Tags
Updating ID3V2 tags require to use frames (AbstractID3V2Frame) for each attribute and need to save the mp3File as many times as there are attributes.
1.Title (SongTitle) - FrameBodyTIT2
AbstractID3v2Frame titleFrame = id3v2.getFrame("TIT2");
if(titleFrame != null){
((FrameBodyTIT2)titleFrame.getBody()).setText(title);
}
else{
titleFrame = new ID3v2_4Frame(new FrameBodyTIT2((byte) 0, title));
id3v2.setFrame(titleFrame);
}
mp3File.setID3v2Tag(id3v2);
mp3File.save();
2.Album - FrameBodyTALB;
3.Artist - FrameBodyTPE1;
4.Composer - FrameBodyTCOM;
5.Year - FrameBodyTYER;
6.Track - FrameBodyTRCK;
7.Genre - FrameBodyTCON;
8.Copyright - FrameBodyTCOP;
9.Url - FrameBodyWXXX;
10.Encoded By - FrameBodyTENC;
11.Comment - FrameBodyCOMM;
12.Original Artist - FrameBodyTOPE;
/*While updating the id3 tag*/
While updating the id3 tag a file with a .original.mp3 extension of the same name is created.
The way i tackled this problem was by deleting this additionally created file by finding the file with .original extension to it.
1.Title (SongTitle) - FrameBodyTIT2
AbstractID3v2Frame titleFrame = id3v2.getFrame("TIT2");
if(titleFrame != null){
((FrameBodyTIT2)titleFrame.getBody()).setText(title);
}
else{
titleFrame = new ID3v2_4Frame(new FrameBodyTIT2((byte) 0, title));
id3v2.setFrame(titleFrame);
}
mp3File.setID3v2Tag(id3v2);
mp3File.save();
2.Album - FrameBodyTALB;
3.Artist - FrameBodyTPE1;
4.Composer - FrameBodyTCOM;
5.Year - FrameBodyTYER;
6.Track - FrameBodyTRCK;
7.Genre - FrameBodyTCON;
8.Copyright - FrameBodyTCOP;
9.Url - FrameBodyWXXX;
10.Encoded By - FrameBodyTENC;
11.Comment - FrameBodyCOMM;
12.Original Artist - FrameBodyTOPE;
/*While updating the id3 tag*/
While updating the id3 tag a file with a .original.mp3 extension of the same name is created.
The way i tackled this problem was by deleting this additionally created file by finding the file with .original extension to it.
Subscribe to:
Posts (Atom)