Package ru.vassaev.core.thread

Examples of ru.vassaev.core.thread.Process


  public State process(Context cntx) throws TaskException, SysException,
      InterruptedException {
    State st = paramsValidateAndPrepare(cntx);
    if (st != null)
      return st;
    Process prc = Process.currentProcess();
    FindStringStream fss = new FindStringStream();
    fss.add("<%prm.", Prm.class);
    fss.add("%>", End.class);
    fss.setOutSkipped(true);
    // //////
    FileInputStream fis = null;
    java.io.InputStreamReader isr = null;
    try {
      if (source instanceof File) {
        try {
          fis = new FileInputStream(((File) source).getCanonicalPath());
          prc.regResource(fis);
          isr = new java.io.InputStreamReader(fis, "utf-8");
          prc.regResource(isr);
        } catch (FileNotFoundException e) {
          throw new TaskException(State.DONE_ERR, e);
        } catch (UnsupportedEncodingException e) {
          try {
            fis.close();
          } catch (IOException e1) {
            e1.printStackTrace();
          }
          throw new TaskException(State.DONE_ERR, e);
        } catch (IOException e) {
          try {
            fis.close();
          } catch (IOException e1) {
            e1.printStackTrace();
          }
          throw new TaskException(State.DONE_ERR, e);
        }
      }
      StringBuffer sb = new StringBuffer();
      prc.regResourceName(sb, "msg.full");
      int ch;
      Object r;
      Object prev = null;
      Object o = null;
      try {
        while ((ch = isr.read()) != -1) {
          fss.write((char) ch);
          if (fss.available() > 0)
            while ((r = fss.readObject()) != null) {
              if (r instanceof Skipped) {
                if ((prev != null) && (prev instanceof Prm)) {
                  ((Prm) prev).name = r.toString();
                  try {
                    o = cntx.getPrmByFullName(((Prm) prev).name);
                  } catch(Throwable e) {
                    o = e.getMessage();
                  }
                  String v = o.toString();
                  prc.regResourceName(v, "msg.part");
                  if (!Null.equ(v))
                    sb.append(v);
                  prev = null;
                } else {
                  String v = ((Skipped) r).getValue();
                  prc.regResourceName(v, "msg.part");
                  if (!Null.equ(v))
                    sb.append(v);
                }
                cntx.getPrmByFullName("update");
              } else if (r instanceof Prm) {
                prev = r;
                // System.out.println(r.toString() + "\t=\t" + r.getClass());
              } else {
                prev = null;
              }
            }
        }
        fss.write(FindStringStream.EOF);

        if (fss.available() > 0)
          while ((r = fss.readObject()) != null)
            if (r instanceof Skipped) {
              if ((prev != null) && (prev instanceof Prm)) {
                ((Prm) prev).name = r.toString();
                try {
                  o = cntx.getPrmByFullName(((Prm) prev).name);
                } catch(Throwable e) {
                  o = e.getMessage();
                }
                String v = o.toString();
                prc.regResourceName(v, "msg.part");
                if (!Null.equ(v))
                  sb.append(v);
                prev = null;
              } else {
                String v = ((Skipped) r).getValue();
                prc.regResourceName(v, "msg.part");
                if (!Null.equ(v))
                  sb.append(v);
              }
              cntx.getPrmByFullName("update");
            } else if (r instanceof Prm) {
View Full Code Here


      xr.setErrorHandler(handler);
      if (x != null)
        if (x.exists())
          r = new FileInputStream(x.getCanonicalPath());
        else r = null;
      Process p = prcs == null ? new Process() : prcs.occupyOrNew();
      try {
        p.setTarget(new Prc());
        p.setName("QUEUE");
        p.start(cntx);
        p.waitStartTarget();
        if (r != null)
          xr.parse(new InputSource(r));
      } finally {
        queue.put(new Data(TYPE.END));
        p.waitEndProcessing();
        if (prcs == null) p.kill();
        else prcs.free(p);
        if (r != null)
          r.close();
      }
    } catch (FileNotFoundException e) {
View Full Code Here

  private Process prc;
  private Queue<Data> queue = new Queue<Data>();
  private class Prc implements Processed<Context> {

    public void processing(Context cntx) throws Throwable {
      Process prc = Process.currentProcess();
      Data data = null;
      while(TYPE.ELEMENT.equals((data = queue.get(0)).type)) {
        data.reg(prc);
        cntx.getPrmByFullName("update");
      }
View Full Code Here

    String hostname = "localhost";
    InetAddress addr = InetAddress.getLocalHost();
    addr.getAddress();
    hostname = addr.getHostName();
   
    Process prc = Process.currentProcess();
    String poolProcessName = el.getAttribute("process_pool");
    executor = (PoolThread) ApplicationManager.getPoolByName(poolProcessName, Process.class);
    server = HttpServer.create(new InetSocketAddress(port), 0);
    server.createContext("/", new HttpHandler(prc, this));
    server.setExecutor(executor);
    server.start();
   
    System.out.println("Server (http://" + hostname + ":" + port + "/" + ") is ready...");
   
    try {
      while (true) {
        if (prc.isWillBreak())
          return;
        Thread.sleep(2000);//!!!
      }
    } catch (InterruptedException e1) {
      throw new SysException(e1);
View Full Code Here

        server = HttpServer.create(new InetSocketAddress(port), 0);
      server.createContext(path, new XMLHttpHandler(cntx));
      server.setExecutor(executor);
      server.start();

      Process prc = Process.currentProcess();
      while (true) {
        try {
          if (prc.isWillBreak())
            return State.BROKEN;
          State st = cntx.ta.getState(cntx.id_task);
          if (st.getType().equals(TypeOfState.LAST))
            return st;
          if (st.equals(State.BREAKING))
View Full Code Here

        Object o = info.prms.get(k);
        if (o != null && o instanceof Saved) {
          Saved s = (Saved) o;
          Saved.TIME t = s.timeSaved();
          if (t != null && t.equals(tm)) {
            Process prc = pool.occupy();
            if (prc != null) {
              Method m;
              try {
                m = s.getClass().getMethod("save", Context.class);
                list.add(prc.startMethod(m, s, cntx));
              } catch (SecurityException e1) {
                e1.printStackTrace();
              } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
              } finally {
View Full Code Here

 
  public State process(Context cntx) throws TaskException, SysException, InterruptedException {
    State st = paramsValidateAndPrepare(cntx);
    if (st != null)
      return st;
    Process prc = Process.currentProcess();
    URL url = null;
    URLConnection con = null;
    try {
      url = new URL(urls);
      if (proxy != null) {
        con = url.openConnection(proxy);
      } else
        con = url.openConnection();
    } catch (MalformedURLException e) {
      throw new TaskException(State.DONE_ERR, e);
    } catch (IOException e) {
      throw new TaskException(State.DONE_ERR, e);
    }
    Map<String, Object> headers = cntx.getGroupParams(grp_in + ".header");
    String encode = cntx.getPrmNvl("charset", "UTF8");

    Object body = cntx.getPrmByFullName(grp_in + "/body");
    if (body != null) {
      long length = -1;
      con.setDoOutput(true);
      InputStream is = null;
      OutputByteBuffer obb = null;
      if (body instanceof InputStream) {
        try {
          obb = Process.getByteBuffer(8000, (InputStream) body);
          is = obb.getIS();
        } catch (IOException e) {
          e.printStackTrace();
        }
      } else if (body instanceof OutputByteBuffer) {
        obb = (OutputByteBuffer)body;
        is = obb.getIS();
      } else if (body instanceof Element) {
        Element e = (Element) body;
        if (Strings.parseBooleanNvl(cntx.getPrmByFullName("skip-header-xml-body"), false)) {
          String n = e.getNodeName();
          try {
            obb = Process.getByteBuffer(8000, ("<"+n).getBytes(encode));
          } catch (UnsupportedEncodingException e1) {
            throw new TaskException(State.DONE_ERR, e1);
          }
        } else
          obb = Process.getByteBuffer(8000);
        XMLFileWaiter.putStream(e, obb, encode);
        try {
          obb.close();
          length = obb.getLength();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
        is = obb.getIS();
      } else if (body instanceof Document) {
        Document e = (Document) body;
        if (Strings.parseBooleanNvl(cntx.getPrmByFullName("skip-header-xml-body"), false)) {
          String n = e.getDocumentElement().getNodeName();
          try {
            obb = Process.getByteBuffer(8000, ("<"+n).getBytes(encode));
          } catch (UnsupportedEncodingException e1) {
            throw new TaskException(State.DONE_ERR, e1);
          }
        } else
          obb = Process.getByteBuffer(8000);
        XMLFileWaiter.putStream(e, obb, encode);
        try {
          obb.close();
          length = obb.getLength();
        } catch (IOException e1) {
          e1.printStackTrace();
        }
        is = obb.getIS();
      } else {
        String r = Strings.getString(body);
        byte[] b = null;
        try {
          b = r.getBytes(encode);
          length = b.length;
        } catch (UnsupportedEncodingException e) {
          throw new TaskException(State.DONE_ERR, e);
        }
        is = new ByteArrayInputStream(b);
      }
      System.out.println("=============Request=============");
      if (length >= 0) {
        prc.regResourceName(obb, "query.body.length");
        String n = "Content-Length";
        String v = Long.toString(length);
        con.addRequestProperty(n, v);
        System.out.println(((n == null) ? "" : n + ":") + v);
      }
      for(Map.Entry<String, Object> i : headers.entrySet()) {
        String n = i.getKey();
        n = n.substring((grp_in + ".header/").length());
        Object v = i.getValue();
        if (v != null) {
          if (v instanceof Type)
            try {
              v = ((Type)v).getValue(cntx);
            } catch (CalculateException e) {
              v = null;
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          con.addRequestProperty(n, Strings.getString(v));
          System.out.println(((n == null) ? "" : n + ":") + Strings.getString(v));
        } else
          con.setRequestProperty(n, null);
      }

      {
        int ch = 0;
        try {
          OutputStream os = con.getOutputStream();
          while((ch = is.read()) != -1) {
            System.out.print((char)ch);
            os.write(ch);
          }
          os.write("\r\n".getBytes());
          System.out.print("\r\n");
        } catch (IOException e) {
          throw new TaskException(State.DONE_ERR, e);
        } finally {
          try {
            is.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    } else {
      for(Map.Entry<String, Object> i : headers.entrySet()) {
        String n = i.getKey();
        n = n.substring((grp_in + ".header/").length());
        Object v = i.getValue();
        if (v != null)
          con.addRequestProperty(n, Strings.getString(v));
        else
          con.setRequestProperty(n, null);
      }
    }

    System.out.println("=============Response=============");
    InputStream is = null;
    try {
      Map<String, List<String>> flds = con.getHeaderFields();
      for(Map.Entry<String, List<String>> fld : flds.entrySet()) {
        List<String> val = fld.getValue();
        StringBuffer sb = new StringBuffer();
        for(String v:val) { 
          sb.append(v).append("\r\n");
        }
        if (sb.length() > 0)
          sb.setLength(sb.length() - 2);
        String n = fld.getKey();
        System.out.println(((n == null)? "" : n + ":") + sb.toString());
        cntx.ta.setParamObject(cntx.id_task, grp_out+".header/" + fld.getKey(), sb.toString());
      }
      is = con.getInputStream();
      int l = con.getContentLength();
      int l_b = l >= 0 ? (l < 1024*1024 ? l : 1024*1024) : 8000;
      OutputByteBuffer obb = new OutputByteBuffer(l_b);
      obb.write(is);
      obb.close();
      prc.regResourceName(obb, "msg.body");
      if (Strings.parseBooleanNvl(cntx.getPrmString("save-out-body"), true)) {
        String fn = cntx.getPrmString("filename");
        if (fn == null)
          cntx.ta.setParamObject(cntx.id_task, grp_out + "/body", obb);
        else {
View Full Code Here

    }
    if (State.BREAKING.equals(cur))
      return State.BROKEN;
    // Запуск процесса ожидания соединений
    ServerSocketAcceptProcessed ssap = new ServerSocketAcceptProcessed(prcs, fSocketProcessed);
    Process p = prcs.start_target("ServerSocket " + ss.getInetAddress().toString(), ssap, ss, null);
    // Ожидание прерывания задания
    while (!State.BREAKING.equals(cur = cntx.ta.getState(cntx.id_task))) {
      if (!cur.equals(State.PROCESSING))
        throw new TaskException(State.DONE_ERR, "System error");
      synchronized (sync) {
        sync.wait(1000);//TODO
      }
    }
    // Завершение обработчика
    p.willBreak();
    p.interrupt();
    return State.BROKEN;
  }
View Full Code Here

      getContext().log(false, "Start processing message ", prms.getKey());
      ITaskAPI ta = getTaskAPI();
      long id_processor = getProcessorID();
      Context child;
      Context cntx = getContext();
      Process cp = Process.currentProcess();
      try {
        cp.regResourceName(prms, "msg");
        PrmInterface prm = prms.getPrmInterface();
        ArrayList<String> flds = prm.getFieldNames();
        for (String k : flds) {
          String v = prm.getField(k);
          cp.regResourceName(v, "msg." + k);
        }
        child = getChild().createAndReadyTask(ta, cntx, id_processor, msg_grp_in, prms);
        if (child == null)
          return;
        long wait = Strings.parseIntegerNvl(cntx.getPrmString("wait"), 0);
View Full Code Here

    public void handle(HttpExchange httpExchange) throws IOException {
      Process.currentProcess().setParent(main_thread);
      try {
        InetSocketAddress ra = httpExchange.getRemoteAddress();
        Process cp = Process.currentProcess();
        Map<String, Object> prms = new HashMap<String, Object>();

        setPrms(cp, prms, "protocol", httpExchange.getProtocol());
        setPrms(cp, prms, "method", httpExchange.getRequestMethod());
        //setPrms(cp, prms, "remote.hostName", ra.getHostName());
View Full Code Here

TOP

Related Classes of ru.vassaev.core.thread.Process

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.