Tuesday, March 20, 2012

jquery onclick handler

Well today I was working on what I thought would be a simple problem to solve and perhaps for the javascript/jquery gurus out there it is. However, it took sometime to get this simple task to work across browsers so I thought I would sum up my findings.

The Problem


We have a legacy web application with several input submit buttons that are used to launch download report actions. Some of these buttons will just submit their parent form action and others will have inline onclick handlers defined.


  • Example of submitting form:
    <input type="submit" name="DownloadRemit" value="Download Remit">
  • Example of inline onclick handler:
    <input type="submit" onclick="document.myForm.action='/sample1.php'; document.myForm.submit(); return false;" name="DownloadSummary" value="Download Summary">


Now we needed to intercept the onclick event and first prompt the user to acknowledge the download before sending down the report.

The Solution


The basic idea was to just use a jquery selector to select each of the Download buttons, and intercept any current onclick handler and inject the confirm prompt to acknowledge the download and allow user to cancel. Actually, the cancel was the hard part, I was easily able to intercept and prompt but cancel wouldn't work. Even with the common answer to "return false;" in the click handler.

Finally, the following script was our solution, using a dynamic closure.

<script>
function postConfirmation(){
var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ';
return confirm(text);
}
$(document).ready(function() {
/*
at startup we find all the download buttons and change their
onClick event to point to our new function postConfirmation;
*/
$('input[name^="Download"]').each( function(idx,obj){
var objOnClick = $(this).attr('onclick');
var callbackFn = function () {
if (postConfirmation()){
if(objOnClick) {
objOnClick();
}
}else{
return false;
}
};
$(this).click(callbackFn );
obj.setAttribute('onclick',null);
});
});
</script>

Tuesday, August 23, 2011

Missing Author field in Eclipse Team view

Okay so a few days ago I was installing some plugins for my SpringSource Tool Suite IDE and somehow corrupted the Team view such that it no longer displayed the Author field. After some Googling I was not able to find a solution so started digging into the plugins. Well the solution is pretty simple:

locate the dialog_settings.xml file in the {workspace}/.metadata/.plugins/org.eclipse.team.cvs.ui directory

open with favorite xml/text editor and look for this section, my issue had COL_AUTHOR had a value of 0, I changed to 135 and restarted Eclipse. All is well.

<section name="org.eclipse.team.internal.ccvs.ui.CVSHistoryTableProvider" >
<item value="188" key="COL_DATE"/>
<item value="277" key="COL_COMMENT"/>
<item value="136" key="COL_TAGS"/>
<item value="128" key="SORT_COL_DIRECTION"/>
<item value="COL_TAGS" key="SORT_COL_NAME"/>
<item value="160" key="COL_REVISIONID"/>
<item value="135" key="COL_AUTHOR"/>
</section>




Hope this was helpful.

Saturday, February 26, 2011

Eclipse Compare Files

Ever wonder how to compare two similar files in Eclipse? Well it's actually pretty simple once we look for it.

In the navigator view select both files using ctrl+click in the context menu (right click) choose Compare With>> Each Other.

That's it you get the standard diff window with side by side compare and edit capability. Oh and if one file is already open, no worries edits are all sychronized.


Hope this is helpful, gotta love Eclipse.

Tuesday, January 18, 2011

jMeter Beanshell example

Okay so today I wanted to extract a value from an html form select dropdown to use later in my test samples. The select dropdown has several options and each test iteration can yield a different one. So I needed to extract the value that is already selected when the page returns. I ran across several blogs and forum entries and one hinted that with some Java experience you can you the Beanshell post-processor. Never using Beanshell before I read up and yielded this simple script to do what I needed:


import java.util.regex.Pattern;
import java.util.regex.Matcher;
String stringData = new String(data);
Pattern p = Pattern.compile("<select name=\"creditCardAccount.creditCardType\">(.*?)</select>", Pattern.DOTALL);
Matcher m = p.matcher(stringData);
if (m.find())
{
String options = m.group(1);
p = Pattern.compile("<option.*value=\"(.*?)\".*selected=\"selected\">");
m = p.matcher(options);
if (m.find()){
vars.put("CCTYPE",m.group(1));
}else{
vars.put("CCTYPE","NOT FOUND");
log.debug("options pattern = " + p.toString());
}
}else{
vars.put("CCTYPE_SELECT","bsh pattern match failed");
}

Wednesday, April 21, 2010

Java DNS Cache

Well today has been fun, we have an application that calls out to a third party service via xml over https calls. The third party had scheduled some maintenance and notified us that their DNS entries would be updated so traffic would be redirected to their DR site. Well our webapp running under tomcat in the mid tier that makes the outbound requests never picked up the new DNS entries until we bounced the application. At first I investigated our connection pool Apache's MultiThreadedHttpConnectionManager but that was working fine and even if I emptied the pool the new DNS would not change. Well after some googling I came across this article that proved very helpful. http://www.rgagnon.com/javadetails/java-0445.html it turns out the JVM caches DNS entries and by default keeps them cached forever or at least until the application is restarted.

Solution: We needed to change the java.security file as described in the article. We choose to change networkaddress.cache.ttl=60 and add an external way to block our connection pool for 60 seconds during these regularly scheduled redirects rather than not cache any entries ever which is the case if you set
networkaddress.cache.ttl=0 .

Note: I was not able to change the value dynamically under JDK 1.5 as was suggested in the reference above. I could set the value, verify it was set but it would have no effect.

Testing:

To test this I used various levels of testing first and most basic was looking at how the InetAddress.getName(String name); would would with various settings in the java.security file. Basically, I edited the windows host file located in c:\windows\system32\drivers\etc and changed the ip address of one of our test servers.



import java.net.InetAddress;

public class Foo {
public static void main(String[] args) throws Exception{
System.out.println(sun.net.InetAddressCachePolicy.get());
System.out.println("testDNSLoop ..: " +
java.security.Security.getProperty("networkaddress.cache.ttl"));
String name = "dopey";
InetAddress localAddr = InetAddress.getByName(name);
System.out.println("reset timeout cache value to .: " +
java.security.Security.getProperty("networkaddress.cache.ttl"));
System.out.println("start loop...");
while(true){
localAddr = InetAddress.getByName(name);
String host = localAddr.getHostAddress();
System.out.println("IP: " + host + " ... sleeping 5 secs.");
Thread.sleep(5000);
}
}
}



The next level of testing was confirming that the connection pool actually
followed the same behavior. For this I just put a simple test.html file on 2 of our test servers and ran a class similar to the above watching the output.

Hope this helps someone.

Wednesday, December 16, 2009

Installing Eclipse Plugin Error

Okay so today I was trying to play with installing the CheckStyle plugin for my Eclipse 3.4 environment. This is a relatively painless process normally however I kept getting:
"An error occurred while collecting items to be installed No repository found containing: org.apache.oro/osgi.bundle/2.0.8.v200903061218"

I couldn't see any dependencies or where this was getting pulled in. Finally, I went to the Available Software tab and checked Manage Sites, here I found that even though I was only requesting to install from http://eclipse-cs.sf.net/update/ there were a number of other sites checked. I removed all checks except the checkstyle one and the install worked great.

If your not using checkStyle it's a great tool for forcing coding standards check it out.

Wednesday, November 11, 2009

Upgrading Dell E1505 hard disk

Well this was more than a simple task with many lessons painfully learned. Hopefully this will help someone out there to not have to learn them also. So the goal was pretty simple I had a 60Gb hard drive pretty full and wanted to upgrade to a larger one. The Dell was three years old and I now know was running Media Center 2. There are plenty of blogs and sites out there that now reference the issue with just cloning the hard drive, unfortunately I didn't see them till I had truncated my 320 Gb drive to 60Gb. If you have done this already the tool to fix it is here just make sure you are running it on a 32bit processor and yes you will need to plug it into a desktop since it can't work over usb. (At least at this writing).

Okay so here is how I finally was successful, may be other ways but this is what I did.
Assuming you purchased your new drive, you have an external drive casing so you can connect your disk via usb and that you have the Dell media CD to reinstall the OS. If this is not the case go out to Dell.com I believe you can order them.

1. Pull out old drive and put in new one, this is just a few screws, but Dell.com support can help you if you are not familiar.
2. Put in CD and boot to install OS.
3. Partition new drive:
I made two partitions, c: = partition 250Gb d:= 65GB. format ntfs quick
4. Install OS.
5. Once installed I didn't bother with the drivers, just the base OS.
I put the old drive in the external casing and connected via usb. Fire up cloning software. (Acronis Home Image, Clonezilla, Partition Magic, etc; I was using seagate drives so I used disk wizard from seagate which is a limited version of Acronis Home Image)
Create a partition image of the old c:\ drive and place it in the new d:\drive space. ( Here we are assuming that your old drive partition image will fit in the d:\drive. If not you would probably need a third drive to do it this way. Also note your old drive will have several detected partitions, the largest was your old c:\drive it will have a different drive label now obviously)
6. Using your cloning software Restore from image the newly created partition image to your new hard drive c:\ partition.
7. Reboot and done.

Other notes: if you have space to defragement the drive before hand it is recommended on some blogs.

Hope this helps.