private JsonNode encodeSettings(Map<String, Object> p) throws JSONException {
   SettingDefinitionEncoder enc = new SettingDefinitionEncoder();
   Set<SettingDefinition<?, ?>> defs =
       getDatasource()
           .getChangableSettingDefinitions(
               getDatasource().getDatasourceProperties(getSettings(), p));
   for (SettingDefinition<?, ?> def : defs) {
     setDefaultValue(def, p.get(def.getKey()));
   }
   JsonNode settings = enc.encode(enc.sortByGroup(defs));
   ObjectNode node = JSONUtils.nodeFactory().objectNode();
   node.put(SETTINGS, settings);
   return node;
 }
 protected Map<String, Object> parseDatasourceSettings(
     Set<SettingDefinition<?, ?>> defs, HttpServletRequest req) {
   Map<String, String> parameters = new HashMap<String, String>(req.getParameterMap().size());
   Enumeration<?> e = req.getParameterNames();
   while (e.hasMoreElements()) {
     String key = (String) e.nextElement();
     parameters.put(key, req.getParameter(key));
   }
   Map<String, Object> parsedSettings = new HashMap<String, Object>(parameters.size());
   for (SettingDefinition<?, ?> def : defs) {
     SettingValue<?> newValue =
         getSettingsManager()
             .getSettingFactory()
             .newSettingValue(def, parameters.get(def.getKey()));
     parsedSettings.put(def.getKey(), newValue.getValue());
   }
   return parsedSettings;
 }
 @SuppressWarnings("unchecked")
 protected void setDefaultValue(SettingDefinition<?, ?> def, Object val) {
   if (val != null) {
     switch (def.getType()) {
       case BOOLEAN:
         SettingDefinition<?, Boolean> bsd = (SettingDefinition<?, Boolean>) def;
         bsd.setDefaultValue((Boolean) val);
         break;
       case FILE:
         SettingDefinition<?, File> fsd = (SettingDefinition<?, File>) def;
         fsd.setDefaultValue((File) val);
         break;
       case INTEGER:
         SettingDefinition<?, Integer> isd = (SettingDefinition<?, Integer>) def;
         isd.setDefaultValue((Integer) val);
         break;
       case NUMERIC:
         SettingDefinition<?, Double> dsd = (SettingDefinition<?, Double>) def;
         dsd.setDefaultValue((Double) val);
         break;
       case STRING:
         SettingDefinition<?, String> ssd = (SettingDefinition<?, String>) def;
         ssd.setDefaultValue((String) val);
         break;
       case URI:
         SettingDefinition<?, URI> usd = (SettingDefinition<?, URI>) def;
         usd.setDefaultValue((URI) val);
         break;
     }
   }
 }