2 Replies Latest reply on Sep 16, 2014 5:06 AM by lewis.watson

    Camel overriding type converter

    lewis.watson

      Hello All,

       

      I'm having problems with a type converted in my Switchyard project.

       

      Inside my DocumentConverter class I have a type converter for converting an ArrayList (from an SQL binding) into an array of Document Objects

       

      @Converter
      public class DocumentConverter {
         
          ...
      
          @Converter
          public static Document[] fromAllDocuments(ArrayList<Map<String, Object>> objects) {
              Document[] docs = null;
              if (objects != null && !objects.isEmpty()) {
                  docs = new Document[objects.size()];
                  for (int i = 0; i < objects.size(); i++) {
                      System.out.println("ArrayList");
                      docs[i] = generateDocumentFromMap(objects.get(i));
                  }
              }
              return docs;
          }
         
          ...
      }
      
      

       

      The Document object is a simple POJO

       

      public class Document {
         
          private String id;
          private String details;
         
          public Document(String id, String details){
              this.id = id;
              this.details = details;
          }
      
          public String getId() {
              return id;
          }
      
          public void setId(String id) {
              this.id = id;
          }
      
          public String getDetails() {
              return details;
          }
      
          public void setDetails(String details) {
              this.details = details;
          }
      
          /* (non-Javadoc)
           * @see java.lang.Object#toString()
           */
          @Override
          public String toString() {
              return "Document [id=" + id + ", details=" + details + "]";
          }
         
         
      
      }
      
      

       

      When I perform a GET request on a document in debug mode I put a break point inside fromAllDocuments and see that it is called correctly and the results are fine too. I can then do another GET on a different document ID and see that fromAllDocuments is correctly called again. However, at some point after process exits fromAllDocuments I get the following warning

       

      WARN [org.apache.camel.impl.converter.DefaultTypeConverter] (http-localhost/127.0.0.1:8080-7) Overriding type converter from: StaticMethodTypeConverter: public static com.blah.data.Document[] com.blah.lookup.CaseConverter.fromCase(java.util.List) to: org.apache.camel.impl.converter.ArrayTypeConverter@6530d3aa
      
      

       

      Subsequent GET requests all fail with an error 500 and the following warning

       

      12:55:18,008 WARN [org.jboss.resteasy.core.SynchronousDispatcher] (http-localhost/127.0.0.1:8080-7) failed to execute: javax.ws.rs.WebApplicationException: java.lang.reflect.UndeclaredThrowableException
      at org.switchyard.component.resteasy.composer.RESTEasyMessageComposer.decompose(RESTEasyMessageComposer.java:75) [switchyard-component-resteasy-1.1.1-p5-redhat-1.jar:1.1.1-p5-redhat-1]
      ...
      Caused by: org.switchyard.HandlerException: org.apache.camel.TypeConversionException: Error during type conversion from type: java.util.ArrayList to the required type: com.blah.data.Document[] with value [{id=1, details=Hello World, case_id=1}] due null
      ...
      Caused by: org.apache.camel.TypeConversionException: Error during type conversion from type: java.util.ArrayList to the required type:
      ...
      Caused by: java.lang.ArrayStoreException
      ...
      
      

       

      I'm at a loss to think why its overriding my conversion method during runtime. Any suggestions will be appreciated!

       

      Switchyard version: 1.1.1

       

      Regards

       

      Lewis

        • 1. Re: Caml overriding type converter
          lewis.watson

          Hello again. I'm still experiencing this issue. If there is more information that I could provide that would help please ask.

           

          Regards

           

          Lewis

          • 2. Re: Re: Caml overriding type converter
            lewis.watson

            The problem was due to my converter class fromAllDocuments returning null values when the SQL binding returned a null resultset.

             

            When a converter returns a null value then Switchyard assumes it is broken and attempts to find an alternative converter. In this case it finds an ArrayList converter.

             

            Initialising docs to an array of the same size as the objects parameter resolves the issue.

             

            @Converter  
            public class DocumentConverter {  
            
                ... 
            
                @Converter  
                public static Document[] fromAllDocuments(ArrayList<Map<String, Object>> objects) {  
                    Document[] docs = new Document[objects.size()];  
                    if (objects != null && !objects.isEmpty()) {  
                        docs = new Document[objects.size()];  
                        for (int i = 0; i < objects.size(); i++) {  
                            System.out.println("ArrayList");
                            docs[i] = generateDocumentFromMap(objects.get(i)); 
                        } 
                    } 
                    return docs;  
                } 
            
                ... 
            } 
            

             

            However, I still have converters that return single instances of Document. I have a workaround for those what involves initialising the Document object but not setting any of the internal variables but that isn't a long term solution.

            1 of 1 people found this helpful