use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class ResourceHelper method cloneValueMap.
public static Map<String, Object> cloneValueMap(final ValueMap vm) throws InstantiationException {
List<Exception> hasReadError = null;
try {
final Map<String, Object> result = new HashMap<String, Object>(vm);
for (final Map.Entry<String, Object> entry : result.entrySet()) {
if (entry.getValue() instanceof InputStream) {
final Object value = vm.get(entry.getKey(), Serializable.class);
if (value != null) {
entry.setValue(value);
} else {
if (hasReadError == null) {
hasReadError = new ArrayList<Exception>();
}
final int count = hasReadError.size();
// let's find out which class might be missing
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream((InputStream) entry.getValue());
ois.readObject();
} catch (final ClassNotFoundException cnfe) {
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "'", cnfe));
} catch (final IOException ioe) {
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "'", ioe));
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException ignore) {
// ignore
}
}
}
if (hasReadError.size() == count) {
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "'"));
}
}
}
}
if (hasReadError != null) {
result.put(PROPERTY_MARKER_READ_ERROR_LIST, hasReadError);
}
return result;
} catch (final IllegalArgumentException iae) {
// the JCR implementation might throw an IAE if something goes wrong
throw (InstantiationException) new InstantiationException(iae.getMessage()).initCause(iae);
}
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class ConfigurationResolverValueMapIT method testNonExistingConfig.
@Test
public void testNonExistingConfig() throws Exception {
Resource resourcePage1 = resourceBuilder.resource(PAGE_PATH).getCurrentParent();
ConfigurationResolver configResolver = teleporter.getService(ConfigurationResolver.class);
ValueMap props = configResolver.get(resourcePage1).name("test").asValueMap();
assertNotNull(props);
assertNull(props.get("stringParam", String.class));
assertEquals(0, (int) props.get("intParam", 0));
assertEquals(false, props.get("boolParam", false));
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class MockedResource method adaptTo.
@SuppressWarnings("unchecked")
@Override
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
if (type.equals(Node.class)) {
try {
return (AdapterType) getSession().getNode(getPath());
} catch (Exception e) {
logger.error("Exception occurred: " + e, e);
throw new RuntimeException("Exception occurred: " + e, e);
}
} else if (type.equals(ValueMap.class)) {
try {
Session session = getSession();
Node node = session.getNode(getPath());
HashMap<String, Object> map = new HashMap<String, Object>();
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property p = properties.nextProperty();
if (p.getType() == PropertyType.BOOLEAN) {
map.put(p.getName(), p.getBoolean());
} else if (p.getType() == PropertyType.STRING) {
map.put(p.getName(), p.getString());
} else if (p.getType() == PropertyType.DATE) {
map.put(p.getName(), p.getDate().getTime());
} else if (p.getType() == PropertyType.NAME) {
map.put(p.getName(), p.getName());
} else if (p.getType() == PropertyType.LONG) {
map.put(p.getName(), p.getLong());
} else {
throw new RuntimeException("Unsupported property type: " + p.getType());
}
}
ValueMap valueMap = new ValueMapDecorator(map);
return (AdapterType) valueMap;
} catch (Exception e) {
logger.error("adaptTo failed with : " + e);
return null;
}
} else if (type.equals(ModifiableValueMap.class)) {
return (AdapterType) new ModifiableValueMap() {
public Collection<Object> values() {
throw new UnsupportedOperationException();
}
public int size() {
throw new UnsupportedOperationException();
}
public Object remove(Object arg0) {
Session session = getSession();
try {
final Node node = session.getNode(getPath());
final Property p = node.getProperty(String.valueOf(arg0));
if (p != null) {
p.remove();
}
// the return value is never used
return null;
} catch (PathNotFoundException pnfe) {
// perfectly fine
return null;
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
public void putAll(Map<? extends String, ? extends Object> arg0) {
throw new UnsupportedOperationException();
}
public Object put(String arg0, Object arg1) {
Session session = getSession();
try {
final Node node = session.getNode(getPath());
Object result = null;
if (node.hasProperty(arg0)) {
final Property previous = node.getProperty(arg0);
if (previous == null) {
// null
} else if (previous.getType() == PropertyType.STRING) {
result = previous.getString();
} else if (previous.getType() == PropertyType.DATE) {
result = previous.getDate();
} else if (previous.getType() == PropertyType.BOOLEAN) {
result = previous.getBoolean();
} else if (previous.getType() == PropertyType.LONG) {
result = previous.getLong();
} else {
throw new UnsupportedOperationException();
}
}
if (arg1 instanceof String) {
node.setProperty(arg0, (String) arg1);
} else if (arg1 instanceof Calendar) {
node.setProperty(arg0, (Calendar) arg1);
} else if (arg1 instanceof Boolean) {
node.setProperty(arg0, (Boolean) arg1);
} else if (arg1 instanceof Date) {
final Calendar c = Calendar.getInstance();
c.setTime((Date) arg1);
node.setProperty(arg0, c);
} else if (arg1 instanceof Long) {
node.setProperty(arg0, (Long) arg1);
} else if (arg1 == null) {
node.setProperty(arg0, (Value) null);
} else {
throw new UnsupportedOperationException();
}
return result;
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
public Set<String> keySet() {
Session session = getSession();
try {
final Node node = session.getNode(getPath());
final PropertyIterator pi = node.getProperties();
final Set<String> result = new HashSet<String>();
while (pi.hasNext()) {
final Property p = pi.nextProperty();
result.add(p.getName());
}
return result;
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
public boolean isEmpty() {
throw new UnsupportedOperationException();
}
public Object get(Object arg0) {
Session session = getSession();
try {
final Node node = session.getNode(getPath());
final String key = String.valueOf(arg0);
if (node.hasProperty(key)) {
return node.getProperty(key);
} else {
return null;
}
} catch (RepositoryException re) {
throw new RuntimeException(re);
}
}
public Set<Entry<String, Object>> entrySet() {
throw new UnsupportedOperationException();
}
public boolean containsValue(Object arg0) {
throw new UnsupportedOperationException();
}
public boolean containsKey(Object arg0) {
Session session = getSession();
try {
final Node node = session.getNode(getPath());
return node.hasProperty(String.valueOf(arg0));
} catch (RepositoryException re) {
throw new RuntimeException(re);
}
}
public void clear() {
throw new UnsupportedOperationException();
}
public <T> T get(String name, T defaultValue) {
throw new UnsupportedOperationException();
}
public <T> T get(String name, Class<T> type) {
Session session = getSession();
try {
final Node node = session.getNode(getPath());
if (node == null) {
return null;
}
if (!node.hasProperty(name)) {
return null;
}
Property p = node.getProperty(name);
if (p == null) {
return null;
}
if (type.equals(Calendar.class)) {
return (T) p.getDate();
} else if (type.equals(String.class)) {
return (T) p.getString();
} else {
throw new UnsupportedOperationException();
}
} catch (RepositoryException e) {
throw new RuntimeException(e);
}
}
};
} else {
return super.adaptTo(type);
}
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class IdMapService method readIdMap.
private Map<Integer, String> readIdMap(ResourceResolver resourceResolver) throws PersistenceException {
Resource resource = ResourceHelper.getOrCreateResource(resourceResolver, getIdMapPath());
ValueMap idmapValueMap = resource.adaptTo(ValueMap.class);
Map<Integer, String> idmap = new HashMap<>();
for (String slingId : idmapValueMap.keySet()) {
Object value = idmapValueMap.get(slingId);
if (value instanceof Number) {
Number number = (Number) value;
idmap.put(number.intValue(), slingId);
}
}
return idmap;
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class ResourceHelper method getPropertiesForLogging.
/** Compile a string builder containing the properties of a resource - used for logging **/
public static StringBuilder getPropertiesForLogging(final Resource resource) {
ValueMap valueMap;
try {
valueMap = resource.adaptTo(ValueMap.class);
} catch (RuntimeException re) {
return new StringBuilder("non-existing resource: " + resource + " (" + re.getMessage() + ")");
}
if (valueMap == null) {
return new StringBuilder("non-existing resource: " + resource + " (no ValueMap)");
}
final Set<Entry<String, Object>> entrySet = valueMap.entrySet();
final StringBuilder sb = new StringBuilder();
for (Iterator<Entry<String, Object>> it = entrySet.iterator(); it.hasNext(); ) {
Entry<String, Object> entry = it.next();
sb.append(" ");
sb.append(entry.getKey());
sb.append("=");
sb.append(entry.getValue());
}
return sb;
}
Aggregations