Beispiel #1
0
 @Test
 public void wsdlExists() throws Exception {
   final URL url =
       new URL(
           "http://localhost:" + port + "/simple-webservice-without-interface/Calculator?wsdl");
   assertTrue(IOUtils.readLines(url.openStream()).size() > 0);
   assertTrue(IOUtils.readLines(url.openStream()).toString().contains("CalculatorWsService"));
 }
  public ProcessOutput execute(
      JobConsoleLogger console,
      boolean showConsoleOutput,
      String workingDir,
      List<String> command,
      Map<String, String> envMap) {
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    Process process = null;
    ProcessOutput processOutput = null;
    long startTime = System.currentTimeMillis();
    try {
      logger.info(String.format("External process '%s' started", command.get(0)));
      if (workingDir != null) {
        processBuilder.directory(new File(workingDir));
      }
      processBuilder.redirectErrorStream(true).environment().putAll(envMap);
      process = processBuilder.start();

      if (showConsoleOutput) {
        LinesInputStream output = new LinesInputStream(process.getInputStream());
        console.readOutputOf(output);
        int returnCode = process.waitFor();
        waitUntilEmpty(2000, output, null);
        processOutput = new ProcessOutput(returnCode, output.getLines(), new ArrayList<String>());
      } else {
        List output = IOUtils.readLines(process.getInputStream());
        int returnCode = process.waitFor();
        List pendingOutput = IOUtils.readLines(process.getInputStream());
        output.addAll(pendingOutput);
        processOutput = new ProcessOutput(returnCode, output, new ArrayList<String>());
      }
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    } finally {
      if (process != null) {
        closeQuietly(process.getInputStream());
        closeQuietly(process.getErrorStream());
        closeQuietly(process.getOutputStream());
        process.destroy();
        long estimatedTime = System.currentTimeMillis() - startTime;
        logger.info(
            String.format(
                "External process '%s' returned %d after %dms.",
                command.get(0), processOutput.getReturnCode(), estimatedTime));
      }
    }
    return processOutput;
  }
  public static List<DataSet> loadPPTest(int from, int to) throws IOException {
    ClassPathResource resource = new ClassPathResource("/pptest.dat");
    @SuppressWarnings("unchecked")
    List<String> lines = IOUtils.readLines(resource.getInputStream());
    List<DataSet> list = new ArrayList<>();
    INDArray ret = Nd4j.ones(Math.abs(to - from), 4);
    double[][] outcomes = new double[lines.size()][3];
    int putCount = 0;

    for (int i = from; i < to; i++) {
      String line = lines.get(i);
      String[] split = line.split(",");

      addRow(ret, putCount++, split);

      String outcome = split[split.length - 1];
      double[] rowOutcome = new double[3];
      rowOutcome[Integer.parseInt(outcome)] = 1;
      outcomes[i] = rowOutcome;
    }

    for (int i = 0; i < ret.rows(); i++) {
      DataSet add = new DataSet(ret.getRow(i), Nd4j.create(outcomes[from + i]));
      list.add(add);
    }
    return list;
  }
  private void doExecuteScript(final Resource scriptResource) {
    if (scriptResource == null || !scriptResource.exists()) return;
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    String[] scripts;
    try {
      String[] list =
          StringUtils.delimitedListToStringArray(
              stripComments(IOUtils.readLines(scriptResource.getInputStream())), ";");
      scripts = list;
    } catch (IOException e) {
      throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
    }
    for (int i = 0; i < scripts.length; i++) {
      final String script = scripts[i].trim();
      TransactionTemplate transactionTemplate =
          new TransactionTemplate(new DataSourceTransactionManager(dataSource));
      transactionTemplate.execute(
          new TransactionCallback<Void>() {

            @Override
            public Void doInTransaction(TransactionStatus status) {
              if (StringUtils.hasText(script)) {
                try {
                  jdbcTemplate.execute(script);
                } catch (DataAccessException e) {
                  if (!script.toUpperCase().startsWith("DROP")) {
                    throw e;
                  }
                }
              }
              return null;
            }
          });
    }
  }
 protected void processFile(File file) {
   System.out.println(
       "processing file "
           + file.getName()
           + "at time "
           + sdf.format(Calendar.getInstance().getTime()));
   FileInputStream openInputStream = null;
   try {
     openInputStream = FileUtils.openInputStream(file);
     List<String> lines = IOUtils.readLines(openInputStream);
     VrpProblem problem = VrpUtils.createProblemFromStringList(lines);
     try {
       Solution solution = createInitialSolution(problem);
       createLeiEtAlSolution(solution);
     } catch (Exception e) {
       System.err.println(
           "An error occurred while calculating the solution with the Lei et al. heuristic.");
       e.printStackTrace();
     }
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     IOUtils.closeQuietly(openInputStream);
   }
 }
  private void loadActionsFromFile(final Set<Class<?>> actionClasses) {
    try {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Properties properties = new Properties();
      try (InputStream stream = loader.getResourceAsStream("/configuration.properties")) {
        if (stream == null) {
          throw new RuntimeException();
        }
        properties.load(stream);
      }

      for (FenixFrameworkArtifact artifact :
          FenixFrameworkArtifact.fromName(properties.getProperty("app.name")).getArtifacts()) {
        try (InputStream stream =
            loader.getResourceAsStream(artifact.getName() + "/.actionAnnotationLog")) {
          if (stream != null) {
            List<String> classnames = IOUtils.readLines(stream);
            for (String classname : classnames) {
              try {
                Class<?> type = loader.loadClass(classname);
                actionClasses.add(type);
              } catch (final ClassNotFoundException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }
    } catch (IOException | FenixFrameworkProjectException e) {
      e.printStackTrace();
    }
  }
Beispiel #7
0
  public static Map<String, String> loadMappings(InputStream input) {

    Map<String, String> res = new HashMap<String, String>();
    // load the mapping rule
    try {
      List<String> maps = IOUtils.readLines(input);
      for (String map : maps) {
        if (StringUtils.isBlank(map)) {
          continue;
        }

        if (map.trim().startsWith("#")) {
          continue;
        }

        String[] ss = StringUtils.split(map, "=");
        if (ss.length == 2) {
          res.put(ss[0], ss[1]);

        } else {
          debug("skipping map=" + map);
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new RuntimeException(ex);
    }

    return res;
  }
 @Test
 public void resolve() {
   Map<String, Integer> map = new TreeMap<>();
   try (InputStream input = new FileInputStream("data" + File.separator + "p022.txt")) {
     List<String> lines = IOUtils.readLines(input);
     for (String line : lines) {
       String[] names = line.replace("\"", "").split(",");
       for (String name : names) {
         map.put(name, score(name));
       }
     }
     int index = 1;
     for (String name : map.keySet()) {
       map.put(name, map.get(name) * index);
       index++;
     }
     long sum = 0;
     for (Integer s : map.values()) {
       sum += s;
     }
     print(sum);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 /**
  * Create the filtered base data for the model to train on
  *
  * @return
  * @throws IOException
  * @throws ClassNotFoundException
  */
 private LMClassifier<?, ?> createModelInputAndTrain(
     boolean readExistingModel, String trainingDataFile)
     throws IOException, ClassNotFoundException {
   final String NAIVE_BAYES_SERIALIZED_MODEL = "naiveBayesModel";
   if (readExistingModel) {
     return deserialize(NAIVE_BAYES_SERIALIZED_MODEL);
   }
   List<String> lines = IOUtils.readLines(classLoader.getResourceAsStream(INPUT_TWEETS_ORIG_TXT));
   List<String> linesNoStopWords = new ArrayList<String>();
   LMClassifier<?, ?> cls =
       NaiveBayesClassifier.createNGramProcess(new String[] {POSITIVE, NEGATIVE, NEUTRAL}, 7);
   Classification posClassification = new Classification(POSITIVE);
   Classification negClassification = new Classification(NEGATIVE);
   Classified<String> classified = null;
   for (String line : lines) {
     String newLine = NLPUtils.removeStopWords(line);
     if (POSITIVE.equals(newLine.split(" ")[0])) {
       classified = new Classified<String>(newLine, posClassification);
     } else {
       classified = new Classified<String>(newLine, negClassification);
     }
     linesNoStopWords.add(NLPUtils.removeStopWords(line));
     ((ObjectHandler) cls).handle(classified);
   }
   AbstractExternalizable.compileTo((Compilable) cls, new File(NAIVE_BAYES_SERIALIZED_MODEL));
   return cls;
 }
 private void testArguments(String... args)
     throws IOException, InterruptedException, TimeoutException {
   byte[] bytes = printArguments(args).execute().output();
   List<String> expected = Arrays.asList(args);
   List<String> actual = IOUtils.readLines(new ByteArrayInputStream(bytes));
   Assert.assertEquals(expected, actual);
 }
Beispiel #11
0
 private String readResponseBodyFromTemplate(String name) throws IOException {
   String folderName = getClass().getPackage().getName().replace(".", "/");
   try (InputStream inputStream =
       getContextClassLoader().getResourceAsStream(folderName + "/" + name)) {
     return join(IOUtils.readLines(inputStream, Charset.forName("utf-8")), "\n");
   }
 }
  @Before
  public void prepareTest() throws Exception {

    syncConfiguration =
        MAPPER.readValue(
            MongoElasticsearchSyncIT.class.getResourceAsStream("/testSync.json"),
            MongoElasticsearchSyncConfiguration.class);

    syncConfiguration.getDestination().setClusterName(cluster().getClusterName());

    MongoPersistWriter setupWriter = new MongoPersistWriter(syncConfiguration.getSource());

    setupWriter.prepare(null);

    InputStream testActivityFolderStream =
        MongoElasticsearchSyncIT.class.getClassLoader().getResourceAsStream("activities");
    List<String> files = IOUtils.readLines(testActivityFolderStream, Charsets.UTF_8);

    for (String file : files) {
      LOGGER.info("File: " + file);
      InputStream testActivityFileStream =
          MongoElasticsearchSyncIT.class.getClassLoader().getResourceAsStream("activities/" + file);
      Activity activity = MAPPER.readValue(testActivityFileStream, Activity.class);
      activity.getAdditionalProperties().remove("$license");
      StreamsDatum datum = new StreamsDatum(activity, activity.getVerb());
      setupWriter.write(datum);
      LOGGER.info("Wrote: " + activity.getVerb());
      srcCount++;
    }

    setupWriter.cleanUp();
  }
  public void testSplitMessageWithHeader2() throws Exception {
    LineCountSplitter s = new LineCountSplitter();
    s.setKeepHeaderLines(2);
    s.setSplitOnLine(10);
    s.setIgnoreBlankLines(true);

    final String HEADER_LINE_1 = "HEADER LINE 1";
    final String HEADER_LINE_2 = "HEADER LINE 2";
    List<AdaptrisMessage> result =
        toList(
            s.splitMessage(
                createLineCountMessageInputWithHeader(
                    new String[] {HEADER_LINE_1, HEADER_LINE_2})));

    assertEquals("5 split messages", 5, result.size());

    for (AdaptrisMessage m : result) {
      try (Reader reader = m.getReader()) {
        List<String> lines = IOUtils.readLines(reader);
        assertEquals("12 lines per message", 12, lines.size());
        assertEquals("Must be header line 1", HEADER_LINE_1, lines.get(0));
        assertEquals("Must be header line 2", HEADER_LINE_2, lines.get(1));
        for (int i = 2; i < 12; i++) {
          assertEquals("Must be regular line", LINE, lines.get(i));
        }
      }
    }
  }
  @Override
  public Object[] extract(T item) {
    ExpressionParser parser = new SpelExpressionParser();
    EvaluationContext elContext = new CoreMappingEvaluationContext(item);
    elContext.getPropertyAccessors().add(new ExtendedAttributePropertyAccessor());

    try (InputStream iStream = resource.getInputStream()) {
      List<String> expressions = IOUtils.readLines(iStream);
      List<Object> fields = Lists.newArrayListWithCapacity(expressions.size());
      for (String expression : expressions) {
        String value = parser.parseExpression(expression).getValue(elContext, String.class);
        if (value != null && value.contains("\"") && !"\"\"".equals(value)) {
          value = value.replaceAll("\"", "\"\"");
        }
        if (value != null && value.contains(",")) {
          StringBuilder sb = new StringBuilder(value.length() + 2);
          sb.append("\"");
          sb.append(value);
          sb.append("\"");
          value = sb.toString();
        }
        fields.add(value);
      }
      return fields.toArray(new Object[fields.size()]);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  private static void processConfigFile(File configFile) throws IOException, FileNotFoundException {
    if (configFile.isDirectory() || !configFile.getName().endsWith(".cfg")) {
      logger.debug("Ignoring file '{}'", configFile.getName());
      return;
    }
    logger.debug("Processing config file '{}'", configFile.getName());
    ConfigurationAdmin configurationAdmin =
        (ConfigurationAdmin) ConfigActivator.configurationAdminTracker.getService();
    if (configurationAdmin != null) {
      // we need to remember which configuration needs to be updated
      // because values have changed.
      Map<Configuration, Dictionary> configsToUpdate = new HashMap<Configuration, Dictionary>();

      // also cache the already retrieved configurations for each pid
      Map<Configuration, Dictionary> configMap = new HashMap<Configuration, Dictionary>();

      String pid;
      String filenameWithoutExt = StringUtils.substringBeforeLast(configFile.getName(), ".");
      if (filenameWithoutExt.contains(".")) {
        // it is a fully qualified namespace
        pid = filenameWithoutExt;
      } else {
        pid = getServicePidNamespace() + "." + filenameWithoutExt;
      }

      List<String> lines = IOUtils.readLines(new FileInputStream(configFile));
      if (lines.size() > 0 && lines.get(0).startsWith(PID_MARKER)) {
        pid = lines.get(0).substring(PID_MARKER.length()).trim();
      }
      for (String line : lines) {
        String[] contents = parseLine(configFile.getPath(), line);
        // no valid configuration line, so continue
        if (contents == null) continue;
        if (contents[0] != null) {
          pid = contents[0];
        }
        String property = contents[1];
        String value = contents[2];
        Configuration configuration = configurationAdmin.getConfiguration(pid, null);
        if (configuration != null) {
          Dictionary configProperties = configMap.get(configuration);
          if (configProperties == null) {
            configProperties = new Properties();
            configMap.put(configuration, configProperties);
          }
          if (!value.equals(configProperties.get(property))) {
            configProperties.put(property, value);
            configsToUpdate.put(configuration, configProperties);
          }
        }
      }

      for (Entry<Configuration, Dictionary> entry : configsToUpdate.entrySet()) {
        entry.getKey().update(entry.getValue());
      }
    }
  }
 protected void loadConfigFromStream(InputStream in) throws IOException {
   config = new Properties();
   try {
     byte[] byteArray = IOUtils.toByteArray(in);
     config.load(new ByteArrayInputStream(byteArray));
     originalConfig = IOUtils.readLines(new ByteArrayInputStream(byteArray), "UTF-8");
   } finally {
     in.close();
   }
 }
Beispiel #17
0
 private void assertContentIs(final String fileName, String... str) throws IOException {
   List list = IOUtils.readLines(new FileInputStream(new File(baseDir, fileName)));
   assertThat(
       "Expected content lines and number-of-lines-in-file do not match",
       list.size(),
       is(str.length));
   for (int i = 0; i < str.length; i++) {
     assertThat((String) list.get(i), is(str[i]));
   }
 }
 public SimpleCorpus(File simpleTextFile) throws IOException {
   try (FileInputStream fis = new FileInputStream(simpleTextFile)) {
     lines.addAll(
         IOUtils.readLines(fis)
             .stream()
             .filter(line -> line.matches("\\d+\\..*"))
             .collect(Collectors.toList()));
   }
   System.out.println("Loaded " + lines.size() + " example sentences");
 }
  private void checkOutputFile(String fileName) throws IOException {
    @SuppressWarnings({"unchecked", "resource"})
    List<String> outputLines = IOUtils.readLines(new FileInputStream(fileName));

    String output = "";
    for (String line : outputLines) {
      output += line;
    }

    assertEquals(EXPECTED_OUTPUT_FILE, output);
  }
  protected List<String> loadAll(String resourceName) {
    List<String> list;

    try (InputStream in = ListChooser.class.getResourceAsStream(resourceName)) {
      list = IOUtils.readLines(in, Charset.defaultCharset());
    } catch (IOException e) {
      LOG.error(e.getMessage());
      list = new ArrayList<>();
    }
    return list;
  }
 public static void main(String[] args) throws IOException {
   LingPipeClassifier classifier = new LingPipeClassifier();
   List<String> lines =
       IOUtils.readLines(
           Thread.currentThread().getContextClassLoader().getResourceAsStream(TEST_TWEETS_TXT));
   for (String line : lines) {
     String[] words = line.split("\\t");
     int sentiment = classifier.predict(words[1]);
     System.out.println(
         "Tweet: " + words[1] + " :OrigSentiment: " + words[0] + ":Prediction:" + sentiment);
   }
 }
Beispiel #22
0
 static {
   InputStream inputStream = null;
   try {
     inputStream = FileUtils.openInputStream(new File(Database.DICTIONARY));
     for (String word : IOUtils.readLines(inputStream, "UTF-8")) {
       WORDS.add(org.apache.commons.lang3.StringUtils.capitalize(word));
     }
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     IOUtils.closeQuietly(inputStream);
   }
 }
Beispiel #23
0
 @Override
 protected List<String> getAnswerSetStrings(Process exec) throws IOException {
   InputStream inputStream = exec.getInputStream();
   List<String> allLines = IOUtils.readLines(inputStream);
   List<String> answerSetLines = new ArrayList<>();
   for (String line : allLines) {
     if (line.startsWith("%") || line.startsWith("SATISFIABLE")) {
       continue;
     }
     answerSetLines.add(line);
   }
   return answerSetLines;
 }
Beispiel #24
0
 /**
  * Write manifest in the install jar.
  *
  * @throws IOException for any I/O error
  */
 protected void writeManifest() throws IOException {
   // Add splash screen configuration
   List<String> lines = IOUtils.readLines(getClass().getResourceAsStream("MANIFEST.MF"));
   if (splashScreenImage != null) {
     String destination = String.format("META-INF/%s", splashScreenImage.getName());
     mergeManager.addResourceToMerge(splashScreenImage.getAbsolutePath(), destination);
     lines.add(String.format("SplashScreen-Image: %s", destination));
   }
   lines.add("");
   File tempManifest = com.izforge.izpack.util.file.FileUtils.createTempFile("MANIFEST", ".MF");
   FileUtils.writeLines(tempManifest, lines);
   mergeManager.addResourceToMerge(tempManifest.getAbsolutePath(), "META-INF/MANIFEST.MF");
 }
  @Override
  public T from(final Reader json) {
    try {
      final String jsonString =
          isJsonFormat()
              ? JsonValue.readHjson(json).toString()
              : IOUtils.readLines(json).stream().collect(Collectors.joining());

      return this.objectMapper.readValue(jsonString, getTypeToSerialize());
    } catch (final Exception e) {
      throw new IllegalArgumentException(e);
    }
  }
  public static void main(String[] args) throws IOException {
    FileInputStream inputStream = new FileInputStream(STAGE_SKILL_SPEC_PATH);
    try {
      List<String> projectStageAndSkillsString = IOUtils.readLines(inputStream);
      String skillString = projectStageAndSkillsString.get(1);
      List<IProjectStageSkill> skills =
          Collections.unmodifiableList(extractProjectStageSkills(skillString));
      //			System.out.println(Joiner.on(",").join(skills));

      String stageString = projectStageAndSkillsString.get(0);
      List<IProjectStage> stages = extractProjectStages(stageString, skills);
      //			System.out.println(Joiner.on(",").join(stages));

      List<String> teamsEmployeesStrings =
          IOUtils.readLines(new FileInputStream(TEAMS_EMPLOYEES_SPEC_PATH));
      List<ITeam> teams = extractTeamsList(teamsEmployeesStrings, skills);
      //	    	System.out.println(Joiner.on(",").join(teams));

      List<String> epicsStrings = IOUtils.readLines(new FileInputStream(EPICS_SPEC_PATH));
      Map<IEpic, Map<IStory, Set<String>>> epicsToStoryPredsMap =
          extractEpicsToStoryPredsMap(epicsStrings, stages, skills);
      Set<IEpic> epics = epicsToStoryPredsMap.keySet();
      //			System.out.println(Joiner.on(",").join(epics));

      Set<IStoryRelation> storyRelations =
          extractStoryRelations(epicsToStoryPredsMap.values(), epics);
      BusinessDomainModel domainModel =
          new BusinessDomainModel(Sets.newHashSet(teams), stages, epics, storyRelations);

      IRoadmapProblem problem = BusinessModelToRmppTransformer.createRoadmapProblem(domainModel);
      RmppConstructionAlgorithm algo = new RmppConstructionAlgorithm();
      IRoadmapSchedule construct = algo.construct(problem);
      System.out.println(construct);
      System.out.println("makespan: " + construct.getMakeSpan());
    } finally {
      inputStream.close();
    }
  }
 private List<String> execute(String... args) throws IOException {
   String[] command;
   if (runAsArgs != null) {
     command = new String[runAsArgs.length + args.length];
     System.arraycopy(runAsArgs, 0, command, 0, runAsArgs.length);
     System.arraycopy(args, 0, command, runAsArgs.length, args.length);
   } else {
     command = args;
   }
   Process process = new ProcessBuilder(command).start();
   @SuppressWarnings("unchecked")
   List<String> lines = IOUtils.readLines(process.getInputStream());
   return lines;
 }
 String readKeyFromFile(URL url, int majorVersion) throws IOException {
   String majorVersionStr = String.valueOf(majorVersion);
   List<String> lines = IOUtils.readLines(url.openStream());
   String defaultKey = null;
   for (String line : lines) {
     String[] parts = line.split("\\s*=\\s*");
     if (parts.length < 2) {
       defaultKey = parts[0].trim();
     }
     if (parts[0].equals(majorVersionStr)) {
       return parts[1].trim();
     }
   }
   return defaultKey;
 }
    @Override
    public List<String> getEvents() {
      try {
        List<String> events = new ArrayList<>();
        for (String line : (List<String>) IOUtils.readLines(url.openStream())) {
          Matcher m = LOG_PATTERN.matcher(line);
          m.find();
          events.add(m.group(1));
        }

        return events;
      } catch (IOException ex) {
        throw new AssertionError("Audit trail log not exposed", ex);
      }
    }
 @SuppressWarnings("unchecked")
 @ModelAttribute("svnRevisionText")
 public List<String> getSvnRevisionText() {
   if (revisionText == null) {
     List<String> txt = new ArrayList<String>();
     try {
       InputStream resourceAsStream = getClass().getResourceAsStream("/svn-revision.txt");
       txt = IOUtils.readLines(resourceAsStream);
     } catch (Exception e) {
       txt.add("Not Available : " + e.getMessage());
     }
     revisionText = txt;
   }
   return revisionText;
 }