View Javadoc
1   /*
2    * Copyright 2008, 2009 Ange Optimization ApS
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.simuline.octave.exec;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.Reader;
21  import java.util.concurrent.Callable;
22  
23  import eu.simuline.octave.exception.OctaveIOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * Callable that reads from the octave process. 
30   * Used in {@link OctaveExec#evalRW(WriteFunctor, ReadFunctor)} only. 
31   */
32  final class OctaveReaderCallable implements Callable<Void> {
33  
34      private static final String MSG_IOE_READ = 
35  	"IOException from ReadFunctor";
36      private static final String MSG_IOE_CLS = 
37  	"IOException during close";
38  
39      private static final Log LOG = LogFactory
40  	.getLog(OctaveReaderCallable.class);
41  
42  
43      private final BufferedReader processReader;
44  
45      private final ReadFunctor readFunctor;
46  
47      private final String spacer;
48  
49      /**
50       * @param processReader
51       * @param readFunctor
52       * @param spacer
53       */
54      OctaveReaderCallable(final BufferedReader processReader, 
55  			 final ReadFunctor readFunctor, 
56  			 final String spacer) {
57          this.processReader = processReader;
58          this.readFunctor   = readFunctor;
59          this.spacer        = spacer;
60      }
61  
62      /**
63       * Calling reads from an {@link OctaveExecuteReader} 
64       * made up from {@link #processReader} 
65       * with end of stream sign {@link #spacer}. 
66       * Exceptions are logged on {@link #LOG}. 
67       *
68       * @throws OctaveIOException 
69       *    if underlying readers/read functors 
70       *    have thrown an {@link IOException}. 
71       */
72      @Override
73      public Void call() {
74          final Reader reader = new OctaveExecuteReader(this.processReader, 
75  						      this.spacer);
76          try {
77              this.readFunctor.doReads(reader);
78          } catch (final IOException e) {
79  	    LOG.debug(MSG_IOE_READ, e);
80              throw new OctaveIOException(MSG_IOE_READ, e);
81          } finally { // NOPMD
82              try {
83                  reader.close();
84              } catch (final IOException e) {
85                  LOG.debug(MSG_IOE_CLS, e);
86                  throw new OctaveIOException(MSG_IOE_CLS, e);
87              }
88          }
89          return null;
90      }
91  
92  }