Skip to main content
summaryrefslogtreecommitdiffstats
blob: 466a8be37756964321f017e032ce97b1f3286132 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.branch.management.exchange.export;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import org.eclipse.osee.framework.branch.management.IExchangeTaskListener;
import org.eclipse.osee.framework.branch.management.exchange.handler.ExportItem;

/**
 * @author Roberto E. Escobar
 */
public abstract class AbstractExportItem implements Callable<Boolean> {
   private final ExportItem id;
   private final String fileName;
   private final Set<IExchangeTaskListener> exportListeners;

   private File writeLocation;
   private boolean cancel;

   public AbstractExportItem(ExportItem id) {
      this.id = id;
      this.fileName = id.getFileName();
      this.cancel = false;
      this.exportListeners = Collections.synchronizedSet(new HashSet<IExchangeTaskListener>());
   }

   public String getSource() {
      return id.getSource();
   }

   public String getFileName() {
      return fileName;
   }

   public String getName() {
      return id.toString();
   }

   public int getPriority() {
      return id.getPriority();
   }

   public int getBufferSize() {
      return (int) Math.pow(2, 20);
   }

   public void setWriteLocation(File writeLocation) {
      this.writeLocation = writeLocation;
   }

   public File getWriteLocation() {
      return writeLocation;
   }

   public void addExportListener(IExchangeTaskListener exportListener) {
      if (exportListener != null) {
         this.exportListeners.add(exportListener);
      }
   }

   public void removeExportListener(IExchangeTaskListener exportListener) {
      if (exportListener != null) {
         this.exportListeners.remove(exportListener);
      }
   }

   public void cleanUp() {
      this.setWriteLocation(null);
      this.exportListeners.clear();
   }

   @Override
   public final Boolean call() throws Exception {
      boolean wasSuccessful = false;
      long startTime = System.currentTimeMillis();
      try {
         if (!isCancel()) {
            executeWork();
         }
         wasSuccessful = true;
      } catch (Exception ex) {
         notifyOnExportException(ex);
         throw ex;
      } finally {
         notifyOnExportItemCompleted(System.currentTimeMillis() - startTime);
      }
      return wasSuccessful;
   }

   protected void notifyOnExportException(Throwable ex) {
      for (IExchangeTaskListener listener : this.exportListeners) {
         listener.onException(getName(), ex);
      }
   }

   protected void notifyOnExportItemCompleted(long timeToProcess) {
      for (IExchangeTaskListener listener : this.exportListeners) {
         listener.onExportItemCompleted(getName(), timeToProcess);
      }
   }

   protected abstract void executeWork() throws Exception;

   public void setCancel(boolean cancel) {
      this.cancel = cancel;
   }

   public boolean isCancel() {
      return this.cancel;
   }
}

Back to the top