Category: Java


I am a big fan of regular expressions.  They are so mysterious and elegant, I love them and hate them, cherish them and despise them.  In other words they are both beautiful and nasty, but they can really get the job done.  I was intrigued today when a colleague of mine was asking how to match a string but if it started with a specific sequence then do not match.  In my regex coolness I said “Sure, that’s easy man!”, whipped out QuickREx in Eclipse, wrote down a quick jolt of brilliance and BAMM!! I hit the brick wall at 90! Nothing I was trying would work, characters where matched not the word:

   1: ^([^search].*)dev\.domain

Basically match any incoming domain that ends with dev.domain but it should not be preceded by the word search.  I really thought I nailed it with that regex, but NOOO, it matches character classes not full words.  So I had to revert my humble ego and go back to the books and voila: Positive and Negative LookAhead!

Granted, some regex engines do not support look behinds, but thankfully java does.  Here is a cool definition:

(?!regex)

Zero-width negative lookahead. Identical to positive lookahead, except that the overall match will only succeed if the regex inside the lookahead fails to match.

And finally and AHA!! moment.  I can use the lookahead:

   1: ^(?!search|training).*dev\.domain

AND BUYAAA!!!  I think this can help somebody out there. It helped me!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

This is more of a reminder than anything else.  A co-worker needed to figure out if one array was contained as a subset of another and of course we go back to our good ‘ol friend java for this and the collections interface:


isSubset = arrayA.containsAll( arrayB )

Simple and easy!  Here are the Java API Docs just in case you want to read some more!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

Why Model with UML?

As you all know by know, ColdFusion is an excellent OO platform for web development and as our systems grew in complexity and maintainability we need to plan REALLY WELL!! The best way we can is UML, the Unified Modeling Language. The purpose of the Unified Modeling Language is to provide a language-independent and platform independent modeling notation. UML tools are as versatile as the UML is foundational.

I am so used to modeling in UML now that it is a REQUIREMENT for any project, no matter how small or how big. In that sense, some of you may be beginning to see the benefits of object modeling and may wonder how in the world to start. Well, I found a great article in the NetBeans website that touches on why to use UML and provides some great samples. Although it is directed to the Java developer, I believe a ColdFusion developer can gain some benefit from it.

Why Model with UML?

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

The following is a very useful JAVA tip for ColdFusion developers. Ever wanted to log a structure or array easily into a log file or use it as a string without actually looping and parsing to a string variable? Well, you always had the method right there lurking in the misterious but powerful Java world. The all ecompassing toString() method.

Actually all java.lang.Object inerit a toString() method as you can read in the java docs.

Below is the actual info:

toString

public String toString()

Returns a string representation of the object. In general, the toString method returns a string that “textually represents” this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + ‘@’ + Integer.toHexString(hashCode())

Returns:
a string representation of the object.

As you can see, this method can be very useful. So how do I use it in Coldfusion? Very easily, jeje. look below:


//Create a structure
myStruct = structNew();
myStruct.today = now();
myStruct.name = "luis majano";
myStruct.url = "www.luismajano.com";

//Convert to string
#myStruct.toString()#

And there you go ladies and gentleman!! Enjoy this quick tip!!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

As you know, ColdBox is now fully supported for BlueDragon and Railo. However, to port it was a bit tedious when it came to the auto-registration engine for handlers, since I need to traverse the handlers directory recursively and create an array of handlers. This is so easy in Adobe ColdFusion, but BlueDragon and Railo, does not support it (BD 6, railo 1). Here is the Adobe ColdFusion code:



That is awesome!! So easy!! Well, to do it the Java Way, takes some patience and well, some insight into recursion. The following example is using the CF/JAVA hybrid notation, but the end result is the same as above. And with some speed tests, the code actually compares, not that much of a slow down. So :)

Here is my call to the recursion, the entry point:




What I do is call my recurseListing method with the handlerArray I want the results in, the handlersPath into the initial directory and the working directory. Why do I send in the HandlerPath again? Well, I do it, to actually clean the path to show me the invocation paths, mumbo jumbo!! Give me the code!! Here it is:

Remember that this code is not to get the query like cfdirectory, it could, but you will have to do that, I am just showing how I solved my problem. This is a great exercise also.






//Init the oDirectory object with the initial working directory (Java File Object)

var oDirectory = CreateObject("java","java.io.File").init(arguments.Directory);
//Get an array listing of the objects in the directory

var Files = oDirectory.list();
//My Index.

var i = 1;
//My temp File Object

var tempfile = "";
//My clean handler string.

var cleanHandler = "";

//Loop Through listing if any files found.

for ( i=1; i lte arrayLen(Files); i=i+1 ){
//get first File reference as a File Object

tempFile = CreateObject("java","java.io.File").init(oDirectory,Files[i]);
//Directory Check for recursion, if directory, then recurse.

if ( tempFile.isDirectory() ){
//recurse, directory found, send in the new directory path.

arguments.fileArray = recurseListing(arguments.fileArray,tempFile.getPath(), arguments.HandlersPath);
}
else{
//File found
//Filter only cfc's, I could have used a Filter Java object, but was lazy.

if ( listlast(tempFile.getName(),".") neq "cfc" )
continue;
//Clean entry by using Handler Path (For my specific purposes)

cleanHandler = replacenocase(tempFile.getAbsolutePath(),arguments.handlersPath,"","all");
//Clean OS separators accordingly

if ( getSetting("OSFileSeparator",1) eq "/")
cleanHandler = removeChars(replacenocase(cleanHandler,"/",".","all"),1,1);
else
cleanHandler = removeChars(replacenocase(cleanHandler,"\",".","all"),1,1);
//Clean Extension right off baby

cleanHandler = getPlugin("fileUtilities").ripExtension(cleanhandler);
//Add data to array, a cleaned Handler, thank you GOD, bless you.

ArrayAppend(arguments.fileArray,cleanHandler);
}
}
//else return the empty array or filled array.

return arguments.fileArray;


There you go, you can follow the comments to tell you what I did. This is an awesome exercise and shows you how ColdFusion is really powerful by leveraging its Java backbone. When things like this just work, it just amazes me of how ColdFusion has evolved and how EASY it is to overcome problems by leveraging Java. Viva Java Mis Amigos!! Viva!! Viva!!

Hope you enjoy this little java/cf/recursion tutorial. This example shows potential of how you can build your own recursion methods.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

Java 6 is out and its open source!!

If you have not heard this, SUN just launched Java SE 6 and made it open source!!!

WOW!!!

Open java Feature

Java SE 6

So please check it out.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

Argo UML a great UML documentation Tool

If you like documentation like me!! Well, I guess I am kinda in the dark here since not many people like documentation. This is the tool for you. I am now using it more and more. It is called ArgoUML and you can find it here:

http://argouml.tigris.org/

It can be used either through Java Web Start or a full package download. Give it a try and please DOCUMENT!!!

If you want people to use your software to build software, then document. Make it easy!!

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

I need help trying to make netbeans 5.0 connect to my CVS repository via SSH. I have created my ssh keygen pairs on my laptop and on the server, so when I go into the terminal and type:

ssh -l luis www.domain.com

I get logged in directly without any challenge. My key pairs work. I then go to Netbeans to the CVS module and click checkout.

I fill out the EXT part of the repository and for ssh connection I choose external shell and type:

ssh -l luis www.domain.com

Then click on next and it says “Checking network connection….”

It stays there forever…..

I am not behind a firewall or proxy. So I have no clue whatsoever, what wuold be wrong. Under Eclipse, cvs just connects fine!! So its not my connection.

Please help!!! Netbeans is my IDE of choice for JAVA development, but I need to start verisoning using CVS and I cannot make this work. Thanks

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

Finally Oracle came up with their own graphical tool for creating and browsing database objects. This is the perfect companion to Oracle XE. The look and feel is great and the responsiveness of the application is amazing. It has all the tools that I need as an Oracle developer to finally develop on any platform. Yep, totally JAVA!!! This is amazing!! The tool has SQL worksheets, SQL formatting, PL/SQL snippets, a great help system, a great PL/SQL editor, which will keep beign improved on. Overall, the application is great, and best of all, I can finally run this on my Mac and get rid of my DB PC. Ohh, and did I mention that it is also free!! Thank you Oracle!!

Oracle SQL Developer is a new, free graphical tool that enhances productivity and simplifies database development tasks. With SQL Developer, you can browse database objects, run SQL statements and SQL scripts, and edit and debug PL/SQL statements. You can also run any number of provided reports, as well as create and save your own.
SQL Developer can connect to any Oracle Database version 9.2.0.1 and later. We support SQL Developer running on Windows, Linux and Mac OSX.

Link:
http://www.oracle.com/technology/products/database/sql_developer/index.html

Anyways, here are some good screenshots for you:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter

Coldfusion has a great function called “gettickcount()” which basically gives you the current time in milliseconds. This is a great function to time code. Look at the example below:

<cfset stime = getTickCount()>
<!— All my code to time here —>
<cfloop from=“1″ to=“100″ index=“i”>
<cfoutput>testing #i#</cfoutput>
<!— Make a call here —>
</cfloop>
<cfset totalTime = getTickCount() – stime>
<cfoutput>The total time of execution for the code was: #totalTime#</cfoutput>

This utility is great. The question is, how do I do this in Java?? Well very simple, we use the System class. The following is a simple snippet of how to time in java.

double stime = System.currentTimeMillis();
//My Code Here


for(int i=0;i< 1000; i++){
System.out.println(i);
}
double TotalTime = System.currentTimeMillis() – stime;
//Display Total Time

System.out.println(“Total Time of Execution: “ + TotalTime + “ms.”);

There you go!! Now you can time your java code.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • DZone
  • LinkedIn
  • Posterous
  • Slashdot
  • Tumblr
  • Twitter
Powered by WordPress. Theme: Motion by 85ideas.