Package letweb.semanticum.aes

Source Code of letweb.semanticum.aes.AudioDownloader$ThreadCallDownload

package letweb.semanticum.aes;

//JAVA
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; //UIMA
import java.util.concurrent.Future;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.StringArray;
import org.apache.uima.resource.ResourceInitializationException; //Type Descriptor
import org.apache.uima.util.Level;

import letweb.semanticum.tsd.Audio;
import letweb.semanticum.tsd.LinkType; //Personal Class
import letweb.semanticum.aes.AudioVideoProcessing;

public class AudioDownloader extends JCasAnnotator_ImplBase {
 
  public static final String tempPath = "tempPath";
 
  private String tp;
 
  //Classe privata per gestire un thread con interfaccia Callable
  //per restituire il path del download
  private class ThreadCallDownload implements Callable<String> {
   
    private String uri;
    private String sourceType;
    private String audioDir;
   
    public ThreadCallDownload(String uri, String sourceType, String audioDir){
      this.uri = uri;
      this.sourceType = sourceType;
      this.audioDir = audioDir;
    }
   
    @Override
    public String call() throws IOException, InterruptedException {
      return AudioVideoProcessing.audioDownloadEndExtraction(sourceType, uri, audioDir);
    }
   
  }

  @Override
  public void initialize(UimaContext aContext) throws ResourceInitializationException {
    super.initialize(aContext);
   
    tp  = (String) aContext.getConfigParameterValue(tempPath);
   
    if(!tp.endsWith(File.separator))
      tp = tp + File.separator;
   
  }

  @Override
  public void process(JCas aJCas) throws AnalysisEngineProcessException {
   
    Audio audio = new Audio(aJCas);
   
    // Prelievo dell'array di link dal aJCas
    Iterator linkIT = aJCas.getAnnotationIndex(LinkType.type).iterator();
    LinkType link = (LinkType) linkIT.next();
    // LinkType uri=null;
    // StringArray tempPath=null;
    // int i=0;
    // FSIndex linkT = aJCas.getAnnotationIndex(LinkType.type);
   
    // Assegnazione dell'array di link alla variabile locale
    StringArray videoUri = link.getArrayVideoUri();
   
    //Log degli uri dei video
    int i;
    for(i = 0; i < videoUri.size(); i++)
      getContext().getLogger().log(Level.WARNING, videoUri.get(i));
   
    // Ciclo per il download dei singoli video
    // Thread download = null;
    // for(i=0; i<videoUri.size(); i++){
    // //Istanziazione dei thread
    // download = new Thread( new Runnable() {
    // public void run() {
    // try {
    // //Assegnazione all'iesimo elemento il path in cui è stato scaricato
    // il file audio
    // tempPath.set(i,
    // AudioVideoProcessing.audioDownloadEndExtraction(uri.getSourceType(),
    // videoUri.get(i)));
    // } catch (IOException e) {
    // getContext().getLogger().log(Level.WARNING, "Error: " + e.getMessage());
    // } catch (InterruptedException e) {
    // getContext().getLogger().log(Level.WARNING, "Error: " + e.getMessage());
    // }
    // }
    // });
    // //Partenza di ogni singola istanza
    // download.start();
    // }
    // //Attesa della fine dei thread
    // try {
    // download.join();
    // } catch (InterruptedException e) {
    // getContext().getLogger().log(Level.WARNING, "Error: " + e.getMessage());
    // }
   
    //Inizializzazione dell'insieme di threads
    ExecutorService es = Executors.newCachedThreadPool();
    ArrayList<Callable<String>> tasks = new ArrayList<Callable<String>>();
    List<Future<String>> results = new ArrayList<Future<String>>();
   
    StringArray path = new StringArray(aJCas, videoUri.size());
    // Assegnazione dell'array di competenza
    audio.setAudioPath(path);
   
    for (i = 0; i < videoUri.size(); i++)
      tasks.add(new ThreadCallDownload(videoUri.get(i),link.getSourceType(),tp));
   
    try {
      //Metodo che esegue tutti i threads e restituisce i risultati una volta
      //terminati tutti i trheads
      results = es.invokeAll(tasks);
      for(i = 0; i < results.size(); i++){
        audio.setAudioPath(i, results.get(i).get());
        getContext().getLogger().log(Level.WARNING, "Path: " + results.get(i).get());
      }
     
    } catch (InterruptedException e) {
      getContext().getLogger().log(Level.WARNING, "InterruptedError: " + e.getMessage());
    } catch (ExecutionException e) {
      getContext().getLogger().log(Level.WARNING, "IOError/InterruptedError: " + e.getMessage());
    }
   
    // Aggiornamento degli indici
    audio.addToIndexes();
  }

  @Override
  public void destroy() {
    super.destroy();
  }

}
TOP

Related Classes of letweb.semanticum.aes.AudioDownloader$ThreadCallDownload

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.