I tried to put GWT in an existing project and after my first try to make a gwt compile, I got the following exception:


java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding.closestReferenceMatch()Lorg/eclipse/jdt/internal/compiler/lookup/ReferenceBinding;


The problem exist, because the existing libraries in my project had an conflict with the gwt libraries. Putting the gwt libraries in a higher position in the build path fixes the problem.

Wednesday, July 15, 2009

Loading beans in spring...

We developed an e-shop (a front end and a back end) as an eclipse project. The front end produces its own war and the back end a different one. Both web applications uses the same API and have differences in the GUI. A major difference is that the back end uses scheduling (quartz) and the front end not. The API is configured with spring.

We decided to configure quartz with spring and the job we needed to run with it. The result was, that the front end, using the same configuration as the back end, initialized the quartz framework and enabled its own scheduling. But that was not what we wanted to do.

The solution was simple. At the definition of a spring bean in the configuration there is an attribute in the bean tag named lazy-init. The default value for this attribute is false, and this means that the container will create a bean of that kind at the initialization process. If you give the value true, then the bean will be created only at the time when you request it.













So, the scheduler initializes in the back end, because we want it to do so, and in the front end it stays only as a bean in the spring configuration.

Wednesday, July 1, 2009

The ol/li problem

I was setting up a web site with joomla and at last the time for the data input has come. Evething seemed to work well. I checked evething in Safari, Firefox and IE8 but in IE7 there was a problem. The first number (1) of an ol/li list has gone.

Trying to solve the problem I isolated the code with the problem in a html and a css file.

In Firefox 3 the result looks lite this:
In Safari 4 it looks like this:

The html page code is this:






Here is the title



tool1

tool2







and the css code:

body {

}

.page {
width: 100%;
height: 500px;
}

.content {
width: 300px;
}

.toolbar {
background: #ECECEC none repeat scroll 0 0;
border-top: 1px solid #DDDDDD;
clear: both;
display: block;
float: left;
margin-bottom: 15px;
width: 100%;
}

.leftToolbar {
float: left;
padding: 5px;
width: 80%;
}

.rightToolbar {
float: right;
width: 15%;
}

.menu {

}


I could not find a solution but luckily I found a work around. The problem occurs because the first element of the list doesn't have enough space to show up in the right position. Making the content boarder (try resizing the browser, or try resizing the content with firebug) solves the problem. Not a nice solution be an acceptable workaround.

In the old days I used to work with flash and it was always a problem to send data to the flash from the HTML level. As it looks it is really simple now to do exactly this: simple calling a java script method described in this tutorial from adobe. In this tutorial you can learn how to send and receive a string from (and to) a text field in flash to (and from) a HTML form using java script function calls.

Wednesday, June 17, 2009

Testing with app engine in Java

I wanted to make a template for my tests in a web app using spring and google app engine. The test uses of course JUnit and annotations. Here is the result:


@ContextConfiguration(locations = {"classpath:test-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TheTest {

@Before
public void setUp() throws Exception {
ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")) {
});
}

@After
public void tearDown() throws Exception {
ApiProxy.setDelegate(null);
ApiProxy.setEnvironmentForCurrentThread(null);
}

@Autowired
private ToBeTestedRepository toBeTestedRepository ;

@Test
public void testLottoFileReader() {
// Do some tests here using the toBeTestedRepository
}

public void setToBeTestedRepository (ToBeTestedRepository toBeTestedRepository ){
return this.toBeTestedRepository = toBeTestedRepository;
}

public ToBeTestedRepository getToBeTestedRepository () {
return toBeTestedRepository;
}

The methods setUp() and tearDown() tell the datastore that we are using it localy.

While learning to use JDO with datanucleus for a project to be run in app engine I tried to implement a method that gets a collection of persistant objects from the database. Trying to test the results I used the size() method on the collection. The result was following exception:

org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed
at org.datanucleus.ObjectManagerImpl.assertIsOpen(ObjectManagerImpl.java:3816)
at org.datanucleus.ObjectManagerImpl.findObjectUsingAID(ObjectManagerImpl.java:2073)
....

The solution is simple. To iterate throw the objects of the collection, they have to be in a detached state, not in persistant state.

So the code in the dao class would look like:



public Collection getAllValueObjects() {
Collection results = Collections.checkedCollection(getJdoTemplate().find(ValueObject.class), ValueObject.class);

// To iterate the collection it has to be detached
getPersistenceManager().detachCopyAll(results);

return results;
}

I experience sporadically the following problem in Joomla 1.5:

I have a menu and in it a menu item of type (lets say) blog section. The section shows 3 articles. Under unknown conditions although at the beginning everything works well, comes a time when I change parameters on the view of this section and this changes never change the real output by selecting the menu item. It is a very strange behavior.

A workaround for this is to make a copy of the menu item, place the new copy in the same position like the old one and work the parameters there. After verifying that the new button works as expected, you can delete the old one.

Still searching for an answer to that.

There was a GWT application that used c3p0 as a connection pool manager. This application had two connections for two different databases, one for the authentication and the other for the data retrieval.

Starting the application the user had to fill the login form and wait for the authorization. This action didn't use the c3p0. Then in the next step the application showed up 3 dropdown, each populated with its own query. Those 3 queries got the connection throw a datastore which used the c3p0 to serve connections.

So, after the authendication, many exceptions showed up in the console, many failures from the c3p0, like this:
WARN [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] com.mchange.v2.resourcepool.BasicResourcePool (1841) - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@425eb9 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (3). Last acquisition attempt exception:
java.sql.SQLException: An SQLException was provoked by the following failure: com.mchange.v2.resourcepool.ResourcePoolException: Attempted to use a closed or broken resource pool
Those exceptions showed up more than once. After some search in the internet I couldn't find a unique solution, everyone seems to have found his own way to solve the problem. And so did I.

The problem was those 3 drop downs after the loggin procedure. This 3 queries required 3 connections, one for each. At this time, the datasource was null and because the server calls in GWT are asynchronous, for every call was the datasource null... So, there was a tripple c3p0 initialization and this created the problems.

I have created a project in eclipse with the google plugin that should run on the app engine. Therefore it uses the datanucleus framework and JDO. Because of eclipse everytime you change a class in the build path, the datanucleus Enhancer runs and enhances the classes that should be persistent (capable or award).

I wrote a test case to test some code using the annotations @RunWith and @Test, plus some more for the spring configuration (@ContextConfiguration and @Autowired). When I saved the first version of the test (it compiled successfully at last) I noticed that on the console there was a warning from datanucleus enhancher:

11 Ιουν 2009 12:49:02 μμ org.datanucleus.metadata.annotations.AnnotationManagerImpl getMetaDataForClass
WARNING: Class "the class name" has annotations but there is no registered AnnotationReader. Please check your CLASSPATH and the annotations in the class for validity.

It seems that JPOX (the base of datanucleus) supports plugins for annotations through an AnnotationReader interface. The desirable solution for my case were to tell datanucleus to ignore the test class through (for example) an annotation.

This post applies to eclipse IDE.

If you have an interface (say Test) with a method declared (say testMethod) and you create a new class that implements this interface, there will be an error, that the class you declared must implement the methods defined in the interface. There is also a quick fix available that implements this methods in respect to a code template. If you choose the quick fix the result will be like:


@Override
public String testMethod() {
// TODO Auto-generated method stub
return null;
}

Say, you are working in a team (this is the default situation for a company), and you forget to implement the method. The result will be in the best case will be a null pointer exception somewhere in the code, but surely not in the method above. The return null is not the best solution for the default implementation. Even worse, the method could return an int value, then the default return value from the template would be 0, so no error is visible at runtime...

It would be better if the code would show exactly where the problem is (that means in the method testMethod) and even better, who is responsibly for this error. Both tasks are covered with the following code:

@Override
public String testMethod() {
// TODO Auto-generated method stub
throw new IllegalStateException("User X didn't do his work!");
}

This piece of code cures both problems. An exception is thrown, so everyone knows where the error is, and even better, they know who is responsible for this.

To change the template in eclipse go to eclipse preferences Java > Code Style > Code Templates
There in the tree select Code > Method body.
Press Edit... and change the pattern in

@Override
// ${todo} Auto-generated method stub
throw new IllegalStateException("${user} didn't do his work!");

Press OK and OK again and test the pattern.

If you are using log4j then you probably are tired of the copy/pasting the logger from on class to an other. There is a simple way to automate this by using the templates in the java editor in eclipse.


Try this out
  • open the eclipse Preferences
  • in the search field type template or follow the path: Java > Editor > Templates
  • There you can find any predefined templates you can use while editing a java file
  • press New...
  • In the name field type logger (this is the keyword you should type in the editor so that the code assistance activates the current temaplate). You could type log if you wish....
  • As pattern you can try this (of course you can change this as you wish by using the predefined variables in eclipse): private static final Logger logger = Logger.getLogger(${enclosing_type}.class);
  • ${enclosing_type} is a variable and returns the class name of the file that is being currently edited.
  • press OK and then again OK
  • try it out in a java class. Just write logger under the class declaration and press ctrl + space. Select logger from the assistant
  • press alt + shift + O to automatically import the log4j Logger class
The result should be something like this:


import org.apache.log4j.Logger;

public class ExampleClass {
private static final Logger logger = Logger.getLogger(ExampleClass.class);
}

Some time now I had a problem with running a dynamic web app project in eclipse as a web application (right click on project name and then RUN AS > RUN ON SERVER).

I had mostly spring enabled applications and the problem was that the application-context.xml file was not deployed in tomcat (in the webapps directory of tomcat, not the workspace direcory). The reason was that the xml file, and all other xml files were not copied in the build (in most cases bin) directory by eclipse. I couldn't find the reason for that.

After searching in internet I found this thread . I don't have the TPTP project installed in my eclipse, so I don't think that this is the reason. Another thread here.

The solution is simple although I don't know if there are any side effects.

"Change the Java/Compiler/Building preference, and remove *.xml from the filtered resources."

And that worked... at last.

I thought it would be a nice way to kill some time, by making a mini project in google app engine using JDO. I made my model bean, made it persistent, I wrote a repository for it, and then I thought lets give it a try... The result was the following exception:

javax.jdo.JDOUserException: You have either specified for this PMF to use a "persistence-unit" of "transactions-optional" (yet this doesnt exist!) or you called JDOHelper.getPersistenceManagerFactory with "transactions-optional" as the name of a properties file (and this doesnt exist in the CLASSPATH)

This is strange, because I use the google plug-in for eclipse and my project is set up for GWT 1.6 + App engine 1.2 by the google plug-in, which creates automaticaly a file in META_INF directory named jdoconfig.xml. In this file is the persistance manager factory defined with the name "transactions-optional". Why can't the web-app find the file?


Seems that the problem was the non included xml file which configures jdo in the application. Look also at this post.

I try to learn JDO and I have some problems already. After I succeeded to configure datanucleus with spring using HSQLDB, I started to play around.

I have a Worker class and a Tool class. A Worker can have more than one tool so we have a 1-n relationship.

There are 2 ways to define a 1-n relationship. One case is with a foreign key and the other is throw a middle join table.

Here the solution with the foreign key (using Annotations):


public class Worker {


@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Override
public String toString() {
return this.getClass().getName() + " [" +
"id:"+id+", " +
"tools:"+getTools()+", " +
"]";
}

@Persistent
private String name;

@Persistent
@Element(types=gr.open.thohapi.model.Tool.class)
private Set tools = new HashSet();
.
.
.
}


and here the solution with the join table (using Annotations):

public class Worker {


@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Override
public String toString() {
return this.getClass().getName() + " [" +
"id:"+id+", " +
"tools:"+getTools()+", " +
"]";
}

@Persistent
private String name;

@Persistent
@Element(types=gr.open.thohapi.model.Tool.class)
@Join(table="WORKER_TOOLS", column="WORKER_ID")
private Set tools = new HashSet();
.
.
.
}

Another spring configuration propeblem:

I have a test case I want to run using a specific configuration file. The test case looks like this:


@ContextConfiguration(locations = {"classpath:system-test-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class ThohapiTests {

private final static Logger logger = Logger.getLogger(ThohapiTests.class);

@Resource(name="sampleService")
private SampleService service;

@Test
public void testSampleService() {

Worker result1 = service.storeWorker();
logger.debug(result1);
assertNotNull(result1);

Worker result2 = service.storeWorker();
logger.debug(result2);
assertNotNull(result2);

logger.debug(service.getWorker(result1.getId()));
logger.debug(service.getWorker(result2.getId()));
}

@Required
public void setSampleService(SampleService sampleService) {
this.service = sampleService;
}
}


My configuration file, where I define my sampleService is this:












org.datanucleus.jdo.JDOPersistenceManagerFactory
jdbc:hsqldb:mem:dbname
sa

org.hsqldb.jdbcDriver

true














PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED






and the business beans are defined in:




















Running the test case, produces the following error:
org.springframework.beans.factory.BeanInitializationException: Property 'sampleService' is required for bean 'test.ThohapiTests'
at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:121)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:998)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:329)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:93)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:130)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Solution:
Deleting the @Required annotation from the setter of the sampleService makes everything work fine.

I think, I have a spring configuration problem. I use eclipse with the spring tool plug-in enabled for my project. I used to have one configuration file for my business logic beans and for the test environment. Then, I decided to split the configuration file in two files, one for the test environment and one for the business logic beans, do that the second one would be the base for the actuall application.

The configuration file is system-test-config.xml













org.datanucleus.jdo.JDOPersistenceManagerFactory
jdbc:hsqldb:mem:dbname
sa

org.hsqldb.jdbcDriver

true






















PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED,readOnly
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED






and the application file is application-config.xml













With this configurations files it should be working, shouldn't it? The test config xml imports the application configuration, so it should recognize the sampleDAOImpl bean defined in it. But it doesn't.

Eclipse (through the sprint tool) shows me an error:
cvc-id.1: There is no ID/IDREF binding for IDREF 'sampleDAOImpl'.

and the execution of the test has as result the following exception:
Caused by: org.xml.sax.SAXParseException: cvc-id.1: There is no ID/IDREF binding for IDREF 'sampleDAOImpl'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
... 26 more

Solution:
After searching a little bit in google, I found this. It seams that there is a difference between local declared beans and not local beans. The bean sampleDAOImpl is defined in a separate xml file, so I should reference to it not as a local bean, using the ref attribute of the property tag. That's all!

To use the syntax highlighting, use the following tags:

<pre name="code" class="Java">
public void test() {
// Do some stuff here
}
</pre>

You should enter this code in "Edit Html" editor mode. The preview does not include the necessary javascript, so it doesn' t function. You have to publish the post.
The result is:

public void test() {
// Do some stuff here
}
The idea comes from here.

So, use
  • "Java" for java
  • "Sql" for sql
  • "JScript" for java script
  • "Xml" for xml
  • "Css" for css