/**
   * Gets Hibernate session.
   *
   * @param tx Cache transaction.
   * @return Session.
   */
  Session session(@Nullable GridCacheTx tx) {
    Session ses;

    if (tx != null) {
      ses = tx.meta(ATTR_SES);

      if (ses == null) {
        ses = sesFactory.openSession();

        ses.beginTransaction();

        // Store session in transaction metadata, so it can be accessed
        // for other operations on the same transaction.
        tx.addMeta(ATTR_SES, ses);

        if (log.isDebugEnabled())
          log.debug("Hibernate session open [ses=" + ses + ", tx=" + tx.xid() + "]");
      }
    } else {
      ses = sesFactory.openSession();

      ses.beginTransaction();
    }

    return ses;
  }
  /**
   * Creates new HTTP requests handler.
   *
   * @param hnd Handler.
   * @param authChecker Authentication checking closure.
   * @param log Logger.
   */
  GridJettyRestHandler(
      GridRestProtocolHandler hnd, GridClosure<String, Boolean> authChecker, GridLogger log) {
    assert hnd != null;
    assert log != null;

    this.hnd = hnd;
    this.log = log;
    this.authChecker = authChecker;

    // Init default page and favicon.
    try {
      initDefaultPage();

      if (log.isDebugEnabled()) log.debug("Initialized default page.");
    } catch (IOException e) {
      U.warn(log, "Failed to initialize default page: " + e.getMessage());
    }

    try {
      initFavicon();

      if (log.isDebugEnabled())
        log.debug(
            favicon != null ? "Initialized favicon, size: " + favicon.length : "Favicon is null.");
    } catch (IOException e) {
      U.warn(log, "Failed to initialize favicon: " + e.getMessage());
    }
  }
  /** {@inheritDoc} */
  @Override
  public void txEnd(GridCacheTx tx, boolean commit) throws GridException {
    init();

    Session ses = tx.removeMeta(ATTR_SES);

    if (ses != null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null) {
        try {
          if (commit) {
            ses.flush();

            hTx.commit();
          } else hTx.rollback();

          if (log.isDebugEnabled())
            log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
        } catch (HibernateException e) {
          throw new GridException(
              "Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e);
        } finally {
          ses.close();
        }
      }
    }
  }
  /** {@inheritDoc} */
  @Override
  public void put(@Nullable GridCacheTx tx, K key, @Nullable V val) throws GridException {
    init();

    if (log.isDebugEnabled())
      log.debug("Store put [key=" + key + ", val=" + val + ", tx=" + tx + ']');

    if (val == null) {
      remove(tx, key);

      return;
    }

    Session ses = session(tx);

    try {
      GridCacheHibernateBlobStoreEntry entry =
          new GridCacheHibernateBlobStoreEntry(toBytes(key), toBytes(val));

      ses.saveOrUpdate(entry);
    } catch (HibernateException e) {
      rollback(ses, tx);

      throw new GridException(
          "Failed to put value to cache store [key=" + key + ", val" + val + "]", e);
    } finally {
      end(ses, tx);
    }
  }
  /** {@inheritDoc} */
  @SuppressWarnings({"unchecked", "RedundantTypeArguments"})
  @Override
  public V load(@Nullable GridCacheTx tx, K key) throws GridException {
    init();

    if (log.isDebugEnabled()) log.debug("Store load [key=" + key + ", tx=" + tx + ']');

    Session ses = session(tx);

    try {
      GridCacheHibernateBlobStoreEntry entry =
          (GridCacheHibernateBlobStoreEntry)
              ses.get(GridCacheHibernateBlobStoreEntry.class, toBytes(key));

      if (entry == null) return null;

      return fromBytes(entry.getValue());
    } catch (HibernateException e) {
      rollback(ses, tx);

      throw new GridException("Failed to load value from cache store with key: " + key, e);
    } finally {
      end(ses, tx);
    }
  }
  /** {@inheritDoc} */
  @Override
  public void handle(String target, Request req, HttpServletRequest srvReq, HttpServletResponse res)
      throws IOException, ServletException {
    if (log.isDebugEnabled())
      log.debug("Handling request [target=" + target + ", req=" + req + ", srvReq=" + srvReq + ']');

    if (target.startsWith("/gridgain")) {
      processRequest(target, srvReq, res);

      req.setHandled(true);
    } else if (target.startsWith("/favicon.ico")) {
      if (favicon == null) {
        res.setStatus(HttpServletResponse.SC_NOT_FOUND);

        req.setHandled(true);

        return;
      }

      res.setStatus(HttpServletResponse.SC_OK);

      res.setContentType("image/x-icon");

      res.getOutputStream().write(favicon);
      res.getOutputStream().flush();

      req.setHandled(true);
    } else {
      if (dfltPage == null) {
        res.setStatus(HttpServletResponse.SC_NOT_FOUND);

        req.setHandled(true);

        return;
      }

      res.setStatus(HttpServletResponse.SC_OK);

      res.setContentType("text/html");

      res.getWriter().write(dfltPage);
      res.getWriter().flush();

      req.setHandled(true);
    }
  }
  /** {@inheritDoc} */
  @SuppressWarnings({"JpaQueryApiInspection", "JpaQlInspection"})
  @Override
  public void remove(@Nullable GridCacheTx tx, K key) throws GridException {
    init();

    if (log.isDebugEnabled()) log.debug("Store remove [key=" + key + ", tx=" + tx + ']');

    Session ses = session(tx);

    try {
      Object obj = ses.get(GridCacheHibernateBlobStoreEntry.class, toBytes(key));

      if (obj != null) ses.delete(obj);
    } catch (HibernateException e) {
      rollback(ses, tx);

      throw new GridException("Failed to remove value from cache store with key: " + key, e);
    } finally {
      end(ses, tx);
    }
  }
  /**
   * Checks availability of a classpath resource.
   *
   * @param name Resource name.
   * @return {@code true} if resource is available and ready for read, {@code false} otherwise.
   */
  private boolean resourceAvailable(String name) {
    InputStream cfgStream =
        Thread.currentThread().getContextClassLoader().getResourceAsStream(name);

    if (cfgStream == null) {
      log.error("Classpath resource not found: " + name);

      return false;
    }

    try {
      // Read a single byte to force actual content access by JVM.
      cfgStream.read();

      return true;
    } catch (IOException e) {
      log.error("Failed to read classpath resource: " + name, e);

      return false;
    } finally {
      U.close(cfgStream, log);
    }
  }
  /**
   * Process HTTP request.
   *
   * @param act Action.
   * @param req Http request.
   * @param res Http response.
   */
  private void processRequest(String act, HttpServletRequest req, HttpServletResponse res) {
    res.setContentType("application/json");
    res.setCharacterEncoding("UTF-8");

    GridRestCommand cmd = command(req);

    if (cmd == null) {
      res.setStatus(HttpServletResponse.SC_BAD_REQUEST);

      return;
    }

    if (!authChecker.apply(req.getHeader("X-Signature"))) {
      res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

      return;
    }

    GridRestResponse cmdRes;

    Map<String, Object> params = parameters(req);

    try {
      GridRestRequest cmdReq = createRequest(cmd, params, req);

      if (log.isDebugEnabled()) log.debug("Initialized command request: " + cmdReq);

      cmdRes = hnd.handle(cmdReq);

      if (cmdRes == null)
        throw new IllegalStateException("Received null result from handler: " + hnd);

      byte[] sesTok = cmdRes.sessionTokenBytes();

      if (sesTok != null) cmdRes.setSessionToken(U.byteArray2HexString(sesTok));

      res.setStatus(HttpServletResponse.SC_OK);
    } catch (Exception e) {
      res.setStatus(HttpServletResponse.SC_OK);

      U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e);

      cmdRes = new GridRestResponse(STATUS_FAILED, e.getMessage());
    } catch (Throwable e) {
      U.error(log, "Failed to process HTTP request [action=" + act + ", req=" + req + ']', e);

      throw e;
    }

    JsonConfig cfg = new GridJettyJsonConfig();

    // Workaround for not needed transformation of string into JSON object.
    if (cmdRes.getResponse() instanceof String)
      cfg.registerJsonValueProcessor(cmdRes.getClass(), "response", SKIP_STR_VAL_PROC);

    if (cmdRes.getResponse() instanceof GridClientTaskResultBean
        && ((GridClientTaskResultBean) cmdRes.getResponse()).getResult() instanceof String)
      cfg.registerJsonValueProcessor(cmdRes.getResponse().getClass(), "result", SKIP_STR_VAL_PROC);

    JSON json;

    try {
      json = JSONSerializer.toJSON(cmdRes, cfg);
    } catch (JSONException e) {
      U.error(log, "Failed to convert response to JSON: " + cmdRes, e);

      json = JSONSerializer.toJSON(new GridRestResponse(STATUS_FAILED, e.getMessage()), cfg);
    }

    try {
      if (log.isDebugEnabled())
        log.debug("Parsed command response into JSON object: " + json.toString(2));

      res.getWriter().write(json.toString());

      if (log.isDebugEnabled())
        log.debug(
            "Processed HTTP request [action=" + act + ", jsonRes=" + cmdRes + ", req=" + req + ']');
    } catch (IOException e) {
      U.error(log, "Failed to send HTTP response: " + json.toString(2), e);
    }
  }
  /**
   * Initializes store.
   *
   * @throws GridException If failed to initialize.
   */
  private void init() throws GridException {
    if (initGuard.compareAndSet(false, true)) {
      if (log.isDebugEnabled()) log.debug("Initializing cache store.");

      try {
        if (sesFactory != null)
          // Session factory has been provided - nothing to do.
          return;

        if (!F.isEmpty(hibernateCfgPath)) {
          try {
            URL url = new URL(hibernateCfgPath);

            sesFactory = new Configuration().configure(url).buildSessionFactory();

            if (log.isDebugEnabled()) log.debug("Configured session factory using URL: " + url);

            // Session factory has been successfully initialized.
            return;
          } catch (MalformedURLException e) {
            if (log.isDebugEnabled())
              log.debug("Caught malformed URL exception: " + e.getMessage());
          }

          // Provided path is not a valid URL. File?
          File cfgFile = new File(hibernateCfgPath);

          if (cfgFile.exists()) {
            sesFactory = new Configuration().configure(cfgFile).buildSessionFactory();

            if (log.isDebugEnabled())
              log.debug("Configured session factory using file: " + hibernateCfgPath);

            // Session factory has been successfully initialized.
            return;
          }

          // Provided path is not a file. Classpath resource?
          sesFactory = new Configuration().configure(hibernateCfgPath).buildSessionFactory();

          if (log.isDebugEnabled())
            log.debug("Configured session factory using classpath resource: " + hibernateCfgPath);
        } else {
          if (hibernateProps == null) {
            U.warn(
                log, "No Hibernate configuration has been provided for store (will use default).");

            hibernateProps = new Properties();

            hibernateProps.setProperty("hibernate.connection.url", DFLT_CONN_URL);
            hibernateProps.setProperty("hibernate.show_sql", DFLT_SHOW_SQL);
            hibernateProps.setProperty("hibernate.hbm2ddl.auto", DFLT_HBM2DDL_AUTO);
          }

          Configuration cfg = new Configuration();

          cfg.setProperties(hibernateProps);

          assert resourceAvailable(MAPPING_RESOURCE);

          cfg.addResource(MAPPING_RESOURCE);

          sesFactory = cfg.buildSessionFactory();

          if (log.isDebugEnabled())
            log.debug("Configured session factory using properties: " + hibernateProps);
        }
      } catch (HibernateException e) {
        throw new GridException("Failed to initialize store.", e);
      } finally {
        initLatch.countDown();
      }
    } else if (initLatch.getCount() > 0) U.await(initLatch);

    if (sesFactory == null) throw new GridException("Cache store was not properly initialized.");
  }
  /**
   * @param gridName Grid instance name. Can be {@code null}.
   * @param exec Executor service.
   * @param parentLog Parent logger.
   */
  static void runBackgroundCheck(String gridName, Executor exec, GridLogger parentLog) {
    assert exec != null;
    assert parentLog != null;

    final GridLogger log = parentLog.getLogger(GridDiagnostic.class);

    try {
      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-1", log) {
            @Override
            public void body() {
              try {
                InetAddress localHost = U.getLocalHost();

                if (!localHost.isReachable(REACH_TIMEOUT)) {
                  U.warn(
                      log,
                      "Default local host is unreachable. This may lead to delays on "
                          + "grid network operations. Check your OS network setting to correct it.",
                      "Default local host is unreachable.");
                }
              } catch (IOException ignore) {
                U.warn(
                    log,
                    "Failed to perform network diagnostics. It is usually caused by serious "
                        + "network configuration problem. Check your OS network setting to correct it.",
                    "Failed to perform network diagnostics.");
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-2", log) {
            @Override
            public void body() {
              try {
                InetAddress localHost = U.getLocalHost();

                if (localHost.isLoopbackAddress()) {
                  U.warn(
                      log,
                      "Default local host is a loopback address. This can be a sign of "
                          + "potential network configuration problem.",
                      "Default local host is a loopback address.");
                }
              } catch (IOException ignore) {
                U.warn(
                    log,
                    "Failed to perform network diagnostics. It is usually caused by serious "
                        + "network configuration problem. Check your OS network setting to correct it.",
                    "Failed to perform network diagnostics.");
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-3", log) {
            @Override
            public void body() {
              String jdkStrLow = U.jdkString().toLowerCase();

              if (jdkStrLow.contains("jrockit") && jdkStrLow.contains("1.5.")) {
                U.warn(
                    log,
                    "BEA JRockit VM ver. 1.5.x has shown problems with NIO functionality in our "
                        + "tests that were not reproducible in other VMs. We recommend using Sun VM. Should you "
                        + "have further questions please contact us at [email protected]",
                    "BEA JRockit VM ver. 1.5.x is not supported.");
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-4", log) {
            @Override
            public void body() {
              // Sufficiently tested OS.
              if (!U.isSufficientlyTestedOs()) {
                U.warn(
                    log,
                    "This operating system has been tested less rigorously: "
                        + U.osString()
                        + ". Our team will appreciate the feedback if you experience any problems running "
                        + "gridgain in this environment. You can always send your feedback to [email protected]",
                    "This OS is tested less rigorously: " + U.osString());
              }
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-5", log) {
            @Override
            public void body() {
              // Fix for GG-1075.
              if (U.allLocalMACs() == null)
                U.warn(
                    log,
                    "No live network interfaces detected. If IP-multicast discovery is used - "
                        + "make sure to add 127.0.0.1 as a local address.",
                    "No live network interfaces. Add 127.0.0.1 as a local address.");
            }
          });

      exec.execute(
          new GridWorker(gridName, "grid-diagnostic-6", log) {
            @Override
            public void body() {
              if (System.getProperty("com.sun.management.jmxremote") != null) {
                String portStr = System.getProperty("com.sun.management.jmxremote.port");

                if (portStr != null)
                  try {
                    Integer.parseInt(portStr);

                    return;
                  } catch (NumberFormatException ignore) {
                  }

                U.warn(
                    log,
                    "JMX remote management is enabled but JMX port is either not set or invalid. "
                        + "Check system property 'com.sun.management.jmxremote.port' to make sure it specifies "
                        + "valid TCP/IP port.",
                    "JMX remote port is invalid - JMX management is off.");
              }
            }
          });
    } catch (RejectedExecutionException e) {
      U.error(
          log,
          "Failed to start background network diagnostics check due to thread pool execution "
              + "rejection. In most cases it indicates a severe configuration problem with GridGain.",
          "Failed to start background network diagnostics.",
          e);
    }
  }