Skip to main content

Posts

Showing posts with the label code

Enable alwaysUseFullPath with mvc:annotation-driven

mvc:annotation-driven doesnot have alwaysUseFullPath attribute. However, in instances this is needed, adding the following bean BEFORE mvc:annnotation-driven tag will set up the alwaysUseFullPath correctly <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping">         <property name="alwaysUseFullPath" value="true"> </property></bean> So the fuller xml should like this <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping">         <property name="alwaysUseFullPath" value="true">     </property></bean>  <mvc:annotation-driven>   <mvc:message-converters>    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">     <property name="objectMapper" ref="jacks...

Groovy Camel Processors

Fuse ESB 7.0 comes with Camel 2.9 components. Camel has groovy support from 2. 8 9 to create predicates and expressions. There is a GroovyRouteBuilder in 2.9 that lets you build dsl in groovy, albeit with java api. This is especially un-groovy when you have to write a .process .

Developing Userscripts for Chrome (caveats)

To develop Chrome extensions, crx is the best way. But the user scripts that are developed in general for Greasemonkey can also be delivered for Chrome, if a few easy rules are followed. 1. @required and @resource don't work. By default, atleast for now, These two Userscript metatags donot work on Chrome. If you need to load a js file, instead of using @required - try to use document.createElement. Similarly with @resource. 2. Some GreaseMonkey helper methods don't work or are restricted. Methods with GM_ prefix from userscript api may not work. Particularly, unsafeWindow, GM_registerMenuCommand, GM_setValue, or GM_getValue are not supported. GM_xmlhttpRequest will work but not only on the same domain. 3. @include s are not shown while installing. This may not be a deal breaker for developers, but sure is for users. The patterns used in @include is not shown while installing. Instead a generic message is shown as below. This message may scare the users away. ...

Reduce Yahoo mail neo header space for more room

Update: Yahoo mail with new layout renders this obsolete. The newer Yahoo mail beta (neo) really is a faster UI. However, The large header with room that is unused is a let down.  This one time, it appears that, Yahoo did not take into account the importance of real estate on top of the page, hitting User Experience. Reduce Yahoo mail neo header space for more room by installing SarathOnline's " Spacier Yahoo Neo " userscript below Install YNeo UserScript by SarathOnline Before: After: The user script is fairly simple. You could install it in Firefox as a Greasemonkey script. Or install it on chrome directly. If you are new to UserScripts, follow these instructions to install them on your browser. Be assured, it has no malicious code. All it does is install a correcting css to the document. The css file is located here .  And the script itself. (function(){ var d=document,sid='__sar__yneo', fn='http://www.sarathonline.com/dyn/sfs/userscripts/y...

Writing your first UserScript, Template as a beginner's tutorial

UserScripts are fun, useful and often productive . A lot of sites have small improvements you think could add a lot of value. And just because you are not the author of the site, you need not give up. With some basic javascript skills, you could write a userscript, install it on your browser, and make the site behave.

Import Mysql data (or a CSV file) into MongoDB

You have MySQL database. And want to move either all, or some of the data into MongoDB. There is no direct way to do it, as such, It can be done in two steps. (If you are here because you have a csv file instead (or directly) move on to Step 2.) Step 1. Export all of your MySQL data as a CSV file. select columns INTO OUTFILE ' /path/to/csv ' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' from table [ where clause ] Step 2. Import the CSV into MongoDB. Assuming you have MongoDB running (on local, defaults) run the following command mongoimport -d dbname -c collname -type csv -f fields-sep-by-coma --drop /path/to/csv if your csv file has a header row add --headerline . p.s.: for more options on mongoimport look here More tips on mongodb, coming

MySQL > java.sql.SQLException: Error writing file '/tmp/MY1kynpA' (Errcode: 28)

Recently, a heavy burst of traffic into our production webservice caused this error. As with all Mysql errors, this shows little information. Like any sane person would think, we checked if the filesystem was full. We found the /tmp has 98% free space. Permissions are all fine. No locks on table. Increasing /tmp partition - NoGo. Just crazy error message. However, the issue was beating us time and again. It took us a couple of days and we realized the cause was a select query. The query was fetching from a table of about 2 million records. Although it was fetching a limited subset, the column in the order by had no index. This was causing mysql to load records into pagefile in tmp for sorting, apparently. Created the index on the sort column seemed to have solved the problem. We also augmented it with the following options in my.cfg sort_buffer_size=2M table_cache=1800 thread_cache_size=384 tmp_table_size=64M

java turorials: Learn everything Java basic to enterprise

Java Programming basics Introduction to Java programming   ServerSide Java Download Tomcat Demo - Servlets and JSP JSP Tutorial IBM JSP tutorial   Enterprise Java JEE 5 tutorial Develop Web services with RAD Part1 Part2 Crash course in Enterprise JavaBeans 3   Struts IBM Tutorial Apache Wiki (List of Tutorials)   Spring Viral Patel’s turotial Reference Documentation   Ibatis JavaLobby Introduction Apache Tutorial   Hibernate JBoss Tutorial    

Maven Only: type parameters of X cannot be determined

This is a rare error that compiling with Eclipse does not catch, but maven compile goal will. In the case in question, There is a Generic return type as follows, as discussed in java forums private <A> A getValue(){ return getOtherValue(); //<-- Error occurs here } It compiles out an error: type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds int,java.lang.Object The intriguing question is, why does this happen only in maven? Why does it not happen in Eclipse? Few in the java forum have suggested that this is a bug in the compiler. However, an impatient programmer looking for a solution around it, Just cant get it. There is an unanswered stack overflow post here . The java forum is also confusing for a novice. The simple answer here is, the getValue() method (or the getOtherValue, or the method it calls) is, at some place, not really Strongly typed and the compiler is left to guess wildly about...

Hot Deploy resource file in Spring.

The general way to read a Resource file in Spring is something like this new ClassPathResource(pathToFile).getInputStream() However, this will cache the contents of the file (stream) in the class loader. Although this is not a major problem, there may be cases where reading the file fresh everytime (Hot deployable file) is required. That can be achieved with the following code new FileInputStream(new ClassPathResource(templateFile) .getFile().getAbsoluteFile()); This will gives cache free control on the file and its content. (Important) Note : * Doing the AbsoluteFile way, will always read the content of the file. So if it is being done on a High frequency, A Managed Caching layer must be considered * AbsoluteFile call will NOT work for resource files inside jars, they have to be in classpath folders (e.g.WEB-INF/classes)

Google Wave: How to create a Live Page on your website using a Public Wave

Yesterday, I created a public live readonly wave and posted it on my blog. Here is how you get it. You need a Wave account. You log into your wave inbox. and start a wave.   Add participant "Public". Note that "Public" is not in your address book. But shows up when start typing public. Then make the "Public" participant's Access to this wave readonly. Then get the code to insert from the *Link to wave* menu. Copy paste it on your website and You get your self a live reporter.

Update multiple Dynamic DNS servers using ddclient

Running Linux, ddclient is highly suggested software for updating DynamicDNS servers. It supports updating a variety of servers. Some of them are dyndns.com opendns.com and no-ip.com. ddclient supplies documentation to update multiple hostnames (on the same server) but does not have an example with multiple servers. Here is an example that covers all those combinations. ## ddclient configuration file daemon=600 # check every 600 seconds syslog=yes # log update msgs to syslog mail-failure=your@emailaddress.com # Mail failed updates to user # requires sendmail installed. pid=/var/run/ddclient.pid # record PID in file. ssl=yes #use ssl ## Detect IP with our CheckIP server use=web, web=checkip.dyndns.com/, web-skip='IP Address' protocol=dyndns2, login=user1, password=YOURPASSWORD \ some.selfip.org protocol=dyndns2, login=user2, password=ANOTHERPASS\ someother.selfip.org protocol=dyndns2, server=updates.opendns.com, login=user3, password=psswd3 \ home

Create your own Search Engine Provider for FF and IE7+

Search Engine Providers , you know the ones you see on the browser - next to the address bar.These have become more and more popular with new age browsers getting open to user search. Its easy to create your own Provider. A real application for this would be like, say, your own site for search. Or putting all your favorite forums in the search bar. This example uses a Google custom search engine(CSE).   A Google CSE (create your own here ), can be used to include exclude content and show results accordingly. Like all other Google products, a lot of customizations can be done on this. Most of the sites that use Google Site Search can be considered a candidate CSE. Once you have your own CSE, To create a search provider, You will need a place to host one xml file and one HTML file. The xml file is a OpenSearch descriptor. Its like an installation file for your browser. <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"> <ShortName>SarathOnli...

Error handling and Production

When you are developing a customer front web application, you should take every measure to hide technical details from the user. This is especially true for JEE applications. Not only because the user is pissed off looking at a huge stack trace but also it reveals a lot. Look at the following figure This famous shopping site has been developed to show stack trace on the web page. The developer cleverly hid the trace when the page loads up. But leaves a link that uses DHTML to show the hidden div. That kinda defeats the whole purpose. If the user sees this link, he is less likely NOT to click it. If you really want to have a handle on this. Print out a unique token (a timestamp, md5 hash or something - get creative here), and correspondingly log it in your appserver along with stack trace. Have the user (customer or your pain-in-the-neck-QA guy) email you that token. Now you can grep that log for it. Now, you know, I have less confidence in that site this day forward.

javascript maxlength for textarea with \r\n breaks in java (esp Firefox)

Textareas allow new lines to enter. These are represented by \n (1) or \r\n (2) characters. But when you save to DB you have a limit to certain length of chars. There is no maxlength attribute in HTML that will stop you from entering data. This is generally acomplished by Javascript. You do a onkeyup hook and stop event or trim after textarea.value.length > maxlength. There are many other solutions out there.. But.. Here is the problem that most of those solutions overlook, How do you deal with the count on \n and \r\n representations. Lets first see how it matters. If the text entered has new lines, the length is calculated differently in Firefox and IE. When you enter a Text like 01234 567890 You expect the textarea.value.length to be 11. (10 chars + new line).On the backend, however, java would recieve it as 12 chars (10 chars + \r\n) (this is irrespective of FF or IE). So you are effectively saving 12 chars to DB. Worse yet, IE seems to figure textarea.value.length as 12 (...

iBatis SQL with dynamic like operator

In iBatis, a parameter is escaped, autoquoted and replaced automatically. So a #stringParam# will be auto autoquoted and then replaced. Its also escaped, meaning symbols like ' and % are escaped. This causes a problem when you have to do wildcard searches. With a like operator. Say, You have to look up employees by first name. The SQL would look like select * from emp where first_name = 'sarath' and the iBatis Query would be simply <select id="getEmpByFName"> select * from emp where first_name = #value# </select> On the same lines, A wildcard search of firstname would be (in SQL) select * from emp where first_name like '%sar%' But, if you just make an iBatis select like: <select id="getEmpByFName"> select * from emp where first_name like #value# </select> and make it concatinate with "%" before passing it to queryForList(), You will see nothing in results. This is because iBatis will escape % too. ...

Ubuntu Broken Upgrade, Lost GUI, /dev/null: Permission denied

I am toying with Karmic's alpha releases. So, in comes a new partial upgrade rolled out and I chose to get it. At the end of the upgrade, I had a rare - stuck linux - situation. So I hard rebooted; Only to find that the system now restarts to a GDM that is broken. It gets to a blank screen with a *loading* mouse icon. I was contemplating a complete redo(download the daily build for karmic and install it). However, I thought its worth troubleshooting. So on that blank GDM screen, I pressed Ctrl+Alt+F2 to open a tty. When I logged in, I got a lot of /dev/null: Permission denied Apparently, My upgrade dint finish well. First thing that came to my mind is to reconfigure the whole setup. sudo dpkg --configure -a But I was getting the same errors /dev/null: Permission denied . Pretty much everything I did got me to the same error. I rebooted to my Jaunty install. A quick search showed me how I could install this all important /dev/null . All you have to do was to remove and re...

Write iBatis dynamic query that returns results only when atleast one parameter is sent

iBatis allows building dynamic query conditionally based on properties sent to the named SQL. Suppose We have a Person implementation where the search page allows search by id or by email id (assuming both are unique). We could write two queries <select id="getPersonById" resultClass="Person" > select * from PERSON where ID = #value# </select> <select id="getPersonByManagerId" resultClass="Person" > select * from PERSON where EMAIL_ID = #value# </select> OR we could make a dynamic Query like the following <select id="getPerson" resultClass="Person" parameterClass="java.util.map"> select * from PERSON <dynamic prepend="where" > <isNotNull parameter="id"> ID = #id# </isNotNull> <isNotNull parameter="managerId"> EMAIL_ID = #emailId# </isNotNull> </dynamic> </select> However there is one pitfall. If the paramete...