Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  The MIT License
   *
   *  Copyright 2011 Sony Ericsson Mobile Communications. All rights reserved.
   *  Copyright 2012 Sony Mobile Communications AB. All rights reserved.
   *
   *  Permission is hereby granted, free of charge, to any person obtaining a copy
   *  of this software and associated documentation files (the "Software"), to deal
   *  in the Software without restriction, including without limitation the rights
  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  *  copies of the Software, and to permit persons to whom the Software is
  *  furnished to do so, subject to the following conditions:
  *
  *  The above copyright notice and this permission notice shall be included in
  *  all copies or substantial portions of the Software.
  *
  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  *  THE SOFTWARE.
  */
 package com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data;
 
 
 import java.util.List;

A com.thoughtworks.xstream.XStream converter that can marshal/unmarshal TriggerContexts. This aids in the backwards comparability issue when refactoring the TriggerContext class.

Author(s):
Robert Sandell <robert.sandell@sonyericsson.com>
Since:
2.2.0
 
 public class TriggerContextConverter implements Converter {
 
     private static final Logger logger = LoggerFactory.getLogger(TriggerContextConverter.class);
 
     @Override
     public void marshal(Object sourceHierarchicalStreamWriter writerMarshallingContext context) {
         TriggerContext tc = (TriggerContext)source;
         if (tc.getEvent() != null) {
             writer.startNode("event");
             writer.addAttribute("class"tc.getEvent().getClass().getName());
             context.convertAnother(tc.getEvent());
             writer.endNode();
         }
         if (tc.getThisBuild() != null) {
             writer.startNode("thisBuild");
             marshalItemEntity(tc.getThisBuild(), writer);
             writer.endNode();
         }
         if (tc.getOthers() != null && tc.getOthers().size() > 0) {
             writer.startNode("others");
             for (TriggeredItemEntity entity : tc.getOthers()) {
                 if (entity != null) {
                     writer.startNode("triggeredItemEntity");
                     marshalItemEntity(entitywriter);
                     writer.endNode();
                 }
             }
             writer.endNode();
         }
     }

    
Marshals an instance of TriggeredItemEntity.

Parameters:
entity the entity.
writer the XStream writer.
 
     private void marshalItemEntity(TriggeredItemEntity entity,
                                    HierarchicalStreamWriter writer) {
         writer.startNode("buildNumber");
         if (entity.getBuildNumber() != null) {
             writer.setValue(entity.getBuildNumber().toString());
         }
         writer.endNode();
         writer.startNode("projectId");
         writer.setValue(entity.getProjectId());
         writer.endNode();
     }

    
Unmarshals an instance of TriggeredItemEntity.

Parameters:
reader the XStream reader, placed at the position of the entity.
context the context.
Returns:
an entity.
        Integer buildNumber = null;
        String projectId = null;
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            if ("buildNumber".equalsIgnoreCase(reader.getNodeName())) {
                String buildNumberStr = reader.getValue();
                if (buildNumberStr != null && buildNumberStr.length() > 0) {
                    try {
                        buildNumber = Integer.parseInt(buildNumberStr);
                    } catch (NumberFormatException e) {
                        throw new ConversionException("Wrong buildNumber format!"e);
                    }
                }
            } else if ("projectId".equalsIgnoreCase(reader.getNodeName())) {
                projectId = reader.getValue();
            }
            reader.moveUp();
        }
        return new TriggeredItemEntity(buildNumberprojectId);
    }
    @Override
    public Object unmarshal(HierarchicalStreamReader readerUnmarshallingContext context) {
        TriggerContext tc = new TriggerContext();
        while (reader.hasMoreChildren()) {
            reader.moveDown();
            if ("event".equalsIgnoreCase(reader.getNodeName())) {
                String clazz = reader.getAttribute("class");
                Class<? extends GerritTriggeredEventtheClass = calculateEventClass(clazz);
                GerritTriggeredEvent event = (GerritTriggeredEvent)context.convertAnother(tctheClass);
                tc.setEvent(event);
            } else if ("thisBuild".equalsIgnoreCase(reader.getNodeName())) {
                TriggeredItemEntity entity = unmarshalItemEntity(readercontext);
                tc.setThisBuild(entity);
            } else if ("others".equalsIgnoreCase(reader.getNodeName())) {
                List<TriggeredItemEntitylist = new LinkedList<TriggeredItemEntity>();
                while (reader.hasMoreChildren()) {
                    reader.moveDown();
                    TriggeredItemEntity entity = unmarshalItemEntity(readercontext);
                    list.add(entity);
                    reader.moveUp();
                }
                tc.setOthers(list);
            }
            reader.moveUp();
        }
        return tc;
    }

    
Tries to load the the class if it is not null. If null or failure it returns com.sonyericsson.hudson.plugins.gerrit.gerritevents.dto.events.PatchsetCreated as a safety measure.

Parameters:
clazz the class to try and load.
Returns:
the class.
    private Class<? extends GerritTriggeredEventcalculateEventClass(String clazz) {
        if (clazz == null) {
            //Probably old data, assume PatchsetCreated
            return PatchsetCreated.class;
        }
        Class<? extends GerritTriggeredEventtheClass = null;
        try {
            theClass = (Class<? extends GerritTriggeredEvent>)Class.forName(clazz);
            return theClass;
        } catch (ClassNotFoundException e) {
            .error("Failed to unmarshall event type for trigger context!"e);
        } catch (ClassCastException e) {
            .error("Failed to unmarshall event type for trigger context!"e);
        }
        //Fallback to PatchsetCreated and pray
        return PatchsetCreated.class;
    }
    @Override
    public boolean canConvert(Class type) {
        return type != null && type.equals(TriggerContext.class);
    }
New to GrepCode? Check out our FAQ X