Weblogic Tips #2

Issue: Your JSP containing JSTL running under Weblogic 10.3 gives the following error:

“weblogic.xml.jaxp.RegistrySAXParserFactory cannot be cast to javax.xml.parsers.SAXParserFactory”

Solution:Ensure xml-apis-1.3.03 is not present after the Maven build or remove it from the WEB-INF/lib.

Advertisements

Weblogic Tips #1

Weblogic throws NoClassDefFoundError: weblogic.util.NestedUtilException

Environment: WebLogic 10.3

Symptoms
Parsing xml in a standalone mode (out of weblogic 10.3 container) but app has dependency on weblogic.jar file.

If you are trying to run an application (say test case) in standalone mode (ie outside Weblogic container) and you have a dependency on weblogic.jar file, xml parsing might end up throwing the NoClassDefFoundError: weblogic.util.NestedUtilException (For eg loading log4j.xml via DOMConfigurator).

WebLogic has a RegistryXmlParserDocumentBuilderFactory class which may come in between the classpath if you are trying to parse an xml. This in turn depends on several other weblogic modules, not all of which will be in classpath. So you end up getting the NestedUtilException which gobbles up the actual error.

Solution:
Set the -Djava.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl value in the jvm arguments.

Clean bowled!

Long long ago, C++ dinosaurs were roaming the software world. Then the Java-steroid came and destroyed almost all of it, with the remaining C++ became fossilized code and mutating silently into smaller species. Evolution continued with Servlets, JSP and a new thing called EJB. Everyone thought EJB was the greatest thing to happen and it really ended up being a “thing”. Programming in JSP was a pain. Then came Struts and it was good for a while. Then programming in Struts was a pain. Then sprang Spring which made everyone think “Why didn’t I think of it before?”. Yes – DI was the way to go; Of course all configurations must be in XML; Of course all states must be in XML. Along came Hibernate – Of course all DB tables, columns, attributes must be defined in XML; In the midst a new movie called JSF was released (working title was JuSt Forget). JSF is analogous to a Hollywood movie to me – JSF apps are nice to look at, rich interfaces, but try making one yourself. Spring core is too good at DI. Thats why there is only one other real competitor (Guice). If Spring MVC was too good, why would we have 30 other web frameworks? Programming in Spring MVC/SWF is becoming a pain because of lot of scaffolding.

And then I found Wicket. When I first read Peter Thomas’ “Spring MVC vs Wicket” and him favoring the latter I just did not believe him. How could anything be better than Spring MVC, injecting beans via Xml or using referenceData to populate the dropdowns? But then I started using Wicket once.

Since then I have been bowled over. Get it? Clean bowled? Get it? 🙂 Sorry I just could not resist comparing Wicket and Cricket terminologies.

Using mathematical induction, will Wicket be a pain in the next 2 years? I do not know. But for now Wicket seems to be very satisfactory to get my web applications done in less than 1/3rd of the time and absolutely having fun coding.

Wicket Guard

One of the cool things I like about Wicket is — its name! It reminds me of cricket, even if Google does not recognize it right away. Search for wicket in google and you do not come across cricket in the first page! (Source: Wicket In Action)

“Taking guard” is a cricket terminology where the batsman stands in the right place before the stumps (off stump guard, leg stump guard etc) before facing the ball. Its a very important part of strategy of playing the game.

So Wicket Guard is word play on that 😛

Wicket Guard #2

Here is a silly Wicket program I wrote. It continues to amaze me how Wicket makes complex stuff simple and simple stuff unbelievably simpler.

Imagine a totally bored Browser that wants to flip coins and display results. Sure you can do that in Servlet/JSP/Spring with meta refresh tag, querying the server every few seconds and redisplay the page. And good luck if you wanna try that with Ajax. But it becomes ridiculously simple with Wicket. The code literally took me less than 20 minutes and I am Wicket beginner (I have Swing background though).

A couple of gotchas:

  • In the FlipMyCoinPage.html, I had to use the style ul.list-style-type: none. With the default bullet type, wicket prints an empty bullet after every flip.
  • In FlipMyCoinPage.java, with Wicket 1.3.6, I was able to add the label with a wicket:id like “Flip coin:” (including the colon). When I recompiled with Wicket 1.4-rc4, there was a runtime error that colon is not allowed as part of id.
//FlipMyCoinPage.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.time.Duration;
import org.learnspace.core.AbstractModel;

public class FlipMyCoinPage extends WebPage {

	final List<IModel> flipperList = new ArrayList<IModel>();
	private int heads = 0;
	private int tails = 0;
	
	public FlipMyCoinPage() {
		super();
		final WebMarkupContainer section;
		section = new WebMarkupContainer("section");
		add(section);
		section.setOutputMarkupId(true);
		section.setOutputMarkupPlaceholderTag(true);
		
		final Label coinResult = new Label("coinResult", new Model<String>());
		RefreshingView coinFlipper = new RefreshingView("coinFlipper") {
			@Override
			protected Iterator getItemModels() {
				Model<Integer> m = new Model<Integer>((int) (Math.random() * 2));
				flipperList.add(m);
				if (m.getObject() == 0) heads++;
				else tails++;
				int total = heads + tails;
				String s = String.format("Tries = %d; Heads = %d (%5.2f%%); Tails = %d (%5.2f%%)", total, heads, (double) heads/total*100, tails, (double) tails/total*100);
				coinResult.setDefaultModelObject(s);
				return flipperList.iterator();
			}

			@Override
			protected void populateItem(Item _item) {
				add(new Label("Flip " + _item.getIndex(), new HeadOrTailModel(_item.getModel())));
			}
		};
		section.add(coinFlipper);
		section.add(coinResult);
		section.add(new AjaxSelfUpdatingTimerBehavior(Duration.seconds(3)));
	}
	
	//
	class HeadOrTailModel extends AbstractModel {
		
		public HeadOrTailModel(IModel<Integer> _model) {
			super(_model);
		}
		
		public Object getObject() {  
			return ((Integer) getModelObject() == 0) ? "Heads" : "Tails";  
		}  
	}

}
//HeadOrTailModel uses this AbstractModel for brevity
//Based on Jeremey Thomson's blog
import org.apache.wicket.model.IModel;

public abstract class AbstractModel implements IModel {

	private final IModel nestedModel;
	
	public AbstractModel(IModel _model) {
		nestedModel = _model;
	}
	
	@Override
	public void setObject(Object object) {
		nestedModel.setObject(object);
	}
	
	public IModel getModel() {
		return nestedModel;
	}
	
	public Object getModelObject() {
		return nestedModel.getObject();
	}

	@Override
	public void detach() {
		nestedModel.detach();
	}
}

And here is the corresponding html.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd" 
			xml:lang="en">
<head>
	<title>Coin Flipper Demo!</title>
	<style type="text/css">
		ul {
			font-family: tahoma;
			font-size: 9pt;
			list-style-type: none;
			color: black
		}
		
		span#result {
			color: lightgreen;
			font-family: consolas;
			font-size: 12pt;
			font-weight: bold;
			padding: 1em 1em 1em 1em;
			background-color: black
		}
	</style>
</head>
<body>
<div wicket:id="section">
<h3>A really bored browser is flipping a coin ...</h3>
<p>
<span id="result" wicket:id="coinResult"></span>
</p>
<p></p>
<ul>
 <li wicket:id="coinFlipper"></li>
</ul>
</div>
</body>
</html>

Wicket Guard #1

Wicket Guard #1 – Cannot use wicket:id elements in substituted (replaced) panels.

I was using a panel html, but wanted to have the flexibility of designing it in main page also. So I copy the panel’s contents in the Main page too just for designing. This panel content should eventually be replaced by Wicket at runtime.

Panel.html

<wicket:panel>
  <div id="panelContainer">
    <ul>
      <li wicket:id="element">...</li>
    </ul>
  </div>
</wicket:panel>

Main.html

<div id="panel" wicket:id="panel">
    <ul>
      <li wicket:id="element">...</li>
    </ul>
</div>

When I ran the above code, wicket complained that <div> element does not have a close tag. Wicket obviously replaces the contains within div#panel in the Main.html with the Panel.html contents. Then why would it complain? After a bit of digging, found out that the Main.html also contains a not-so-obvious <li wicket:id=”element”>. Wicket does not blindly replace the panel within div#panel, instead it errors out if the element within that has a wicket:id.

Corrected Main.html

<div id="panel" wicket:id="panel">
    <ul>
      <li>...</li>
    </ul>
</div>

So now I have the ability to design Main.html and still have the panel in a separate file.