in

 

Bytes of Wisdom

  • Just wanted to share something that made my day.

    Sniff...sniff...I think I'm in love...

     http://www.youtube.com/watch?v=T3iryBLZCOQ

     

  • Seeking Closures

    So, what's a closure and why should I care?

    As Bob Dylan said, "...the times they are a changin'."   The strange world of functional programming, once restricted to more esoteric languages such as LISP, Erlang and Haskell, is now invading the mainstream.  Languages such as C# and Java are now incorporating concepts once unique to functional programming languages.  These changes should bring simplicity to these more mainstream languages, but I find they are often misunderstood (by myself for certain) and thereby misused or not taken advantage of.  I've had a fair understanding of closures for a while, but wanted to develop a more solid understanding, ergo this post.

    One aspect of functional languages, called closures, have been talked about since .Net 2.0 came on the scene.  Of course, what many presented as closures in 2.0 really weren't -- for a number of reasons that I don't care to get into at this time.  More recently with the 3.0 framework (and the same will hopefully be true with Java 7), we see the introduction of lambda expressions; and finally have true support for closures (remember, not all anonymous functions are closures).  You may ask, however, what the heck are closures anyway?  Well, I'll do my best to give an accurate illustration.

    First off, what are they.  As the Wikipedia link above stated, "a closure is a function that is evaluated in an environment containing one or more bound variables."  Neal Gafter, during a Google Talk given in January 2007, put it another way by saying that a closure is "a function that refers to free variables in its lexical context."  We all should know what functions are...they are constructs that take a number of parameters and return a result (this includes more than just returning void).  A free variable (or bound variable) is defined as a name (or symbol) that has a definition outside of the closure itself.  The "lexical context" can be thought of as the context of the caller.  I'll give an example later.

    One thing that I found particularly interesting about Gafter's presentation was how he helped define closure by describing what they should support.  Among other things, he mentioned that closures should have access to private members of the "lexical context" in which they are used.  In C# and Java, they can also access the "this" instance of the caller.  They should also support return values and support throwing exceptions (the latter is more of a Java concern).  He also pointed out that although they are executed within the context of the caller, they can only return control to the caller (and not force the caller to return). 

    Gafter also pointed out that closures -- as proposed in Java at least -- are methods that act as new statements in the language and can be used to extend the native API (he calls these "control abstraction").  He described them as "concise function literals" that should behave as built-in statement, such as constructs such as for, while, lock, using, etc. in C# but without the problems associated with anonymous delegates and the like.  Again, they are not anonymous class instances (such as delegates, which are not pointers to methods, but rather classes used to box methods).

    Okay, so we have at least a rudimentary definition of closures; now let's see how they can be useful.  To again steal from Gafter's presentation, I'll use a couple of his examples that I found useful. 

    First off, what's one of the problems closures can address?  Gafter was describing a bit of code that was used repeatedly throughout an application at Google to gather run-time performance metrics on various operations.  He argued that closures could be used to extend the native API and offer a more elegant syntax.  Here's what the code looked like:

    image

    Granted this is only 8 lines of code, but only one of the 8 lines relates to the business logic of the application.  The additional code (the other 7 lines) ends up cluttering the code and thereby obscures the surrounding business code.  It would be nice if this code could be replaced by something like this (this would probably be a static method from some util class):

    image

    Which with the Java's proposed new method invocation syntax would mean the same thing as:

    image

    Neat, huh?  With this example, the time() function is used in a manner similar to "for", "using", and other such operations native to the API.  This is an instance where using a closure could be used to wrap common functionality around business logic in a more sensible way.  Since the time method is a closure, we could also access doSomething() is it was a private method, such as this:

    image

    Pretty groovy huh? (pun intended)

     

    From the horse's mouth...

    *WARNING: If you have a burning lack of curiosity or can't fathom looking at code other than code written in your favorite language DO NOT continue reading.  Reading LISP code without an open mind can lead to a general sense of disaffection and in more extreme cases unreasonable zealotry for your preferred language. 

    As stated recently I've been digging deeper into LISP lately.  In LISP such features are native (and natural) to the language.  For some reason I tend to think that closures look for magical (as well as cleaner) in LISP programs.  I'll give another example of closures, but this time with one of the parent languages to all this closure goodness.

    So here's the scenario...which, by the way, is an adaptation of the practical exercise in Chapter 3 of Practical Common Lisp.  In the example I'm writing a simple little application to help break down the living things in my yard into a truncated taxonomy.  Once finished, I'll be able to look up groups of living things by either kingdom or phylum.  Alternately, I can look up a living thing by name and see to which kingdom and phylum they belong.  Bear in mind I probably would never bother with this in real life...

    Setting things up:

    First off, I'll create a global symbol for my collection of living things, a function that creates a new living thing, and a function to add a new living thing to my global collection.  Now I know that I should be using the CLOS, but I don't want to confuse anyone with slots and whatnot.

    image

    That was simple enough.  We see that *living-things* is initialized as an empty list (or null set).  The function "make-living-thing" uses the "list" operator to create a special kind of list called a "property list"...think of it as a hashmap.  The "add-living-thing" function simply pushes whatever is passed to it onto the *living-things* collection (which I'm using as a stack in this case).

    Now I'll walk around my yard and add the living things that I find...

    image

    Okay, so I left Bermuda Grass out...most of the grass in my yard is dead anyway.  I can print out the contents of *living-things* and see the raw data:

    image

    Now to make things look a little prettier (only a little, mind you), I'll add a few functions for displaying the data in a more readable format:

    image

    And now I have something like this (output is truncated):

    image

    And now for something completely different...

    Okay, now we are nearing where closures enter the example.  Next I want be able to pull data from *living-things* using a SQL-like syntax.  Don't ask why, I blame it on too much HQL. 

    Since I have only one entity type, I'll just copy the list passed to the from clause and return it (nothing to it...copy-list came free with Common LISP).  The select statement takes a data source (ultimately using the from function) and n comparison predicates.  The function "remove-if-not" is a Common LISP function that removes items from the list (passed as the second parameter) whenever the predicate evaluates to false/nil (the first parameter).

    image

    If you run the code from the comments you get the following:

    image

    Here you see the first use of a lambda expression to make an anonymous function (a function not associated with a symbol).  The inner function, getf, is just a getter function that gets the value associated with the symbol :kingdom.  The variable x in the getf is the current list item.  For instance, the following gets the value associated with :kingdom for the first item in *living-things*:

    image

    The equal function returns true (T) if the value for :kingdom at x is equal to 'PLANTAE.  That's all that there is to the lambda expression...it simply serves as the comparison predicate for "remove-if-not".  There's still no closure here...any symbol could be used instead of :kingdom and the expression would still be valid:

    image

    See? return NIL, no problem. 

    End game...

    Now we'll work on the where clause.  Passing lambda expressions is kind of ugly, though, so we'll want to create an operator that will handle the where clause.  Once finished, we should be able to make something like the following call and get all the matching entities back:

    (select (from *living-things*) (where :kingdom 'ANIMALIA :phylum 'ARTHOPODA))

    This part gets a little funky because we're going to use macros.  Macros in LISP are similar to macros in C, but pumped up on steroids.  Macros in LISP are used for run-time code generation (but much more elegant than IL generation in .Net).  I'll skip over some of the details on macros but will attempt to get at the spirit of this whole thing:

    image

    This part's a little hard to follow.  We are focusing on the "where" macro, but I think we'll start with the simpler predicate functions. 

    Birds eye view: First the "where" macro utilizes the "make-comparisons-list" function to generate a comparison predicate from a set of criteria.  The "make-comparisons-list" function utilizes "make-comparison" for create a part of the overall comparison predicate using a field/value pair (such as :kingdom 'ANIMALIA).

    The "make-comparison" function works just like the lambda function in the example above.  First it gets the value associated with the specified symbol (such as :kingdom) and then checks that value against value that was passed as a parameter.  Here's where we see a closure, where "living-thing" appears out of nowhere.  First off, the backquote before the list containing "equal" is used to evaluate the list as code, the commas latter in the list mark variables.  Without the backquote and commas we'd have an error because "living-thing" is not a bound variable (and that's bad).  This backquote facility is very important in that it allows the coder to easily define code that can be evaluated later.  So, if you execute "make-comparison" you get a piece of the code that the macro will ultimately execute.  Again, I'm sure you noticed that "living-thing" isn't a bound variable...yet.  We'll get back to that.

    Since we want to support more than one comparison predicate, we've also created the "make-comparison-list" function with iterates over a list of fields and values (or properties and their associated values).  The "loop while x collecting y" construct is an iterator with an accumulator.  Basically it is iterating over all the field/value pairs (x), popping the field then the value and passing them to "make-comparison", and finally appending the resulting code (y) to a new list (this is a Common LISP feature).  The result is a list of comparison predicate expressions. 

    Here the macro takes everything following the "where" operator and constructs the appropriate of comparison predicates.  The where function will iterate over the list returned from the "from" function and pass each list item to the lambda expression. The variable "living-thing" is introduced as a parameter in the lambda expression, which will be bound by the time the code resulting from "make-comparison" is evaluated (where the closure is).  The "and" function is used for the conjunction between all the comparisons resulting from calling "make-comparisons-list".  Ideally, I'd use a more generic name for the bound variable used in "make-comparison", so that I could create predicates for any other property list.

    To illustrate the code that's generated I'll make a few calls to the function macroexpand-1 (it's kind of like reflecting over the macro):

    image

    So, now you've seen the final lambda expression generated by the macro when using both multiple field/value paris and when using only one.  Now we'll put together the entire select statement and test it...I'll get all animals from *living-things*.

    image

    ...and now we'll select all plants and use the "friendlier" display:

    image

    So, as far as using closures, the closure on "make-comparison" was handy for supporting the lambda expression in the "where" function.  I think that LISP is nice to clearly illustrate how closures work.  One doesn't NEED to use closures...but at times they are quite useful for making code a bit simpler and easier to read.  I hope this post was helpful in elucidating the nature of closures and how they might be used.  I also hope it helps show how fresh a 50+ year old language can be.

    For more information on closures:

    This article seemed like a decent illustration of their use in C#.

    If you can make it through the near 2 hour presentation, Neal Gafter's discussion of Closures in Java about a year and a half ago was an interesting illustration of what they might look like in Java.  I also used some of his discussion as an example in this article.  (It's also fun to watch the original crowd of near 60 people dwindle down to about 10 by the end of the presentation)

    Then of course there are the scores of books and sites about LISP, Erlang, Haskell, Ruby, JavaScript and other languages that feature closures as a central feature of the language.

    Happy Coding!

    Joshua

  • Learning to Speak with a LISP

    So it's now my second week of playing with LISP and I have to tell you it's been a heck of a ride so far.  I've played with LISP in the past, but never with such intensity.  I figured I'd share some of my experiences as I engage with this language once more.

    It's hard to convey the value of learning LISP.  I've played with Scheme in the past as essentially mental gymnastics...the LISP family really challenges the way one thinks about programming in general.  More recently, the increased popularity of languages such as Ruby has increased interest in functional languages...many of which trace there pedigree back to LISP (which has been around since the 50s).  For you .Net and Java developers out there, the increased importance of functional languages could easily be justified by recent additions to the feature sets of both languages (lambda expressions, closures, anonymous functions...all LISP).

    My first experience with the LISP was in 1998 while taking "Survey of Programming Languages".  As part of the class each student had to start off by writing a paper about any programming language that they wished.  I though that LISP sounded neat and wrote a paper on Common LISP with a focus on the CLOS.  Much to my chagrin, I soon found that our next task was to implement a set of programs in our language of choice.  I began to think that maybe the guys who picked languages like Fortran and Pascal weren't such idiots after all...in short I was hating life.

    My first major hurtle in learning LISP enough to survive the course - by survive I mean earn an 'A' - was picking an implementation of LISP.  I found that there weren't a whole lot of options for my Windows 95 box.  I ended up settling with Harlequin's implementation, but it wasn't near as nice as what I'd used for C++.  I delivered my code on time, made the marks I wanted, and then chucked LISP near the dusty bottom of my mind...along with Pascal and numerous flavors of Basic.

    Now 10 years later I've dug out the language am giving it another go.  I'm now aware of quite a few LISP implementation to choose from and picking the right one for me has been a bit of a chore.  Dr. Scheme was pretty cool, but I really wanted to learn Common LISP...so I finally settled with Allegro CL (for the time being at least).  The IDE choices are equally disappointing, but there are a few reasonable choices.

    I'm currently using LispBox, which packages Emacs-Slime-AllegroCL in one nifty little package (Allegro's IDE is terrible...for my uses at least).  I must agree that Emacs is probably not for everyone.  There are implementations supported by vi as well as a number of commercial products.  For Dev Studio guys the IDE options are pretty depressing.  There are numerous references to an Eclipse plugin called CUSP that sounds pretty good, but I haven't used it myself.  As far as Eclipse, I'm using Ganymede.  When I tried to install CUSP in the past I was thwarted by a bad URL for the update site...I tried an alternate URL that worked, but wouldn't run on Ganymede (looks like it was written for Europa).  Dandelion seems to work okay, I just haven't played with it yet (comes with CLISP implementation).

    As far as learning the language itself, I've found a couple of sites that did a wonderful job at justifying learning LISP.  Erik Rasmussen did a great job in explaining both the delight of learning LISP and the horror of picking an IDE.  Erik also referenced another article that did an amazing job of describing LISP in terms familiar to C++/C#/Java people...it's a little long but well worth the read.  My favorite book to date on the subject has been Practical Common Lisp by Peter Seibel.  Seibel's book is geared toward developers already fluent in another language...so it doesn't beat around the bush too much. 

    Well, that's it for now...I'll share what I learn if I think I can present something of value.

  • Awesome response to an interview question...

    Just a quicky here.  I've been reading "Practical Common Lisp" by Peter Seibel (great book by the way) and came across a really funny footnote.  I think I'll use the interviewing engineer's response if given the chance in the future.

    Here it is:

    "A friend of mine was once interviewing an engineer for a programming job and asked him a typical interview question: how do you know when a function or method is too big? Well, said the candidate, I don't like any method to be bigger than my head. You mean you can't keep all the details in your head? No, I mean I put my head up against my monitor, and the code shouldn't be bigger than my head."

     The system I'm currently working on has methods with hundreds of lines of code...I wish everyone would use the "head check".

  • Note on sets in hibernate/nHibernate

    I just wanted to share this with anyone who is not aware...

    Caveat:  After reviewing my original post and the comments that follow I had initially elected to change the container tag in the example below from set to list.  After thinking about it further I've returned the tag to 'set', since this is after all what I was trying to illustrate in the first place (although understanding the risk of using set should be understood).  Granted, a list probably should have been used, but this was a solution to a problem relating to an existing system that I'm currently working on.  I apologize if it offends your senses, it sort of does mine too, but the tests do pass and the code does work as expected.  Hopefully others facing similar constraints may find it of use in any case.  Smile

    We are using hibernate of my current project, but this should be the same for nHibernate.  In any case, we ran into a situation where we needed to ensure that a collection (Set) on a class that we were mapping needed to ensure the order of list items when used in the middle tier.  The class had a sequenceNumber field that was used to indicate ordinality in the Set.  The knee-jerk solution proposed was to modify the class to support sorting, but it was work that no one wanted to pick up so the code stayed broken.

    I, being industriously lazy, didn't want to do it either but wanted it done none the less.  I had never set the order-by in the hbm configs before but figured this would be as good a reason to use it as any.  So I made one simple change to our mapping file and the broken unit tests magically started passing.  Here's a code example:

    <set name="myCollectionOfThings" lazy="true" inverse="true" order-by="SEQUENCE_NUMBER ASC" > <key> <column name="THING_HOLDER_FK" /> </key> <one-to-many class="blah.blablah.ThingHolder" /> </set>

    It's the "order-by" attribute in the "set" tag is what is used to define the default OrderBy behavior.  The contents are straight SQL that get's tagged on the end of the rendered prepare statement that is executed against the database.

     

    Hope that's of use!

  • Virtual Joy

    At home I have only my Linux box to work with.  I'm quite content with it, but I do have some DOS and older Windows apps that I like to use.  Unfortunately, by DOS floppy's are now dead (as is my floppy drive) and the MSDN DOS download is for making DOS floppies.  What I really wanted was an ISO for DOS that I could use to install on a VM.  Fortunately, I came across FreeDOS (http://www.freedos.org/)!

    FreeDOS is an open source DOS implementation.  It has been around for some time, but I was pretty excited to find it,  It comes with a huge tool set (much more than I need but includes a MASM compatibale assembler, Pascal interpreter, VIM, etc.) and would have been awesome to have in the early 90s.  If you still play DOS games it's a must.

    Just thought I'd share in the event that you aren't aware of this OS.

     

    Side Note:  If you want a floppy boot images for installing Win95/Win98/NT, These guys have them: http://www.allbootdisks.com/

  • Nested classes with JUnit

    Recently I was playing with JUnit 4.X.  I wanted to be able to define tests in nested classes as I had before with NUnit.  This was to facilitate BDD-ish test definitions, where I break up unit tests by test context.

    My first attempt failed (of course) because JUnit was unable to see the nested classes and the failed to run.  After a bit of digging, I ran across a sparsely documented feature that allowed me to do what I wanted. 

    Namely by using: @RunWith(Enclosed.class)

    Below is an example, hope it's of use:

    package junitSampleTests; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Enclosed; import junitSample.*; @RunWith(Enclosed.class) public class SimpleFractionTest { public static class WhenConstructingSimpleFractions { protected SimpleFraction f1, f2; @Before public void setUp() throws Exception { f1 = new SimpleFraction(15, 25); f2 = new SimpleFraction(-27, 6); } @Test(expected=InvalidOperationException.class) public void shouldNotAllowZeroForDenominator() throws Exception { f1 = new SimpleFraction(1, 0); } @Test public void shouldEnsureOnlyTheNumeratorHoldsTheFractionSign() throws Exception { f1 = new SimpleFraction(1, -1); assertEquals(-1, f1.getNumerator()); assertEquals(1, f1.getDenominator()); } } public static class WhenUtilizingAccessors { protected SimpleFraction f1, f2; @Before public void setUp() throws Exception { f1 = new SimpleFraction(15, 25); f2 = new SimpleFraction(-27, 6); } @Test public void shouldGetNumerator() { assertEquals(15, f1.getNumerator()); assertEquals(-27, f2.getNumerator()); } @Test public void shouldGetDenominator() { int result = f1.getDenominator(); assertTrue("getDenominator() returned " + result + " instead of 25.", result == 25); result = f2.getDenominator(); assertEquals(6, result); } } public static class WhenSimplifying { protected SimpleFraction f1, f2; @Before public void setUp() throws Exception { f1 = new SimpleFraction(15, 25); f2 = new SimpleFraction(-27, 6); } @Test public void shouldSimplify() { f1.simplify(); assertEquals(3, f1.getNumerator()); assertEquals(5, f1.getDenominator()); } @Test public void shouldSimplifyWhenNoSimplificationIsPossible() throws Exception { f1 = new SimpleFraction(15, 29); f1.simplify(); assertEquals(15, f1.getNumerator()); assertEquals(29, f1.getDenominator()); } @Test public void shouldSimplifyNegativeFractions() { f2.simplify(); assertEquals(-9, f2.getNumerator()); assertEquals(2, f2.getDenominator()); } @Test public void shouldSimplifyWhenNumeratorIsZero() throws Exception { f1 = new SimpleFraction(0, 29); f1.simplify(); assertEquals(0, f1.getNumerator()); assertEquals(29, f1.getDenominator()); } } }
  • Using SSL Websphere MQSeries and .Net

    Hi all, I've haven't posted anything since my last contract...sorry about that (what can I say, I've been busy...Ma, AgileJoe's been abusing me!).  In any case, this is as much for my benefit (and documentation for the current team) as it is for the community at large (I never remember this stuff so I want to be able to google it).  I did see some other blogs that tackle this subject...but alas...the web *** have blocked most technical blogs here.

    I was recently tasked with getting an MQSeries .net client to support mutual authentication.  I'd done the same the week before for a couple of web services (which was relatively easy) and figured this would be a snap.  In reality the only snap was the sound of my morale being crushed.  First off, let me say that we wasted about a week trying to get things working with what was later found to be a mis-configured queue manager.  We tried to get the team responsible for the queues to ensure they were set up correctly but encountered show-stoppers on two occasions before they finally got it right.  Overall, the .Net documentation is spotty and left us to figure out a number of critical steps.  If you yourself are trying to setup SSL/Mutual Authentication for a .Net client, be sure to be familiar with the basics of SSL first.  It helps quite a bit.

    Note: IBM's on-line publication library doesn't update the URL in your address bar, so if you find a useful page and want to bookmark it be sure to get the target URL from the document frame and use that to create the bookmark.

    Initial Skirmish:

    On our end, we were trying to get things set up using the XMS client for 6.0 (we switched to the XMS client later in the game).  We had already integrated the client successfully with an unsecured queue and figured that the following was all we needed to add to get up and running:

    connectionFactory.SetStringProperty(XMSC.WMQ_SSL_CIPHER_SUITE, "SSL_RSA_WITH_NULL_SHA");
    connectionFactory.SetStringProperty(
    XMSC.WMQ_SSL_KEY_REPOSITORY, @"X:\KeyStorePath\key");
    connectionFactory.SetStringProperty(
    XMSC.WMQ_SSL_PEER_NAME, @"CN=SERVER_CERT_COMMON_NAME");

    Simple enough...we used CIPHER_SUITE (instead of CIPHER_SPEC) as a good client should, set the path to the key store(created using the "IBM Key Management" tool that comes with the client, and we caught on fairly quickly to how the Peer Name is used.

    Fog of War:

    The basic client configuration was pretty simple, but we were constantly getting MQExceptions that were akin to getting null pointer exceptions.  The exceptions basically said "Something has gone terribly wrong in your application"...well I figured as much...I'm getting exceptions after all!  Logging the exceptions was useless because we were logging all Exception types.  I later found that the MQExceptions (most, not all...some have the exception reason codes in the message itself) had a couple Properties that gave a bit more information, namely "Reason Code".  Once I started capturing the reason codes I found (after a little digging) a couple of reason code lists that gave a little (often precious little) information about the codes.  One list was useful for client-specific reason codes and the other was for subscriber-specific reason codes.  Also, I found that I couldn't turn on the client trace with the .Net client library, but we did stumble over the Web Sphere client error log(C:\Program Files\IBM\WebSphere MQ\errors), which was often much more useful than any information found in the exceptions (although you still end up using the reason codes).

    Setting up the key store was fun too.  In the aforementioned link, you'll see in Figure 4 the following next to Key Label: "<client identifier of your choice>".  I'm not sure if this is due to our MQ guys, but we had no choice here.  First off, the example was for Self Signed certs, so they didn't really apply in our case.  We were using a pfx (p12) cert store with the client key, the trust cert, and the exported private key.  I soon learned that the IBM Key Manager requires that the pfx is password protected (I know, it's a good practice anyway).  Then we had to ensure that the Peer Name on the server end matched the common name found in the certificate's distinguished name.  Finally, we found that our "client identifier of choice" had to be the string "ibmwebspheremq" followed by the name of the account under which the process was running...all in lower caps...not much of a choice (e.g. ibmwebspheremqsvcacctname).  We probably never would have figured the last one without the help if the MQ team's manager.  If the Key Label isn't set up correctly we get the following:

    CWSMQ0006E: An exception was received during the call to the method ConnectionFactory.CreateConnection: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
       at IBM.WMQ.MQClient.MQCONNX(String qMgrName, MQCNO& mqcno, Int32& pHconn, Int32& pCompCode, Int32& pReason)
       at IBM.WMQ.MQClientConnector.MQCONNX(String qMgrName, MQConnectOptions& cno, Int32& hConn, Int32& compCode, Int32& reason)
       at IBM.WMQ.MQQueueManager.Connect(String queueManagerName)
       at IBM.WMQ.MQQueueManager..ctor(String queueManagerName, Hashtable properties)
       at IBM.WMQ.MQSPIQueueManager..ctor(String queueManagerName, Hashtable properties)
       at IBM.XMS.WMQ.WmqConnection.CreateQueueManager(String qmgrName, Boolean isSessionHConn)
       at IBM.XMS.WMQ.WmqConnection.CreateQueueManager(String qmgrName)
       at IBM.XMS.WMQ.WmqConnection..ctor(WmqConnectionFactory cf, String userName, String password)
       at IBM.XMS.WMQ.WmqConnectionFactory.CreateConnection(String userName, String password). During execution of the specified method an exception was thrown by another component. See the linked exception for more information.

    ...Clear as mud...we wasted some time fiddling with directory permissions, recreating the key store, etc.  To make it work with our unit tests (without requiring each developer to set up a private key store for each development workstation that they work on) I threw together an Impersonator class based on an MSDN article.  We configured the Key Label using the service account, included the dev key store in source control, and impersonated the service account in our unit test Setup and Tear Down methods.  Worked like a charm.

    Victory...almost

    Finally, we'd run across something earlier in IBM's document "WebShpere MQ Using .Net Version 6.0" that didn't quite make sense at first.  We had brushed over this until we started getting the following:

    "CWSMQ0046E: An environment error occurred when a connection to specified queue manager was attempted.  Current libraries are not supported with the specified queue manager. Ensure that appropriate libraries and appropriate versions of these libraries are in use."

    Great, I think, I must have downloaded the wrong client.  Before I do anything rash, however, I consult my handy-dandy IBM documentation.  On page 24 you see the following: "The following section does not apply to the fully-managed client."  Okay, so I search for "unmanaged client"...no matches.  Then I search for "fully-managed client"...the only match is the line above and one other place (BTW, if you are using XMS, don't confuse yourself with this document...use the XMS doc first...although the XMS documentation refers you to it for "in-depth information"...lol).  I check "managed client" with a few more IBM docs and finally find what I'm looking for in the XMS documentation ("Message Service Clients for C/C++ and .Net"):

    "XMSC_WMQ_CM_CLIENT_UNMANAGED (for .NET only) A connection to a queue manager which forces an unmanaged client stack."

    I find an answer in the on-line documentation, but am still a little unsure what this means...Are we supposed to use 'unsafe' blocks?...Do I have to rewrite this using pointers?...Do I make external DLL calls like we do with Win32API calls?...What's going on here?!?  Well, in the end it turns out the answer is "not much"  We simply had to set another connection property.  Why this isn't included in the documentation's SSL code examples is another question.  Add this call to your ConnectionFactory instance and the pain will go away:

    connectionFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED);

    Okay, now Victory!

    And that's it!  Aside from the Key Store setup and the basic queue configuration that was already complete, all we needed were the four horsemen of MQSeries: 

    1. Make sure you've specified a Cipher Suite that is compatible with the Queue's Cipher Spec
      1. connectionFactory.SetStringProperty(XMSC.WMQ_SSL_CIPHER_SUITE, "SSL_RSA_WITH_NULL_SHA");
    2. Provide a valid path to your Key Store (.kdb)  (and effective know Key Label conventions for the client cert)
      1. connectionFactory.SetStringProperty(XMSC.WMQ_SSL_KEY_REPOSITORY, @"X:\KeyStorePath\key");
      2. Also, we couldn't get the client to work with the Windows cert store...one of you can figure this out and educate me
    3. Setup the Peer Name so that the server certificate will be accepted by the client during the SSL handshake
      1. connectionFactory.SetStringProperty(XMSC.WMQ_SSL_PEER_NAME, @"CN=SERVER_CERT_COMMON_NAME");
    4. Set connection mode to unmanaged so that you can actually use SSL with the queue
      1. connectionFactory.SetIntProperty(XMSC.WMQ_CONNECTION_MODE, XMSC.WMQ_CM_CLIENT_UNMANAGED);

    There's really not much to it...I'm not sure why they didn't just tell me what to do in the documentation.  I know that this is really geared toward a small set of .Net developers, but I hope this can be of use.  This little task was pretty painful and took a couple of weeks to resolve.

    As far as why the documentation for this is so bad...I think, ultimately, that the Java guys basically just hate us.  They are in it with the Canadians (yeah you Scott).  This is all part of their twisted plan to dominate the Earth. 

    In fact, I'll be a Java developer again on my next contract...I'm already starting to hate my .Net self. 

  • Metaphors and Mathematics

    I haven't posted myself in a bit and decided to go ahead and drop a few lines...albeit a little off topic.

    I've been looking for supplemental maths books for both my children.  As you Computer Science types know, texts for elementary Maths (and more advanced maths for that matter) can be painfully dully and often confusing.  In searching for texts for my two kids, daughter 13 and son 10, I wanted to find materials that were more interesting than their usual fare.  This while addressing their wildly differing attitudes toward the subject.

    My son is the easy case, he loves math.  He goes back and forth between wanting to be a doctor (because he loves Biology, Anatomy and Chemistry) and an engineer (because he thinks Physics is pretty cool too).  At 10 he's started his journey into Pre-Algebra.  He tends to grasp topics quickly and will often knock out textbook problem sets in a few minutes and ask to use the rest of his math time to play Legend of Zelda.  I just had to find a book with a large set of challenging problems to push him a bit more and keep him happily occupied.

    My daughter, on the other hand, holds no particular love for Maths.  She wants to be a photo-journalist and has already asked about minimum math requirements for the degree she wants.  She's quite bright (reads at a college level...although she didn't quite have the patience to finish the unabridged version of Les Miserables), but Maths to her present chores to be tolerated and nothing more.  I don't expect her to be a mathematician, but I want her to have a broad enough background so that her future options are not limited (you know, the basics like Algebra, Trig, some Calc).

    Fortunately, I've found two books that I believe may suit her.  The first, titled "Math Doesn't Suck", covers elementary arithmetic mainly but uses mnemonics and metaphors to explain the rules of the game.  It's written by a female mathematician to appeal to young girls.  The book arrived yesterday and my daughter has been tearing through it like a novel.  She read the first 50 or so pages last night and concluded that she loves the book. 

    I'm reading the second book as she goes through the first so that I can tie concepts as presented in the text to her "normal" Maths text.  It's titled "Algebra Unplugged" and is written by the author of my favorite introductory electronics book titled "There Are No Electrons: Electronics for Earthlings" (and co-authored by a maths professor from Denver).  It's also presenting the subject in prose and focuses on understanding fundamental concepts in Algebra rather than presenting things to memorize and long lists of problems to solve.  My favorite quote from the book thus far is, "Reversibility is a god to whom mathematicians bring many bananas and mangos."

    With the help of these two books I'm better equipped to kindle a greater interest of Maths in my daughter.  That's because they both effectively use metaphors to maintain her interest and present topics in familiar lights.  They also attempt to speak in a language that she can relate to.  We Computer Science types, on the other hand, tend to gravitate toward the language of Mathematics (or at least something similar to it).  The language that we love to use is also the language that we utilize when talking to the Business types.

    In the early days of Agile...we had to walk 10 miles a day for the sake of our projects, barefoot, over hot coals and razors!  Just kidding.  In the early days (XP in particular), agile key practices included the use of Metaphor to better communicate technical concepts to the customer/business.  Over the years the use of Metaphor has loss emphasis and has even been ignored by many as a key practice.  Many of us have forgotten that our customers do not necessarily speak the same language we do. 

    To illustrate my point I have an mp3 click from a focus group meeting that took place during a DoD project that I was working on.  The customer asked our project manager, "Do your developers, software developers, talk to real people or do they just talk to themselves?"  Brilliant question.  On this project we failed utterly to effectively communicate with the customer.  We had generated tomes of requirements and design docs using dashing technical jargon and probably the most complete UML model that I've ever seen (Thanks Rational Rose!...ugh).  We did all the "right stuff" but left our customer confused, frustrated, and ultimately failed to discern what the system truly needed to do.  We were really good at talking to ourselves, but talking to "real people" was another matter.

    Now I have just as much difficulty in effectively communicating to customers as the next guy.  When I took the "Which Computer Language Are You?" test I got "Binary" as an answer (AgileJoe will attest to this, although I've taken it again and got C++, so maybe I'm improving).  Regardless of the difficulty, however, I think that it is crucial that we make an effort to speak using the customer's language.  Developing a ubiquitous language is a start, but is by no means sufficient in and of itself.  The use of Metaphor is also key in communicating in terms of the business itself while saving the customer from confusing technical and often abstract terminology.  It's an often ignored tool that we can use when speaking to "real people". 

    Because of the use of Metaphor (and humor for that matter), the texts that I recently bought my daughter have captured her interest in a normally dry topic and have helped her both understand and memorize key concepts.  Granted, I may prefer that she learn formal terminology, but I'm satisfied with her being able to understand and apply key concepts even if it means her using strange terminology.  For instance, she now thinks of prime numbers as primate numbers and the process of creating a factor tree as the "Hanging Monkey Method".  I'm really not concerned about the strange terminology that she's adopting, rather I'm pleased that she's understanding the concepts and spending her free hours pondering them (we'll test long term memory later).

    The bottom line is that we, as developers, must make greater efforts to speak in terms that the customer can easily understand and Metaphor is a tool that aids in just that.  If you see your customer's eyes glazing over, stop to think about what you are saying, then try presenting it in another manner that they can (hopefully)  better understand.  The customer is not stupid because he can't understand your language, he simply speaks a different language (Just as you are not stupid because you don't speak Tagalog, Esperanto, SNOBOL, or any number of other languages).  With continued practice we can become increasingly proficient at speaking to "real people".

    P.S.  I must admit, however, that I do love my little mp3 clip and am happy that I was at the wrong place at the right time to get it.  We lived up to the quote of one of my favorite demotivational posters (for Mistakes) which reads, "It could be that the purpose of your life is only to serve as a warning to others".  Happy coding!

  • Calling .Net Web Services from Oracle PL/SQL

    I'm working relatively closely these days with an Oracle DBA on this super-cool mainframe integration project (anyone want to join me?  LOL).  The old batch ops were managed through cron'd jobs in Oracle and I was asked to take a different approach.  I haven't found anything like Control-M here and was toying with the idea of rolling my own scheduling services.  The management wanted the integration pieces written in VB.Net, not PL/SQL scripts as had been done in the past, fair enough.  In a later meeting, the DBA had said that he REALLY wanted to replace the cron jobs with Oracle Jobs...hmmm...what to do, what to do...

    I'd already planned on publishing .Net web services to support calls to the actual service objects.  A scheduler would be configured to call the web services at set intervals and kick of the batch processing.  I did a little research and found the utl_http library in Oracle.  I ran a quick test if it was installed and working and was delighted.

    With utl_http it's pretty easy to call an XML web service.  It supports using PL/SQL to utilize HTTP requests and responses.  To test it I threw together a very simple web service that allows anonymous callers to write to a custom event log.  Now the DBA can automate sending me nasty grams (none received yet, I'm a little disappointed).

    Here's the PL/SQL script that calls the web service.  All you have to do is define the requesting soap envelope, set the appropriate HTTP header info, point to your target using the right protocol and fire!

    declare http_req utl_http.req; http_resp utl_http.resp; request_env varchar2(32767); response_env varchar2(32767); begin request_env:=' <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <LogMessage xmlns="http://tempuri.org/"> <message>This is my message</message> </LogMessage> </soap:Body> </soap:Envelope> '; dbms_output.put_line('Length of Request:' || length(request_env)); dbms_output.put_line ('Request: ' || request_env); http_req := utl_http.begin_request('http://wsXXXX/Test_WebService/Service.asmx', 'POST', utl_http.HTTP_VERSION_1_1); utl_http.set_header(http_req, 'Content-Type', 'text/xml; charset=utf-8'); utl_http.set_header(http_req, 'Content-Length', length(request_env)); utl_http.set_header(http_req, 'SOAPAction', '"http://tempuri.org/LogMessage"'); utl_http.write_text(http_req, request_env); dbms_output.put_line(''); http_resp := utl_http.get_response(http_req); dbms_output.put_line('Response Received'); dbms_output.put_line('--------------------------'); dbms_output.put_line ( 'Status code: ' || http_resp.status_code ); dbms_output.put_line ( 'Reason phrase: ' || http_resp.reason_phrase ); utl_http.read_text(http_resp, response_env); dbms_output.put_line('Response: '); dbms_output.put_line(response_env); utl_http.end_response(http_resp); end;

     

    See? Easy as PI!  It's practically self explanatory (and the web service itself gives the client pretty much the info they need to get wired in).  As you see, I used varchars to declare vars for the request and the response.  With larger SOAP messages you'd want to take a different approach (reading chunks into a buffer) because the varchar is so small. 

    Here's the console output showing what was sent and received (reformatted a bit for readability):

    Length of Request:324 Request: <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <LogMessage xmlns="http://tempuri.org/"> <message>This is my message</message> </LogMessage> </soap:Body> </soap:Envelope> Response Received -------------------------- Status code: 200 Reason phrase: OK Response: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <LogMessageResponse xmlns="http://tempuri.org/"> <LogMessageResult>Logged Message: [This is my message]</LogMessageResult> </LogMessageResponse> </soap:Body> </soap:Envelope>

    Now how cool is that?  This was really my first time consuming (or starting to anyway) .Net web services from a disparate technology and hadn't really spent much quality time with SOAP since around 2002.  Now kicking of the jobs through Oracle will be a breeze...after I iron out authentication, of course.

    Now, to learn how to use the 'out of the box'  XML parser that comes with Oracle...

  • Subversion Tip of the Day - Moving Files

    Many of you are beginning to use Subversion for your source control repository these days.  I am also sure that you have found SVNTortoise to be a welcome addition to Subversion and use it also.  Moving files and directories at the command line is a pretty straight-forward affair, but doing so with Tortoise is a little counterintuitive (but never-the-less simple).

    Here's the easiest way that I've found to move a file or subdirectory around using Tortoise...

     

    Before I begin, here is folder/file structure for my demonstration

    folders are named based on initial hierarchy

    image

    documents are likely named...the following are documents in folder1.1

    image

    and here are the contents of folder1

    image

     

    Moving a file/directory

    Many times (especially early in projects) we may find that we've decided that a file should be in another directory.  How to move a file was not readily apparent to me and during the error phase of my trial and error I often ended up exporting the file and adding it to the new location.  This is bad because I lost history on the file at it's new location. 

    Moving the Wrong Way:

    Here's the revision graph for doc1.1.1.txt before a bad move:

    image

    Now I'll drag and drop it to folder 2 by using cut and paste:

    image

    image

    This is the revision graph after committing the move:

    image

    As you can see the revision history is lost for this item because Subversion was unaware of the move.

    Moving the right way:

    To move files the right way using Tortoise, highlight the file(s) you want to move and drag and drop to the new location using the RIGHT mouse button.

    Revision graph of doc1.1.3.txt before the move:

    image

    When you drop the file(s) to the new location you will be presented with a popup menu

    image

    Here you can select a SVN move, copy, or export.  I selected 'SVN Move versioned files here'.

    image

    image

    Notice the 'Add' symbol for the moved file.

    The revision graph for the file hasn't changed (because the contents are unaltered)

    image

    The revision graph for the parent folder shows the file move

    image

    So, as you can see, the history is preserved.  The same process is used to move multiple files and directories to new locations.  You can also use 'Rename' to move individual files, but I'm not too fond of the approach (it's also not good if done at the folder level).  I do wish that there was a hotkey for this functionality or at least inclusion of the move command into the normal SVN menu.  The TortoiseSVN documentation does cover the subject briefly but without the pretty pictures.  I sort of wish that I had RTFM before screwing up the history on some of my repositories, but where's the fun in that? 

  • VB.Net oddity of the day - Array Declarations

    I ran into a recent issue today relating to array declarations in VB.Net.  I looked up array declarations on the net and found something that gave me hope, but alas...

    "Arrays

    In VB6 declaring an array

       Dim Items(5) As String
    

    gives you 6 items from index 0 to index 5. In VB.NET, this same declaration will yield 5 elements from index 0 through index 4. Be on the look out for "out of bounds" type errors. Also, all arrays in .NET must now be zero-based."

    My personal favorite is the last line.  He says it like it's a bad thing!  In any case, I didn't include the source of the quote because the guys is wrong (or at least no longer correct for all versions after .Net beta1, I believe).  True, all arrays in VB.Net must now be zero-based, and thank the computing gods for that, but for the sake of backwards compatibility VB.Net still supports the good(bad)-ole-way of declaring arrays. 

    Coming from C# (and C and Java...) this was a bit of a surprise.  In particular because I have the habit of declaring know max sizes for arrays as constants.  If I was only a VB programmer this wouldn't be so annoying, but it is.  Also, handy little tools like Convert.Net don't handle this case at all, so if you used it to convert C# snippets to VB you would be serving yourself up an instant logical error.  Those Visual Fred guys, always trying to be different...

    So, "Dim Items(UpperBound) as String" is just the way things work, I'll just have to accept it.  Thank God for TDD, that's all I have to say.  If I wrote a bunch of code on my previous assumptions without coverage I would have hated life ferreting out all the places that I would have made the same mistake.  I know that this is the way VB6 worked, but again, I thought that VB6 was pretty stupid when it came to arrays.

     

    It's kind of sad though, why can't the VB folks conform a little to the masses?

    I.E. Here are some other languages declaring arrays containing 10 elements (My COBOL is way weak, so excuse any syntax errors):

    C/C++: &nbs