OSDN Git Service

CommandにSwingWorkerを継承させる
authoryukihane <yukihane.feather@gmail.com>
Mon, 22 Aug 2011 01:08:19 +0000 (10:08 +0900)
committeryukihane <yukihane.feather@gmail.com>
Mon, 22 Aug 2011 01:08:19 +0000 (10:08 +0900)
frontend/src/saccubus/ConvertStopFlag.java
frontend/src/saccubus/converter/AbstractCommand.java
frontend/src/saccubus/converter/ConvertProgress.java [new file with mode: 0644]
frontend/src/saccubus/converter/ConvertResult.java [new file with mode: 0644]
frontend/src/saccubus/converter/Converter.java
frontend/src/saccubus/converter/FfmpegCommand.java
frontend/src/saccubus/converter/FfmpegCommandProgress.java [new file with mode: 0644]
frontend/src/saccubus/converter/FfmpegCommandResult.java [new file with mode: 0644]

index e1eb00b..27cea8e 100644 (file)
@@ -4,22 +4,23 @@ package saccubus;
  * <p>
  * タイトル: さきゅばす
  * </p>
- * 
+ *
  * <p>
  * 説明: ニコニコ動画の動画をコメントつきで保存
  * </p>
- * 
+ *
  * <p>
  * 著作権: Copyright (c) 2007 PSI
  * </p>
- * 
+ *
  * <p>
  * 会社名:
  * </p>
- * 
+ *
  * @author 未入力
  * @version 1.0
  */
+// TODO 不要になる予定
 public class ConvertStopFlag {
 
     public interface StateChangeListener {
index 8e8b1d1..ff62ba5 100644 (file)
@@ -1,6 +1,7 @@
 /* $Id$ */
 package saccubus.converter;
 
+import javax.swing.SwingWorker;
 import static org.apache.commons.lang.Validate.*;
 import saccubus.ConvertStopFlag;
 import saccubus.net.TextProgressListener;
@@ -9,22 +10,19 @@ import saccubus.net.TextProgressListener;
  *
  * @author yuki
  */
-public abstract class AbstractCommand {
+public abstract class AbstractCommand<T, V> extends SwingWorker<T, V> {
 
     private final TextProgressListener listener;
-    private final ConvertStopFlag StopFlag;
 
-    public AbstractCommand(TextProgressListener listener, ConvertStopFlag stopFlag) {
+    public AbstractCommand(TextProgressListener listener) {
         notNull(listener);
-        notNull(stopFlag);
 
         this.listener = listener;
-        this.StopFlag = stopFlag;
     }
 
-    protected void stopFlagReturn() throws InterruptedException {
-        if (getStopFlag().needStop()) {
-            throw new InterruptedException("中止しました。");
+    protected void checkStop() throws InterruptedException {
+        if (Thread.interrupted()) {
+            throw new InterruptedException("中止要求を受け付けました");
         }
     }
 
@@ -38,11 +36,4 @@ public abstract class AbstractCommand {
     protected TextProgressListener getListener() {
         return listener;
     }
-
-    /**
-     * @return the StopFlag
-     */
-    protected ConvertStopFlag getStopFlag() {
-        return StopFlag;
-    }
 }
diff --git a/frontend/src/saccubus/converter/ConvertProgress.java b/frontend/src/saccubus/converter/ConvertProgress.java
new file mode 100644 (file)
index 0000000..069a295
--- /dev/null
@@ -0,0 +1,9 @@
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class ConvertProgress {
+
+}
diff --git a/frontend/src/saccubus/converter/ConvertResult.java b/frontend/src/saccubus/converter/ConvertResult.java
new file mode 100644 (file)
index 0000000..9c78b68
--- /dev/null
@@ -0,0 +1,18 @@
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class ConvertResult {
+
+    private final boolean resultValue;
+
+    ConvertResult(boolean b) {
+        this.resultValue = b;
+    }
+
+    public boolean getResultValue() {
+        return resultValue;
+    }
+}
index ce1ceb8..11b630b 100644 (file)
@@ -4,7 +4,6 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.util.concurrent.Callable;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 import nicobrowser.GetFlvResult;
 import nicobrowser.NamePattern;
@@ -34,7 +33,7 @@ import saccubus.converter.profile.ProxyProfile;
  * @author 未入力
  * @version 1.0
  */
-public class Converter extends AbstractCommand implements Callable<Boolean> {
+public abstract class Converter extends AbstractCommand<ConvertResult, ConvertProgress> implements Callable<Boolean> {
 
     private static final Logger logger = Logger.getLogger(Converter.class.getName());
     private final Profile profile;
@@ -50,37 +49,38 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
      */
     public Converter(String id, Profile profile,
             TextProgressListener listener, ConvertStopFlag flag) {
-        super(listener, flag);
+        super(listener);
         this.movieId = id;
         this.profile = profile;
     }
 
     @Override
     public Boolean call() throws Exception {
-        boolean result = false;
         try {
-            result = runConvert();
+            final ConvertResult result = doInBackground();
+            return Boolean.valueOf(result.getResultValue());
         } finally {
-            getStopFlag().finished();
+            // TODO 何か処理が必要?
+//            getStopFlag().finished();
         }
-        return Boolean.valueOf(result);
     }
 
-    // TODO Runnableを実装しなくなったので削除する
-    public void run() {
-        try {
-            call();
-        } catch (Exception ex) {
-            String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
-            sendText(text);
-            logger.log(Level.SEVERE, null, ex);
-        }
-    }
+//    // TODO Runnableを実装しなくなったので削除する
+//    public void run() {
+//        try {
+//            call();
+//        } catch (Exception ex) {
+//            String text = (ex.getMessage() != null) ? ex.getMessage() : "予期しないエラー発生のため中断しました。";
+//            sendText(text);
+//            logger.log(Level.SEVERE, null, ex);
+//        }
+//    }
 
-    private boolean runConvert() throws IOException, InterruptedException, Exception {
+    @Override
+    protected ConvertResult doInBackground() throws Exception {
         if (!shouldRun(profile)) {
             sendText("何もすることがありません");
-            return true;
+            return new ConvertResult(true);
         }
 
         validSetting();
@@ -110,8 +110,7 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
             }
         }
 
-        stopFlagReturn();
-
+        checkStop();
 
         File commentFile;
         if (profile.getCommentSetting().isDownload()) {
@@ -130,7 +129,7 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
             commentFile = profile.getCommentSetting().getLocalFile();
         }
 
-        stopFlagReturn();
+        checkStop();
 
         File videoFile;
         GetFlvResult vf = null;
@@ -156,7 +155,7 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
 
         if (!profile.getOutputFileSetting().isConvert()) {
             sendText("動画・コメントを保存し、変換は行いませんでした。");
-            return true;
+            return new ConvertResult(true);
         }
 
         if (!videoFile.isFile()) {
@@ -175,18 +174,12 @@ public class Converter extends AbstractCommand implements Callable<Boolean> {
         final boolean isNotLow = (vf == null) ? true : (vf.getStatus() != Status.GET_LOW);
         File convertedVideoFile = getOutputFileName(vi.getTitleInWatchPage(), isNotLow);
 
-        boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
-                convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
-        return res;
-    }
-
-    public boolean isConverted() {
-        return getStopFlag().isFinished();
-    }
+        return new ConvertResult(true);
 
-    @Override
-    public ConvertStopFlag getStopFlag() {
-        return super.getStopFlag();
+        // TODO FFMPEG 実行開始は別タスクとして実装する.
+//        boolean res = new FfmpegCommand(getListener(), getStopFlag(), commentFile, videoFile,
+//                convertedVideoFile, profile.getFfmpeg(), profile.getGeneralSetting()).execute();
+//        return res;
     }
 
     /** @return 何か実行すべき処理があればtrue. */
index c20ece2..ddbd716 100644 (file)
@@ -28,7 +28,7 @@ import yukihane.swf.Cws2Fws;
  *
  * @author yuki
  */
-public class FfmpegCommand extends AbstractCommand {
+public class FfmpegCommand extends AbstractCommand<FfmpegCommandResult, FfmpegCommandProgress> {
     private static final Logger logger = Logger.getLogger(FfmpegCommand.class.getName());
     private final File commentMiddleFile;
     private final File tcommMiddleFile;
@@ -40,7 +40,7 @@ public class FfmpegCommand extends AbstractCommand {
 
     FfmpegCommand(TextProgressListener listener, ConvertStopFlag flag, File commentFile,
             File videoFile, File convertedVideoFile, ConvertProfile ffmpeg, GeneralProfile general) throws IOException {
-        super(listener, flag);
+        super(listener);
         this.commentFile = commentFile;
         this.videoFile = videoFile;
         this.convertedVideoFile = convertedVideoFile;
@@ -52,7 +52,9 @@ public class FfmpegCommand extends AbstractCommand {
         TMP_CWS = File.createTempFile("cws", ".swf", tmpDir);
     }
 
-    public boolean execute() throws InterruptedException, IOException {
+
+    @Override
+    protected FfmpegCommandResult doInBackground() throws Exception {
         try {
             return exec();
         } finally {
@@ -68,7 +70,7 @@ public class FfmpegCommand extends AbstractCommand {
         }
     }
 
-    private boolean exec() throws InterruptedException, IOException {
+    private FfmpegCommandResult exec() throws InterruptedException, IOException {
         final HideCondition ngSetting = getFfmpeg().getNgSetting();
         if (commentFile != null) {
             sendText("コメントの中間ファイルへの変換中");
@@ -76,10 +78,10 @@ public class FfmpegCommand extends AbstractCommand {
                     getWord());
             if (!conv) {
                 sendText("コメント変換に失敗。ファイル名に使用できない文字が含まれているか正規表現の間違い?");
-                return false;
+                return new FfmpegCommandResult(false);
             }
         }
-        stopFlagReturn();
+        checkStop();
 //        if (tcommFile != null) {
 //            sendText("投稿者コメントの中間ファイルへの変換中");
 //            boolean conv = ConvertToVideoHook.convert(tcommFile, tcommMiddleFile, ngSetting.getId(), ngSetting.getWord());
@@ -88,17 +90,17 @@ public class FfmpegCommand extends AbstractCommand {
 //                return false;
 //            }
 //        }
-        stopFlagReturn();
+//        stopFlagReturn();
         sendText("動画の変換を開始");
         int code;
         if ((code = converting_video(videoFile, convertedVideoFile, (commentFile != null), commentMiddleFile.getPath(),
                 false, tcommMiddleFile.getPath(), getFfmpeg().getFfmpegOption())) == 0) {
             sendText("変換が正常に終了しました。");
-            return true;
+            return new FfmpegCommandResult(true);
         } else {
             sendText("変換エラー:" + convertedVideoFile.getPath());
         }
-        return false;
+        return new FfmpegCommandResult(false);
     }
 
     private int converting_video(File videoFile, File convertedVideoFile, boolean addComment, String commPath,
@@ -167,9 +169,10 @@ public class FfmpegCommand extends AbstractCommand {
         }
         logger.log(Level.INFO, argMsg.toString());
 
+        Process process = null;
         try {
             logger.log(Level.INFO, "\n\n----\nProcessing FFmpeg...\n----\n\n");
-            Process process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
+            process = Runtime.getRuntime().exec(cmdList.toArray(new String[0]));
             BufferedReader ebr = new BufferedReader(new InputStreamReader(
                     process.getErrorStream()));
             String e;
@@ -182,9 +185,8 @@ public class FfmpegCommand extends AbstractCommand {
                 }
 
                 try {
-                    stopFlagReturn();
+                    checkStop();
                 } catch (InterruptedException ex) {
-                    process.destroy();
                     throw ex;
                 }
 
@@ -192,6 +194,10 @@ public class FfmpegCommand extends AbstractCommand {
             process.waitFor();
             return process.exitValue();
         } finally {
+            // TODO 正常終了した場合もdestroyしていいのか?
+            if (process != null) {
+                process.destroy();
+            }
             if (fwsFile != null) {
                 fwsFile.delete();
             }
diff --git a/frontend/src/saccubus/converter/FfmpegCommandProgress.java b/frontend/src/saccubus/converter/FfmpegCommandProgress.java
new file mode 100644 (file)
index 0000000..fc530b7
--- /dev/null
@@ -0,0 +1,9 @@
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class FfmpegCommandProgress {
+
+}
diff --git a/frontend/src/saccubus/converter/FfmpegCommandResult.java b/frontend/src/saccubus/converter/FfmpegCommandResult.java
new file mode 100644 (file)
index 0000000..e5c7089
--- /dev/null
@@ -0,0 +1,18 @@
+package saccubus.converter;
+
+/**
+ *
+ * @author yuki
+ */
+public class FfmpegCommandResult {
+
+    private final boolean resultValue;
+
+    FfmpegCommandResult(boolean b) {
+        this.resultValue = b;
+    }
+
+    public boolean isResultValue() {
+        return resultValue;
+    }
+}