コード例 #1
0
ファイル: Exporter.java プロジェクト: chrisdolan/aQute
  public static void toJSON(ConfigurationAdmin admin, Writer osw, String filter) throws Exception {

    Configuration[] list = admin.listConfigurations(filter);
    Encoder encoder = codec.enc().to(osw);

    Protocol p = new Protocol();
    p.version = 1;
    p.date = new Date();
    p.size = list.length;
    encoder.put(p).append('\n');

    if (list != null)
      for (Configuration c : list) {
        Dictionary<String, Object> d = c.getProperties();
        Export export = new Export();
        export.values = new HashMap<String, Object>();
        export.factoryPid = c.getFactoryPid();
        export.pid = c.getPid();

        for (Enumeration<String> e = d.keys(); e.hasMoreElements(); ) {
          String k = e.nextElement();
          Object v = d.get(k);

          if (!(v instanceof String)) {

            if (export.types == null) export.types = new HashMap<String, Type>();

            Type type = new Type();

            Class<?> clazz = v.getClass();
            if (v instanceof Collection) {
              Collection<?> coll = (Collection<?>) v;
              clazz = String.class;
              if (coll.size() > 0) type.vectorOf = shortName(coll.iterator().next().getClass());
              else type.vectorOf = shortName(String.class);
            } else if (v.getClass().isArray()) {
              type.arrayOf = shortName(clazz.getComponentType());
            } else type.scalar = shortName(v.getClass());

            export.types.put(k, type);
          }
          export.values.put(k, v);
        }

        encoder.mark().put(export);
        // encoder.put(encoder.digest());
        encoder.append('\n');
      }
    osw.flush();
  }
コード例 #2
0
ファイル: Exporter.java プロジェクト: chrisdolan/aQute
  private static Dictionary<String, Object> getDictionary(
      Map<String, Type> types, Map<String, Object> values) throws Exception {
    Hashtable<String, Object> ht = new Hashtable<String, Object>();
    for (Map.Entry<String, Object> e : values.entrySet()) {
      String key = e.getKey();
      Object value = e.getValue();

      Type type;
      if (types != null && (type = types.get(key)) != null) {

        if (type.scalar != null) {
          value = convert(value, type.scalar);
        } else if (type.vectorOf != null) {
          Collection<Object> coll = (Collection<Object>) value;
          Vector vector = new Vector();
          for (Object o : coll) {
            vector.add(convert(o, type.vectorOf));
          }
          value = vector;
        } else if (type.arrayOf != null) {
          if (value instanceof String && type.arrayOf.equals("byte")) {
            value = Base64.decodeBase64((String) value);
          } else {
            Collection<Object> coll = (Collection<Object>) value;
            Object array = Array.newInstance(getClass(type.arrayOf), coll.size());
            int n = 0;
            for (Object o : coll) {
              Array.set(array, n++, convert(o, type.arrayOf));
            }
            value = array;
          }
        } else {
          throw new IllegalArgumentException(
              "Key " + key + " has type but neither scalar, vectorOf, nor arrayOf is set");
        }
      }
      ht.put(key, value);
    }
    return ht;
  }