MyThinkPond

On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

  • Recent Posts

    August 2017
    M T W T F S S
    « Jul    
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Subscribe Options

  • Awards

    JavaCodeGeeks
  • Most Valuable Blogger @ DZone
  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 172 other followers

  • Follow MyThinkPond on WordPress.com
  • Blog Stats

    • 364,739 hits
  • General Options

Archive for the ‘Scala’ Category

Scala compile message – Class file or Jar is broken

Posted by Venkatt Guhesan on November 16, 2009

Sometimes when compiling my Scala code, the compiler output may need a little bit of help in sorting out what’s the real issue.

For example, when I compiled today I received the following error:


[scalac] error: error while loading DefaultHttpClient, class file 'C:\A_OpenSource\WicketNotesApp\lib\httpclient-4.0.jar(org/apache/http/impl/client/DefaultHttpClient.class)' is broken
[scalac] (class net.jcip.annotations.ThreadSafe not found.)

[scalac] error: error while loading AbstractHttpClient, class file 'C:\A_OpenSource\WicketNotesApp\lib\httpclient-4.0.jar(org/apache/http/impl/client/AbstractHttpClient.class)' is broken
[scalac] (class net.jcip.annotations.ThreadSafe not found.)

And my instinct tells me to focus on the jar file or the class file and in this case, “DefaultHttpClient.class” or “AbstractHttpClient.class” or “httpclient-4.0.jar”. There’s noting wrong with your jar file or the Java class. The actual problem is in the next line, namely “class net.jcip.annotations.ThreadSafe not found.” It’s a class-not-found error.

So if you encounter a “broken” error when compiling Scala – the following line after the broken message can help you find the real error. It can save you a whole lot of head-aches and chasing the wrong tail.

If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.

Posted in Scala | Tagged: | 4 Comments »

Forwarding to a URL from Wicket

Posted by Venkatt Guhesan on November 13, 2009

Sometimes we need to forward to a servlet or resource that’s outside of Wicket such as a servlet. Here’s how:

1. Modify your web.xml to define your servlet as well as your Wicket application “ignorePaths” init-param:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>Wicket Example</display-name>
 <filter>
 <filter-name>MyApplication</filter-name>
 <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
 <init-param>
 <param-name>applicationClassName</param-name>
 <param-value>com.gu.MyApplication</param-value>
 </init-param>
 <init-param>
 <param-name>wicket.configuration</param-name>
 <param-value>deployment</param-value>
 </init-param>
 <init-param>
 <param-name>ignorePaths</param-name>
 <param-value>images/,s/</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>MyApplication</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
 <servlet-name>SomeServlet</servlet-name>
 <servlet-class>com.gu.SomeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>SomeServlet</servlet-name>
 <url-pattern>/s/koolkat.do</url-pattern>
 </servlet-mapping>
</web-app>

The first observation is that Wicket is running as a Filter and not a Servlet. Second, the init-param – “ignorePaths” says that anything in the “/images” and “/s/*” will be ignored by Wicket Filter and passed along downstream.

2. Next in your Wicket Form, within your onSubmit method override you can do the following:

class TestForm extends Form(id){
  @override
  public TestForm(String id, SomeFormModel ghfm)
  {
    //Code removed
  }
  @override
  public onSubmit()
  {
    String url = "/s/koolkat.do"; //some url within the same web-context
    getRequestCycle().setRequestTarget(new RedirectRequestTarget(url));
  }
}

Notice that the url field does not contain the “ContextPath”. Wicket takes care of that in the background. On the other hand, suppose you wanted to redirect to a URL ourside of your web context or to an external site then you would set the variable like this.

String url = "https://mythinkpond.wordpress.com/2009/11/13/forwarding-to-a-url-from-wicket/";  //or
url = "http://myserver:port/othercontext/someurl";  

On some rare occasions, you might want to get the servlet Request Dispatcher to forward a request. For that it gets a little tricky.

— In your class you want to grab the “HttpServletResponse”. I’ve been unsuccessful in getting it in the class.

class xyz extends WebPage{
  public xyz()
  {
     HttpServletResponse response = getWebRequestCycle().getWebResponse().getHttpServletResponse();
     //additional code
     xyxForm thisForm = new xyzForm(thisModel, response);
  }
}

class xyzForm extends Form{
  HttpServletResponse response;

  public xyzForm(xyzModel model, HttpServletResponse thisResponse)
  {
     response = thisResponse;
  }
    
   public onSubmit() {
     HttpServletRequest request = getWebRequest().getHttpServletRequest();
     //additional code here
     getWebRequest().getHttpServletRequest().getRequestDispatcher(url).forward(request, response);
  }
}

Please note that the above code is pseudo-code and not actual code because I was writing it in Scala and not Java. But for the audience, I’m trying to express it in Java so that it can have the greatest impact.

That’s it! Happy Coding!

If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.

Posted in Java, Scala, Wicket | Tagged: , , | 1 Comment »

ContextPath in Apache Wicket

Posted by Venkatt Guhesan on November 5, 2009

One of the first things I found as I was getting familiar with Wicket is how do you point to resources that are part of a page. Well I did some digging around to find out so I’m documenting it here for others who might be facing the same challenges as they discover Apache Wicket.

Problem:
You have pages in Wicket that are HTML with “Wicket” tags. But then you might want to point to resources such as CSS style sheets or JavaScript that’s part of a page. Here’s an example of a typical JSP page:


<html>
<head>
<style type="text/css" media="screen">@import \" + request.getContextPath() + "/styles/site.css\";</style>
</head>
...

Well, the above code works when you have a JSP page that gets compiled at run-time before being served. But in the case of Wicket, it does not. So how do you handle such situations?

Solution:

Step-1: Add “<wicket:head>” and “</wicket:head>” between your HTML page like this:


<html>
<head>
<wicket:head>
<!-- Other existing HTML tags can continue to go here -->
</wicket:head>
</head>
...

Step-2: Get the ServletContext and in turn the Context-Path for the web application in your Java/Scala code.

For application servers implementing Servlet 2.5 and above: (Tomcat 6.x implements Servlet 2.5)


ServletContext servletContext = WebApplication.get().getServletContext();
String contextPath = servletContext.getContextPath();

For application servers implementing Servlet 2.4: (Tomcat 5.5x implements Servlet 2.4)


String contextPath = getWebRequestCycle().getWebRequest().getHttpServletRequest().getContextPath();

What’s the difference? Well, in Servlet 2.5, a new method was added
String javax.servlet.ServletContext.getContextPath()
But if you are deploying your code in an earlier Tomcat or other application servers that do not implement Servlet 2.5, you will be at a disadvantage. So in those cases, use the other approach.

Step-3: In your Java/Scala Wicket code, just as you add labels or other components to your page you can add the following:


add(new StringHeaderContributor("<style type=\"text/css\" media=\"screen\">@import \"" + contextPath + "/styles/site.css\";</style>"));

So here’s what happens. In step-2, we created a variable called “contextPath” with the Servlet Context Path as it’s value. And when you add a “StringHeaderContributor”, it adds an entry into the pages header. It’s that simple!

So now after your browser goes through the request you now have the correct “ContextPath” inserted into your application:


<style type="text/css" media="screen">@import "/sampleapp/styles/site.css";</style>

After I posted the article I noticed I had left out a few additional details on this topic. If you are
adding resources such as images on your page, Wicket provides you with a easy way to do this. There is a org.apache.wicket.markup.html.image.ContextImage class that you can use to create image html tags in your
page.

In your Java or Scala class you can do the following:


add(new ContextImage("logo", contextPath + "/i/logo.jpg"))

and in your html (Wicket) page you can add a html-wicket element like this:


<img wicket:id="logo"/>

Wicket matches the wicket:id “logo” with the element added in the code and displays the image with the correct context-path.

 

If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.

Posted in Java, Scala, Wicket | Tagged: , , , , | Leave a Comment »

First Date with IntelliJ Community Edition and Scala

Posted by Venkatt Guhesan on November 4, 2009

Downloaded the Community Edition of IntelliJ a few days ago. Although most J2EE features are not available in the community edition, one of the features that did like was the Scala Plugin.

After I downloaded it and added the scala plug-in, I was very impressed with the tool. I have been using Eclipse (a little Netbeans) for most of my Java development and although I have tried the scala plugin that’s available for Eclipse, I wasn’t all that impressed. But the plug-in for IntelliJ was well worth the try.

So far it’s been very positive experience. Attached below is a screen-shot of a sample Scala Wicket “Hello World”. As you can see the syntax highlighting is awesome.

sample scala code

Update from November 09, 2009 [Issues with the latest Scala Plugin]

A few folks have expressed concern that after their plug-in update (version: 0.3.108 from Nov 06, 2009) their Scala facelet failed to load. Well, I updated today and had the same issue. Rather than trying to fix the root cause, I felt that it’s better for now to get IntelliJ working again. So here’s what I did to fix my Scala Plugin issue.

Close intelliJ (if you haven’t already done so) but make sure you have installed the broken scala plugin.

In windows, the plugins are located here:
C:\Documents and Settings\<usernamehere>\.IntelliJIdea90\config\plugins
I went to the location where I can download the earlier version of the Scala Plugin:
http://plugins.intellij.net/plugin/?id=1347
Downloaded the version: 0.3.75 manually and unzipped it’s contents under above “…\plugins\Scala\lib\” folder. This replaces all the files under the lib directory with the earlier version.

Start intelliJ and you should be golden. This is a quick fix until the issues are resolved.

If you find this article useful, consider signing up for my RSS feed or Email Newsletter. See links on the right side.

Posted in Scala | Tagged: , | Leave a Comment »