예제 #1
1
 public static boolean testCommand(
     final Context context,
     final String command,
     final String contains,
     final List<String> arguments) {
   try {
     final List<String> commandAndArgs = new ArrayList<String>();
     commandAndArgs.add(command);
     commandAndArgs.addAll(arguments);
     final ProcessBuilder pb = new ProcessBuilder(commandAndArgs);
     logCommand(context, pb);
     final Process compilation = pb.start();
     final ConsumeStream result = ConsumeStream.start(compilation.getInputStream(), null);
     final ConsumeStream error = ConsumeStream.start(compilation.getErrorStream(), null);
     compilation.waitFor();
     result.join();
     error.join();
     return error.output.toString().contains(contains)
         || result.output.toString().contains(contains);
   } catch (IOException ex) {
     context.log(ex.getMessage());
     return false;
   } catch (InterruptedException ex) {
     context.log(ex.getMessage());
     return false;
   }
 }
예제 #2
0
 public static List<File> findFiles(
     final Context context, final File path, final List<String> extensions) {
   context.log("Searching for files...");
   for (final String ext : extensions) {
     context.log("Matching: " + ext);
   }
   final List<File> foundFiles = new LinkedList<File>();
   findFiles(context, path, foundFiles, extensions);
   return foundFiles;
 }
예제 #3
0
 private static void logCommand(final Context context, final ProcessBuilder builder) {
   final StringBuilder description = new StringBuilder("Running: ");
   for (final String arg : builder.command()) {
     description.append(arg).append(" ");
   }
   context.log(description.toString());
 }
예제 #4
0
 private static void unpackZip(
     final Context context,
     final File path,
     final URL remoteUrl,
     final ArrayList<File> unpackedFiles,
     final int retry)
     throws IOException {
   try {
     final InputStream response = remoteUrl.openConnection().getInputStream();
     final ZipInputStream zip = new ZipInputStream(new BufferedInputStream(response));
     ZipEntry entry;
     final byte[] buffer = new byte[8192];
     while ((entry = zip.getNextEntry()) != null) {
       long size = 0;
       final File file = new File(path, entry.getName());
       unpackedFiles.add(file);
       final FileOutputStream fos = new FileOutputStream(file);
       int len;
       while ((len = zip.read(buffer)) != -1) {
         fos.write(buffer, 0, len);
         size += len;
       }
       fos.close();
       context.log("Unpacked: " + entry.getName() + ". Size: " + (size / 1024) + "kB");
       zip.closeEntry();
     }
     zip.close();
   } catch (IOException io) {
     context.error(io);
     for (final File f : unpackedFiles) {
       if (f.delete()) {
         context.log("Cleaned up: " + f);
       } else {
         context.log("Failed to clean up: " + f);
       }
     }
     if (retry > 0) {
       context.log("Retrying download... from " + remoteUrl);
       unpackZip(context, path, remoteUrl, new ArrayList<File>(), retry - 1);
     } else throw io;
   }
 }
예제 #5
0
 public static void saveFile(final Context context, final File file, final String content)
     throws IOException {
   context.log("Saving file: " + file.getAbsolutePath());
   final FileOutputStream fos = new FileOutputStream(file);
   try {
     final Writer writer = new OutputStreamWriter(fos, "UTF-8");
     writer.write(content);
     writer.close();
   } finally {
     fos.close();
   }
 }
예제 #6
0
 public static Either<String> findCommand(
     final Context context, final String path, final String name, final String contains) {
   final String simple = path != null ? new File(path, name).getAbsolutePath() : name;
   if (testCommand(context, simple, contains, new ArrayList<String>())) {
     context.log("Found " + name + " in " + simple);
     return Either.success(simple);
   }
   if (isWindows()) {
     final String bat =
         path != null ? new File(path, name + ".bat").getAbsolutePath() : name + ".bat";
     if (testCommand(context, bat, contains, new ArrayList<String>())) {
       context.log("Found " + name + " in " + bat);
       return Either.success(bat);
     }
     final String cmd =
         path != null ? new File(path, name + ".cmd").getAbsolutePath() : name + ".cmd";
     if (testCommand(context, cmd, contains, new ArrayList<String>())) {
       context.log("Found " + name + " in " + cmd);
       return Either.success(cmd);
     }
   }
   return Either.fail("File not found: " + name);
 }
예제 #7
0
 @Override
 public void run() {
   if (reader == null) {
     return;
   }
   final char[] buffer = new char[8192];
   int len;
   try {
     while ((len = reader.read(buffer)) != -1) {
       output.append(buffer, 0, len);
       if (context != null) {
         context.log(buffer, len);
       }
     }
     reader.close();
   } catch (IOException ex) {
     exception = ex;
   }
 }
예제 #8
0
 private static void findFiles(
     final Context context,
     final File path,
     final List<File> foundFiles,
     final List<String> extensions) {
   for (final String fn : path.list()) {
     final File f = new File(path, fn);
     if (f.isDirectory()) {
       findFiles(context, f, foundFiles, extensions);
     } else {
       for (final String e : extensions) {
         if (f.getName().endsWith(e)) {
           context.log("Found: " + f.getAbsolutePath());
           foundFiles.add(f);
           break;
         }
       }
     }
   }
 }
예제 #9
0
  /**
   * Initializes the servlet context, based on the servlet context. Parses all context parameters
   * and passes them on to the database context.
   *
   * @param sc servlet context
   * @throws IOException I/O exception
   */
  static synchronized void init(final ServletContext sc) throws IOException {
    // skip process if context has already been initialized
    if (context != null) return;

    // set servlet path as home directory
    final String path = sc.getRealPath("/");
    System.setProperty(Prop.PATH, path);

    // parse all context parameters
    final HashMap<String, String> map = new HashMap<String, String>();
    // store default web root
    map.put(MainProp.HTTPPATH[0].toString(), path);

    final Enumeration<?> en = sc.getInitParameterNames();
    while (en.hasMoreElements()) {
      final String key = en.nextElement().toString();
      if (!key.startsWith(Prop.DBPREFIX)) continue;

      // only consider parameters that start with "org.basex."
      String val = sc.getInitParameter(key);
      if (eq(key, DBUSER, DBPASS, DBMODE, DBVERBOSE)) {
        // store servlet-specific parameters as system properties
        System.setProperty(key, val);
      } else {
        // prefix relative paths with absolute servlet path
        if (key.endsWith("path") && !new File(val).isAbsolute()) {
          val = path + File.separator + val;
        }
        // store remaining parameters (without project prefix) in map
        map.put(key.substring(Prop.DBPREFIX.length()).toUpperCase(Locale.ENGLISH), val);
      }
    }
    context = new Context(map);

    if (SERVER.equals(System.getProperty(DBMODE))) {
      new BaseXServer(context);
    } else {
      context.log = new Log(context);
    }
  }