示例#1
0
  /**
   * Used to get the base ssl context in which to create the server socket. This is basically just
   * so we can have a custom location for key stores.
   */
  public SSLContext getSSLContext(String keyStoreName, String password) throws IOException {
    try {
      // Check the key manager factory
      KeyManagerFactory kmf = KeyManagerFactory.getInstance(this.keyManagerType);

      File ksFile = new File(keyStoreName);
      if (!ksFile.exists() || !ksFile.isFile())
        throw new WinstoneException(
            SSL_RESOURCES.getString("HttpsListener.KeyStoreNotFound", ksFile.getPath()));
      InputStream in = new FileInputStream(ksFile);
      char[] passwordChars = password == null ? null : password.toCharArray();
      KeyStore ks = KeyStore.getInstance("JKS");
      ks.load(in, passwordChars);
      kmf.init(ks, passwordChars);
      Logger.log(Logger.FULL_DEBUG, SSL_RESOURCES, "HttpsListener.KeyCount", ks.size() + "");
      for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
        String alias = (String) e.nextElement();
        Logger.log(
            Logger.FULL_DEBUG,
            SSL_RESOURCES,
            "HttpsListener.KeyFound",
            new String[] {alias, ks.getCertificate(alias) + ""});
      }

      SSLContext context = SSLContext.getInstance("SSL");
      context.init(kmf.getKeyManagers(), null, null);
      Arrays.fill(passwordChars, 'x');
      return context;
    } catch (IOException err) {
      throw err;
    } catch (Throwable err) {
      throw new WinstoneException(
          SSL_RESOURCES.getString("HttpsListener.ErrorGettingContext"), err);
    }
  }
示例#2
0
 public String toString() {
   return DS_RESOURCES.getString(
       "WinstoneDataSource.StatusMsg",
       new String[] {
         this.name, "" + this.usedRealConnections.size(), "" + this.unusedRealConnections.size()
       });
 }
示例#3
0
 private void log(int level, String msgKey, String arg[], Throwable err) {
   if (getLogWriter() != null) {
     getLogWriter().println(DS_RESOURCES.getString(msgKey, arg));
     if (err != null) {
       err.printStackTrace(getLogWriter());
     }
   } else {
     Logger.log(level, DS_RESOURCES, msgKey, arg, err);
   }
 }
 public void contextInitialized(ServletContextEvent sce) {
   this.webAppConfig = (WebAppConfiguration) sce.getServletContext();
   this.interrupted = false;
   synchronized (this) {
     this.loadedClasses.clear();
   }
   Thread thread = new Thread(this, CL_RESOURCES.getString("ReloadingClassLoader.ThreadName"));
   thread.setDaemon(true);
   thread.setPriority(Thread.MIN_PRIORITY);
   thread.start();
 }
  protected boolean doRoleCheck(
      HttpServletRequest request, HttpServletResponse response, String pathRequested)
      throws IOException, ServletException {
    // Loop through constraints
    boolean foundApplicable = false;
    for (int n = 0; (n < this.constraints.length) && !foundApplicable; n++) {
      Logger.log(
          Logger.FULL_DEBUG,
          AUTH_RESOURCES,
          "BaseAuthenticationHandler.EvalConstraint",
          this.constraints[n].getName());

      // Find one that applies, then
      if (this.constraints[n].isApplicable(pathRequested, request.getMethod())) {
        Logger.log(
            Logger.FULL_DEBUG,
            AUTH_RESOURCES,
            "BaseAuthenticationHandler.ApplicableConstraint",
            this.constraints[n].getName());
        foundApplicable = true;

        if (this.constraints[n].needsSSL() && !request.isSecure()) {
          Logger.log(
              Logger.DEBUG,
              AUTH_RESOURCES,
              "BaseAuthenticationHandler.ConstraintNeedsSSL",
              this.constraints[n].getName());
          response.sendError(
              HttpServletResponse.SC_FORBIDDEN,
              AUTH_RESOURCES.getString(
                  "BaseAuthenticationHandler.ConstraintNeedsSSL", this.constraints[n].getName()));
          return false;
        } else if (!this.constraints[n].isAllowed(request)) {
          // Logger.log(Logger.FULL_DEBUG, "Not allowed - requesting auth");
          requestAuthentication(request, response, pathRequested);
          return false;
        } else {
          // Logger.log(Logger.FULL_DEBUG, "Allowed - authorization accepted");
          // Ensure that secured resources are not cached
          setNoCache(response);
        }
      }
    }

    // If we made it this far without a check being run, there must be none applicable
    Logger.log(Logger.FULL_DEBUG, AUTH_RESOURCES, "BaseAuthenticationHandler.PassedAuthCheck");
    return true;
  }
示例#6
0
  /** Get a read-write connection - preferably from the pool, but fresh if needed */
  protected Connection getConnection(int retriesAllowed) throws SQLException {
    Connection realConnection = null;

    synchronized (this.unusedRealConnections) {
      // If we have any spare, get it from the unused pool
      if (this.unusedRealConnections.size() > 0) {
        realConnection = (Connection) this.unusedRealConnections.get(0);
        this.unusedRealConnections.remove(realConnection);
        this.usedRealConnections.add(realConnection);
        log(
            Logger.FULL_DEBUG,
            "WinstoneDataSource.UsingPooled",
            new String[] {
              "" + this.usedRealConnections.size(), "" + this.unusedRealConnections.size()
            },
            null);
        try {
          return prepareWrapper(realConnection);
        } catch (SQLException err) {
          // Leave the realConnection as non-null, so we know prepareWrapper failed
        }
      }

      // If we are out (and not over our limit), allocate a new one
      else if (this.usedRealConnections.size() < maxHeldCount) {
        realConnection = makeNewRealConnection(this.usedRealConnections);
        log(
            Logger.FULL_DEBUG,
            "WinstoneDataSource.UsingNew",
            new String[] {
              "" + this.usedRealConnections.size(), "" + this.unusedRealConnections.size()
            },
            null);
        try {
          return prepareWrapper(realConnection);
        } catch (SQLException err) {
          // Leave the realConnection as non-null, so we know prepareWrapper failed
        }
      }
    }

    if (realConnection != null) {
      // prepareWrapper() must have failed, so call this method again
      realConnection = null;
      return getConnection(retriesAllowed);
    } else if (retriesAllowed <= 0) {
      // otherwise throw fail message - we've blown our limit
      throw new SQLException(
          DS_RESOURCES.getString("WinstoneDataSource.Exceeded", "" + maxHeldCount));
    } else {
      log(
          Logger.FULL_DEBUG,
          "WinstoneDataSource.Retrying",
          new String[] {"" + maxHeldCount, "" + retriesAllowed, "" + retryPeriod},
          null);

      // If we are here, it's because we need to retry for a connection
      try {
        Thread.sleep(retryPeriod);
      } catch (InterruptedException err) {
      }
      return getConnection(retriesAllowed - 1);
    }
  }