Friday, April 8, 2016

Firebug for Chrome

As a Firefox user I have normally used Firebug to debug HTTP requests and trace the execution of a request.
In Chrome most people see to be happy with the Control-Shift-I (= “more tools” / “Developer tools”) , then in the Network tab, enable “preserve logs”


Thursday, April 7, 2016

EJB : Stateless vs Singleton

http://stackoverflow.com/questions/14464823/difference-between-stateless-and-singleton and in particular this answer
http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html#gipmt
The EXCELLENT book "Beginning EJB 3: Java EE 7 Edition" (that I highly recommend to buy) explains it very well:
"Introduced in EJB 3.1, a singleton session bean is a session bean component that is instantiated only once per application. For an application, only one instance of a singleton session bean can ever exist. Once instantiated, a singleton session bean lives for the full duration of the application. The singleton session bean maintains its state between client invocations, but it cannot save that state after a container shutdown or crash. Similar to stateless and stateful session beans, the singleton session bean is comprised of a bean class and, optionally, one or more business interfaces."


https://docs.oracle.com/javaee/6/api/javax/ejb/Singleton.html
http://docs.oracle.com/javaee/6/api/javax/ejb/Stateless.html


Monday, April 4, 2016

WebLogic: No known valid port for Default[t3]: 7001, No available router to destination.

if you get a funny error like:
The Message-Driven EJB failed while creating a JMS Connection ... java.io.IOException: No known valid port for: 'Default[t3]:t3(t3):169.133.134.123:7001:null ... No available router to destination.
check Oracle Support document "JMS Servers Fail To Communicate via T3 in WebLogic Server (WLS): "No available router to destination" (Doc ID 948192.1)", it could be that you should declare t3s as default protocol in the config.xml section for the server.

Javassist and code instrumentation

Looking for a way to trace an Error in a static initializer block, I have stumbled upon this http://stackoverflow.com/a/2211150/651288 example of how to dynamically redefine an ExceptionInInitializerError :
import java.lang.instrument.*;
import javassist.*;
import java.io.*;
import java.security.*;

public class InitializerLoggingAgent implements ClassFileTransformer {
  public static void premain(String agentArgs, Instrumentation inst) {
    inst.addTransformer(new InitializerLoggingAgent(), true);
  }

  private final ClassPool pool = new ClassPool(true);

  public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)  {
    try {
      if (className.equals("java/lang/ExceptionInInitializerError")) {
        CtClass klass = pool.makeClass(new ByteArrayInputStream(classfileBuffer));
        CtConstructor[] ctors = klass.getConstructors();
        for (int i = 0; i < ctors.length; i++) {
          ctors[i].insertAfter("this.printStackTrace();");
        }
        return klass.toBytecode();
      } else {
        return null;
      }
    } catch (Throwable t) {
      return null;
    }
  }
}

http://jboss-javassist.github.io/javassist/tutorial/tutorial.html
Download javassist zip from here http://jboss-javassist.github.io/javassist/ and explode it
Download Ant (Ant? yes, Ant!) from here https://ant.apache.org/bindownload.cgi
Build javassist source and put the javassist.jar file in the lib folder of your project, add it to build classpath.
Follow instructions to jar your JavaAgent with a MANIFEST.MF containing
Manifest-Version: 1.0
Premain-Class: InitializerLoggingAgent
Can-Retransform-Classes: true

run java -javaagent:agentjar.jar MainClass
It works! Your Error is intercepted and stacktrace is printed! No more silently failing static initializers!
If you forget to add "Can-Retransform-Classes: true" in the MANIFEST.MF, you get the infamous "java.lang.UnsupportedOperationException: adding retransformable transformers is not supported in this environment"


Sunday, April 3, 2016

HTTP/1.1 502 Bad Gateway

com.sun.xml.ws.client.ClientTransportException: HTTP transport error: java.io.IOException: Unable to tunnel through proxy. 
Proxy returns "HTTP/1.1 502 Bad Gateway" HTTP transport error: 
java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 502 Bad Gateway" at 
com.sun.xml.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:141) at 
com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:187) at 
com.sun.xml.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:124) at 
com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:118) at 
com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:866) at 
com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:815) at 
com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:778) at 
com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:680) at 
com.sun.xml.ws.client.Stub.process(Stub.java:272) at 
com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:153) at 
com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:115) at 
com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:95) at 
com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:136) 



It's not clear what is causing this problem...



ExceptionInInitializerError , NoClassDefFoundError and ClassNotFoundException

https://docs.oracle.com/javase/7/docs/api/java/lang/ExceptionInInitializerError.html
"an exception occurred during evaluation of a static initializer or the initializer for a static variable."

https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html
"no definition of the class could be found"

the above 2 are LinkageError (Error). The one below is an Exception:
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
Thrown when an application tries to load in a class through its string name using:

    The forName method in class Class.
    The findSystemClass method in class ClassLoader .
    The loadClass method in class ClassLoader. 

but no definition for the class with the specified name could be found. 

Difference between NoClassDefFoundError and ClassNotFoundException is well explained here:
http://www.javaroots.com/2013/02/classnotfoundexception-vs.html "This is thrown when at compile time the required classes are present, but at run time the classes are changed or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath, but one of the classes which are required by this class are either removed or failed to load by compiler. So you should see the classes which are dependent on this class."

Examples: this will throw a
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
 at com.pierre.pvtest.Test.(Test.java:4)



package com.pierre.pvtest;

public class Test {
 static int a = 1 / 0;
 public static void main(String[] args) {
  System.out.println("hello");
 }
}



If I compile this:
package com.pierre.pvtest;

public class Test {
 
 public static void main(String[] args) {
  Test1 test1 = new Test1();
  test1.hello();
  System.out.println("hello");
 }
}

package com.pierre.pvtest;

public class Test1 {
 public void hello() {
  System.out.println("Test1 hello");
 }

}

then I delete Test1.class, I get this:
java com.pierre.pvtest.Test
Exception in thread "main" java.lang.NoClassDefFoundError: com/pierre/pvtest/Test1
        at com.pierre.pvtest.Test.main(Test.java:6)
Caused by: java.lang.ClassNotFoundException: com.pierre.pvtest.Test1
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more




Saturday, April 2, 2016

EJB 3.2 tutorial with Manning, EJB 3 in Action




The book is decent, but half of the prose could be removed and the narrative could be much more terse. It seems that often the authors get carried away by their literary velleity. Unfortunately the code examples are incomplete, as always happens the "import" statement are omitted, so that it really takes some effort to try the examples. However, code is provided on the Manning site.
@Stateless
@Stateful
@Singleton
@Lock
@Asynchronous
@Local
@Remote
@Remove
@Inject
@Resource
@PreDestroy
@Startup
@EJB
@Named
@Produces
@RequestScoped
@LoggedIn
@PersistenceContext
@JMSConnectionFactory
@MessageDriven


java:/[app-name]//[!fully-qualifiedinterface- name] java:comp java:module java:app java:global

@AroundInvoke @Interceptors , default interceptor in ejb-jar-xml
@InterceptorBinding


@TransactionAttribute @ApplicationException
@Schedule @Schedules
@RunAs
@WebService @SOAPBinding @WebMethod @WebResult
@Produces @Consumes
@EntityManager @Entity