/** * Creates query plans. * * @param comp compiled flag */ private void plan(final boolean comp) { if (comp != options.get(MainOptions.COMPPLAN)) return; // show dot plan try { if (options.get(MainOptions.DOTPLAN)) { final String path = options.get(MainOptions.QUERYPATH); final String dot = path.isEmpty() ? "plan.dot" : new IOFile(path).name().replaceAll("\\..*?$", ".dot"); try (final BufferOutput bo = new BufferOutput(dot)) { try (final DOTSerializer d = new DOTSerializer(bo, options.get(MainOptions.DOTCOMPACT))) { d.serialize(qp.plan()); } } } // show XML plan if (options.get(MainOptions.XMLPLAN)) { info(NL + QUERY_PLAN + COL); info(qp.plan().serialize().toString()); } } catch (final Exception ex) { Util.stack(ex); } }
/** * Returns the image url. * * @param name name of image * @return url */ private static URL url(final String name) { final String path = "/img/" + name + ".png"; URL url = BaseXImages.class.getResource(path); if (url == null) { Util.stack("Image not found: " + path); url = BaseXImages.class.getResource("/img/warn.png"); } return url; }
/** * Set desktop hints (not supported by all platforms). * * @param g graphics reference */ public static void hints(final Graphics g) { if (HINTS != null && hints) { try { ((Graphics2D) g).addRenderingHints(HINTS); } catch (final Exception ex) { Util.stack(ex); hints = false; } } }
@Override public boolean lock(final boolean lock) { try { if (lock) { if (exclusiveLock()) return true; if (sharedLock()) return false; } else { if (sharedLock()) return true; } } catch (final IOException ex) { Util.stack(ex); } throw Util.notExpected((lock ? "Exclusive" : "Shared") + " lock could not be acquired."); }
@Override public synchronized void flush(final boolean all) { try { table.flush(all); if (all) { write(); texts.flush(); values.flush(); if (textIndex != null) ((DiskValues) textIndex).flush(); if (attrIndex != null) ((DiskValues) attrIndex).flush(); } } catch (final IOException ex) { Util.stack(ex); } }
/** * Returns the contents of the specified transferable. * * @param tr transferable * @return contents */ @SuppressWarnings("unchecked") public static ArrayList<Object> contents(final Transferable tr) { final ArrayList<Object> list = new ArrayList<>(); try { if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { for (final File fl : (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor)) list.add(fl); } else if (tr.isDataFlavorSupported(DataFlavor.stringFlavor)) { list.add(tr.getTransferData(DataFlavor.stringFlavor)); } } catch (final Exception ex) { Util.stack(ex); } return list; }
@Override public synchronized void close() { if (closed) return; closed = true; try { write(); table.close(); texts.close(); values.close(); close(IndexType.TEXT); close(IndexType.ATTRIBUTE); close(IndexType.FULLTEXT); } catch (final IOException ex) { Util.stack(ex); } }
/** * Reads a block from disk. * * @param b block to fetch */ private void readBlock(final int b) { if (!bm.cursor(b)) return; final Buffer bf = bm.current(); try { if (bf.dirty) writeBlock(bf); bf.pos = b; if (b >= blocks) { blocks = b + 1; } else { file.seek(bf.pos * IO.BLOCKSIZE); file.readFully(bf.data); } } catch (final IOException ex) { Util.stack(ex); } }
@Override protected void quit() throws IOException { if (!running) return; running = false; for (final ClientListener cs : context.sessions) cs.quit(); super.quit(); context.close(); try { // close interactive input if server was stopped by another process if (console) System.in.close(); esocket.close(); socket.close(); } catch (final IOException ex) { log.write(ex.getMessage()); Util.stack(ex); } console = false; }
/** Exits the session. */ public synchronized void quit() { running = false; if (log != null) log.write(this, "LOGOUT " + context.user.name, OK); // wait until running command was stopped if (command != null) { command.stop(); while (command != null) Performance.sleep(50); } context.delete(this); try { new Close().execute(context); socket.close(); if (events) { esocket.close(); // remove this session from all events in pool for (final Sessions s : context.events.values()) s.remove(this); } } catch (final Exception ex) { if (log != null) log.write(ex.getMessage()); Util.stack(ex); } }
/** * Prints the exception stack trace if the {@link Prop#debug} flag is set. * * @param ex exception */ public static void debug(final Throwable ex) { if (Prop.debug && ex != null) stack(ex); }
@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; }