1 package org.simantics.db.common;
3 import java.lang.reflect.Method;
6 import org.simantics.db.Metadata;
7 import org.simantics.db.Session;
8 import org.simantics.db.common.utils.Logger;
9 import org.simantics.db.exception.ServiceException;
11 public class MetadataUtils {
13 private static final boolean TRACE_METADATA_SERIALIZATION = false;
15 public static <T extends Metadata> boolean hasMetadata(Map<String,byte[]> metadata, Class<T> clazz) {
16 return metadata.containsKey(clazz.getName());
19 @SuppressWarnings("unchecked")
20 public static <T extends Metadata> T getMetadata(Session session, Map<String,byte[]> metadata, Class<T> clazz) throws ServiceException {
21 byte[] data = metadata.get(clazz.getName());
22 if (TRACE_METADATA_SERIALIZATION) {
24 System.out.println("deserializing " + data.length + " bytes of metadata into " + clazz);
28 m = clazz.getMethod("deserialise", Session.class, byte[].class);
29 return (T) m.invoke(null, session, data);
30 } catch (Exception e) {
32 Logger.defaultLogError(e.toString());
37 public static <T> void addMetadata(Session session, Map<String,byte[]> metadata, Metadata data) throws ServiceException {
38 byte[] blob = data.serialise(session);
39 if (TRACE_METADATA_SERIALIZATION)
40 System.out.println("serialized " + blob.length + " bytes of metadata into for " + data.getClass());
41 metadata.put(data.getClass().getName(), blob);