|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package com.kpelykh.docker.client.model; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.codehaus.jackson.JsonGenerator; |
| 26 | +import org.codehaus.jackson.map.JsonSerializer; |
| 27 | +import org.codehaus.jackson.map.SerializerProvider; |
| 28 | +import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Kevin A. Archie <karchie@wustl.edu> |
| 33 | + * |
| 34 | + */ |
| 35 | +@JsonSerialize(using=BoundHostVolumes.Serializer.class) |
| 36 | +public class BoundHostVolumes { |
| 37 | + private static final String[] STRING_ARRAY = new String[0]; |
| 38 | + private final String[] dests, binds; |
| 39 | + |
| 40 | + /** |
| 41 | + * |
| 42 | + * @param specs Iterable of String binding specs, each of form "{host-path}:{container-patch}:[rw|ro]" |
| 43 | + * @throws MalformedVolumeSpecException if any specs are null or empty |
| 44 | + */ |
| 45 | + public BoundHostVolumes(final Iterable<String> specs) { |
| 46 | + final List<String> dests = new ArrayList<String>(), binds = new ArrayList<String>(); |
| 47 | + for (final String spec : specs) { |
| 48 | + if (null == spec || "".equals(spec)) { |
| 49 | + // skip empty spec lines |
| 50 | + } else { |
| 51 | + final String[] sspec = spec.split(":"); |
| 52 | + dests.add(sspec.length > 1 ? sspec[1] : sspec[0]); |
| 53 | + binds.add(spec); |
| 54 | + } |
| 55 | + } |
| 56 | + this.dests = dests.toArray(STRING_ARRAY); |
| 57 | + this.binds = binds.toArray(STRING_ARRAY); |
| 58 | + } |
| 59 | + |
| 60 | + public String[] asBinds() { |
| 61 | + return binds; |
| 62 | + } |
| 63 | + |
| 64 | + private BoundHostVolumes writeVolumes(final JsonGenerator jg) throws IOException { |
| 65 | + jg.writeStartObject(); |
| 66 | + for (final String dest : dests) { |
| 67 | + jg.writeObjectFieldStart(dest); |
| 68 | + jg.writeEndObject(); |
| 69 | + } |
| 70 | + jg.writeEndObject(); |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * This is an ugly hack. We assume that the serializer only gets called when |
| 76 | + * a containing ContainerConfig gets serialized, when POSTing to |
| 77 | + * /containers/create . In that context, we pass only the container-path |
| 78 | + * part (the key in the volumes map). |
| 79 | + * |
| 80 | + * @author Kevin A. Archie <karchie@wustl.edu> |
| 81 | + * |
| 82 | + */ |
| 83 | + public static class Serializer extends JsonSerializer<BoundHostVolumes> { |
| 84 | + /* (non-Javadoc) |
| 85 | + * @see org.codehaus.jackson.map.JsonSerializer#serialize(java.lang.Object, org.codehaus.jackson.JsonGenerator, org.codehaus.jackson.map.SerializerProvider) |
| 86 | + */ |
| 87 | + @Override |
| 88 | + public void serialize(final BoundHostVolumes volumes, final JsonGenerator jg, final SerializerProvider sp) |
| 89 | + throws IOException { |
| 90 | + volumes.writeVolumes(jg); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments