View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.booter.spi;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.IOException;
24  import java.net.MalformedURLException;
25  
26  import org.apache.maven.surefire.api.booter.MasterProcessChannelDecoder;
27  import org.apache.maven.surefire.api.booter.MasterProcessChannelEncoder;
28  import org.apache.maven.surefire.api.fork.ForkNodeArguments;
29  import org.apache.maven.surefire.api.util.internal.WritableBufferedByteChannel;
30  
31  import static org.apache.maven.surefire.api.util.internal.Channels.newBufferedChannel;
32  
33  /**
34   * Producer of encoder and decoder for process pipes.
35   * <br>
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 3.0.0-M5
39   */
40  public class LegacyMasterProcessChannelProcessorFactory extends AbstractMasterProcessChannelProcessorFactory {
41      private static final int FLUSH_PERIOD_MILLIS = 100;
42  
43      @Override
44      public boolean canUse(String channelConfig) {
45          return channelConfig.startsWith("pipe://");
46      }
47  
48      @Override
49      public void connect(String channelConfig) throws IOException {
50          if (!canUse(channelConfig)) {
51              throw new MalformedURLException("Unknown channel string " + channelConfig);
52          }
53      }
54  
55      @Override
56      public MasterProcessChannelDecoder createDecoder(@Nonnull ForkNodeArguments forkingArguments) {
57          return new CommandChannelDecoder(newBufferedChannel(System.in), forkingArguments);
58      }
59  
60      @Override
61      public MasterProcessChannelEncoder createEncoder(@Nonnull ForkNodeArguments forkingArguments) {
62          WritableBufferedByteChannel channel = newBufferedChannel(System.out);
63          schedulePeriodicFlusher(FLUSH_PERIOD_MILLIS, channel);
64          return new EventChannelEncoder(channel);
65      }
66  }