dynamic

How to dynamically add a Scala trait to an object instance

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 8.8, “How to dynamically add a Scala trait to an object instance.”

Problem

Rather than add a trait to an entire class, you just want to add a trait to an object instance when the object is created.

Solution

Add the trait to the object when you construct it. This is demonstrated in a simple example:

How to mix Scala and XML to dynamically generate XML

Problem: You want to dynamically generate XML from your Scala source code, such as creating output for a SOAP web service.

Solution

A great feature of Scala’s XML support is that you can interweave XML and regular Scala source code together. This lets you dynamically generate XML from your Scala code.

To create XML with dynamic, embedded data, just put your Scala code in curly braces inside the XML tags, as shown in the following example:

Generating dynamic XML from Scala source code (like a template)

Scala FAQ: How can I dynamically generate XML from my Scala source code, such as creating output for a SOAP web service?

Solution

A great feature of Scala’s XML support is that you can interweave XML and regular Scala source code together. This lets you dynamically generate XML from your Scala code.

To create XML with dynamic, embedded data, just put your Scala code in curly braces inside the XML tags, as shown in the following example:

A Mac OS X translucent JFrame with a dynamic slider control alvin May 29, 2009 - 8:31am

Earlier today I wrote a blog entry about how to create a transparent/translucent JFrame on a Mac OS X system, and then I thought, "Wouldn't it be cool if you could dynamically control the transparency level of the frame?" I did a little bit of research to make sure this can be done, and sure enough, it can.

Dynamically build a Java classpath in a Windows batch file

I don't work in the DOS (Windows command (CMD.EXE)) world too much these days, but I needed to today. The basic problem was that I needed to build an environment variable dynamically. In this case I was trying to build a variable named CLASSPATH from a collection of Java JAR files in a sibling directory named LIB. This variable had to be built correctly for my Java application to work properly.

Generate Java code from your database design

I don't write much in the way of business application code any more, but if I did, I would generate Java code like crazy.

Think about it, whenever you write database-driven applications, one common denominator is that every database driven project has the database design completed before you start coding. And then, once you start coding, I'll bet 80% of the code is related to what I call CRUD ("create, read, update, delete") functionality.

And you know what -- you can generate this Java database code, either statically, or more powerfully, dynamically.

How to build a dynamic Java classpath in a shell script

Here's a Bourne Shell (sh) script I use to run a Java anti-spam program I wrote. The program I'm running isn't important, but what is worth sharing about this shell script is how I dynamically build the Java CLASSPATH by including all of the jar files in the lib directory.

Other parts of the shell script (showing a shell script for loop) may be worth sharing as well, but I think that building the Java classpath dynamically in the shell script is probably the most important part.

With that being said, here's the shell script: