Added SessionFactory shutdown hook, some code cleanup.

This commit is contained in:
neviyn 2016-03-22 20:54:02 +00:00
parent c5ac14fed1
commit ac2ddda462
6 changed files with 9 additions and 17 deletions

View File

@ -4,9 +4,7 @@ import com.google.inject.Guice;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Injector; import com.google.inject.Injector;
import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.callbacks.PlayerCallbackInterface;
import musicplayer.db.IDatabase;
import musicplayer.library.ILibrary; import musicplayer.library.ILibrary;
import musicplayer.model.Song;
import musicplayer.player.IPlayer; import musicplayer.player.IPlayer;
import musicplayer.playlist.IPlaylist; import musicplayer.playlist.IPlaylist;
import musicplayer.util.ConfigManager; import musicplayer.util.ConfigManager;

View File

@ -11,7 +11,7 @@ import musicplayer.player.IPlayer;
import musicplayer.playlist.IPlaylist; import musicplayer.playlist.IPlaylist;
import musicplayer.playlist.JTablePlaylist; import musicplayer.playlist.JTablePlaylist;
public class SwingUIModule extends AbstractModule { class SwingUIModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {

View File

@ -2,7 +2,6 @@ package musicplayer.callbacks;
import com.google.inject.ImplementedBy; import com.google.inject.ImplementedBy;
import musicplayer.PlayerGUI; import musicplayer.PlayerGUI;
import musicplayer.model.Song;
@ImplementedBy(PlayerGUI.class) @ImplementedBy(PlayerGUI.class)
public interface PlayerCallbackInterface { public interface PlayerCallbackInterface {

View File

@ -20,7 +20,7 @@ import java.util.Properties;
*/ */
public class HibernateDatabase implements IDatabase{ public class HibernateDatabase implements IDatabase{
private static SessionFactory sessionFactory; private SessionFactory sessionFactory;
/** /**
* Create a database connection. * Create a database connection.
@ -57,6 +57,8 @@ public class HibernateDatabase implements IDatabase{
.addAnnotatedClass(Artist.class) .addAnnotatedClass(Artist.class)
.addAnnotatedClass(Song.class) .addAnnotatedClass(Song.class)
.buildSessionFactory(); .buildSessionFactory();
Thread hookShutdown = new Thread(sessionFactory::close);
Runtime.getRuntime().addShutdownHook(hookShutdown);
} }
/** /**

View File

@ -29,8 +29,8 @@ import java.util.stream.Collectors;
public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInterface{ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInterface{
IDatabase database; private IDatabase database;
IPlaylist playlist; private IPlaylist playlist;
private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode(); private static final DefaultMutableTreeNode updatingNode = new DefaultMutableTreeNode();
private final AtomicBoolean libraryUpdating = new AtomicBoolean(false); private final AtomicBoolean libraryUpdating = new AtomicBoolean(false);
private final JComboBox<String> libraryDisplayType = new JComboBox<>(); private final JComboBox<String> libraryDisplayType = new JComboBox<>();
@ -182,7 +182,8 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
int selRow = libraryTree.getRowForLocation(e.getX(), e.getY()); int selRow = libraryTree.getRowForLocation(e.getX(), e.getY());
if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2 && selRow != -1) if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2 && selRow != -1)
try { try {
Object selectedItem = ((DefaultMutableTreeNode) libraryTree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent()).getUserObject(); @SuppressWarnings("ConstantConditions") Object selectedItem =
((DefaultMutableTreeNode) libraryTree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent()).getUserObject();
if (selectedItem != null) { if (selectedItem != null) {
if (selectedItem instanceof Song) { if (selectedItem instanceof Song) {
playlist.addSong((Song) selectedItem); playlist.addSong((Song) selectedItem);
@ -198,6 +199,7 @@ public class JTreeLibrary extends JPanel implements ILibrary, LibraryCallbackInt
private class LibraryTreeCellRenderer implements TreeCellRenderer { private class LibraryTreeCellRenderer implements TreeCellRenderer {
private final JLabel label; private final JLabel label;
@SuppressWarnings("ConstantConditions")
private final Icon missingIcon = new ImageIcon(JTreeLibrary.class.getClassLoader().getResource("missing.gif")); private final Icon missingIcon = new ImageIcon(JTreeLibrary.class.getClassLoader().getResource("missing.gif"));
LibraryTreeCellRenderer() { LibraryTreeCellRenderer() {

View File

@ -1,16 +1,7 @@
package musicplayer.model; package musicplayer.model;
import javax.imageio.ImageIO;
import javax.persistence.*; import javax.persistence.*;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.NoSuchElementException;
import java.util.Optional;
@Entity @Entity
public class Song implements Comparable<Song>, IDBType { public class Song implements Comparable<Song>, IDBType {