Skip to content

Commit

Permalink
Re-implemented JsonLoader.fromResource to avoid Hadoop exceptions (cl…
Browse files Browse the repository at this point in the history
…oses #25)
  • Loading branch information
alexanderdean committed Aug 11, 2015
1 parent 5d3135c commit 4143939
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package repositories

// Java
import java.io.IOException
import java.net.URL

// Apache Commons
import org.apache.commons.lang3.exception.ExceptionUtils
Expand Down Expand Up @@ -45,6 +46,8 @@ import utils.{ValidationExceptions => VE}
*/
object EmbeddedRepositoryRef {

private val LeadingSlash = "^/+".r

implicit val formats = DefaultFormats

/**
Expand Down Expand Up @@ -100,6 +103,37 @@ object EmbeddedRepositoryRef {
case me: MappingException => s"Could not extract connection.embedded.path from ${compact(render(config))}".fail.toProcessingMessage
}

/**
* Loads a local resource. Adapted from:
* https://github.com/fge/jackson-coreutils/blob/9f01ffc94ae16addce7cb5ca00356b1c55d278dd/src/main/java/com/github/fge/jackson/JsonLoader.java#L74
*
* Re-implemented because Hadoop doesn't play
* nice with JsonLoader.fromResource (sigh).
*
* @param resource The resource to load
* @return the JsonNode at the resource, Option-boxed
*/
def fromResource(resource: String): Option[JsonNode] = {

val thisClass = classOf[EmbeddedRepositoryRef]
val url: Option[URL] = Option(thisClass.getResource(resource)) match {
case u @ Some(_) => u
case None => {
val classLoader = Option(Thread.currentThread.getContextClassLoader) match {
case cl @ Some(_) => cl
case None => Option(thisClass.getClassLoader)
}

for {
cl <- classLoader
path = LeadingSlash.replaceFirstIn(resource, "")
url <- Option(cl.getResource(path))
} yield url
}
}

for (u <- url) yield JsonLoader.fromURL(u)
}
}

/**
Expand Down Expand Up @@ -137,7 +171,7 @@ case class EmbeddedRepositoryRef(
def lookupSchema(schemaKey: SchemaKey): Validated[Option[JsonNode]] = {
val schemaPath = s"${path}/schemas/${schemaKey.toPath}"
try {
JsonLoader.fromResource(schemaPath).some.success
EmbeddedRepositoryRef.fromResource(schemaPath).success
} catch {
case jpe: JsonParseException => // Child of IOException so match first
s"Problem parsing ${schemaPath} as JSON in ${descriptor} Iglu repository ${config.name}: %s".format(VE.stripInstanceEtc(jpe.getMessage)).fail.toProcessingMessage
Expand Down

0 comments on commit 4143939

Please sign in to comment.