QueTwo's Blog

thouoghts on telecommunications, programming, education and technology

Tag Archives: Java

Making ColdFusion Sing

One of the coolest things about CFML is the fact that it was designed for rapid application development.  Even more so, as the language (and server platform) evolved, it quickly turned into one of those tools that knew how to talk to just about everything.  From FTP, to the Web, to SSH connection, to specialized JDNI connections, it is pretty darn simple to make ColdFusion the glue that holds disparate systems together.

Last month I was tasked with coming up with a tool to allow some of my PBX users to record announcements.  The current system they were used to involved them calling a phone number, entering in a special code and then recording their announcements.  It was easy because the phone already had a mic, speakers and everything else setup to the point where the user didn’t have to worry about all the related computer settings.  I tried to re-create a solution that was just as easy using the Flash Player (and ever a Java applet) — but it turns out that very few people have microphones hooked up to their computers — and those who do have no idea how to tune it so that the sound levels are descent.

I came up with a nifty idea.  I wrote a really quick CFML app to interact with Twilio (an online phone company that provides a really wicked API for dealing with live telephone calls).  Essentially, they post back form requests to you, and you pass back XML documents back to them.  They even have an API that can do recordings.

The only problem was the recordings they saved weren’t in a format that my PBX understood.  While they provided me mono, 16-bit PCM, 16k wave files, I needed 64-bit, 8k mono uLaw wave files.  To a normal computer, these look the same, but to these two different systems, they were radically different.

The solution?  I found a really cool Java application known as JAVE.  JAVE is a wrapper for ffmpeg, which is a very well known application that can work with audio and video files.  JAVE allows me to convert from one type of wave file to another, or even from an mp3 to a wave, or from a Windows Media file to an MP4 file.

Using it?  Like dealing with most Java classes, you drop it into your CFML’s LIB directory.  I tried it both with Adobe ColdFusion and Railo and it worked flawlessly.  Once you have the Java class (jave-1.0.2.jar) in the lib directory and re-start your CFML server, you are ready to start writing some code.  Here is what I came up with to convert between my two wave formats :

<cfscript>
 jave = createObject("java","it.sauronsoftware.jave.Encoder");
 incomingFile = createObject("java","java.io.File").init("C:\temp\incoming.wav");
 outgoingFile = createObject("java","java.io.File").init("C:\temp\outgoing.wav");

 audio = createObject("java","it.sauronsoftware.jave.AudioAttributes");
 audio.setCodec("pcm_mulaw");
 audio.setBitRate(64);
 audio.setChannels(1);
 audio.setSamplingRate(8000);

 attrib = createObject("java","it.sauronsoftware.jave.EncodingAttributes");
 attrib.setFormat("wav");
 attrib.setAudioAttributes(audio);

 jave.encode(incomingFile, outgoingFile, attrib);

</cfscript>

That’s pretty much it.  If you do a cfdump of the “jave.getAudioDecoders()”, “jave.getSupportedDecodingFormats()” you will get a good feeling of all the different file types you can convert between.

This is more of the type of magic we need to be talking about.  Sure, converting between file formats can seem dull, but being able to come up with a solution to do it quickly is what makes a language, and a platform useful.

Destination properties available at runtime

In my last blog post I mentioned that there were various properties available when you create your own BlazeDS / LiveCycle DS destination at runtime.  The properties available to you are based on which type of adapter you are using.

Each set of properties is baked into a ConfigMap instance that you pass to the initialize() method of the adapter.  In my research, I’ve found that there are some adapters that fire up ok without any properties defined (these tend to be the open-source ones), and some which cause all sorts of issues unless you have certain magical properties defined (these tend to be the closed-source ones).

In addition to the regular properties, there are also “ServerSettings” and “NetworkProperties”, which are not covered here.  By not setting those properties, your destination will assume the defaults.

So, what are the available properties that you can set?  Below is a list of the service/adapter/properties:

  • remoting-service    (used for Flash Remoting, Remote Object connections, and “AMF” connections)
    • ColdFusionAdapter “cf-object”   (used to connect AMF messages to CFCs)
      • access  (as ConfigMap)
        • use-mappings    (true/false) ***
        • method-access-level   (“public”/”remote”) ***
      • property-case  (as ConfigMap)
        • force-cfc-lowercase   (true/false)
        • force-query-lowercase  (true/false)
      • use-accessors (true/false)
      • use-structs (true/false)
    • Java Adapter “java-object”   (used to connect AMF messages to Java classes)
      • include-methods (if defined, this is the list of methods that are allowed to be called)
      • exclude-methods (a list of methods that are blocked from being called. do not use with include-methods)
      • method
      • name
  • messaging-service   (used for realtime messaging applications)
    • cfgateway  (used to have ColdFusion mediate messages on the destination)
      • gatewayid  (the dot-location to the cfc that will handle the message) ***
      • gatewayhost  (used to define which host is hosting the cfcs, if it is not localhost — for example if your BlazeDS server is different from your CF server)
      • allowedIPs (which servers are allowed send data onto the destination back to the clients)
      • remote-username  (bakes in this username into all the packets sent to CF)
      • remote-password  (bakes in this password into all the packets sent to CF)
    • actionscript
      • (there are no properties for ActionScript.  Send in an empty ConfigMap)
    • jms
      • jms (as ConfigMap)
        • destination-type
        • message-type
        • connection-factory
        • destination-jndi-name
        • delivery-mode
        • message-priority
        • preserve-jms-headers
        • acknowledge-mode
        • max-producers

There are also services for proxying and data-management, however I’ve never used them (well, I have used data-management a lot, but since the licensing changes to LCDS, it’s pretty much dead to me).  I don’t know much about the jms adapter, so I can’t really say what all the properties mean, but I do know they are there. 

I have marked some properties with a ***.  If you see that, you need to include it, or you will get varied results in creating the destination at runtime (most likely, it will fail the first time you send data to it, rather when you create it).

Creating runtime remoting destinations in BlazeDS and LiveCycle Data Services

Ok, lets dive right into a really geeky topic… destinations in BlazeDS and LiveCycle Data Services.

For one of my customers, I was given the requirement to create a runtime destination in BlazeDS.  BlazeDS, along with its bigger brother LiveCycle allow you to create destinations in one of two ways — via the configuration XML files (services-config.xml, and related files), or via Java calls.  I won’t talk about the XML file method because it is extremely well documented.  However, the biggest disadvantage of the XML file method of creating destinations is that you usually have to reload or reboot your server in order for the changes to take affect. 

Runtime Destinations are not permanent — they do not persist during restarts.  They are also not magically added to the XML documents either.  Their creation is also often blocked on shared hosting servers as well.  But besides that, they act and smell just like ones created by the XML documents.  Why would you want to use them?   There are quite a few reasons for wanting them — for example to help secure your server by only exposing your destinations on demand, or by providing private destinations to clients so there is no way that data can be shared (in that case you COULD use subtopics, but that is controlled by the client).  In my case I was asked to provide a solution where the client would not have to change any configuration files when they installed our application.  We were using ColdFusion 9 as our back-end server, but these techniques work for Java based solutions too (you would just have to change the Syntax a bit).

I’ll start off by saying that the documentation is extremely poor (I wish Adobe would make this stuff clearer one day!), but with the help of two people, Sven Ramuschkat and Marko Simic, I was able to get to the point where reading the source code made sense.

The first step in creating the dynamic destination was getting the instance of the Message Broker.  The Message Broker in BlazeDS/LCDS is the guy who does all the work for remoting, messaging, data services and proxying.  You can access it in ColdFusion with the following commands

this.blazeDSClass = createObject("java", "flex.messaging.MessageBroker");
this.blazeDS = this.blazeDSClass.getMessageBroker(javacast("null", ""));

Read more of this post

Why ColdFusion should NOT be taught in schools

I already know that this post won’t be popular with my close friends, but I think it needs to be said anyway…   Don’t worry, this isn’t a “ColdFusion is dead” article.

I was recently asked to sit down for lunch with two computer science professors from two different schools in my area.  For the most part, I was in “a fly on the wall” mode as these professors talked about various things such as how they catch people copying code, to how they teach good memory use, etc.  This wasn’t a formal meeting by any sense, but rather just a meeting of two friends who happen to work in the same profession.

Chalk BoardAbout half-way through lunch, I decided to ask about using a more stable language, like ColdFusion (CFML) to teach web programming. Currently one of the schools teach Python (and considering Ruby) and the other teaches PHP for their courseware.  After both chuckled at me, I dove into my Adobe taught ColdFusion isn’t dead, ColdFusion has a growing population, ColdFusion has some open-source alternatives, etc.  Both sat there and soaked in my sales pitch.  One of the two even used to use ColdFusion on some side work in the past.

“It’s not about the servers — it’s truly about the language.” spoke the gent from the larger school.  “For a formal, introductory computer science class, ColdFusion [CFML] really doesn’t offer us anything that another language does.  And it adds in a lot of complexities and guessing that make other languages easier to teach in the classroom

As he explained his position, it started to soak in to me — ColdFusion while really easy to pick up and learn teaches some really god-awful practices.  And if you take a look at the ColdFusion community at large, it seems to be a real mess.  Some of the great points from his argument :

  • CFML is not a typeful language.  For the end user to not be able to specify to the language/compiler that you want to store a String vs. a Number can be troublesome and confusing to both sides.  What is often worse is that to force ColdFusion to choose a particular variable type is very awkward and more-or-less a hack.
  • There is no good CFML debugger.  If you do your work in the current Official CFML editor by Adobe (Dreamweaver) there is no debugger.  You have to install another, 3rd party application (Eclipse) to do your debugging.  Even then, it is cumbersome, and not really feature complete.  How much memory is this CFC using?  Why am I getting this response from the Query?  These are the questions that the debugger still fails to answer properly.
  • The documentation is poor.  The LiveDocs, while OK, are very un-organized and seem to be very short on examples.  The community is good at sharing examples, if you can find them.  The Adobe search engine is a joke on their site.  There are only 4 current books, by two authors (although, one plus to this is they both live in Michigan).
  • CFML, by its design will fight you in implementing the most common design patterns.  Lets face it, most of the MVC frameworks are a hack.  In fact, when you take a look at it, most frameworks that exist for ColdFusion — even Adobe’s own Model-Glue are exceedingly complex because they have to implement so many workarounds to get their pattern to work. 
  • Available Open Source / Free / Alternative engines are not 100% compatible.  Even BlueDragon, the engine that has implemented the most compatibility, is only about 90% there.  This is a huge disadvantage if you use the “there are other engines out there” argument — most, if not all, examples and documentation are written for the Adobe engine. 
  • Some features are just some “black box”.  This is one item that raised a huge concern, while being a huge advantage for the regular business community.  Programmers, specifically students want to know how things work.  They want to know the nuances of why FTP would be a sessionful protocol, rather than something like LDAP. These tags/engines are essentially black boxes with ColdFusion, with very limited visibility as to what is happening on the back end — all we know is that they ‘work’, and get the job done. 

Some of these things are changing in future versions of ColdFusion, I’m sure.  Others are just breed from the history of ColdFusion, and for the regular community, that’s not a bad thing.  For example, does a web / CFML programmer really want to know how the POP protocol works?  No!  That’s why they use ColdFusion! 

So parting from my experience, I do ask the question — where SHOULD CFML be learned?  That is a really tacky question, one that is really hard to answer.  From the above, CFML shouldn’t be chosen to be taught in a formal programming class, but how about in a web programming class?  Rather than teach HTML, how about HTML and CFML?  Should CFML be taught at a community college?  How about at the high-school level where programming theory is not nearly as important? What are your thoughts?

Follow

Get every new post delivered to your Inbox.

Join 27 other followers