示例#1
0
  /**
   * Creates an item iterator for the given XML fragment.
   *
   * @param xml fragment
   * @param frag fragment flag
   * @return iterator
   */
  private ItemCache toIter(final String xml, final boolean frag) {
    final ItemCache it = new ItemCache();
    try {
      String str = frag ? "<X>" + xml + "</X>" : xml;
      final Data d = CreateDB.xml(IO.get(str), context);

      for (int p = frag ? 2 : 0; p < d.meta.size; p += d.size(p, d.kind(p)))
        it.add(new DBNode(d, p));
    } catch (final IOException ex) {
      return new ItemCache(new Item[] {Str.get(Long.toString(System.nanoTime()))}, 1);
    }
    return it;
  }
示例#2
0
  /**
   * Returns a number character sequence.
   *
   * @param tb token builder
   * @param n number to be formatted
   * @param mp marker parser
   * @param form language-dependent formatter
   * @param start start character
   */
  private static void number(
      final TokenBuilder tb,
      final long n,
      final FormatParser mp,
      final Formatter form,
      final int start) {

    // count optional-digit-signs
    final String pres = mp.pres;
    int o = 0;
    for (int i = 0; i < pres.length(); ++i) {
      if (pres.charAt(i) == '#') ++o;
    }
    // count digits
    int d = 0;
    for (int i = 0; i < pres.length(); ++i) {
      final char ch = pres.charAt(i);
      if (ch >= start && ch <= start + 9) ++d;
    }

    // create string representation and build string
    final String s = Long.toString(n);
    final StringBuilder tmp = new StringBuilder();
    final int r = o + d - s.length();
    for (int i = r; i > o; i--) tmp.append((char) start);
    for (int i = 0; i < s.length(); i++) {
      tmp.append((char) (s.charAt(i) - '0' + start));
    }

    for (int p = pres.length() - 1, t = tmp.length() - 1; p >= 0 && t >= 0; p--, t--) {
      final char ch = pres.charAt(p);
      if (ch < start && ch > start + 9 && ch != '#') tmp.insert(t, ch);
    }

    // add ordinal suffix
    tb.add(tmp.toString()).add(form.ordinal(n, mp.ord));
  }
示例#3
0
  /**
   * Watches an event.
   *
   * @throws IOException I/O exception
   */
  private void watch() throws IOException {
    server.initEvents();

    // initialize server-based event handling
    if (!events) {
      out.writeString(Integer.toString(context.mprop.num(MainProp.EVENTPORT)));
      out.writeString(Long.toString(getId()));
      out.flush();
      events = true;
    }
    final String name = in.readString();
    final Sessions s = context.events.get(name);
    final boolean ok = s != null && !s.contains(this);
    final String message;
    if (ok) {
      s.add(this);
      message = WATCHING_EVENT_X;
    } else if (s == null) {
      message = EVENT_UNKNOWN_X;
    } else {
      message = EVENT_WATCHED_X;
    }
    info(Util.info(message, name), ok);
  }
示例#4
0
  @Override
  public void run() {
    // initialize the session via cram-md5 authentication
    try {
      final String ts = Long.toString(System.nanoTime());
      final byte[] address = socket.getInetAddress().getAddress();

      // send {TIMESTAMP}0
      out = PrintOutput.get(socket.getOutputStream());
      out.print(ts);
      send(true);

      // evaluate login data
      in = new BufferInput(socket.getInputStream());
      // receive {USER}0{PASSWORD}0
      final String us = in.readString();
      final String pw = in.readString();
      context.user = context.users.get(us);
      running = context.user != null && md5(string(context.user.password) + ts).equals(pw);

      // write log information
      if (running) {
        log.write(this, "LOGIN " + context.user.name, OK);
        // send {OK}
        send(true);
        server.unblock(address);
        context.add(this);
      } else {
        if (!us.isEmpty()) log.write(this, ACCESS_DENIED + COLS + us);
        new ClientDelayer(server.block(address), this, server).start();
      }
    } catch (final IOException ex) {
      Util.stack(ex);
      log.write(ex.getMessage());
      return;
    }
    if (!running) return;

    // authentification done, start command loop
    ServerCmd sc = null;
    String cmd = null;

    try {
      while (running) {
        command = null;
        try {
          final int b = in.read();
          if (b == -1) {
            // end of stream: exit session
            quit();
            break;
          }

          last = System.currentTimeMillis();
          perf.time();
          sc = ServerCmd.get(b);
          cmd = null;
          if (sc == ServerCmd.CREATE) {
            create();
          } else if (sc == ServerCmd.ADD) {
            add();
          } else if (sc == ServerCmd.WATCH) {
            watch();
          } else if (sc == ServerCmd.UNWATCH) {
            unwatch();
          } else if (sc == ServerCmd.REPLACE) {
            replace();
          } else if (sc == ServerCmd.STORE) {
            store();
          } else if (sc != ServerCmd.COMMAND) {
            query(sc);
          } else {
            // database command
            cmd = new ByteList().add(b).add(in.readBytes()).toString();
          }
        } catch (final IOException ex) {
          // this exception may be thrown if a session is stopped
          quit();
          break;
        }
        if (sc != ServerCmd.COMMAND) continue;

        // parse input and create command instance
        try {
          command = new CommandParser(cmd, context).parseSingle();
        } catch (final QueryException ex) {
          // log invalid command
          final String msg = ex.getMessage();
          log.write(this, cmd, ERROR_C + msg);
          // send 0 to mark end of potential result
          out.write(0);
          // send {INFO}0
          out.writeString(msg);
          // send 1 to mark error
          send(false);
          continue;
        }

        // start timeout
        command.startTimeout(context.mprop.num(MainProp.TIMEOUT));
        log.write(this, command.toString().replace('\r', ' ').replace('\n', ' '));

        // execute command and send {RESULT}
        boolean ok = true;
        String info;
        try {
          command.execute(context, new EncodingOutput(out));
          info = command.info();
        } catch (final BaseXException ex) {
          ok = false;
          info = ex.getMessage();
          if (info.startsWith(INTERRUPTED)) info = TIMEOUT_EXCEEDED;
        }
        // stop timeout
        command.stopTimeout();

        // send 0 to mark end of result
        out.write(0);
        // send info
        info(info, ok);

        // stop console
        if (command instanceof Exit) {
          command = null;
          quit();
        }
      }
    } catch (final IOException ex) {
      log.write(this, sc == ServerCmd.COMMAND ? cmd : sc, ERROR_C + ex.getMessage());
      Util.debug(ex);
      command = null;
      quit();
    }
    command = null;
  }