Author: usenkanov

Python imports: demystified

* the most confusing part of Python i think (it’s too simple, that’s why)
Good discussion here: http://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python
Python imports use the environment variable $PYTHONPATH to search for packages.
Here is what I currently have in my .bashrc file:
export PYTHONPATH=.:/usr/bin/python2.7:/work/bcom/stats-processing
Relative imports do not work as they should for now.
Therefore, people usually create a file in the root folder, which imports everything needed.

How to mock static methods : PowerMock

1. First check versions of Junit being used and download matching PowerMock.
2. Those are the dependencies that you will need if you are using Mockito already:
<dependency org="org.powermock" name="powermock-module-junit4" rev="1.4.12" />
 <dependency org="org.powermock" name="powermock-api-mockito" rev="1.4.12" />
powermock-module-junit4 : this provides integration with Junit4
powermock-api-mockitor : this provides PowerMock classes to work with Mockito
3. Usage is quite simple. Check the link above.
– include those annotations on the class level:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ControllerUtil.class)
– include the following line first (if you have @Before method, it’s best to keep it there)
PowerMockito.mockStatic(ControllerUtil.class);
– do the mocking:
Mockito.when(ControllerUtil.roleExists(anyString())).thenReturn(true);

 

Tomcat stuck, no app is running, doesn’t show any log (catalina.out) messages either

This happened when I tried to restart a tomcat server.
While starting, catalina.out shows no messages related to the apps that should be loading.
Apps, as expected, don’t work either.
To get more information about what is going on, kill tomcat in a particular way:
While killing tomcat use:
kill -3 [pid]
This tells JVM to kill the process and dump the thread stack, which will show right in catalina.out.
In my case, this happened because I initialized a Spring bean which makes a request to another service, which was not running.
Service was not running, because it was being deployed on the same Tomcat server.
Deadlock occurred because my app was stuck waiting for the service app, which, probably, was down the line but never had a chance to start.

Absent Code attribute in method

I needed to include @RolesAllowed annotation into the project.
For that I’ve used the following IVY dependency:
<dependency org=”javax” name=”javaee-api” rev=”6.0″ conf=”compile->default”/>
This dependency basically provides interface to most of java ee components without implementation.
Hence, it is usually used to compile the project, because implementation is usually within the web container (eg. Tomcat).

However, the project used to fail with an error above whenever I ran ant test task.
Mock of HttpServletRequest object was failing. My guess is that reflection was trying to go deeper into actual implementation which dependency above does not provide.

It was even harder to spot, because IDE was running all tests just fine. Probably, because classes were loaded in a different order in IDE and in ANT.
JVM grabs whichever class is loaded first and ignores the next one, hence error did not show in IDE.
IDE was loading full implementation, while ANT was loading interface (dependency above) first.

Getting a smaller jar file that contains implementation of javax.annotation.security package helped solve the problem.
<dependency org=”javax.annotation” name=”jsr250-api” rev=”1.0″ conf=”compile->default”/>

Good times!

ORA-00957: duplicate column name with Hibernate

This error was particularly annoying, because it didn’t appear on my local machine, but only on the remote environment. Environments were exactly the same: Apache versions, Oracle driver versions,  the same DB is used. Everything else is same, because builds are managed by IVY. 

Looking at SQL that was generated, Hibernate was generating duplicate column names when an entity with several JOINED entities was being retrieved. I couldn’t set alias for related entities because I am using Criteria API and all relationships are configured by annotations.

I am still not sure what is the problem. The way I solved it is by making all related entities load LAZILY. Please let me know if someone finds an answer to this.

org.tigris.subversion.javahl.ClientException: svn: No repository found in …

This happened when I was trying to use repository from Eclipse using Subclipse plugin. The first time I was setting up repository in Eclipse I specified password based authentication, password was memorized and never asked again (because I clicked on checkbox to memorize it).

Because Subclipse and JavaHL got compatibility issues, and the error was mentioning JavaHL I was thinking problem is still with Subclipse and JavaHL versions incompatibility. Tried to re-install different Subclipse versions. Didn’t help.

So, here goes solution:

1. Clean passwords saved in Eclipse. Remove .keyring file from (eclipse_installed_dir/configuration/org.eclipse.core.runtime/)

2. Change authentication type to File based and specify path to your ~/.ssh/id_rsa (note, not id_rsa.pub)

3. Do any SVN action (update / commit / checkout … )

Deploy multiple virtual hosts on Tomcat 6

Dedicated to those of you who have spent TOO much time on this way under-documented topic:

First let’s start with a tomcat file structure.

TOMCAT FILE STRUCTURE

1. webapps – folder where applications should be placed to run (unless unpackWARs is set to false)
2. work/Catalina/host_name – place where temporary files are created when Tomcat is running, eg: servlet classes
When “unpackWARs” is set to false (in server.xml) entire site structure is created
* we should not care about this directory much because it is generated dynamically each time

DEPLOYING HOSTS without actual file structure
Add line as follows, and that’s it:

<Host name=”xxx” appBase=”webapps”
unpackWARs=”false” autoDeploy=”true”
xmlValidation=”false” xmlNamespaceAware=”false” >
<Context path=”” docBase=”/wherever/xxx.war” debug=”0″ reloadable=”true”/>
</Host>

* note: this way application FILE STRUCTURE is not created, probably because “webapps” folder cannot be used. Not sure about this one though, whatever the reason is what is important that no error messages are generated and application gets created in work/Catalina directory and, apparently, works happily from there.

– The problem with this approach is that if we cannot deploy web application that will read files on the server side.
– Application will fail to find files on the server, because files do not exists. Actually they exist in work/Catalina directory, however it is not the path application looks for while running. Application MUST be physically located in WEBAPPS directory for files to be found.

DEPLOYING HOSTS with actual file structure (THE RIGHT WAY!)
Add line as follows:

<Host name=”xxx” appBase=”webapps/xxx”
unpackWARs=”false” autoDeploy=”true”
xmlValidation=”false” xmlNamespaceAware=”false” >
<Context path=”” docBase=”/wherever/xxx.war” debug=”0″ reloadable=”true”/>
</Host>

Create directory in webapps folder called xxx. Copy-paste “manager” and “host-manager” directories from “webapps” directory to this directory. This step is very important, otherwise you will get this extremely annoying error in log files:

SEVERE: Error starting static Resources

…xxx/host-manager does not exist or is not a readable directory

Some online documentation will claim that you will need to mess with /conf/Catalina/xxx folder. Maybe with older Tomcat versions you will need to do that. I tested with version 6.0.32, all files in that folder are automatically generated.

Hope it makes someone’s life easier!