Package videoconverter

Source Code of videoconverter.Vigilar

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package videoconverter;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author daniel
*/
public class Vigilar implements Runnable{
    private final String rutaInicio   = "./PELICULAS";
    private final String rutaDestino  = "./PELICULAS_CONVERTIDAS";
    private Path path;
    private WatchService watchService;
    private WatchKey watchKey;
    private List<Archivos> archivos;
    private final Map<String,Archivos> cola = new HashMap<>();
    private String activo;
    private Thread ir = null;
   
    public void Vigilar() throws InterruptedException{
        activo  =  null;
       
        // Ahora vamos a comprobar que la ruta exista, y si no la creamos
        File dir;
        dir = new File(rutaInicio);
       
        if (!dir.exists()){
            System.out.println("La ruta " + rutaInicio + "no existe, vamos a crearla");
            if (dir.mkdir()){
                System.out.println("Hemos creado el directorio " + rutaInicio + " correctamente");
            } else {
                System.out.println("Error creando el directorio: " + rutaInicio + ". Ponte en contacto con el desarrollador");
            }
        }
       
        for (String list : dir.list()) {
            String ar = list.toString();
            Archivos ret = compruebaItems(ar);
            if (ret != null){
                cola.put(ar, ret);
            }
        }
       
        listado();
       
        vigilando();
    }
   
    /**
     * Método que crea el Watcher en el directorio de inicio para vigilar los cambios que se produzcan aquí.
     */
    public void vigilando(){
       
       
       
        System.out.println("Estamos vigilando: " + rutaInicio);
       
        path  = Paths.get(rutaInicio);
        try {
            watchService   = FileSystems.getDefault().newWatchService();
            watchKey     = path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        } catch (IOException e){
            System.out.println(e);
        }
       
        boolean notDone;
        notDone     = true;
       
        while (notDone){
            try {
                WatchKey claveBusqueda = watchService.poll(7, TimeUnit.DAYS);
                List<WatchEvent<?>> events = claveBusqueda.pollEvents();
                if (events.size() > 0){
                    for (WatchEvent event : events){
                        //Procesamos los eventos
                        switch (event.kind().toString()){
                            case "ENTRY_CREATE":
                                Archivos ret    = compruebaItems(event.context().toString());
                                if (ret != null){
                                    System.out.println("Añadimos " + ret.getNombre());
                                    cola.put( event.context().toString(), ret);
                                }
                                break;
                            case "ENTRY_DELETE":
                                cola.remove(event.context().toString());
                                break;
                            default:
                                break;
                        }

                        listado();
                    }
                    if (!claveBusqueda.reset()){
                        // Reseteamos la clave
                    }
                }
            } catch (    InterruptedException | NullPointerException e){
                System.out.println(e);
            }
        }
    }
   
    public void listado() throws InterruptedException{
        //System.out.println("Listamos contenidos");
        // Ahora vamos a listar los elementos
        for (Archivos ar : cola.values()){
            //System.out.println(ar.getNombre());
        }
       
        if (activo == null && cola.size() > 0){
            convertir();
        }
    }
   
    private void convertir() throws InterruptedException{
       
        if (ir == null){
            System.out.println("Vamos a convertir el siguiente");
            ir = new Thread(this);
            ir.start();
        }
    }
   
    private static void terminado() {
       
    }
   
    public Archivos compruebaItems(String ar){
        if (ar.trim().matches("^.*(mp4|avi|mkv|wmv|mpg|divx|mpeg|flv|vob)$")){
            //System.out.println(ar + " es una película");
            Video vid   = new Video(ar.trim().toString());
            return vid;
        } else if (ar.trim().matches("^.*(png|bmp|jpg|jpeg|gif|webp|tiff)$")){
            //System.out.println(ar + " es una imagen");
            Imagen im   = new Imagen(ar.trim().toString());
            return im;
        } else {
            //System.out.println(ar.trim() + " no es un tipo de fichero compatible");
            return null;
        }
    }

    @Override
    public void run() {
        String nombre;
        Archivos ar;
       
        for (Map.Entry<String, Archivos> item : cola.entrySet()){
            ar  = item.getValue();
            activo  = ar.getNombre();
            ar.convertir(ar.getNombre());
            cola.remove(item.getKey());
            activo = null;
            System.out.println("Convertido!!");
            ir = null;
            try {
                listado();
            } catch (InterruptedException ex) {
                Logger.getLogger(videoConverter.class.getName()).log(Level.SEVERE, null, ex);
            }
            return;
        }
    }
}
TOP

Related Classes of videoconverter.Vigilar

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.