Examples of RecordingUserMessageHandler


Examples of org.jostraca.util.RecordingUserMessageHandler

    }
    catch( StandardException se ) { throw se; }
    catch( Exception e ) {

      // REVIEW: this is a hack - need to move formatting out of RecordingUserMessageHandler
      RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();
      rumh.error( e );
      throw new JostracaException( rumh.toString() );
    }
  }
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    PropertySet tmps       = template.getMergedPropertySet();

    boolean     successful = false;
    PrintStream sysOut     = null;
    PrintStream sysErr     = null;
    RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();

    try {
      String   cw   = makeCodeWriter( tmps );
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );

      String jcwp = getCodeWriterPath( tmps );
      jcwp = TextUtil.replace( jcwp, ".java", ".class" );
      File jcwpf = new File( jcwp );
      String cwp = jcwpf.getAbsolutePath();

      iUserMessageHandler.debug( UserText.get(UserText.TXT_generating), "class-file:"+cwp+Standard.SPACE+cwo );

      // NOTE: has to be _generate to prevent exit
      Class  cwClass        = ClassUtil.makeClassFromFile( cwp );
      Object cwInstance     = cwClass.newInstance();
      Method generateMethod = cwClass.getMethod( "_generate", new Class[] { (new String[] {}).getClass(), Boolean.TYPE } );

      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();
      if( null != context ) {
        Method setContextMethod = cwClass.getMethod( "_setContext", new Class[] { Object.class } );
        setContextMethod.invoke( cwInstance, new Object[] { context } );
      }

      boolean throwWriterExceptions = tmps.isYes( Property.main_CodeWriter_throwExceptions );

      // DO NOT put System.out in MessageHandler as this will case infinite loop
      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      Object result = generateMethod.invoke( cwInstance, new Object[] { args, new Boolean(throwWriterExceptions) } );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      if( result instanceof Integer ) {
        successful = ( 0 == ((Integer)result).intValue() );
      }
      else {
        successful = true;
      }

      template.setResult( result );
    }

    catch( InvocationTargetException ite ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      Throwable cause = ite.getCause();
      if( null == cause ) {
        throw new ProcessException( ite );
      }
      else {
        throw new ProcessException( cause );
      }
    }

    // FIX: need a util to handle general exceptions and just rethrow StandardExceptions
    catch( StandardException se ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      throw se;
    }
    catch( Exception e ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      throw new ProcessException( e );
    }
    finally {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }

      String out = rumh.toString( UserMessageHandler.INFO, false );

      if( !TextUtil.isEmptyOrNull( out ) ) {
        // REVIEW: need to do this a better way so that it is automatic
        parseOutResult( out, template );
        iUserMessageHandler.info( out );
      }

      String err = rumh.toString( UserMessageHandler.ERROR, false );
      if( !TextUtil.isEmptyOrNull( err ) ) {
        iUserMessageHandler.error( err );
      }
    }
  }
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    PropertySet tmps       = template.getMergedPropertySet();

    boolean                     successful = false;
    PrintStream                 sysOut     = null;
    PrintStream                 sysErr     = null;
    RecordingUserMessageHandler rumh       = new RecordingUserMessageHandler();
    Object                      result     = null;

    try {
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );

      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();

      boolean throwWriterExceptions = tmps.isYes( Property.main_CodeWriter_throwExceptions );

      String script = template.getCodeWriterSource();

      Context cx = Context.enter();

      // DO NOT put System.out in MessageHandler as this will cause infinite loop
      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      Scriptable scope = cx.initStandardObjects();
      cx.evaluateString(scope, script, "CodeWriter Line Number: ", 1, null);     

      Object codewriterObj = scope.get( "codewriter", scope );
      if( null == codewriterObj || !(codewriterObj instanceof Scriptable) ) {
        throw new RuntimeException( "no codewriter in script" );
      }
      Scriptable codewriter = (Scriptable) codewriterObj;

      Object _setContextObj = codewriter.get( "_setContext", codewriter );
      if( null == _setContextObj || !(_setContextObj instanceof Function) ) {
        throw new RuntimeException( "no codewriter._setContext in script" );
      }

      Function _setContext = (Function) _setContextObj;
      _setContext.call(cx,scope,codewriter,new Object[]{context});

      Object _generateObj = codewriter.get( "_generate", codewriter );
      if( null == _generateObj || !(_generateObj instanceof Function) ) {
        throw new RuntimeException( "no codewriter._generate in script" );
      }

      Function _generate = (Function) _generateObj;
      result = _generate.call(cx,scope,codewriter,new Object[]{args,new Boolean(throwWriterExceptions)});

      template.setResult( result );
    }

    catch( StandardException se ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      throw se;
    }
    catch( Exception e ) {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }
      throw new ProcessException( e );
    }
    finally {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }

      Context.exit();

      String out = rumh.toString( UserMessageHandler.INFO, false );
      if( !TextUtil.isEmptyOrNull( out ) ) {
        parseOutResult( out, template );
        iUserMessageHandler.info( out );
      }

      String err = rumh.toString( UserMessageHandler.ERROR, false );
      if( !TextUtil.isEmptyOrNull( err ) ) {
        iUserMessageHandler.error( err );
      }
    }
  }
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    PropertySet tmps       = template.getMergedPropertySet();

    boolean     successful = false;
    PrintStream sysOut     = null;
    PrintStream sysErr     = null;
    RecordingUserMessageHandler rumh = new RecordingUserMessageHandler();

    try {
      String   cw   = tmps.get( Property.main_CodeWriter );
      String   cwo  = makeCodeWriterOptions( tmps, template );
      String[] args = ArgUtil.splitQuoted( cwo );
      String   cwp  = getCodeWriterPath( tmps );

      Properties jythonProps = new Properties();
      jythonProps.put( "python.home", tmps.get("jython.home") );
      PythonInterpreter.initialize( System.getProperties(), jythonProps, args );

      PyStringMap       dict  = new PyStringMap();
      PySystemState     pysys = new PySystemState();
      PythonInterpreter pi    = new PythonInterpreter( dict, pysys );


      // DO NOT use CommandLineUserMessageHandler here - causes infinite recursion
      MessageHandlerOutputStream  out  = new MessageHandlerOutputStream( UserMessageHandler.INFO,  rumh );
      MessageHandlerOutputStream  err  = new MessageHandlerOutputStream( UserMessageHandler.ERROR, rumh );

      // space out dots to avoid accidental replace on System DOT out search
      sysOut = System . out;
      sysErr = System . err;
   
      // set context if not null
      Object context = template.getContext();
      if( null != context ) {
        pi.set( "context", context );
      }

      System.setOut( new PrintStream( out ) );
      System.setErr( new PrintStream( err ) );

      pi.execfile( cwp );
     
      String jythonexec
        = "cw = "+cw+"()\n"
        + (null!=context?"cw._setContext( context )\n":"")
        + "cw.main( sys.argv )\n"
        + "result = cw._getResult()\n"
        ;
      pi.exec( jythonexec );
      WayPointRecorder.add( BasicWayPoint.ExecutingCodeWriter.make( template.getCodeWriterPath().getAbsolutePath() ) );

      Object result = pi.get("result");

      if( result instanceof Integer ) {
        successful = ( 0 == ((Integer)result).intValue() );
      }
      else {
        successful = true;
      }

      template.setResult( result );
    }

    // FIX: need a util to handle general exceptions and just rethrow StandardExceptions
    catch( StandardException se ) {
      throw se;
    }
    catch( Exception e ) {
      throw new ProcessException( e );
    }
    finally {
      if( null != sysOut ) {
        System.setOut( sysOut );
      }
      if( null != sysErr ) {
        System.setErr( sysErr );
      }

      String out = rumh.toString( UserMessageHandler.INFO, false );
      if( !TextUtil.isEmptyOrNull( out ) ) {
        // REVIEW: need to do this a better way so that it is automatic
        parseOutResult( out, template );
        iUserMessageHandler.info( out );
      }

      String err = rumh.toString( UserMessageHandler.ERROR, false );
      if( !TextUtil.isEmptyOrNull( err ) ) {
        iUserMessageHandler.info( err );
      }

    }
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    ps.set( Property.main_CompileCodeWriter, Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
    g.setCmdPropertySet( ps );
    Template tm = g.generate( FileUtil.findFile( "org/jostraca/test/simple.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    simple_txt = FileUtil.findFile( "org/jostraca/test/simple.txt" );
    assertTrue( simple_txt.exists() );

    assertEquals( "simple: simple", FileUtil.readFile( simple_txt ).trim() );
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    ps.set( Property.main_CompileCodeWriter, Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter, Standard.YES );
    g.setCmdPropertySet( ps );
    g.generate( FileUtil.findFile( "org/jostraca/test/parse.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    parse_txt = FileUtil.findFile( "org/jostraca/test/parse.txt" );
    assertTrue( parse_txt.exists() );

    assertEquals( "parse: parse", FileUtil.readFile( parse_txt ).trim() );
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    if( internal_txt.exists() ) {
      internal_txt.delete();
    }

    // just compile first
    RecordingUserMessageHandler rumh = null;
    Generator   g  = new Generator();
    PropertySet ps = new PropertySet();
    ps.set( Property.main_MakeBackup,         Standard.NO  );
    ps.set( Property.main_CompileCodeWriter,  Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter,  Standard.NO  );
    g.setCmdPropertySet( ps );

    g.generate( FileUtil.findFile( "org/jostraca/test/internal.jtm" ) );
    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 1, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    internal_txt = FileUtil.findFile( "org/jostraca/test/internal.txt" );
    assertTrue( !internal_txt.exists() );

    // and now execute
    ps.set( Property.main_CompileCodeWriter,    Standard.EMPTY );
    ps.set( Property.main_ExecuteCodeWriter,    Standard.YES  );

    g.setCmdPropertySet( ps );
    g.generate( FileUtil.findFile( "org/jostraca/test/internal.jtm" ), new RecordingUserMessageHandler() );

    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    assertTrue( internal_txt.exists() );
    assertEquals( "internal: internal", FileUtil.readFile( internal_txt ).trim() );
  }
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    if( context_txt.exists() ) {
      context_txt.delete();
    }

    // just compile first
    RecordingUserMessageHandler rumh = null;
    Generator   g  = new Generator();
    PropertySet ps = new PropertySet();
    ps.set( Property.main_MakeBackup,         Standard.NO  );
    ps.set( Property.main_CompileCodeWriter,  Standard.YES );
    ps.set( Property.main_ExecuteCodeWriter,  Standard.NO  );
    g.setCmdPropertySet( ps );
   
    String c = "context";
    g.setContext( c );

    g.generate( FileUtil.findFile( "org/jostraca/test/context.jtm" ) );
    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 1, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    context_txt = FileUtil.findFile( "org/jostraca/test/context.txt" );
    assertTrue( !context_txt.exists() );

    // and now execute
    ps.set( Property.main_CompileCodeWriter,    Standard.EMPTY );
    ps.set( Property.main_ExecuteCodeWriter,    Standard.YES  );

    g.setCmdPropertySet( ps );
    g.generate( FileUtil.findFile( "org/jostraca/test/context.jtm" ), new RecordingUserMessageHandler() );

    rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 6, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    assertTrue( context_txt.exists() );
    assertEquals( "context: context", FileUtil.readFile( context_txt ).trim() );
  }
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

    Generator   g  = new Generator();
    PropertySet ps = Jostraca.parseCommandLine( "-Bcg -Dfoo=bar", new ArrayList(), new ArrayList() );
    g.setCmdPropertySet( ps );
    Template tm = g.generate( FileUtil.findFile( "org/jostraca/test/cmdargs.jtm" ) );

    RecordingUserMessageHandler rumh = (RecordingUserMessageHandler) g.getUserMessageHandler();
    assertTrue( null != rumh );
    //System.out.println( rumh );
    assertEquals( 7, ((Vector)rumh.get( UserMessageHandler.DEBUG )).size() );
    assertEquals( 2, ((Vector)rumh.get( UserMessageHandler.INFO  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.WARN  )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.ERROR )).size() );
    assertEquals( 0, ((Vector)rumh.get( UserMessageHandler.FATAL )).size() );

    cmdargs_txt = FileUtil.findFile( "org/jostraca/test/cmdargs.txt" );
    assertTrue( cmdargs_txt.exists() );

    assertEquals( "cmdargs: cmdargs, foo: bar", FileUtil.readFile( cmdargs_txt ).trim() );
View Full Code Here

Examples of org.jostraca.util.RecordingUserMessageHandler

  }



  public void testMessages() {
    RecordingUserMessageHandler umh = new RecordingUserMessageHandler();
    umh.add( UserMessageHandler.DEBUG, "debug01" );
    umh.debug( "debug02" );

    assertEquals( "DEBUG: debug01\nDEBUG: debug02\n", umh.toString( ) );
    assertEquals( "DEBUG: debug01\nDEBUG: debug02\n", umh.toString( UserMessageHandler.DEBUG ) );

    umh.info"info01" );
    umh.warn"warn01" );
    umh.error( "error01" );
    umh.fatal( "fatal01" );

    assertEquals( "DEBUG: debug01\nDEBUG: debug02\n", umh.toString( UserMessageHandler.DEBUG ) );
    assertEquals( "INFO: info01\n", umh.toString( UserMessageHandler.INFO ) );
    assertEquals( "WARN: warn01\n", umh.toString( UserMessageHandler.WARN ) );
    assertEquals( "FATAL: fatal01\n", umh.toString( UserMessageHandler.FATAL ) );
    assertEquals( "ERROR: error01\n", umh.toString( UserMessageHandler.ERROR ) );

    assertEquals( "DEBUG: debug01\n"+
                  "DEBUG: debug02\n"+
                  "INFO: info01\n"+
                  "WARN: warn01\n"+
                  "ERROR: error01\n"+
                  "FATAL: fatal01\n", umh.toString() );

  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.