Skip to main content

Posts

Showing posts with the label java

Spring MVC: mvn sonar fails when mvn test passes

Stranger things have happened. But this blows many a mind. mvn test on a restful services project with spring mvc succeeds but mvn sonar:sonar fails with 404s on controllers. This happens when the controllers are marked @transactional for the restful service to be exposed as a unit of work. Don't fret. Its easy to solve this.

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...

Eclipse Indigo without mylyn

Eclipse's new release Indigo (3.7) comes pre installed with Mylyn. And unlike previous installations Mylyn is tightly integrated with Eclipse platform package. The "installed features" does not show it as a separate feature to uninstall. It appears as if it cannot be uninstalled at all.

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...

Modularity and Abstraction

This is probably my favorite interview question. When I ask Modularity and Abstraction in practice, many a time, I get a mixed, confused or confusing answer. The reason is, there is a very thin line in what we perceive Modularity and Abstraction are. We all do it everyday, but knowing it for real.. From MIT's reading material. Modularity is the idea of building components that can be re-used; and abstraction is the idea that after constructing a module (be it software or circuits or gears), most of the details of the module construction can be ignored and a simpler description used for module interaction (the module computes the square root, or doubles the voltage, or changes the direction of motion). And that proabably is as clear as it can get.

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 App Engine / Java / cold Starup benchmarks (issues).

This is connected to the post in Google App Engine Group .  Although Google Appengine gives out a java container, I had to roll back to the python code I used for version SarathOnline 3.1 – for a sole reason – perfromance. My (blog) site has a very low traffic (compared to what google thinks a regular traffic application). Averaging ~100 visits/day. All the css and js is dynamically generated. My current python code is doing pretty well servicing these requests @ a maximum of 2.something secs

Sun Tech Days – My Day 2

Day 2 missed James. But the tech series remained just as educating as Day1's. I dug deep into the JEE6 foundation laid by Yesterday's  sessions. Today: I Jugged in knoweldge from Java Persistence API 2 Overview of Servlet 3.0 ZFS (a Whole lot on ) Context and Dependency Injection in JavaEE 6 Java EE 6 Tools Show: NetBeans, Eclipse, IntelliJ I have to bold all of them, but the ZFS really instigated me to push OpenSolaris into my dev space. Look forward to some OpenSolaris entries coming.

Been There (Done That)

Finally, I been to Sun Techdays – here in Hyderabad. Now I can say.. "Been there".. done that!.. But it was an amazing experience, with more than 1500 people in HICC, Novatel, Hyderabad. Best of all, I was able to see James Gosling. Live. In Person. They were very exhaustively educating 9 hrs. Although I was keen on Enterprise Track, I was able to sneak some time in JavaFX and Solaris Technologies too. I stuck myself in: Java EE 6 & GlassFish v3: Paving the path for the future. Designing and Implementing Secure RESTful Web Services. Hands On Lab: Build End-to-End RIA Applications Using JavaFX, Dojo, REST, JPA, and MySQL. Glassfish and JEE 6 Features (Stall at the exhibition). OpenSolaris Installation desk (Okay, I got OpenSolaris on  my vbox). Enterprise Java Persistence with Oracle TopLink. OSGi & Java EE in GlassFish. I loved ArunGupta 's talks about Glassfish. Especially, the OSGi one. Overall, its was a fascinating experience. I will loo...

Production: Just the kind of messages your users must not see

Reliance is a big name in Indian Telecom sector. And they better hire some production support professionals, because I am looking at this for the past 25 mins. Still the same thing. This is just the kind of messages you don't want your production users to see. It exposes a lot of technical details.Something you, as a developer, don't want to expose to outside world.

Spring Framework 3.0 GA released!

SpringSource announced its GA release today. My personal reasons to "go for it" - REST and Huge improvements in Annotation support. It will be interesting to see how this release is accepted by the community. 2.5.6 seems just yesterday. People (like me) with roots (and lot of existing applications (with issues )) in Spring 2 are just getting their feet wet with migration to 2.5.6 and the magical world of annotation based configuration. Now, I have to calculate ROI on migrating my still-in-development applications (on 2.5.6) to 3.0. ah. I am so in a jinx.

Error creating bean with name 'tilesConfigurer'

While building a skeleton web app, using Spring and Tiles, I encountered this strange error. I am trying to configure Spring 2.5.6 and Tiles 2.2.1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined i n ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: Class org.apache.tiles.web.util.ServletContextAdapter not recognized a TilesApplicationContext at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController....

Speed up Eclipse from a slow workspace

After working on a few Java/JEE projects, your workspace really gets to become fat. I mean, it gets very very slow. This is because you opened and closed a lot of projects with a loooot of resources in it. Also because every time you press that Ctrl+Shift+R, or Ctrl+Shift+O, or Ctrl+T in java or run some free text search, You are loading up eclipse's cache of index. Also JDT slowly keeps a track of it, for Type Hierarchies, Implementations. The easiest way is to open a workspace for every unrelated project. Keep it small. But sometimes, it is not possible. Dependencies. Ease to work with one monolithic workspace (opening and closing projects - yea that's me). For these cases, You could just clean up the .metadata\.plugins\org.eclipse.jdt.core folder in your workspace. If you are feeling even lucky, go ahead, clean up .metadata\.plugins\org.eclipse.core.resources\.history ... a. a. aa. Wait, That folder, contains all that local history of files you have changed - LOCALLY ...

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.

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. ...

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...

Java TimeZone ++: mapping Calendar to Oracle Date or TimeStamp

In Oracle Database date and timestamp columns can be read in java using s[g]etTimeStamp() methods for storing date and time information. However, they do not save the TimeZone information! The read and write operations are done on java.sql.TimeStamp which is a subclass of java.util.Date. When reading and writing to database, they just write out the *face value* (read my previous post on dates for explaination) in Java VM's default TimeZone the code is running on. And read the value too 'TO' the java VM's Default TimeZone. So assuming you WriteDate program on a VM in EST and ReadDate program on another VM in PST (or just change system timezone) - You are getting a different *value* in the date Object. So, How do you tackle this issue. The best way is to set your application to run on a specified (constant) default timezone. As mentioned in the earlier post this can be achieved by TimeZone.setDefault() . If you are unable to do it (For reasons unknown to me , so far),...