Utterances of a Zimboe

Programming the Internet.

The Stinky Bits

leave a comment »

These are hardly of any inspiration for anyone else, but just to show the code(s) that ‘sparked’ my previous observation:

override def init(ctx: ComponentContext) {
  val descs = descriptions(new File(ctx.getInstallRoot))
  provides.foreach { svcName =>
    debug("Searching description for service %s", svcName)
    descs.map(t => (t._1.getService(svcName), t._2)).find(_ != null) match {
      case Some(t) => services = t :: services
      case None => warn("No description found for service '%s'", svcName)
    }
  }
  info("Found %d services: [%s]", int2Integer(services.size), services.map(_._1.getName).mkString(", "))
}
def descriptions(dir: File): Iterator[(Description, Document)] = {
  val cache = new HashMap[String, Option[(Description, Document)]]
  dir.listFiles(wsdlFilter).elements.map(f =>
    cache.getOrElseUpdate(
      f.getAbsolutePath,
      try {
        wsdlReader.synchronized {
          debug("Reading WSDL '%s'", f.getName)
          val doc = domParser.synchronized(domParser.parse(f))
          Some((wsdlReader.readWSDL(wsdlReader.createWSDLSource | { s => s.setSource(doc) ; s.setBaseURI(f.toURI) },
                new WodenHandler(f.getName)), doc))
        }
      } catch {
        case t: Throwable => error(format("Exception while reading '%s': %s",
                                   f.getName, t.getMessage), t)
                             None
      })
  ).filter(_.isDefined).map(_.get)
}

That’s just a bit of JBI-related framework logic, which lazily parses some WSDL-files and caches the structures until the required descriptions are found.

Works fine. :)

ps. definitely not the best show-case for abstraction, but I just wanted to play around with files a little.

pps. my editor is 100 cols wide, and the parts that don’t fit are irrelevant.

Written by Janne Savukoski

November 7, 2007 at 1:46 am

Posted in Programming

Leave a comment