Example #1
1
  /**
   * Start this server. If we manage an own HttpServer, then the HttpServer will be started as well.
   */
  public void start() {
    // URL as configured takes precedence
    String configUrl =
        NetworkUtil.replaceExpression(config.getJolokiaConfig().get(ConfigKey.DISCOVERY_AGENT_URL));
    jolokiaHttpHandler.start(
        lazy, configUrl != null ? configUrl : url, config.getAuthenticator() != null);

    if (httpServer != null) {
      // Starting our own server in an own thread group with a fixed name
      // so that the cleanup thread can recognize it.
      ThreadGroup threadGroup = new ThreadGroup("jolokia");
      threadGroup.setDaemon(false);

      Thread starterThread =
          new Thread(
              threadGroup,
              new Runnable() {
                @Override
                public void run() {
                  httpServer.start();
                }
              });
      starterThread.start();
      cleaner = new CleanupThread(httpServer, threadGroup);
      cleaner.start();
    }
  }
Example #2
1
  private String detectAgentUrl(
      HttpServer pServer, JolokiaServerConfig pConfig, String pContextPath) {
    serverAddress = pServer.getAddress();
    InetAddress realAddress;
    int port;
    if (serverAddress != null) {
      realAddress = serverAddress.getAddress();
      if (realAddress.isAnyLocalAddress()) {
        try {
          realAddress = NetworkUtil.getLocalAddress();
        } catch (IOException e) {
          try {
            realAddress = InetAddress.getLocalHost();
          } catch (UnknownHostException e1) {
            // Ok, ok. We take the original one
            realAddress = serverAddress.getAddress();
          }
        }
      }
      port = serverAddress.getPort();
    } else {
      realAddress = pConfig.getAddress();
      port = pConfig.getPort();
    }

    return String.format(
        "%s://%s:%d%s", pConfig.getProtocol(), realAddress.getHostAddress(), port, pContextPath);
  }
Example #3
0
  /**
   * Initialize the backend systems, the log handler and the restrictor. A subclass can tune this
   * step by overriding {@link #createRestrictor(String)} and {@link
   * #createLogHandler(ServletConfig, boolean)}
   *
   * @param pServletConfig servlet configuration
   */
  @Override
  public void init(ServletConfig pServletConfig) throws ServletException {
    super.init(pServletConfig);

    Configuration config = initConfig(pServletConfig);

    // Create a log handler early in the lifecycle, but not too early
    String logHandlerClass = config.get(ConfigKey.LOGHANDLER_CLASS);
    logHandler =
        logHandlerClass != null
            ? (LogHandler) ClassUtil.newInstance(logHandlerClass)
            : createLogHandler(pServletConfig, Boolean.valueOf(config.get(ConfigKey.DEBUG)));

    // Different HTTP request handlers
    httpGetHandler = newGetHttpRequestHandler();
    httpPostHandler = newPostHttpRequestHandler();

    if (restrictor == null) {
      restrictor =
          createRestrictor(NetworkUtil.replaceExpression(config.get(ConfigKey.POLICY_LOCATION)));
    } else {
      logHandler.info("Using custom access restriction provided by " + restrictor);
    }
    configMimeType = config.get(ConfigKey.MIME_TYPE);
    backendManager = new BackendManager(config, logHandler, restrictor);
    requestHandler = new HttpRequestHandler(config, backendManager, logHandler);

    initDiscoveryMulticast(config);
  }
Example #4
0
 // Try to find an URL for system props or config
 private String findAgentUrl(Configuration pConfig) {
   // System property has precedence
   String url = System.getProperty("jolokia." + ConfigKey.DISCOVERY_AGENT_URL.getKeyValue());
   if (url == null) {
     url = System.getenv("JOLOKIA_DISCOVERY_AGENT_URL");
     if (url == null) {
       url = pConfig.get(ConfigKey.DISCOVERY_AGENT_URL);
     }
   }
   return NetworkUtil.replaceExpression(url);
 }
 protected JolokiaHandler(Map<String, String> configParameters, Restrictor restrictor) {
   log = new JolokiaLogHandler(LoggerFactory.getLogger(JolokiaHandler.class));
   Configuration config = initConfig(configParameters);
   if (restrictor == null) {
     restrictor =
         createRestrictor(NetworkUtil.replaceExpression(config.get(ConfigKey.POLICY_LOCATION)));
   }
   log.info("Using restrictor " + restrictor);
   BackendManager backendManager = new BackendManager(config, log, restrictor);
   requestHandler = new HttpRequestHandler(config, backendManager, log);
 }
Example #6
0
 /**
  * Description including agent URL
  *
  * @return agent url
  */
 @Override
 public String getDescription() {
   String hostDescr = host;
   try {
     if (hostDescr == null) {
       hostDescr = NetworkUtil.getLocalAddress().getHostName();
     }
   } catch (IOException e) {
     hostDescr = "localhost";
   }
   return "Jolokia Agent: http://" + hostDescr + ":" + getPort() + "/jolokia";
 }
Example #7
0
  // Examines servlet config and servlet context for configuration parameters.
  // Configuration from the servlet context overrides servlet parameters defined in web.xml
  Configuration initConfig(ServletConfig pConfig) {
    Configuration config =
        new Configuration(ConfigKey.AGENT_ID, NetworkUtil.getAgentId(hashCode(), "servlet"));
    // From ServletContext ....
    config.updateGlobalConfiguration(new ServletConfigFacade(pConfig));
    // ... and ServletConfig
    config.updateGlobalConfiguration(new ServletContextFacade(getServletContext()));

    // Set type last and overwrite anything written
    config.updateGlobalConfiguration(
        Collections.singletonMap(ConfigKey.AGENT_TYPE.getKeyValue(), "servlet"));
    return config;
  }
Example #8
0
 // Update the agent URL in the agent details if not already done
 private void updateAgentDetailsIfNeeded(HttpServletRequest pReq) {
   // Lookup the Agent URL if needed
   AgentDetails details = backendManager.getAgentDetails();
   if (details.isInitRequired()) {
     synchronized (details) {
       if (details.isInitRequired()) {
         if (details.isUrlMissing()) {
           String url =
               getBaseUrl(
                   NetworkUtil.sanitizeLocalUrl(pReq.getRequestURL().toString()),
                   extractServletPath(pReq));
           details.setUrl(url);
         }
         if (details.isSecuredMissing()) {
           details.setSecured(pReq.getAuthType() != null);
         }
         details.seal();
       }
     }
   }
 }
 protected Configuration initConfig(Map<String, String> params) {
   Configuration config =
       new Configuration(ConfigKey.AGENT_ID, NetworkUtil.getAgentId(hashCode(), "vertx"));
   config.updateGlobalConfiguration(params);
   return config;
 }