<commit_before>/***
 *   Copyright 2014 Rackspace US, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.rackspace.com.papi.components.checker

import java.io.{InputStream, StringWriter}
import java.lang.management._
import javax.management._
import javax.servlet.FilterChain
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import javax.xml.transform._
import javax.xml.transform.dom._
import javax.xml.transform.sax._
import javax.xml.transform.stream._

import com.codahale.metrics.RatioGauge.Ratio
import com.codahale.metrics.{MetricRegistry, RatioGauge}
import com.rackspace.com.papi.components.checker.handler.ResultHandler
import com.rackspace.com.papi.components.checker.servlet._
import com.rackspace.com.papi.components.checker.step.base.{Step, StepContext}
import com.rackspace.com.papi.components.checker.step.results.Result
import com.rackspace.com.papi.components.checker.util.IdentityTransformPool
import com.rackspace.com.papi.components.checker.wadl.{StepBuilder, WADLDotBuilder}
import com.typesafe.scalalogging.slf4j.LazyLogging
import nl.grons.metrics.scala.{InstrumentedBuilder, Meter, Timer}
import org.apache.commons.codec.digest.DigestUtils.sha1Hex
import org.w3c.dom.Document

import scala.util.Try

object Validator {
  /** The application wide metrics registry. */
  val metricRegistry = new com.codahale.metrics.MetricRegistry()

  def apply (name : String, startStep : Step, config : Config) : Validator = {
    val validator = new Validator(name, startStep, config)
    config.resultHandler.init(validator, None)
    validator
  }

  def apply (name : String, in : Source, config : Config = new Config) : Validator = {
    val builder = new StepBuilder()
    val transformerFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", this.getClass.getClassLoader)

    if (!transformerFactory.getFeature(SAXTransformerFactory.FEATURE)) {
      throw new RuntimeException("Need a SAX-compatible TransformerFactory!")
    }

    val transHandler = transformerFactory.asInstanceOf[SAXTransformerFactory].newTransformerHandler()
    val domResult = new DOMResult
    transHandler.setResult(domResult)
    val step = builder.build(in, new SAXResult(transHandler), config)

    val checker = Some(domResult.getNode.asInstanceOf[Document])
    val validator = new Validator(name, step, config, checker)
    config.resultHandler.init(validator, checker)

    validator
  }
<commit_msg>Updated the Validator helper object to build and start a JMX Reporter so all of the metrics are exported to JMX as expected.<commit_after>/***
 *   Copyright 2014 Rackspace US, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.rackspace.com.papi.components.checker

import java.io.{InputStream, StringWriter}
import java.lang.management._
import javax.management._
import javax.servlet.FilterChain
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import javax.xml.transform._
import javax.xml.transform.dom._
import javax.xml.transform.sax._
import javax.xml.transform.stream._

import com.codahale.metrics.RatioGauge.Ratio
import com.codahale.metrics.{JmxReporter, MetricRegistry, RatioGauge}
import com.rackspace.com.papi.components.checker.handler.ResultHandler
import com.rackspace.com.papi.components.checker.servlet._
import com.rackspace.com.papi.components.checker.step.base.{Step, StepContext}
import com.rackspace.com.papi.components.checker.step.results.Result
import com.rackspace.com.papi.components.checker.util.IdentityTransformPool
import com.rackspace.com.papi.components.checker.wadl.{StepBuilder, WADLDotBuilder}
import com.typesafe.scalalogging.slf4j.LazyLogging
import nl.grons.metrics.scala.{InstrumentedBuilder, Meter, Timer}
import org.apache.commons.codec.digest.DigestUtils.sha1Hex
import org.w3c.dom.Document

import scala.util.Try

object Validator {
  /** The application wide metrics registry. */
  val metricRegistry = new com.codahale.metrics.MetricRegistry()
  val reporter = JmxReporter.forRegistry(metricRegistry).inDomain(getClass.getPackage.getName).build()
  reporter.start()

  def apply (name : String, startStep : Step, config : Config) : Validator = {
    val validator = new Validator(name, startStep, config)
    config.resultHandler.init(validator, None)
    validator
  }

  def apply (name : String, in : Source, config : Config = new Config) : Validator = {
    val builder = new StepBuilder()
    val transformerFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", this.getClass.getClassLoader)

    if (!transformerFactory.getFeature(SAXTransformerFactory.FEATURE)) {
      throw new RuntimeException("Need a SAX-compatible TransformerFactory!")
    }

    val transHandler = transformerFactory.asInstanceOf[SAXTransformerFactory].newTransformerHandler()
    val domResult = new DOMResult
    transHandler.setResult(domResult)
    val step = builder.build(in, new SAXResult(transHandler), config)

    val checker = Some(domResult.getNode.asInstanceOf[Document])
    val validator = new Validator(name, step, config, checker)
    config.resultHandler.init(validator, checker)

    validator
  }

{'fraction_non_alphanumeric': 0.09325842696629214, 'fraction_numerical': 0.003531300160513644, 'mean_word_length': 4.65426497277677, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6126647', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1795, 'n_words': 467}