Пример #1
0
  public static void main(String[] argv) {
    if (argv.length == 3) {
      init(argv[0], argv[1], argv[2]);
    } else {
      usage("Wrong number of arguments.");
    }
    try {
      cat.info("Listening on port " + port);
      ServerSocket serverSocket = new ServerSocket(port);
      for (; ; ) {
        cat.info("Waiting to accept a new client.");
        Socket socket = serverSocket.accept();
        InetAddress inetAddress = socket.getInetAddress();
        cat.info("Connected to client at " + inetAddress);

        LoggerRepository h = (LoggerRepository) server.hierarchyMap.get(inetAddress);
        if (h == null) {
          h = server.configureHierarchy(inetAddress);
        }
        cat.info("Starting new socket node.");
        new Thread(new SocketNode(socket, h)).start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #2
0
  LoggerRepository genericHierarchy() {
    if (this.genericHierarchy == null) {
      File f = new File(this.dir, GENERIC + CONFIG_FILE_EXT);
      if (f.exists()) {
        this.genericHierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
        new PropertyConfigurator().doConfigure(f.getAbsolutePath(), this.genericHierarchy);
      } else {
        cat.warn("Could not find config file [" + f + "]. Will use the default hierarchy.");

        this.genericHierarchy = LogManager.getLoggerRepository();
      }
    }
    return this.genericHierarchy;
  }
Пример #3
0
  LoggerRepository configureHierarchy(InetAddress inetAddress) {
    cat.info("Locating configuration file for " + inetAddress);

    String s = inetAddress.toString();
    int i = s.indexOf("/");
    if (i == -1) {
      cat.warn("Could not parse the inetAddress [" + inetAddress + "]. Using default hierarchy.");

      return genericHierarchy();
    }
    String key = s.substring(0, i);

    File configFile = new File(this.dir, key + CONFIG_FILE_EXT);
    if (configFile.exists()) {
      Hierarchy h = new Hierarchy(new RootLogger(Level.DEBUG));
      this.hierarchyMap.put(inetAddress, h);

      new PropertyConfigurator().doConfigure(configFile.getAbsolutePath(), h);

      return h;
    }
    cat.warn("Could not find config file [" + configFile + "].");
    return genericHierarchy();
  }
Пример #4
0
public class PlayApplication extends JavaApplication {
  protected boolean update(String className, URL url) {
    return false;
  }

  public void onCodeSourceDetected(CodeSourceInfo source) {}

  public void profile() {
    File conf = new File(this.resolvedPath, "conf" + File.separatorChar + "application.conf");
    if (conf.exists()) {
      try {
        String applicationConf = IOUtils.toString(new FileReader(conf));
        fireRuleProviders(applicationConf);
      } catch (Exception e) {
        LOG.error("Problem firing rule providers to " + this.displayName, e);
      }
    }
    try {
      new PlayDiskProfiler(this).profile(this);
    } catch (ProfilingException e) {
      LOG.error("Problem profiling app", e);
    }
    markProfiled();
    for (ClassLoadEvent event : this.missedClassLoadEvents) {
      onNewClassLoad(event.getClassName(), event.getClassLoader(), event.getPd());
    }
    markDirty();
  }

  private void fireRuleProviders(String applicationConf) {
    Collection<RuleProvider> providers = new ArrayList();
    ContrastPolicy policy = Contrast.getPolicy();
    for (Class<RuleProvider> c : policy.getRuleProviders()) {
      try {
        providers.add(c.newInstance());
      } catch (Throwable t) {
        LOG.error("Failed to load rule provider " + c.getName(), t);
      }
    }
    for (RuleProvider rule : providers) {
      ApplicationAnalyzer analyzer = rule.getApplicationAnalyzer();
      if (analyzer != null) {
        LOG.debug("Handing analysis of web app to " + analyzer.getClass().getName());
        try {
          if (analyzer.supports(PlayApplication.class)) {
            analyzer.onApplicationResolution(this, applicationConf);
          }
        } catch (Throwable t) {
          LOG.error("Problem during fireRuleProviders() for " + rule.getClass().getName(), t);
        }
      }
    }
  }

  public void onRequestEnd() {}

  public HttpRequest findRequest() {
    HttpRequest request = null;
    try {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Class<?> contextClass = loader.loadClass("play.mvc.Http$Context");
      Method getContext = contextClass.getMethod("current", ObjectShare.EMPTY_CLASS_ARRAY);
      Object context = getContext.invoke(null, ObjectShare.EMPTY_OBJ_ARRAY);
      Method getRequest = contextClass.getMethod("request", ObjectShare.EMPTY_CLASS_ARRAY);
      Object reqObj = getRequest.invoke(context, ObjectShare.EMPTY_OBJ_ARRAY);
      request = new PlayHttpRequest(reqObj);
    } catch (Exception e) {
      LOG.debug("Couldn't find request in Play app " + this.displayName);
    }
    return request;
  }

  public HttpResponse findResponse() {
    HttpResponse response = null;
    try {
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      Class<?> contextClass = loader.loadClass("play.mvc.Http$Context");
      Method getContext = contextClass.getMethod("current", ObjectShare.EMPTY_CLASS_ARRAY);
      Object context = getContext.invoke(null, ObjectShare.EMPTY_OBJ_ARRAY);
      Method getResponse = contextClass.getMethod("response", ObjectShare.EMPTY_CLASS_ARRAY);
      Object resObj = getResponse.invoke(context, ObjectShare.EMPTY_OBJ_ARRAY);
      response = new PlayHttpResponse(resObj);
    } catch (Exception e) {
      LOG.debug("Couldn't find request in Play app " + this.displayName);
    }
    return response;
  }

  public InputStream getResourceAsStream(String resourcePath) throws IOException {
    return new FileInputStream(this.resolvedPath + File.separatorChar + resourcePath);
  }

  private static final Logger LOG = Logger.getLogger(PlayApplication.class);
}
Пример #5
0
public class SocketServer {
  static String GENERIC = "generic";
  static String CONFIG_FILE_EXT = ".lcf";
  static Logger cat = Logger.getLogger(SocketServer.class);
  static SocketServer server;
  static int port;
  Hashtable hierarchyMap;
  LoggerRepository genericHierarchy;
  File dir;

  public static void main(String[] argv) {
    if (argv.length == 3) {
      init(argv[0], argv[1], argv[2]);
    } else {
      usage("Wrong number of arguments.");
    }
    try {
      cat.info("Listening on port " + port);
      ServerSocket serverSocket = new ServerSocket(port);
      for (; ; ) {
        cat.info("Waiting to accept a new client.");
        Socket socket = serverSocket.accept();
        InetAddress inetAddress = socket.getInetAddress();
        cat.info("Connected to client at " + inetAddress);

        LoggerRepository h = (LoggerRepository) server.hierarchyMap.get(inetAddress);
        if (h == null) {
          h = server.configureHierarchy(inetAddress);
        }
        cat.info("Starting new socket node.");
        new Thread(new SocketNode(socket, h)).start();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  static void usage(String msg) {
    System.err.println(msg);
    System.err.println(
        "Usage: java " + SocketServer.class.getName() + " port configFile directory");

    System.exit(1);
  }

  static void init(String portStr, String configFile, String dirStr) {
    try {
      port = Integer.parseInt(portStr);
    } catch (NumberFormatException e) {
      e.printStackTrace();
      usage("Could not interpret port number [" + portStr + "].");
    }
    PropertyConfigurator.configure(configFile);

    File dir = new File(dirStr);
    if (!dir.isDirectory()) {
      usage("[" + dirStr + "] is not a directory.");
    }
    server = new SocketServer(dir);
  }

  public SocketServer(File directory) {
    this.dir = directory;
    this.hierarchyMap = new Hashtable(11);
  }

  LoggerRepository configureHierarchy(InetAddress inetAddress) {
    cat.info("Locating configuration file for " + inetAddress);

    String s = inetAddress.toString();
    int i = s.indexOf("/");
    if (i == -1) {
      cat.warn("Could not parse the inetAddress [" + inetAddress + "]. Using default hierarchy.");

      return genericHierarchy();
    }
    String key = s.substring(0, i);

    File configFile = new File(this.dir, key + CONFIG_FILE_EXT);
    if (configFile.exists()) {
      Hierarchy h = new Hierarchy(new RootLogger(Level.DEBUG));
      this.hierarchyMap.put(inetAddress, h);

      new PropertyConfigurator().doConfigure(configFile.getAbsolutePath(), h);

      return h;
    }
    cat.warn("Could not find config file [" + configFile + "].");
    return genericHierarchy();
  }

  LoggerRepository genericHierarchy() {
    if (this.genericHierarchy == null) {
      File f = new File(this.dir, GENERIC + CONFIG_FILE_EXT);
      if (f.exists()) {
        this.genericHierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
        new PropertyConfigurator().doConfigure(f.getAbsolutePath(), this.genericHierarchy);
      } else {
        cat.warn("Could not find config file [" + f + "]. Will use the default hierarchy.");

        this.genericHierarchy = LogManager.getLoggerRepository();
      }
    }
    return this.genericHierarchy;
  }
}
Пример #6
0
public class ExactPolicyNode extends PolicyNode {
  protected String className;
  protected SimplePattern methodNamePattern;
  protected String[] parameterTypeNames;
  protected String[] tags;
  protected String target;

  public ExactPolicyNode(
      String className, SimplePattern methodNamePattern, String[] parameterTypeNames) {
    this.className = className;
    this.methodNamePattern = methodNamePattern;
    this.parameterTypeNames = parameterTypeNames;
    this.tags = ObjectShare.EMPTY_STRING_ARRAY;
  }

  public ExactPolicyNode() {}

  public String getClassName() {
    return this.className;
  }

  public void setClassName(String className) {
    this.className = className;
  }

  public SimplePattern getMethodName() {
    return this.methodNamePattern;
  }

  public void setMethodName(SimplePattern methodNamePattern) {
    this.methodNamePattern = methodNamePattern;
  }

  public String[] getParameterTypes() {
    return this.parameterTypeNames;
  }

  public void setParameterTypes(String[] parameterTypes) {
    this.parameterTypeNames = parameterTypes;
  }

  public String[] getTags() {
    return this.tags;
  }

  public void setTags(String[] tags) {
    this.tags = tags;
  }

  public boolean match(ContrastMethod method) {
    return match(method, false);
  }

  public boolean match(ContrastMethod method, boolean debug) {
    String methodClassName = method.getClassName().replace("/", ".");
    if (!this.className.equals(methodClassName)) {
      if (debug) {
        LOG.debug("Classes didn't match [" + this.className + "," + method.getClassName() + "]");
      }
      return false;
    }
    if (!this.methodNamePattern.matches(method.getMethodName())) {
      if (debug) {
        LOG.debug(
            "Method pattern didn't match ["
                + this.methodNamePattern.getSimplePattern()
                + ","
                + method.getMethodName()
                + "]");
      }
      return false;
    }
    String[] actualTypes = method.getParameterTypeNames();
    if (actualTypes.length != this.parameterTypeNames.length) {
      if (debug) {
        LOG.debug(
            "Method type length didn't match ["
                + this.parameterTypeNames.length
                + ","
                + actualTypes.length
                + "]");
      }
      return false;
    }
    for (int i = 0; i < this.parameterTypeNames.length; i++) {
      if (!this.parameterTypeNames[i].equals(actualTypes[i])) {
        if (debug) {
          LOG.debug(
              "Parameter "
                  + i
                  + " didn't match ["
                  + this.parameterTypeNames[i]
                  + ","
                  + actualTypes[i]
                  + "]");
        }
        return false;
      }
    }
    if (debug) {
      LOG.debug("Method matched [" + method.toString());
    }
    return true;
  }

  public String toString() {
    return toString(true);
  }

  public String toString(boolean includeStar) {
    StringBuffer sb = new StringBuffer();
    sb.append(this.className + "." + this.methodNamePattern + "(");
    if (this.parameterTypeNames != null) {
      for (int i = 0; i < this.parameterTypeNames.length; i++) {
        sb.append(this.parameterTypeNames[i]);
        if (i != this.parameterTypeNames.length - 1) {
          sb.append(",");
        }
      }
    }
    sb.append(")");
    if ((includeStar) && (isInheritable())) {
      sb.append("*");
    }
    return sb.toString();
  }

  public boolean hasWildcards() {
    if (this.methodNamePattern.getSimplePattern().contains("*")) {
      return true;
    }
    for (String parameterType : this.parameterTypeNames) {
      if ("*".equals(parameterType)) {
        return true;
      }
    }
    return false;
  }

  public int hashCode() {
    int prime = 31;
    int result = 1;
    result = 31 * result + (this.className == null ? 0 : this.className.hashCode());

    result =
        31 * result
            + (this.methodNamePattern.getSimplePattern() == null
                ? 0
                : this.methodNamePattern.getSimplePattern().hashCode());
    if (this.parameterTypeNames != null) {
      for (String parameterTypeName : this.parameterTypeNames) {
        result = 31 * result + parameterTypeName.hashCode();
      }
    }
    return result;
  }

  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    ExactPolicyNode other = (ExactPolicyNode) obj;
    if (this.className == null) {
      if (other.className != null) {
        return false;
      }
    } else if (!this.className.equals(other.className)) {
      return false;
    }
    if (this.methodNamePattern == null) {
      if (other.methodNamePattern != null) {
        return false;
      }
    } else if (!this.methodNamePattern
        .getSimplePattern()
        .equals(other.methodNamePattern.getSimplePattern())) {
      return false;
    }
    if (!Arrays.equals(this.parameterTypeNames, other.parameterTypeNames)) {
      return false;
    }
    return true;
  }

  private static final Logger LOG = Logger.getLogger(ExactPolicyNode.class);
}