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.IOException;
19  import java.io.Writer;
20  import java.util.concurrent.Callable;
21  
22  import eu.simuline.octave.exception.OctaveIOException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * Callable that writes to the octave process. 
29   * Used in {@link OctaveExec#evalRW(WriteFunctor, ReadFunctor)} only. 
30   */
31  final class OctaveWriterCallable implements Callable<Void> {
32  
33      private static final Log LOG = LogFactory
34  	.getLog(OctaveWriterCallable.class);
35  
36      static final String EXCEPTION_MESSAGE_FUNCTOR = 
37  	"IOException from WriteFunctor";
38  
39      static final String EXCEPTION_MESSAGE_SPACER = 
40  	"IOException when writing spacer";
41  
42  
43      private final Writer processWriter;
44  
45      private final WriteFunctor writeFunctor;
46  
47      private final String spacer;
48  
49      /**
50       * @param processWriter
51       * @param writeFunctor
52       * @param spacer
53       */
54      OctaveWriterCallable(final Writer processWriter, 
55  			 final WriteFunctor writeFunctor, 
56  			 final String spacer) {
57          this.processWriter = processWriter;
58          this.writeFunctor  = writeFunctor;
59          this.spacer        = spacer;
60      }
61  
62      /**
63       * Calling writes to {@link #processWriter}: 
64       * first according to {@link #writeFunctor} 
65       * then printf of {@link #spacer} and then flush. 
66       * Exceptions are logged on {@link #LOG}. 
67       */
68      @Override
69      public Void call() {
70          // Write to process
71          try {
72  	    this.writeFunctor.doWrites(this.processWriter);
73          } catch (final IOException e) {
74              LOG.debug(EXCEPTION_MESSAGE_FUNCTOR, e);
75              throw new OctaveIOException(EXCEPTION_MESSAGE_FUNCTOR, e);
76          }
77          try {
78  	    this.processWriter.write("\nprintf(\"\\n%s\\n\", \"" + 
79  				     this.spacer + "\");\n");
80              this.processWriter.flush();
81          } catch (final IOException e) {
82              LOG.debug(EXCEPTION_MESSAGE_SPACER, e);
83              throw new OctaveIOException(EXCEPTION_MESSAGE_SPACER, e);
84          }
85          LOG.debug("Has written all");
86          return null;
87      }
88  
89  }