QueTwo's Blog

thouoghts on telecommunications, programming, education and technology

Tag Archives: Java

ColdFusion (Railo) on Microsoft Azure Websites with IntelliJ IDEA — Part 2

In Part 1 of this series, I showed how to setup your project on Railo so that you could publish your CFML to Microsoft Azure Websites.  You should have your project in a state that you want published on the web.

For this part, you will need to have an Azure account.  You can sign up for one at http://azure.microsoft.com.  Remember, BizSpark and certain MSDN members get free credits on Microsoft Azure.

  1. Login to the Microsoft Azure “Management Portal”
    1. At the time of writing, there are two versions of the portal — the Classic and New.  I’m going to show the Classic portal as it seems to be changing the least at the moment — but I’m sure the New portal will eventually replace it.
  2. Create a new “Custom” Website
    1. After you’ve logged into the Management Portal, click on the “+ New” button on the bottom of the site.
    2. Chose “Compute” -> “Website” -> “Custom Create”
      azurecreatesite
    3. Chose a URL for the site, Web Hosting Plan, and if you want a database.  Don’t chose the “Publish from Source Control” at this point — we will set this up later.
      azurecreatesitename
    4. It will launch the new site. This will take a few minutes.  Once you see “running” on the dashboard, click on it to modify the settings of the site.
    5. Click on Configure to configure the site.
      1. Enable Java by setting a version.  This will disable PHP, Python and .NET processing.
      2. Make sure to select Tomcat as your container.
      3. Your choice on 64 or 32 bit.  Railo doesn’t care which is running.
      4. Web Sockets will allow for outgoing connections from Railo to outside servers (such as HTTP calls).  If you don’t do this, keep it off.
      5. Always On will keep the site “warm” if it is not in active use.  If this is off, Azure will unload it from memory if there is not traffic to the site within a half-hour.
      6. Scroll down to Default Documents.  Add index.cfm.  You are free to remove the other options if you wish.
      7. Hit “Save”  It will take about 5 minutes for Websites to update your site to use Java.
  3. Upload your site to Azure (Using FTP)
    1. Click back to the Dashboard of your site.  Scroll down about half way through the page and find your FTP credentials.
      azure-ftpcred
    2. Using your favorite FTP client, login to this FTP site.
    3. Change to the /site/wwwroot/webapps/ROOT/ folder on the remote side.  This is where your files will be served out of.
      ftptoazure
    4. Delete the files that exist in the ROOT folder.  Upload ALL the files that exist in the output of your exploded WAR directory (in my case, E:\Projects\SampleWebsite\out\artifacts\SampleWebsite_war_exploded\).
    5. In some cases, you may need to restart the website.  You can do this via the management portal and clicking on “STOP” on the bottom of the page.  You can then click “START” to restart it.
    6. The first load after you upload the files can take 30 seconds.  Don’t be alarmed about this — subsequent loads should be much faster after Railo is loaded into memory.
    7. Keep uploading subsequent changes to your code and play around with settings in Azure.  For example, if you want a “custom domain”, you can change the site to a Standard plan instead of the free one.  You can also configure IntelliJ to automatically upload changes via FTP when you deploy a site.
  4. Upload your site to Azure (Using GIT)
    1. Deploying via GIT is a pretty cool concept, but it does take a few additional steps to get working.  First, we will need to create a new artifact in IntelliJ.  This is so we can keep our GIT repository separate from our working one (if we are using GIT for source control).
    2. Right-Click on the SampleWebsite Module in the Project view.  Chose Open Module Settings (or you can press F4)
    3. Click on the Artifacts tab.
    4. Add a new Artifact “Web Application: Exploded” -> Empty
    5. Give the new artifact an obvious name, like Azure Push.
    6. Make a note of the output directory name.  This will be the root of your new GIT repository
    7. Under Output Layout, click the create directory button to add a new directory named “webapps”
    8. Add another directory below that named “ROOT” (caps matter)
    9. Under Available Artifacts on the right-column, drag “SampleWebsite war:exploded” to be under ROOT
      git-push-setup
    10. Click Apply, then OK to get out of the Artifact setup.
    11. Go back to the Azure Management Portal.  Open your site, and go to the dashboard.
    12. FTP into the site using the site using the instructions above.  You don’t need to upload the files, but you DO need to remove all the files in ROOT in order to get a clean site started.
    13. Go back to the site dashboard.
    14. Click “Setup Deployment From Source Control”
    15. Click on “From Local GIT Repository”
    16. Load your GIT client (like git bash), and go to the output directory of the artifact we just created.
    17. Follow the commands on the Azure site on how to commit your initial files to your local GIT repository.  Don’t clone the existing site.
    18. After you’ve committed your first file, and connected it to Azure GIT, do a git add * , and commit those files to your site.
    19. Your Azure site may need to be restarted after your first commit.  I’ve seen it both ways.  If it still shows a generic Azure is running Java page, try restarting the site.
    20. In order to refresh the site’s contents, follow the following steps :
      1. In IntelliJ, Go to the Rebuild Menu -> Rebuild Artifacts -> Azure Push -> Build.    This will refresh your site’s artifact.  DO NOT CHOSE REBUILD OR CLEAN — these will remove the GIT database files and you will have to re-connect it to GIT.
      2. In GIT, add *, then commit.  This should update the site with the latest.

And that is it.  A few notes, however :

  • You can’t deploy .WAR files onto Azure websites.  It doesn’t work for some odd reason — and when it does (it happens sometimes), it breaks your site because of partial loads.
  • It seems that connections to Amazon S3 are blocked.  You can’t use the s3:// commands.
  • If you use Microsoft’s Azure SQL Server, you can’t use any ORM (hibernate).  Azure SQL Server requires some changes to the SQL Statements to allow for clustered indexes.  You can use their “free” MySQL instance.  Manual SQL statements via Stored Procs and CFQUERY work fine.
  • If you do things like update Railo or upload files locally on your machine, make sure that you update your local project or those files may not make it into your deployed project on Azure.

Hope this tutorial gets you in the right path!  I’ve been deploying a few sites using this method with great success.  (I’m using GIT to deploy my sites — it’s already in my toolchain and seems to work well).

ColdFusion (Railo) on Microsoft Azure Websites with IntelliJ IDEA — Part 1

I was lucky enough to have Microsoft give me some time on Azure through the BizSpark program.  If you don’t know what BizSpark is, it is a program from Microsoft to give small businesses a headstart on the Microsoft stack, grattis for three years.  That includes MSDN, some credits on Azure and more.  Azure is a comparable solution to Amazon’s Web Services that I’ve been using for a few years.  Azure’s Websites solution is similar to AWS Beanstalk where you upload your final project to their servers and they take care of the rest (OS, patches, firewalls, etc.)

As I’ve already written in previous blog posts, Railo is simply a Java application that can sit on top of a J2EE server such as Apache Tomcat.  Railo is compatible with Adobe ColdFusion, and is 95% there for simply letting ACF code run.  The only time you can expect to run into issues is with some of the integration tags like CFEXCHANGE, CFCHART or some of the Active Directory integrations.  But on the flip-side, Railo requires no install, no purchase of a license, and (in my opinion) is easier to work with.

In this walk-through I’m going to use Railo 4.2.1 deployed as a WAR application onto Apache Tomcat running on Azure Websites and using IntelliJ IDEA 13.1 as my IDE.  You should be able to easily substitute Eclipse or ColdFusion Builder for the IDE — I just don’t happen to use those anymore.

Downloading all of the required components:

You will need:

Setting up your environment:

Do the following:

  1. Extract the Tomcat .ZIP file to a directory on your computer.  Doesn’t really matter where — as long as you know where it is.  This will be where your local, testing environment will run from.
  2. Rename the Railo .WAR file to .ZIP.  This will be the files you will start with.
  3. Install IntelliJ, if you haven’t already.
  4. Add your Tomcat setup to IntelliJ.

    1. Go into the IntelliJ Settings by clicking on Configure -> Settings from the Quick Start menu, and search for Application Servers under IDE Settings.
    2. Click the green [+] and then select Tomcat Server.
    3. Provide the Tomcat Home directory.  This is the directory where you extracted Tomcat to.
    4. Hit OK, then Apply to commit this new application server to IntelliJ
      intellijSettings
  5. Add a new Java Project to IntelliJ
    1. Go back to the Quick Start menu, and choose Create New Project
    2. Select Java and then Select Web Application.  You may need to add a new Project SDK by clicking on New… on the upper-left of the screen and letting IntelliJ know where your copy of Java is installed.
      new-project
    3. Don’t select to create the project from a template.  Click Next.
    4. Give the project a name and a location.  Click Finish to create the project.
      createProjectName
    5. After IntelliJ creates the project, in the Project area, you should see a src folder, a web folder, a JSP file, and a WEB-INF folder.
      project-view
  6. Test your application setup with Tomcat.
    1. From the “Run” drop down, select “Edit Configurations…”  to add a new run configuration.
    2. In the “Run/Debug Configuration” screen, click the [+], select Tomcat Server, then Local to add our run config.
      runconfig
    3. Give the Run Config a name (like “Local Debugging”)
    4. Click the “Fix” button near the error that states you don’t have an artifact marked for deployment.  This will tell IntelliJ that we plan on deploying our project to our local test server when we click the Run button.  The name of the base “artifact” is “SampleWebsite:war exploded”.
    5. Change back to the Server tab.  Change the option “On Frame Deactivation” to “Update Resources”.  This means that when you make a change within IntelliJ and switch to your web browser to check that change it will happen automatically.
    6. Click Apply then OK to close the window.
  7. Run the Local Application Server and deploy our base application.
    1. Click the green “Run” button next to the drop-down with the Run configuration we just created.  You will see Tomcat spin up in the console messages on the bottom of the screen.  Eventually you will see a browser launch with a blank page.  This is actually displaying the contents of the index.jsp file in our project.
  8. Copy Railo into our project.
    1. Remember when we renamed Railo to be a ZIP file?  Extract that ZIP file into our web folder within our project.  You most likely have to do this outside the IDE.  You will need to overwrite the WEB-INF folder (including the web.xml that is included within it).
    2. Delete the index.jsp file within the web folder.  You don’t need it anymore.
    3. Within the web folder, create a new file named index.cfm.   Add a <cfdump var=”#server#”>
      runCF
    4. Click on the green “Run” button next to the drop-down with the Run configuration we created earlier.  You will get a dialog asking if you want to update the resources or restart the server.  Chose “Restart the server” so that we can load Railo.  This is the same option you would use if you ever need to restart CF (for example, to unload a CFC that is loaded into the session memory).
      restartcf
    5. You will see Railo unpack and launch with the console window.  After a few seconds, you should see your browser launch with the debug output.  Congratulations, you are now running CF!
  9. Create your application like you normally would.
    1. Any files within the WEB folder of your project are essentially in the root of your web server.
    2. Railo Administration Dashboard (The CFIDE, if you will) is located at http://localhost:8080/railo-context/administrator/server.cfm    This is where you would setup your databases, etc.
  10. Copy your configuration files BACK to your project
    1. Any files that change on your web server are NOT copied back to your project.  This includes configuration changes (like databases) made within the Railo admin.  To copy these files back, go to your project folder (in my case E:\Projects\SampleWebsite), then OUT\Artifacts\SampleWebsite_war_exploded\    and copy all the files back to your project.  In my previous blog entries I mention which files you can copy back if you are simply looking to save the Railo config files.

Part 2 of this entry I will show how to get this posted on Microsoft Azure Websites.

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 29 other followers