Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package brooklyn.entity.rebind;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brooklyn.entity.Entity;
import brooklyn.location.Location;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.reflect.TypeToken;
/**
* Helpers for transforming references to locations into location ids, and vice versa.
* This is used when serializing a location, and when re-binding that location, when the
* location holds references to other locations.
*
* TODO This is unpleasant code. It is required because there is not a clean distinction
* between object construction and object configuration. Therefore, some locations expect
* to be passed (in their constructor for SetFromFlag) references to other locations.
* That makes it hard to serialize a set of inter-connected locations, and subsequently
* rebind them.
*
* TODO Has limited support for transforms - fields that are iterables/maps must be declared
* of type List, Set, Collection, Iterable or Map.
*
* @author aled
*/
public class MementoTransformer {
protected static final Logger LOG = LoggerFactory.getLogger(MementoTransformer.class);
public static T transformIdsToLocations(RebindContext rebindContext, Object value, Class requiredType, boolean removeDanglingRefs) {
if (value == null) {
return (T) value;
} else if (Location.class.isAssignableFrom(requiredType)) {
Location loc = rebindContext.getLocation((String)value);
if (loc == null) {
if (removeDanglingRefs) {
LOG.warn("No location found for "+value+"; returning null for "+requiredType.getSimpleName());
} else {
throw new IllegalStateException("No location found for "+value);
}
}
return (T) loc;
} else if (Iterable.class.isAssignableFrom(requiredType)) {
Collection result = Lists.newArrayList();
for (String id : (Iterable)value) {
Location loc = rebindContext.getLocation(id);
if (loc == null) {
if (removeDanglingRefs) {
LOG.warn("No location found for "+id+"; discarding reference from "+requiredType.getSimpleName());
} else {
throw new IllegalStateException("No location found for "+id);
}
} else {
result.add(loc);
}
}
if (Set.class.isAssignableFrom(requiredType)) {
result = Sets.newLinkedHashSet(result);
}
if (!requiredType.isAssignableFrom(result.getClass())) {
LOG.warn("Cannot transform ids to locations of type "+requiredType+"; returning "+result.getClass());
}
return (T) result;
} else if (value instanceof Map) {
LinkedHashMap