PipedUnblockedStreamFactory.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.io.notifier.unblocked;

  21. import java.io.IOException;

  22. import org.openspcoop2.utils.resources.Loader;
  23. import org.slf4j.Logger;

  24. /**
  25.  * IPipedUnblockedStream
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class PipedUnblockedStreamFactory {

  32.     private static boolean USE_PIPED_BYTES_STREAM_IMPL = true;
  33.     private static boolean USE_PIPED_BYTE_ARRAY_OUTPUT_STREAM_IMPL = false;
  34.     private static String CLASSNAME_IMPL = null;
  35.    
  36.     public static void setImplementation(String className) {
  37.         if(org.openspcoop2.utils.io.notifier.unblocked.PipedBytesStream.class.getName().equals(className)) {
  38.             USE_PIPED_BYTES_STREAM_IMPL = true;
  39.             USE_PIPED_BYTE_ARRAY_OUTPUT_STREAM_IMPL = false;
  40.             CLASSNAME_IMPL = null;
  41.         }
  42.         else if(org.openspcoop2.utils.io.notifier.unblocked.PipedUnblockedStream.class.getName().equals(className)) {
  43.             USE_PIPED_BYTES_STREAM_IMPL = false;
  44.             USE_PIPED_BYTE_ARRAY_OUTPUT_STREAM_IMPL = true;
  45.             CLASSNAME_IMPL = null;
  46.         }
  47.         else {
  48.             USE_PIPED_BYTES_STREAM_IMPL = false;
  49.             USE_PIPED_BYTE_ARRAY_OUTPUT_STREAM_IMPL = false;
  50.             CLASSNAME_IMPL = className;
  51.         }
  52.     }
  53.    
  54.     public static IPipedUnblockedStream newPipedUnblockedStream(Logger log, long sizeBuffer, int timeoutMs, String source) throws IOException {
  55.         try {
  56.             IPipedUnblockedStream pipe = null;
  57.             if(USE_PIPED_BYTES_STREAM_IMPL) {
  58.                 pipe = new org.openspcoop2.utils.io.notifier.unblocked.PipedBytesStream();
  59.             }
  60.             else if(USE_PIPED_BYTE_ARRAY_OUTPUT_STREAM_IMPL) {
  61.                 pipe = new org.openspcoop2.utils.io.notifier.unblocked.PipedUnblockedStream();
  62.             }
  63.             else {
  64.                 pipe = (IPipedUnblockedStream) new Loader().newInstance(CLASSNAME_IMPL);
  65.             }
  66.             pipe.init(log, sizeBuffer, timeoutMs, source);
  67.             return pipe;
  68.         }catch(Throwable t) {
  69.             throw new IOException(t.getMessage(),t);
  70.         }
  71.     }
  72.    
  73. }