Пример #1
0
  /**
   * Understands and applies the following integer properties.
   *
   * <ul>
   *   <li>max.size - setMaximumSize
   *   <li>max.threads - setMaximumThreads
   *   <li>timeout.idle - setIdleTimeout
   *   <li>timeout.transaction - setTransactionTimeout
   *   <li>tune.size - Automatically tunes queue size when "true" and transaction timeout set.
   *   <li>tune.threads - Automatically tunes maximum thread count.
   * </ul>
   */
  public synchronized void applyProperties(PropertyMap properties) {
    if (properties.containsKey("max.size")) {
      setMaximumSize(properties.getInt("max.size"));
    }

    if (properties.containsKey("max.threads")) {
      setMaximumThreads(properties.getInt("max.threads"));
    }

    if (properties.containsKey("timeout.idle")) {
      setIdleTimeout(properties.getNumber("timeout.idle").longValue());
    }

    if (properties.containsKey("timeout.transaction")) {
      setTransactionTimeout(properties.getNumber("timeout.transaction").longValue());
    }

    if ("true".equalsIgnoreCase(properties.getString("tune.size"))) {
      addTransactionQueueListener(new TransactionQueueSizeTuner());
    }

    if ("true".equalsIgnoreCase(properties.getString("tune.threads"))) {
      addTransactionQueueListener(new TransactionQueueThreadTuner());
    }
  }
Пример #2
0
 private void additiveMerge(PropertyMap properties, String property, String separator) {
   String merging = properties.getString(property);
   if (merging != null) {
     String existing = mProperties.getString(property);
     if (existing == null || existing.length() == 0) {
       mProperties.put(property, merging);
     } else {
       mProperties.put(property, existing + separator + merging);
     }
   }
 }
Пример #3
0
  @SuppressWarnings("unchecked")
  private void loadDefaults(PropertyMap properties, Set<String> files) throws Exception {
    // update substitutions if provided
    PropertyMap substitutions = properties.subMap("substitutions");
    if (substitutions != null && substitutions.size() > 0) {
      PropertyMap subs = SubstitutionFactory.getSubstitutions(substitutions, mResourceFactory);

      if (subs != null) {
        mSubstitutions.putAll(subs);
      }

      properties.remove("substitutions");
    }

    // Get file and perform substitution of env variables/system props
    String fileName = properties.getString("properties.file");
    if (mDebugEnabled) {
      mServletContext.log("properties.file: " + fileName);
    }

    if (fileName != null) {
      fileName = SubstitutionFactory.substitute(fileName, mSubstitutions);
    }

    // parse file if not yet parsed
    if (fileName != null && !files.contains(fileName)) {
      // Prevent properties file cycle.
      files.add(fileName);

      // load properties
      PropertyMap props = mResourceFactory.getResourceAsProperties(fileName);

      if (props != null) {
        properties.putAll(props);
      }

      loadDefaults(properties, files);
    } else {
      PropertyMap factoryProps = properties.subMap("properties.factory");
      if (factoryProps != null && factoryProps.size() > 0) {
        PropertyMap map = loadProperties(factoryProps);
        properties.putAll(map);
      }
    }
  }
Пример #4
0
  private void createLog(final ServletContext context) {

    if (mLog == null) {
      try {
        mLog = (Log) context.getAttribute("org.teatrove.trove.log.Log");
      } catch (ClassCastException e) {
      }

      // Log instance may not be provided, so make a Log that passes
      // messages to standard ServletContext log.
      if (mLog == null) {
        mLog = new Log(getServletName(), null);

        mLog.addLogListener(
            new LogListener() {
              public void logMessage(LogEvent e) {
                String message = e.getMessage();
                if (message != null) {
                  context.log(message);
                }
              }

              public void logException(LogEvent e) {
                String message = e.getMessage();
                Throwable t = e.getException();
                if (t == null) {
                  context.log(message);
                } else {
                  context.log(message, t);
                }
              }
            });
      }
    }

    String fullStackTrace = mProperties.getString("log.fullStackTrace", "false");
    if (!fullStackTrace.equals("true")) {
      mLog = new TeaLog(mLog);
    }
  }
Пример #5
0
  private PropertyMap loadProperties(PropertyMap factoryProps) throws Exception {

    String className = null;
    PropertyMapFactory factory = null;
    if (factoryProps != null
        && factoryProps.size() > 0
        && (className = factoryProps.getString("class")) != null) {
      if (mDebugEnabled) {
        mServletContext.log("Using property factory: " + className);
      }

      // Load and use custom PropertyMapFactory.
      Class<?> factoryClass = Class.forName(className);
      java.lang.reflect.Constructor<?> ctor = factoryClass.getConstructor(new Class[] {Map.class});
      factory = (PropertyMapFactory) ctor.newInstance(new Object[] {factoryProps.subMap("init")});
    } else if (factoryProps == null) {
      if (mDebugEnabled) {
        mServletContext.log("factoryProps is null.");
      }
    } else if (factoryProps.size() == 0) {
      if (mDebugEnabled) {
        mServletContext.log("factory props size is 0.");
      }
    } else {
      if (mDebugEnabled) {
        mServletContext.log("className is null");
      }
    }

    // return properties
    PropertyMap result = null;
    if (factory != null) {
      result = factory.createProperties();
      if (mDebugEnabled) {
        mServletContext.log("properties: " + result);
      }
    }
    return result;
  }
Пример #6
0
  protected boolean processStatus(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String path = request.getPathInfo();

    // check password
    String adminKey = mProperties.getString("admin.key");
    String adminValue = mProperties.getString("admin.value");
    if (AdminApplication.adminCheck(
        adminKey, adminValue,
        request, response)) {

      // check if status request
      if ("/system/status.json".equals(path)) {
        printStatus(response);
        return true;
      }

      // once we have initialized, no need to further process
      if (isRunning()) {
        return false;
      }

      // verify our path matches the expected path
      String pattern = mProperties.getString("startup.path");
      if (pattern != null && path.matches(pattern)) {

        // verify the startup file that was provided
        String resource = mProperties.getString("startup.file");
        if (resource != null) {
          InputStream input = TeaServlet.class.getResourceAsStream(resource);

          // copy the data to the response if valid
          if (input != null) {
            int read = 0;
            byte[] data = new byte[512];

            input = new BufferedInputStream(input);
            ServletOutputStream output = response.getOutputStream();

            while ((read = input.read(data)) >= 0) {
              output.write(data, 0, read);
            }

            input.close();

            response.flushBuffer();
            return true;
          }
        }
      }
    }

    // handle error code if not yet initialized
    if (!isInitialized()) {
      // request not processed so send uninitialized error code
      int errorCode = mProperties.getInt("startup.codes.initializing", 503);
      response.sendError(errorCode);
      return true;
    }

    // nothing to process
    return false;
  }