Skip to content

Commit

Permalink
Fixed CSUG#60: Introduce command resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
jushi committed Jul 2, 2012
1 parent 5671cd7 commit 1feea08
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 24 deletions.
7 changes: 6 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object Build extends sbt.Build {
import Unmanaged._

val VERSION = "0.2.3"
val excludeAssemblyJars = Seq("tools.jar", "dom4j-1.6.1.jar","javassist-3.12.1.GA.jar","xml-apis-1.0.b2.jar")

lazy val upload = TaskKey[Unit]("upload","upload assembly jar to github downloads")

Expand All @@ -53,7 +54,7 @@ object Build extends sbt.Build {
),
test in assembly := {},
parallelExecution in Test := false,
excludedJars in assembly <<= (fullClasspath in assembly) map { _ filter {_.data.getName == "tool.jar"} }
excludedJars in assembly <<= (fullClasspath in assembly) map { _ filter {f => excludeAssemblyJars.contains(f.data.getName)} }
)
)

Expand Down Expand Up @@ -99,6 +100,10 @@ object Build extends sbt.Build {
)

lazy val compileLibs = Seq(
"javax.servlet" % "servlet-api" % "2.5",
"org.slf4j" % "slf4j-api" % "1.6.1",
"org.slf4j" % "slf4j-simple" % "1.6.1",
"org.reflections" % "reflections" % "0.9.8",
"asm" % "asm" % "3.3.1",
"asm" % "asm-commons" % "3.3.1",
"com.github.zhongl" %% "yascli" % "0.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.List;
import java.util.Set;

import static com.github.zhongl.housemd.misc.Reflections.simpleNameOf;
import static com.github.zhongl.housemd.misc.ReflectionUtils.*;
import static com.github.zhongl.yascli.JavaConvertions.manifest;
import static com.github.zhongl.yascli.JavaConvertions.none;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.github.zhongl.housemd.command

import java.lang.reflect.{Field, Method}
import java.util.List
import com.github.zhongl.housemd.misc.Reflections._
import com.github.zhongl.housemd.misc.ReflectionUtils._

/**
* @author <a href="mailto:[email protected]">zhongl<a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.zhongl.housemd.command

import com.github.zhongl.housemd.misc.Reflections._
import com.github.zhongl.housemd.misc.ReflectionUtils._
import jline.console.completer.Completer
import instrument.Instrumentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.github.zhongl.housemd.command
import instrument.Instrumentation
import scala.annotation.tailrec
import com.github.zhongl.yascli.{Command, PrintOut}
import com.github.zhongl.housemd.misc.Reflections._
import com.github.zhongl.housemd.misc.ReflectionUtils._
import com.github.zhongl.housemd.misc.Utils._


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.github.zhongl.housemd.command

import java.lang.reflect.Type
import com.github.zhongl.housemd.misc.Reflections._
import com.github.zhongl.housemd.misc.ReflectionUtils._


/**
Expand Down
18 changes: 11 additions & 7 deletions src/main/scala/com/github/zhongl/housemd/command/Resources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ package com.github.zhongl.housemd.command
import instrument.Instrumentation
import com.github.zhongl.yascli.{Command, PrintOut}
import collection.JavaConversions._
import org.reflections.Reflections
import org.reflections.util.{ClasspathHelper, ConfigurationBuilder}
import org.reflections.scanners.ResourcesScanner
import com.google.common.base.Predicate

/**
* @author <a href="mailto:[email protected]">zhongl<a>
*/
class Resources (inst: Instrumentation, out: PrintOut)
extends Command("resources", "list source paths by resource name.", out) {

private val resourceName = parameter[String]("name", "resource name.")
private val regex = parameter[String]("regex", "resource name regex pattern.")

private object Loader {
def unapply(c: Class[_]) = if (c.getClassLoader == null) None else Some(c.getClassLoader)
}

def run() {
val name = resourceName()
val r = regex()
val loaders = inst.getAllLoadedClasses collect {case Loader(cl) => cl }
loaders foreach println
println(name)
val enumerations = loaders.distinct map {_.getResources(name)}
val urls = for (e <- enumerations; url <- e) yield url
val config = new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(loaders: _*)).setScanners(new ResourcesScanner())
val reflections = new Reflections(config)

urls.distinct foreach println
val files = reflections.getResources(java.util.regex.Pattern.compile(r))

files foreach println
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.github.zhongl.housemd.command
import instrument.Instrumentation
import management.ManagementFactory
import com.github.zhongl.yascli.PrintOut
import com.github.zhongl.housemd.misc.Reflections._
import com.github.zhongl.housemd.misc.ReflectionUtils._
import java.util.Date
import collection.immutable.SortedSet
import com.github.zhongl.housemd.instrument.{Hook, Context}
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/com/github/zhongl/housemd/house/House.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ object House extends Command("housemd", "a runtime diagnosis tool of JVM.", Prin
classNameOf[Env] ::
classNameOf[Inspect] ::
classNameOf[Prop] ::
classNameOf[Resources] ::
Nil

private lazy val errorDetailFile = "/tmp/housemd.err." + pid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.security.ProtectionDomain
import java.util.concurrent.atomic.AtomicInteger
import actors.Actor._

import com.github.zhongl.housemd.misc.Reflections._
import com.github.zhongl.housemd.misc.ReflectionUtils._
import com.github.zhongl.yascli.Loggable
import java.lang.System.{currentTimeMillis => now}
import actors.TIMEOUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.lang.reflect.{Method, Modifier}
* @author <a href="mailto:[email protected]">zhongl<a>
*/

object Reflections {
object ReflectionUtils {

private val S = classOf[String]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class ResourcesSpec extends FunSpec with ShouldMatchers{

val resources = new Resources(inst, PrintOut(out))

resources parse("/com/github/zhongl/test/A.class".split("\\s+"))
resources parse(".*res\\.xml".split("\\s+"))

resources run()

out.toString should be ("res.xml")
out.toString should be ("com/github/zhongl/test/res.xml\n")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import com.github.zhongl.housemd.instrument.Advice
/**
* @author <a href="mailto:[email protected]">zhongl<a>
*/
class ReflectionsSpec extends FunSpec with ShouldMatchers {
describe("Reflection") {
class ReflectionUtilsSpec extends FunSpec with ShouldMatchers {
describe("ReflectionUtils") {
it("should define Advice") {
Reflections.loadOrDefine(classOf[Advice], new ClassLoader() {
ReflectionUtils.loadOrDefine(classOf[Advice], new ClassLoader() {
override def loadClass(name: String) = {
if (name == classOf[Advice].getName) throw new ClassNotFoundException()
else super.loadClass(name)
Expand All @@ -36,17 +36,17 @@ class ReflectionsSpec extends FunSpec with ShouldMatchers {

it("shoulde get object native string"){
val o = new Object()
Reflections.toNativeString(o) should be (o.toString)
ReflectionUtils.toNativeString(o) should be (o.toString)
}

it("shoulde get or force to native string") {
val o = new Object()
Reflections.getOrForceToNativeString(o) should be (o.toString)
ReflectionUtils.getOrForceToNativeString(o) should be (o.toString)
}

it("should force to native string") {
val s = "hello"
Reflections.getOrForceToNativeString(s) should be (Reflections.toNativeString(s))
ReflectionUtils.getOrForceToNativeString(s) should be (ReflectionUtils.toNativeString(s))
}
}

Expand Down
Empty file.

0 comments on commit 1feea08

Please sign in to comment.