August 14, 2013

Eclipse shortcuts

There's already tons of posts dealing with this, but I don't care and I'll share my favourite eclipse shortcuts anyway :

Ctrl + s ^^
Ctrl + Space => completion
Ctrl + T => show implementations for an interface method
Ctrl + Shift + O (the letter)=>organise imports
Ctrl + Alt + H => lookup call hierarchy for this method or attribute
Alt + Shift + X, then J => Run a java class containing a main method
Alt + Shift + X, then T => Run junit tests in current class
Alt + up or down arrow => move a line up or down
Alt + left or right arrow => go to last edited location
Ctrl + Alt + up or down arrow => duplicate current line and copy it above or below
Ctrl + page up or page down => move from one tab to another. Really useful, except when you hit the bloody xml editor in your tabs...
Ctrl + L => goto line number ....
Ctrl + Shift + L => pops up the shortcut list
Alt + Enter => Access file properties. Useful to check file encoding, or check version control file URL
Ctrl + Shift + R => Open File
Ctrl + Shift + T => Open Type. Useful to find a class in a jar for example
Ctrl + W => Close file
F12 => Go to editor view
Ctrl + M => maximize/minimize current editor
Ctrl + 1 => show quickfix. has the same effect as hovering over an error with the mouse
Ctrl + E => display list of opened editors
Ctrl + Shift + F => Format code
Ctrl + I => Indent code
F3 => open déclaration
F4 => open hierarchy
Alt + Shift + R => rename field
Alt + Shift + T => show refactor menu

I usually also create a binding for the "generate getters and setters" action.

Please feel free to propose new ones if you feel like I've forgotten important ones.

July 18, 2013

PowerMock and SSL Context : NoSuchAlgorithmException

I was  trying to unit test a method that's sending an HTTP message to a potentially secured URI. Therefore, somewhere in the process the javax SSL Context interface is used.

I obvisouly want to mock that call, because I'm testing the method and not the call itself. When ryunning the test, I get the exception below :

com....TechnicalException: java.security.NoSuchAlgorithmException: class configured for SSLContext: com.sun.net.ssl.internal.ssl.SSLContextImpl not a SSLContext

...

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:69)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)

at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)

at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl$PowerMockJUnit49MethodRunner.executeTestInSuper(PowerMockJUnit49RunnerDelegateImpl.java:117)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl$PowerMockJUnit49MethodRunner.executeTest(PowerMockJUnit49RunnerDelegateImpl.java:77)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:285)

at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:91)

at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:210)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:123)

at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)

at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:125)

at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)

at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)

at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:684)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:391)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: java.security.NoSuchAlgorithmException: class configured for SSLContext: com.sun.net.ssl.internal.ssl.SSLContextImpl not a SSLContext

at sun.security.jca.GetInstance.checkSuperClass(GetInstance.java:242)

at sun.security.jca.GetInstance.getInstance(GetInstance.java:221)

at sun.security.jca.GetInstance.getInstance(GetInstance.java:147)

... 31 more




This is related to the SSLCOntext loading stuff from the upstream classloader - which is Power Mock's one when running the test.

Solution : use @PowerMockIgnore annotation on the top of your test class :

@PowerMockIgnore({"javax.net.ssl.*","javax.security.*"})

public class MessageProcessorTest { ... }


Found the solution thanks to this thread and that issue.
Edit : added javax.security as an exclusion for Apache HttpClient (thanks to William Darby comment - see below).

HTH

Mathieu.

June 02, 2013

ATI Radeon 2600 drivers on Ubuntu 12.04.02 with 3.5.0-32 kernel

I just spent a few hour trying to install proprietary drivers on my (old) laptop for my (old) graphic card.
If the following configuration talks to you, then I might help you :-)

- Ubuntu 12.04.02
- Kernel version 3.5.0-32-generic
- ATI Radeon 2600 graphic card

Since the last kernel update, unable to install Catalyst 13.1 drivers. dpkg creation is successful, but when trying to install those dpkg I am getting a bad exception when fglrx is installing.

Error! Bad return status for module build on kernel: 3.5.

DKMS part of installation failed. Please refer to /usr/share/ati/fglrx-install.log for details

Same result with the graphic installer of course.

After a while I found some 13.4 Catalyst drivers. Installation goes smoothier, but in the end...

aticonfig: No supported adapters detected 

Arrrgh ! They dropped support for 2600 series in this version !

Only solution that worked for me :
https://launchpad.net/~makson96/+archive/fglrx

Hope this help !

May 28, 2013

Concurrent calls in Axis 1.4 : thread safety issue

I had to deal with concurrent calls issue in Axis 1.4.
After googling a bit, it turns out that some guys from axis crew state the following :
- Generated service locators are thread safe
- However, ports stubs retrieved from those locators are NOT thread safe !

So even if the stubs look designed to be reused (they're caching bits of request for performance matter I suppose), that's really a bad idea to do so.

I wanted to know where exactly the problem was, so I dove into the sources, and here is what I found.

Let's suppose I retrieve my stub this way :
axisGeneratedProxy.getMyGeneratedWSStub().theMethodToCall([some Params]);
In the theMethodToCall method, you'll find the following :
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[2]);
_call.setUseSOAPAction(true);
...
If you go up the hierarchy a little, you'll find out that the createCall method is returning a call object generated from the org.apache.axis.client.Stub class. This call object is generated this way :
public abstract class Stub implements javax.xml.rpc.Stub {
 ...
 // The last call object
 protected Call _call = null;public Call _createCall() throws
 ...
   /**
      * Creates a call from the service.
      * @return
      */
     public Call _createCall() throws ServiceException {
         _call = (Call) service.createCall();
          // TODO: There is a lot of code in the generated stubs that
         // can be moved here.
         return _call;
     }
 ...
 }

So what do we have here ? The generated call object is stored temporarly in a _call class attribute. If concurrent threads T1 and T2 get mixed up in this method, we'll end up with both threads sharing the same call object.

So, back to the stub code.
After getting back this call object, it's filled with data. Here, both threads will manipulate the same object, so potentially some data will get mixed up... but as we're in a stub method, that should be the same data so it doesn't really bother.

But the real problem occurs later in the call.invoke() method : when adding the body element to the message, the following private method is used :
 public void addBodyElement(SOAPBodyElement element)
    {
        if (body == null) {
            body = new SOAPBody(this, soapConstants);
        }
        element.setEnvelope(this);
        body.addBodyElement(element);

        _isDirty = true;
    }

Here the body attribute is a class attribute... If two or more threads get mixud up here, the stub sharing the same call element, the two calls will go to the server with the same body element...

Solution : use many stubs, don't reuse one !




May 27, 2013

Google Chrome initiating a google search for local urls with dots in it

I tend to modify my hosts file whenever I'm working on a project : it sometimes help to detect bugs (javascript related mainly).

Problem is, with Google Chrome, when I was typing local.something:8080 in the address bar, the browser would launch a Google search on that address - which is obviously not what I want.

After googling a bit, looks like turning off DNS prefetch does the trick : indeed now Chrome just load my local web page instead of searching on Google.

You can access this property in the browser settings. You have to display advanced settings.
(Below a snapshot - that's in french but hey - I guess you'll find the correct option)

This image shows the google chrome setting needed to be unchecked to turn off DNS prefetching

April 17, 2013

Java Memory in open VZ machines

Hi,

I ran into troubles while trying to run a play application. The server is hosted in an openVZ container, which lead to problems during jvm memory initialization when I entered à play start command :

Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

After googling a while, this SO thread gave me the answer which I'll summarize here.

If you're in a hurry, here is the answer : edit the [JRE location]/lib/[Archi (ex : i386)]/jvm.cfg file and alter the first uncommented line.

Move from


-client IF_SERVER_CLASS -server
-server KNOWN
-hotspot ALIASED_TO -client
-classic WARN
-native ERROR
-green ERROR

to:
#-client IF_SERVER_CLASS -server
-client KNOWN
-server KNOWN
-hotspot ALIASED_TO -client
-classic WARN
-native ERROR
-green ERROR

From what I understand, with the initial configuration, when trying to launch a java process if the machine has 2G+ memory installed  it fires up the JVM in server mode.
Server mode means the JVM will ask for all the memory requested in contiguous memory blocks, and openVZ is not able to do that.

The -client KNOWN option will ask for memory on the fly, thus removing the problem meentionned above.

Hope this help.


April 09, 2013

Yeoman : TypeError('Arguments to path.join must be strings');

Edit : Node 0.10 is now fully supported (working with a 0.10.17 right now) - keeping the old thread for archive. However, I had the error described below with the default Ubuntu 12.04 LTS node version (0.11-pre something) 


Hi all,

Today I was playing with this great Yeoman tool. Basically it's a scaffolding tool (yo) combined with a building (grunt) and dependency manager tool (bower) for building front end apps.



That tool (or that tooling set) is really, really great and you should check it out.

Set up is really easy, everything is packaged as Node.js modules. Only thing I didn't see first when installing the stack : Node.js is fully supported until version 0.8.x for now. Running a higher version (0.10 in my case) will lead to problems like this

path.js:360
        throw new TypeError('Arguments to path.join must be strings');
              ^
TypeError: Arguments to path.join must be strings
    at path.js:360:15
    at Array.filter (native)
    at Object.exports.join (path.js:358:36)
    at Object. (/opt/nodejs/node-v0.10.0-linux-x64/lib/node_modules/yo/node_modules/yeoman-generator/node_modules/bower/lib/core/config.js:41:22)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)


This error doesn't come right away, I had it when trying to use the angular generator (angular:route) to be precise.

Hope this help.