Thursday, December 31, 2015

javax.net.ssl.SSLHandshakeException: Server chose unsupported or disabled protocol: SSLv3

If you get this error message "javax.net.ssl.SSLHandshakeException: Server chose unsupported or disabled protocol: SSLv3",
chances are that your JVM, for vulnerability issues connected to SSLv3, is configured to disable this protocol.
If you REALLY need to support SSLv3, check which file you are using for -Djava.security.properties=/path/to/yourfile, then edit yourfile and make sure your property:
jdk.tls.disabledAlgorithms=SSLv3, DH keySize < 768
doesn't contain SSLv3 (make it jdk.tls.disabledAlgorithms=DH keySize < 768)


Monday, December 21, 2015

Struts 1.2 in WebLogic 12.1.3, german characters encoding issue

"As of WebLogic Server 12.1.3, the default value of the encoding element for the jsp-descriptor element in weblogic.xml is UTF-8 for JSP pages. Prior to WebLogic Server 12.1.3, the default value for JSP encoding was ISO-8859-1."
If you have issues
1) Add <%@ page contentType="text/html;charset=UTF-8" %> line on top of every jsp page. 

 2) Add 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 </properties>

 In pom.xml  

 3) Add 
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
 </filter>
 <mime-mapping>
 <extension>jsp</extension>
 <mime-type>text/html;charset=UTF-8</mime-type>
 </mime-mapping>
 In web.xml 

 4) Add 
 <jsp-config>
 <jsp-property-group > 
 <url-pattern>*.jsp</url-pattern>
 <page-encoding>UTF-8</page-encoding>
 </jsp-property-group>
 </jsp-config> in web.xml – 

5) Add <constant name="struts.i18n.encoding" value="UTF-8"/> In struts-config.xml 


 6) change text encoding to UTF-8 in the properties file. 

7) Add the below to your weblogic.xml.

<charset-params>
 <input-charset>
   <resource-path>/*</resource-path>
   <java-charset-name>ISO8859-1</java-charset-name>
 </input-charset>
</charset-params> 


(see oracle Support "Default JSP Encoding Changed to UTF-8 (Doc ID 2053210.1)" )

(it should be ISO8859-1, not ISO8859_1)

Also, to determine which encoding you are actually using, check in your html for "<meta http-equiv="content-type" content="text/html; charset=utf-8" /> " . And in the browser (IE in my case) you have a menu "view-encoding"...



Friday, December 18, 2015

Umlaut in emails

If in an email you see f�r instead of für, chances are that your weblogic/JavaMail is not using the right encoding (presumably UTF-8)

You can try several options:

-Dfile.encoding=UTF-8
change the template to use escaped (HTML) charachers
see https://issues.apache.org/jira/browse/GERONIMO-5022 :
message.setSubject(subject,"UTF-8");
message.setText(text, "UTF-8");
properties.setProperty("mail.mime.charset","UTF8");






Tuesday, December 15, 2015

org.eclipse.persistence.indirection.IndirectSet locking

one has to be careful with Lazy loading of entities (i.e. INDIRECTION)

http://grepcode.com/file/repo1.maven.org/maven2/org.eclipse.persistence/eclipselink/2.5.1/org/eclipse/persistence/indirection/IndirectSet.java#IndirectSet.getDelegate%28%29

in fact the getDelegate() code contains a synchronized clause, so one can easily end up in a series of blocked threads like the one below waiting on lock 0x0000000785157eb8 :

"[STUCK] ExecuteThread: '33' for queue: 'weblogic.kernel.Default (self-tuning)'" daemon prio=10 tid=0x00007f93100206f0 nid=0x17fc waiting for monitor entry [0x00007f9384ecb000]
   java.lang.Thread.State: BLOCKED (on object monitor)
 at org.eclipse.persistence.indirection.IndirectSet.getDelegate(IndirectSet.java:373)
 - waiting to lock <0x0000000785157eb8> (a org.eclipse.persistence.indirection.IndirectSet)
 at org.eclipse.persistence.indirection.IndirectSet.hashCode(IndirectSet.java:419)
 at java.util.WeakHashMap.hash(WeakHashMap.java:365)
 at java.util.WeakHashMap.getEntry(WeakHashMap.java:494)
 at java.util.WeakHashMap.containsKey(WeakHashMap.java:485)
 at org.apache.commons.lang.builder.ToStringStyle.isRegistered(ToStringStyle.java:162)
 at org.apache.commons.lang.builder.ToStringStyle.appendInternal(ToStringStyle.java:461)
 at org.apache.commons.lang.builder.ToStringStyle.append(ToStringStyle.java:435)
 at org.apache.commons.lang.builder.ToStringBuilder.append(ToStringBuilder.java:851)
 at org.apache.commons.lang.builder.ReflectionToStringBuilder.appendFieldsIn(ReflectionToStringBuilder.java:606)
 at org.apache.commons.lang.builder.ReflectionToStringBuilder.toString(ReflectionToStringBuilder.java:765)
 at org.apache.commons.lang.builder.ReflectionToStringBuilder.toString(ReflectionToStringBuilder.java:287)
 at org.apache.commons.lang.builder.ReflectionToStringBuilder.toString(ReflectionToStringBuilder.java:153)



"[STUCK] ExecuteThread: '50' for queue: 'weblogic.kernel.Default (self-tuning)'" daemon prio=10 tid=0x00007f931003e400 nid=0x2d37 runnable [0x00007f9371cf9000]
   java.lang.Thread.State: TIMED_WAITING (parking)
 at sun.misc.Unsafe.park(Native Method)
 at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:349)
 at com.lmax.disruptor.MultiProducerSequencer.next(MultiProducerSequencer.java:133)
 at com.lmax.disruptor.MultiProducerSequencer.next(MultiProducerSequencer.java:102)
 at com.lmax.disruptor.RingBuffer.next(RingBuffer.java:207)
....
 at org.eclipse.persistence.logging.JavaLog.internalLog(JavaLog.java:290)
 at org.eclipse.persistence.logging.JavaLog.log(JavaLog.java:261)
 at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:3491)
 at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:4731)
 at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:4705)
 at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
 at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
 at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.instantiate(QueryBasedValueHolder.java:129)
 at org.eclipse.persistence.internal.indirection.QueryBasedValueHolder.instantiate(QueryBasedValueHolder.java:116)
 at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:89)
 - locked <0x00000007811573d8> (a org.eclipse.persistence.internal.indirection.QueryBasedValueHolder)
 at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiateImpl(UnitOfWorkValueHolder.java:173)
 at org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder.instantiate(UnitOfWorkValueHolder.java:234)
 at org.eclipse.persistence.internal.indirection.DatabaseValueHolder.getValue(DatabaseValueHolder.java:89)
 - locked <0x0000000785157ee8> (a org.eclipse.persistence.internal.indirection.UnitOfWorkQueryValueHolder)
 at org.eclipse.persistence.indirection.IndirectSet.buildDelegate(IndirectSet.java:211)
 at org.eclipse.persistence.indirection.IndirectSet.getDelegate(IndirectSet.java:375)
 - locked <0x0000000785157eb8> (a org.eclipse.persistence.indirection.IndirectSet)
 at org.eclipse.persistence.indirection.IndirectSet.hashCode(IndirectSet.java:419)
 at java.util.WeakHashMap.hash(WeakHashMap.java:365)
 at java.util.WeakHashMap.getEntry(WeakHashMap.java:494)
 at java.util.WeakHashMap.containsKey(WeakHashMap.java:485)




My 2 cents: LAZY loading should NOT be really the default, unless you REALLY have performance issues (I know, EclipseLink site says the opposite). The effect of loading an object ANYWHERE in your code, while you are for instance in the middle of doing logging, can be really disruptive.



Monday, December 14, 2015

Online Java Thread Dump Analyzer

http://spotify.github.io/threaddump-analyzer/

simple and cool.... it's just what you need in 95% of the cases



Friday, December 11, 2015

Java Mission Control JMC video training

Very good overall coverage of JMC features







weblogic.utils.StackTraceDisabled.unknownMethod()

in recent (>= 12.1.2 ) versions of WLS, as stated in Oracle Doc ID 1624363.1:
"In production mode of WLS 12.1.2, the full Java exception stack trace is not returned to the ejb caller when an exception is thrown."
so all you get on the client is a "weblogic.utils.StackTraceDisabled.unknownMethod()"
Just add -Dweblogic.PrintStackTraceInProduction=true (I guess both on the client and on the server, if they are on different WLS instances...)


Sunday, December 6, 2015

Debugging SSL with javax.net.debug=all

This is a guide to interpreting the output of -Djavax.net.debug=all:
https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/ReadDebug.html
and this is what I get
https://gist.github.com/vernetto/4b5c1e5b9562143998e0
by running this code with args[0]=www.usps.com (code courtesy of the excellent book "Java Network Programming, 4th Edition" that I highly recommend reading)



import java.io.*;
import javax.net.ssl.*;

public class HTTPSClient {

  public static void main(String[] args) {

    if (args.length == 0) {
      System.out.println("Usage: java HTTPSClient2 host");
      return;
    }

    int port = 443; // default https port
    String host = args[0];

    SSLSocketFactory factory
        = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket socket = null;
    try {
      socket = (SSLSocket) factory.createSocket(host, port);

      // enable all the suites
      String[] supported = socket.getSupportedCipherSuites();
      socket.setEnabledCipherSuites(supported);

      Writer out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
      // https requires the full URL in the GET line
      out.write("GET http://" + host + "/ HTTP/1.1\r\n");
      out.write("Host: " + host + "\r\n");
      out.write("\r\n");
      out.flush();

      // read response
      BufferedReader in = new BufferedReader(
          new InputStreamReader(socket.getInputStream()));

      // read the header
      String s;
      while (!(s = in.readLine()).equals("")) {
        System.out.println(s);
      }
      System.out.println();

      // read the length
      String contentLength = in.readLine();
      int length = Integer.MAX_VALUE;
      try {
        length = Integer.parseInt(contentLength.trim(), 16);
      } catch (NumberFormatException ex) {
        // This server doesn't send the content-length
        // in the first line of the response body
      }
      System.out.println(contentLength);

      int c;
      int i = 0;
      while ((c = in.read()) != -1 && i++ < length) {
        System.out.write(c);
      }

      System.out.println();
    } catch (IOException ex) {
      System.err.println(ex);
    } finally {
        try {
          if (socket != null) socket.close();
        } catch (IOException e) {}
    }
  }
}





Friday, December 4, 2015

java.lang.ArrayStoreException

When creating an array, remember that it will be not of the type declared for the variable, but of the type decided in the initializer:


public class ArrayStoreExceptinTest {
 public static void main(String[] args) {
  System.out.println("starting");

  // test 1: all ok

  Object[] myarray1 = new Object[2];
  System.out.println("myarray1 " + myarray1.getClass().getName());
  myarray1[0] = new Integer(0);
  myarray1[1] = new String("");
  myarray1[1] = new Integer(0);

  // test 2: fails with java.lang.ArrayStoreException
  try {
   Object[] myarray2 = new String[2];
   System.out.println("myarray2 " + myarray2.getClass().getName());
   myarray2[0] = new Integer(0);
  } catch (Exception e) {
   e.printStackTrace();
  }

  // test 3: all ok
  Animal[] myarray3 = new Animal[] { new Cat(), new Dog() };
  System.out.println("myarray3 " + myarray3.getClass().getName());
  myarray3[0] = new Dog();

  try {
   // test 4: fails with java.lang.ArrayStoreException
   Animal[] myarray4 = new Cat[] { new Cat(), new Cat() };
   System.out.println("myarray4 " + myarray4.getClass().getName());
   myarray4[0] = new Dog();
  } catch (Exception e) {
   e.printStackTrace();
  }

 }
}

interface Animal {

}

class Dog implements Animal {

}

class Cat implements Animal {

}



this is the output:

starting
myarray1 [Ljava.lang.Object;
myarray2 [Ljava.lang.String;
java.lang.ArrayStoreException: java.lang.Integer
 at ArrayStoreExceptinTest.main(ArrayStoreExceptinTest.java:18)
myarray3 [LAnimal;
myarray4 [LCat;
java.lang.ArrayStoreException: Dog
 at ArrayStoreExceptinTest.main(ArrayStoreExceptinTest.java:32)



so if you initialize an Animal[] as a Cat[], you can't assign to a member a variable of type Dog, even if a Dog is an Animal...