|
| 1 | +package hudson.plugins.git; |
| 2 | + |
| 3 | +import com.thoughtworks.xstream.converters.Converter; |
| 4 | +import com.thoughtworks.xstream.converters.MarshallingContext; |
| 5 | +import com.thoughtworks.xstream.converters.UnmarshallingContext; |
| 6 | +import com.thoughtworks.xstream.core.util.Base64Encoder; |
| 7 | +import com.thoughtworks.xstream.io.HierarchicalStreamReader; |
| 8 | +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
| 9 | + |
| 10 | +import org.eclipse.jgit.lib.ObjectId; |
| 11 | + |
| 12 | +/** |
| 13 | + * Object id converter. This is required for legacy support for originally |
| 14 | + * serializing an ObjectId as a byte[]. The new format supports marshalling as a |
| 15 | + * standard lower-case SHA1 hexadecimal string but unmarshalling both byte[] and |
| 16 | + * String types. |
| 17 | + */ |
| 18 | +public class ObjectIdConverter implements Converter { |
| 19 | + |
| 20 | + private Base64Encoder base64; |
| 21 | + |
| 22 | + /** |
| 23 | + * Create ObjectId converter |
| 24 | + */ |
| 25 | + public ObjectIdConverter() { |
| 26 | + base64 = new Base64Encoder(); |
| 27 | + } |
| 28 | + |
| 29 | + public boolean canConvert(Class type) { |
| 30 | + return ObjectId.class == type; |
| 31 | + } |
| 32 | + |
| 33 | + public void marshal(Object source, HierarchicalStreamWriter writer, |
| 34 | + MarshallingContext context) { |
| 35 | + writer.setValue(((ObjectId) source).name()); |
| 36 | + } |
| 37 | + |
| 38 | + public Object unmarshal(HierarchicalStreamReader reader, |
| 39 | + final UnmarshallingContext context) { |
| 40 | + if (reader.hasMoreChildren() |
| 41 | + && "byte-array".equals(reader.peekNextChild())) { |
| 42 | + reader.moveDown(); |
| 43 | + ObjectId sha1 = ObjectId.fromRaw(base64.decode(reader.getValue())); |
| 44 | + reader.moveUp(); |
| 45 | + return sha1; |
| 46 | + } else |
| 47 | + return ObjectId.fromString(reader.getValue()); |
| 48 | + } |
| 49 | +} |
0 commit comments