private void addAuthenticationRealm(Metadata metadata) throws XPathExpressionException {


        String jdbcRealmBinding = metadata.getRuntimeParameter("glassfish3", "auth-realm.database", null);
        if (jdbcRealmBinding == null) {
            return;
        }

        // Verify that the database binding exists
        Resource resource = metadata.getResource(jdbcRealmBinding);
        if (resource == null || !(resource instanceof Database)) {
            throw new RuntimeException("Database binding '" + jdbcRealmBinding + "' declared for RuntimeParameter " +
                    "glassfish3" + "#" + "auth-realm.database" + " does not exist! Existing Resources " + metadata.getResources().keySet());
        }

        String realmName = "authentication-realm";
        String groupsTable = "cb_groups";
        String usersTable = "cb_users";
        String usernameColumn = "username";
        String passwordColumn = "password";
        String groupNameColumn = "groupname";
        String encodingAlgorithm = "SHA-256";
        String encoding = "Base64";

        logger.info("Insert JdbcRealm '" + realmName + "' associated to database '" + jdbcRealmBinding + "'");

        Element authRealm = xmlDocument.createElement("auth-realm");
        authRealm.setAttribute("name", realmName);
        authRealm.setAttribute("classname", "com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm");
        // jaas-context:jdbcRealm must match an enentry in login.conf
        addProperty("jaas-context", "jdbcRealm", authRealm);
        addProperty("password-column", passwordColumn, authRealm);
        addProperty("datasource-jndi", "jdbc/" + jdbcRealmBinding, authRealm);
        addProperty("group-table", groupsTable, authRealm);
        addProperty("user-table", usersTable, authRealm);
        addProperty("group-name-column", groupNameColumn, authRealm);
        addProperty("digestrealm-password-enc-algorithm", encodingAlgorithm, authRealm);
        addProperty("digest-algorithm", encodingAlgorithm, authRealm);
        addProperty("encoding", encoding, authRealm);
        addProperty("user-name-column", usernameColumn, authRealm);

        Element securityService = XmlUtils.getUniqueElement(xmlDocument, "domain/configs/config[@name='server-config']/security-service");
        securityService.appendChild(authRealm);

    }