Package com.caucho.vfs

Examples of com.caucho.vfs.MemoryStream


   * Compiles the names files.
   */
  private void executeInt(String []path, LineMap lineMap)
    throws JavaCompileException, IOException
  {
    MemoryStream tempStream = new MemoryStream();
    WriteStream error = new WriteStream(tempStream);

    try {
      // String parent = javaPath.getParent().getNativePath();

      ArrayList<String> argList = new ArrayList<String>();
      /* This isn't needed since srcDirName is in the classpath
      if ("1.2".compareTo(System.getProperty("java.version")) <= 0) {
        argList.add("-sourcepath");
        argList.add(srcDirName);
      }
      */
      argList.add("-d");
      argList.add(_compiler.getClassDirName());
      if (_compiler.getEncoding() != null) {
        String encoding = Encoding.getJavaName(_compiler.getEncoding());
        if (encoding != null && ! encoding.equals("ISO8859_1")) {
          argList.add("-encoding");
          argList.add(_compiler.getEncoding());
        }
      }
      argList.add("-classpath");
      argList.add(_compiler.getClassPath());
      ArrayList<String> args = _compiler.getArgs();
      if (args != null)
  argList.addAll(args);

      for (int i = 0; i < path.length; i++) {
  Path javaPath = _compiler.getSourceDir().lookup(path[i]);
  argList.add(javaPath.getNativePath());
      }

      if (log.isLoggable(Level.FINE)) {
        CharBuffer msg = CharBuffer.allocate();
  msg.append("javac(int)");
  for (int i = 0; i < argList.size(); i++) {
    msg.append(" ");
    msg.append(argList.get(i));
  }
        log.fine(msg.close());
      }

      String []argArray = argList.toArray(new String[argList.size()]);

      int status = -1;
     
      Thread thread = Thread.currentThread();
      ClassLoader oldLoader = thread.getContextClassLoader();
      try {
  EnvironmentClassLoader env;
  env = EnvironmentClassLoader.create(ClassLoader.getSystemClassLoader());
        thread.setContextClassLoader(env);

        try {
          Class cl = Class.forName(COMPILER, false, env);
    Constructor xtor = cl.getConstructor(new Class[] { PrintWriter.class, PrintWriter.class, boolean.class });
   
    Object value = xtor.newInstance(error.getPrintWriter(), error.getPrintWriter(), Boolean.FALSE);

    Method compile = cl.getMethod("compile", new Class[] { String[].class });

    Object result = compile.invoke(value, new Object[] { argArray });

    status = Boolean.TRUE.equals(result) ? 0 : -1;
        } catch (ClassNotFoundException e) {
          throw new JavaCompileException(L.l("Can't find internal Java compiler.  Either configure an external compiler with <javac> or use a JDK which contains a Java compiler."));
        } catch (NoSuchMethodException e) {
          throw new JavaCompileException(e);
        } catch (InstantiationException e) {
          throw new JavaCompileException(e);
        } catch (IllegalAccessException e) {
          throw new JavaCompileException(e);
        } catch (InvocationTargetException e) {
          throw new IOExceptionWrapper(e);
        }
     
  error.close();
  tempStream.close();
      } finally {
        thread.setContextClassLoader(oldLoader);
      }

      ReadStream read = tempStream.openReadAndSaveBuffer();
      JavacErrorParser parser = new JavacErrorParser();

      String errors = parser.parseErrors((InputStream) read, lineMap);
      read.close();

      if (errors != null)
  errors = errors.trim();

      if (log.isLoggable(Level.FINE)) {
  read = tempStream.openReadAndSaveBuffer();
  CharBuffer cb = new CharBuffer();
  int ch;
  while ((ch = read.read()) >= 0) {
    cb.append((char) ch);
  }
  read.close();

  log.fine(cb.toString());
      }
      /* XXX: why should warnings be sent as warning?
      else if (status == 0 && errors != null && ! errors.equals("")) {
  final String msg = errors;
 
  new com.caucho.loader.ClassLoaderContext(_compiler.getClassLoader()) {
    public void run()
    {
      log.warning(msg);
    }
  };
      }
      */

      if (status != 0)
  throw new JavaCompileException(errors);
    } finally {
      tempStream.destroy();
    }
  }
View Full Code Here


   * Compiles the names files.
   */
  private void executeInt(String []path, LineMap lineMap)
    throws JavaCompileException, IOException
  {
    MemoryStream tempStream = new MemoryStream();
    WriteStream error = new WriteStream(tempStream);

    try {
      // String parent = javaPath.getParent().getNativePath();

      ArrayList<String> argList = new ArrayList<String>();
      /* This isn't needed since srcDirName is in the classpath
      if ("1.2".compareTo(System.getProperty("java.version")) <= 0) {
        argList.add("-sourcepath");
        argList.add(srcDirName);
      }
      */
      argList.add("-d");
      argList.add(_compiler.getClassDirName());
      if (_compiler.getEncoding() != null) {
        String encoding = Encoding.getJavaName(_compiler.getEncoding());
        if (encoding != null && ! encoding.equals("ISO8859_1")) {
          argList.add("-encoding");
          argList.add(_compiler.getEncoding());
        }
      }
      argList.add("-classpath");
      argList.add(_compiler.getClassPath());
      ArrayList<String> args = _compiler.getArgs();
      if (args != null)
        argList.addAll(args);

      for (int i = 0; i < path.length; i++) {
        Path javaPath = _compiler.getSourceDir().lookup(path[i]);
        argList.add(javaPath.getNativePath());
      }

      if (log.isLoggable(Level.FINE)) {
        CharBuffer msg = CharBuffer.allocate();
        msg.append("javac(int)");
        for (int i = 0; i < argList.size(); i++) {
          msg.append(" ");
          msg.append(argList.get(i));
        }
        log.fine(msg.close());
      }

      String []argArray = argList.toArray(new String[argList.size()]);

      int status = -1;
     
      Thread thread = Thread.currentThread();
      ClassLoader oldLoader = thread.getContextClassLoader();
      try {
        EnvironmentClassLoader env;
        env = EnvironmentClassLoader.create(ClassLoader.getSystemClassLoader());
        thread.setContextClassLoader(env);

        try {
          Class cl = Class.forName(COMPILER, false, env);
          Constructor xtor = cl.getConstructor(new Class[] { PrintWriter.class, PrintWriter.class, boolean.class });

          Object value = xtor.newInstance(error.getPrintWriter(), error.getPrintWriter(), Boolean.FALSE);

          Method compile = cl.getMethod("compile", new Class[] { String[].class });

          Object result = compile.invoke(value, new Object[] { argArray });

          status = Boolean.TRUE.equals(result) ? 0 : -1;
        } catch (ClassNotFoundException e) {
          throw new JavaCompileException(L.l("Can't find internal Java compiler.  Either configure an external compiler with <javac> or use a JDK which contains a Java compiler."));
        } catch (NoSuchMethodException e) {
          throw new JavaCompileException(e);
        } catch (InstantiationException e) {
          throw new JavaCompileException(e);
        } catch (IllegalAccessException e) {
          throw new JavaCompileException(e);
        } catch (InvocationTargetException e) {
          throw new IOExceptionWrapper(e);
        }
     
        error.close();
        tempStream.close();
      } finally {
        thread.setContextClassLoader(oldLoader);
      }

      ReadStream read = tempStream.openReadAndSaveBuffer();
      JavacErrorParser parser = new JavacErrorParser();

      String errors = parser.parseErrors((InputStream) read, lineMap);
      read.close();

      if (errors != null)
        errors = errors.trim();

      if (log.isLoggable(Level.FINE)) {
        read = tempStream.openReadAndSaveBuffer();
        CharBuffer cb = new CharBuffer();
        int ch;
        while ((ch = read.read()) >= 0) {
          cb.append((char) ch);
        }
        read.close();

        log.fine(cb.toString());
      }
      /* XXX: why should warnings be sent as warning?
      else if (status == 0 && errors != null && ! errors.equals("")) {
        final String msg = errors;

        new com.caucho.loader.ClassLoaderContext(_compiler.getClassLoader()) {
          public void run()
          {
            log.warning(msg);
          }
        };
      }
      */

      if (status != 0)
        throw new JavaCompileException(errors);
    } finally {
      tempStream.destroy();
    }
  }
View Full Code Here

TOP

Related Classes of com.caucho.vfs.MemoryStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.