/**
  * Loads the configuration properties in the configuration property file associated with the
  * framework installation; these properties are accessible to the framework and to bundles and are
  * intended for configuration purposes. By default, the configuration property file is located in
  * the <tt>conf/</tt> directory and is called " <tt>config.properties</tt>".
  *
  * @return A <tt>Map<String, Object></tt> instance or <tt>null</tt> if there was an error.
  */
 protected Map<String, String> loadConfigProperties(
     JsonValue configuration, URI projectDirectory) {
   JsonValue systemProperties = configuration.get(CONFIG_PROPERTIES_PROP);
   if (systemProperties.isMap()) {
     // Substitute all variables
     systemProperties = systemProperties.copy();
   } else {
     Properties props =
         loadPropertyFile(
             projectDirectory,
             systemProperties
                 .expect(String.class)
                 .defaultTo(CONFIG_PROPERTIES_FILE_VALUE)
                 .asString());
     if (props == null) return new HashMap<String, String>(0);
     // Perform variable substitution on specified properties.
     systemProperties = (new JsonValue(props, null, Arrays.asList(transformer))).copy();
   }
   Map<String, String> config = new HashMap<String, String>(systemProperties.size());
   for (Map.Entry<String, Object> entry : systemProperties.asMap().entrySet()) {
     if (entry.getValue() instanceof String) {
       // Excluce the null and non String values
       config.put(entry.getKey(), (String) entry.getValue());
     }
   }
   return config;
 }
 /**
  * TODO: Description.
  *
  * @param service
  * @param config TODO.
  * @throws JsonValueException TODO.
  */
 public Policy(SynchronizationService service, JsonValue config) throws JsonValueException {
   this.service = service;
   situation = config.get("situation").required().asEnum(Situation.class);
   JsonValue action = config.get("action").required();
   if (action.isString()) {
     this.action = action.asEnum(Action.class);
     this.script = null;
     this.scriptScope = null;
   } else {
     this.action = null;
     this.script = Scripts.newInstance("Policy", action);
     if (action.isMap() && action.asMap().size() > 2) {
       // If there is additional attributes then copy them
       scriptScope = action.copy().asMap();
       scriptScope.remove("type");
       scriptScope.remove("source");
       scriptScope.remove("file");
     } else {
       scriptScope = null;
     }
   }
   JsonValue pAction = config.get("postAction");
   if (pAction.isNull()) {
     this.postAction = null;
   } else {
     this.postAction = Scripts.newInstance("PostAction", pAction);
   }
 }
 /**
  * Loads the properties in the system property file associated with the framework installation
  * into <tt>System.setProperty()</tt>. These properties are not directly used by the framework in
  * anyway. By default, the system property file is located in the <tt>conf/</tt> directory and is
  * called "<tt>system.properties</tt>".
  */
 protected void loadSystemProperties(JsonValue configuration, URI projectDirectory) {
   JsonValue systemProperties = configuration.get(SYSTEM_PROPERTIES_PROP);
   if (systemProperties.isMap()) {
     for (Map.Entry<String, Object> entry : systemProperties.copy().asMap().entrySet()) {
       // The user.dir MUST not be overwritten!!!
       if (entry.getValue() instanceof String && !"user.dir".equals(entry.getKey())) {
         System.setProperty(entry.getKey(), (String) entry.getValue());
       }
     }
   } else {
     Properties props =
         loadPropertyFile(
             projectDirectory,
             systemProperties
                 .expect(String.class)
                 .defaultTo(SYSTEM_PROPERTIES_FILE_VALUE)
                 .asString());
     if (props == null) return;
     // Perform variable substitution on specified properties.
     for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) {
       String name = (String) e.nextElement();
       if (!"user.dir".equals(name)) {
         Object newValue = ConfigurationUtil.substVars(props.getProperty(name), propertyAccessor);
         if (newValue instanceof String) {
           System.setProperty(name, (String) newValue);
         }
       }
     }
   }
 }
  @Test
  public void testResource() throws Exception {
    ScriptName scriptName = new ScriptName("resource", getLanguageName());
    ScriptEntry scriptEntry = getScriptRegistry().takeScript(scriptName);
    Assert.assertNotNull(scriptEntry);

    Script script = scriptEntry.getScript(new RootContext());
    // Set RequestLevel Scope
    script.put("ketto", 2);
    script.putSafe("callback", mock(Function.class));

    JsonValue createContent = new JsonValue(new LinkedHashMap<String, Object>());
    createContent.put("externalId", "701984");
    createContent.put("userName", "*****@*****.**");
    createContent.put(
        "assignedDashboard", Arrays.asList("Salesforce", "Google", "ConstantContact"));
    createContent.put("displayName", "Babs Jensen");
    createContent.put("nickName", "Babs");

    JsonValue updateContent = createContent.copy();
    updateContent.put("_id", UUID.randomUUID().toString());
    updateContent.put("profileUrl", "https://login.example.com/bjensen");

    final Context context =
        new ApiInfoContext(
            new SecurityContext(new RootContext(), "*****@*****.**", null), "", "");
    script.put("context", context);

    CreateRequest createRequest = Requests.newCreateRequest("/Users", "701984", createContent);
    script.put("createRequest", createRequest);
    ReadRequest readRequest = Requests.newReadRequest("/Users/701984");
    script.put("readRequest", readRequest);
    UpdateRequest updateRequest = Requests.newUpdateRequest("/Users/701984", updateContent);
    script.put("updateRequest", updateRequest);
    PatchRequest patchRequest =
        Requests.newPatchRequest("/Users/701984", PatchOperation.replace("userName", "ddoe"));
    script.put("patchRequest", patchRequest);
    QueryRequest queryRequest = Requests.newQueryRequest("/Users/");
    script.put("queryRequest", queryRequest);
    DeleteRequest deleteRequest = Requests.newDeleteRequest("/Users/701984");
    script.put("deleteRequest", deleteRequest);
    ActionRequest actionRequest = Requests.newActionRequest("/Users", "clear");
    script.put("actionRequest", actionRequest);
    script.eval();
  }
 /**
  * Loads the boot properties in the configuration property file associated with the framework
  * installation; these properties are accessible to the framework and to bundles and are intended
  * for configuration purposes. By default, the configuration property file is located in the
  * <tt>conf/</tt> directory and is called " <tt>config.properties</tt>".
  *
  * @return A <tt>Map<String, Object></tt> instance or <tt>null</tt> if there was an error.
  */
 protected Map<String, Object> loadBootProperties(JsonValue configuration, URI projectDirectory) {
   JsonValue bootProperties = configuration.get(BOOT_PROPERTIES_PROP);
   if (bootProperties.isMap()) {
     // Substitute all variables
     return bootProperties.copy().asMap();
   } else {
     Properties props =
         loadPropertyFile(
             projectDirectory,
             bootProperties.expect(String.class).defaultTo(BOOT_PROPERTIES_FILE_VALUE).asString());
     if (props == null) return new HashMap<String, Object>(0);
     // Perform variable substitution on specified properties.
     return (new JsonValue(props, null, Arrays.asList(transformer)))
         .expect(Map.class)
         .copy()
         .asMap();
   }
 }