svn commit: samba r17635 - in branches/SOC/mkhl/ejs-map: .

mkhl at samba.org mkhl at samba.org
Sun Aug 20 23:07:37 GMT 2006


Author: mkhl
Date: 2006-08-20 23:07:36 +0000 (Sun, 20 Aug 2006)
New Revision: 17635

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=17635

Log:
Sketch out the ejs mapping module a little further.
Martin
Modified:
   branches/SOC/mkhl/ejs-map/ejs_map.c
   branches/SOC/mkhl/ejs-map/entryUUID.js


Changeset:
Modified: branches/SOC/mkhl/ejs-map/ejs_map.c
===================================================================
--- branches/SOC/mkhl/ejs-map/ejs_map.c	2006-08-20 21:58:02 UTC (rev 17634)
+++ branches/SOC/mkhl/ejs-map/ejs_map.c	2006-08-20 23:07:36 UTC (rev 17635)
@@ -41,43 +41,50 @@
 
 	/* validate arguments */
 	if (argc < 2) {
-		ejsSetErrorMsg(eid, "ldbMap invalid number of arguments");
-		goto failed;
+		ejsSetErrorMsg(eid, "ldbMap too few arguments");
+		return -1;
 	}
-
 	if (!mprVarIsNumber(argv[0]) || !mprVarIsString(argv[1])) {
 		ejsSetErrorMsg(eid, "ldbMap invalid arguments");
-		goto failed;
+		return -1;
 	}
-
 	/* validate more arguments */
 	ret = ejs_map_check_argc(mprToInt(argv[0]), argc);
 	if (ret) {
-		goto failed;
+		return ret;
 	}
 
 	/* create object */
-	this = mprObject(mprToString(argv[1]));
-	mprSetVar(this, "type", argv[0]);
-	mprSetVar(this, "local", argv[1]);
+	this = mprInitObject(eid, mprToString(argv[1]), 0, NULL);
 
+	/* create attributes */
+	mprSetProperty(this, "type", argv[0]);
+	mprSetProperty(this, "local", argv[1]);
 	if (argc > 2) {
-		mprSetVar(this, "remote", argv[2]);
+		mprSetProperty(this, "remote", argv[2]);
 	}
 	if (argc > 3) {
-		mprSetVar(this, "to_local", argv[4]);
-		mprSetVar(this, "to_remote", argv[5]);
-		mprSetVar(this, "to_tree", argv[6]);
+		mprSetProperty(this, "to_local", argv[4]);
+		mprSetProperty(this, "to_remote", argv[5]);
+		mprSetProperty(this, "to_tree", argv[6]);
 	}
 
-	mprReturn(eid, this);
 	return 0;
+}
 
-failed:
-	return -1;
+static int ejs_map_set_ldb(MprVarHandle eid, struct ldb_context *ldb)
+{
+	struct MprVar *var;
+
+	ejs_ldb_init(eid, 0, NULL);
+	var = ejsGetReturnValue(eid);
+	mprSetPtrChild(var, "db", ldb);
+
+	return 0;
 }
 
-static struct ldb_parse_tree *ejs_map_convert_operator(const struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, const struct ldb_parse_tree *tree)
+/* TODO:
+static int ejs_map_convert_operator(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, struct ldb_parse_tree **new, const struct ldb_parse_tree *tree)
 {
 	struct MprVar *this, ejs_tree;
 	struct MprArray *args;
@@ -85,39 +92,44 @@
 	int ret;
 
 	this = (struct MprVar *)map->private_data;
+	mprSetProperty(ejsGetLocalObject(eid), "this", this);
 
 	ejs_tree = mprLdbParseTree(module->ldb, tree);
 	args = mprCreateArray();
-	mprAddToArray(args, (void *)ejs_tree);
+	mprAddToArray(args, (void *)&ejs_tree);
 
 	ret = ejsRunFunction(eid, this, "to_tree", args);
 	if (ret) {
+		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldbMap: conversion of parse tree failed!");
 		return NULL;
-		// XXX: Scream and yell!
 	}
 
 	result = mprToLdbParseTree(mem_ctx, ejsGetReturnValue(eid));
 
 	return result;
 }
+*/
 
 static struct ldb_val ejs_map_convert_local(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, const struct ldb_val *val)
 {
+	/* XXX: eid must be stored in context or something... */
 	struct MprVar *this, ejs_val;
 	struct MprArray *args;
 	struct ldb_val result;
 	int ret;
 
 	this = (struct MprVar *)map->private_data;
+	mprSetProperty(ejsGetLocalObject(eid), "this", this);
+	ejs_map_set_ldb(eid, module->ldb);
 
-	ejs_val = mprData(val->data, val->length);
+	ejs_val = mprLdbVal(val);
 	args = mprCreateArray();
-	mprAddToArray(args, (void *)ejs_val);
+	mprAddToArray(args, (void *)&ejs_val);
 
 	ret = ejsRunFunction(eid, this, "to_remote", args);
 	if (ret) {
+		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldbMap: conversion to remote value failed!");
 		return NULL;
-		// XXX: Scream and yell!
 	}
 
 	result = mprToLdbVal(ejsGetReturnValue(eid));
@@ -127,21 +139,24 @@
 
 static struct ldb_val ejs_map_convert_remote(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, const struct ldb_val *val)
 {
+	/* XXX: eid must be stored in context or something... */
 	struct MprVar *this, ejs_val;
 	struct MprArray *args;
 	struct ldb_val result;
 	int ret;
 
 	this = (struct MprVar *)map->private_data;
+	mprSetProperty(ejsGetLocalObject(eid), "this", this);
+	ejs_map_set_ldb(eid, module->ldb);
 
-	ejs_val = mprData(val->data, val->length);
+	ejs_val = mprLdbVal(val);
 	args = mprCreateArray();
-	mprAddToArray(args, (void *)ejs_val);
+	mprAddToArray(args, (void *)&ejs_val);
 
 	ret = ejsRunFunction(eid, this, "to_local", args);
 	if (ret) {
+		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ldbMap: conversion to local value failed!");
 		return NULL;
-		// XXX: Scream and yell!
 	}
 
 	result = mprToLdbVal(ejsGetReturnValue(eid));
@@ -149,6 +164,7 @@
 	return result;
 }
 
+/* TODO:
 static struct ldb_message_element *ejs_map_generate_local(struct ldb_module *module, void *mem_ctx, const struct ldb_map_attribute *map, const char *remote_attr,const struct ldb_message *remote_message)
 {
 	struct MprVar *this, ejs_attr, ejs_msg;
@@ -174,13 +190,16 @@
 
 	return result;
 }
+*/
 
+/* TODO:
 static void ejs_map_generate_remote(struct ldb_module *module, const struct ldb_map_attribute *map, const char *local_attr, const struct ldb_message *old_message, struct ldb_message *remote_message, struct ldb_message *local_message)
 {
 	return;
 }
+*/
 
-static struct ldb_map_attribute ejs_decode_map(void *mem_ctx, struct MprVar *ejsMap)
+static struct ldb_map_attribute *ejs_map_decode(struct ldb_module *module, struct MprVar *ejsMap)
 {
 	struct ldb_map_attribute *map;
 	struct MprVar *var;
@@ -189,15 +208,15 @@
 	/* prepare mapping structure */
 	map = talloc_zero(mem_ctx, struct ldb_map_attribute);
 	if (map == NULL) {
+		ldb_oom(module->ldb);
 		return NULL;
-		// XXX: Scream and yell!
 	}
 
 	/* fetch map type */
 	var = mprGetProperty(ejsMap, "type", NULL);
 	if (var == NULL) {
+		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ejs_map_decode: Failed to fetch attribute 'type' from ldbMap object.")
 		return NULL;
-		// XXX: Scream and yell!
 	}
 	type = mprToInt(var);
 	map->type = type;
@@ -205,14 +224,14 @@
 	/* fetch local name */
 	var = mprGetProperty(ejsMap, "local", NULL);
 	if (var == NULL) {
+		ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ejs_map_decode: Failed to fetch attribute 'local' from ldbMap object.")
 		return NULL;
-		// XXX: Scream and yell!
 	}
 	map->local_name = mprToString(var);
 
 	/* fetch convert_operator */
 	var = mprGetProperty(ejsMap, "to_tree", NULL);
-	if (var) {
+	if (var) {			/* no convert_operator is ok */
 		map->convert_operator = ejs_map_convert_operator;
 	}
 
@@ -220,39 +239,39 @@
 	switch (type) {
 	case MAP_GENERATE:
 		var = mprGetProperty(ejsMap, "to_local", NULL);
-		if (var) {
-			map->generate_local = ejs_map_generate_local;
+		if (var) {		/* no generator is ok */
+			map->u.generate.generate_local = ejs_map_generate_local;
 		}
 
 		var = mprGetProperty(ejsMap, "to_remote", NULL);
-		if (var) {
-			map->generate_remote = ejs_map_generate_remote;
+		if (var) {		/* no generator is ok */
+			map->u.generate.generate_remote = ejs_map_generate_remote;
 		}
 		
-		var = mprGetProperty(ejsMap, "remote_names", NULL);
+		var = mprGetProperty(ejsMap, "remote", NULL);
 		if (var == NULL) {
+			ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ejs_map_decode: Failed to fetch attribute 'remote' from ldbMap object.")
 			return NULL;
-			// XXX: Scream and yell!
 		}
-		map->u.generate.remote_names = mprToArray(mem_ctx, var);
+		map->u.generate.remote_names = mprToArray(map, var);
 		break;
 
 	case MAP_CONVERT:
 		var = mprGetProperty(ejsMap, "to_remote", NULL);
-		if (var) {
-			map->convert_local = ejs_map_convert_local;
+		if (var) {		/* no converter is ok */
+			map->u.convert.convert_local = ejs_map_convert_local;
 		}
 
 		var = mprGetProperty(ejsMap, "to_local", NULL);
-		if (var) {
-			map->convert_remote = ejs_map_convert_remote;
+		if (var) {		/* no converter is ok */
+			map->u.convert.convert_remote = ejs_map_convert_remote;
 		}
 		/* fall through to: */
 	case MAP_RENAME:
 		var = mprGetProperty(ejsMap, "remote", NULL);
 		if (var == NULL) {
+			ldb_debug(module->ldb, LDB_DEBUG_ERROR, "ejs_map_decode: Failed to fetch attribute 'remote' from ldbMap object.")
 			return NULL;
-			// XXX: Scream and yell!
 		}
 		map->u.rename.remote_name = mprToString(var);
 		break;
@@ -263,11 +282,109 @@
 	}
 
 	map->private_data = (void *)ejsMap;
+
+	return map;
 }
 
-/* Initialize ejs bindings for creating ldb mappings. */
-NTSTATUS smb_setup_ejs_ldbMap(void)
+/* XXX: ?!? */
+static void smbscript_ejs_exception(const char *reason)
 {
+	Ejs *ep = ejsPtr(eid);		/* um, won't work... */
+	ejsSetErrorMsg(eid, "%s", reason);
+	fprintf(stderr, "%s", ep->error);
+	exit(127);			/* um, no... */
+}
+
+/* the context init function */
+static int ejs_map_init(struct ldb_module *module)
+{
+	EjsHandle handle = 0;
+	MprVarHandle eid;
+	void *mem_ctx = talloc_new(module);
+	char *script;
+	size_t script_size;
+	struct MprVar *ejsMaps;
+	//struct MprVar *var, *ejsMaps;
+	//const char *name;
+	struct ldb_map_attribute *maps;
+        int i, length, ret;
+
+	/* initialize ejs engine */
+	mprSetCtx(mem_ctx);
+	if (ejsOpen(NULL, NULL, NULL) != 0) {
+		ldb_set_errstring(module->ldb, "ejs_map: ejsOpen(): unable to initialise EJ subsystem\n");
+		goto failed;
+	}
+	smb_setup_ejs_functions(smbscript_ejs_exception); /* XXX: ?!? */
+	if ((eid = ejsOpenEngine(handle, 0)) == (EjsId)-1) {
+		ldb_set_errstring(module->ldb, "ejs_map: ejsOpenEngine(): unable to initialise an EJS engine\n");
+		goto failed;
+	}
+	/* TODO: register ejsClose as mem_ctx destructor? */
+
+	/* setup ejs bindings */
 	ejsDefineCFunction(-1, "ldbMap", ejs_map, NULL, MPR_VAR_SCRIPT_HANDLE);
-	return NT_STATUS_OK;
+
+	/* setup ejs mappings */
+	script = file_load("/path/to/map.js", &script_size, mem_ctx);
+	if (ejsEvalScript(eid, script, ...) == -1) {
+		ldb_set_errstring(module->ldb, talloc_asprintf(module, "ejs_map: ejsEvalScript(): %s\n", emsg));
+		goto failed;
+	}
+
+	/* decode ejs mappings */
+	//var = ejsGetReturnValue(eid);
+	//name = mprToString(mprGetProperty(var, "name", NULL));
+	//ejsMaps = mprGetProperty(var, "maps", NULL);
+	ejsMaps = ejsGetReturnValue(eid);
+	if (ejsMaps == NULL) {
+		ldb_set_errstring(module->ldb, "ejs_map: no ldbMap objects defined\n");
+		goto failed;
+	}
+	// TODO: misc. checks on ejsMaps
+	length = mprToInt(mprGetProperty(ejsMaps, "length", NULL));
+	maps = talloc_array(module, struct ldb_map_attribute, length);
+	for (i = 0; i < length; i++) {
+		// TODO: check this array iteration code
+		char idx[16];
+		mprItoa(i, idx, sizeof(idx));
+		struct MprVar *map = mprGetProperty(ejsMaps, idx, NULL);
+		maps[i] = ejs_map_decode(module, map);
+	}
+
+	/* call ldb_map init */
+	ret = ldb_map_init(module, maps, NULL, name);
+        if (ret != LDB_SUCCESS) {
+		talloc_free(mem_ctx);
+                return ret;
+	}
+
+        ret = ldb_next_init(module);
+	if (ret != LDB_SUCCESS) {
+		talloc_free(mem_ctx);
+	}
+	return ret;
+
+failed:
+	talloc_free(mem_ctx);
+	return LDB_ERR_OPERATIONS_ERROR;
 }
+
+static struct ldb_module_ops ejs_map_ops = {
+	.name		   = "ejs_map",
+	.init_context	   = ejs_map_init,
+};
+
+/* the init function */
+int ldb_ejs_map_module_init(void)
+{
+	struct ldb_module_ops ops = ldb_map_get_ops();
+	ejs_map_ops.add		= ops.add;
+	ejs_map_ops.modify	= ops.modify;
+	ejs_map_ops.del		= ops.del;
+	ejs_map_ops.rename	= ops.rename;
+	ejs_map_ops.search	= ops.search;
+	ejs_map_ops.wait	= ops.wait;
+
+	return ldb_register_module(&ejs_map_ops);
+}

Modified: branches/SOC/mkhl/ejs-map/entryUUID.js
===================================================================
--- branches/SOC/mkhl/ejs-map/entryUUID.js	2006-08-20 21:58:02 UTC (rev 17634)
+++ branches/SOC/mkhl/ejs-map/entryUUID.js	2006-08-20 23:07:36 UTC (rev 17635)
@@ -49,4 +49,4 @@
 var whenChanged = new ldbMapRename("whenChanged", "modifyTimestamp");
 var wildcard = new ldbMapKeep("*");
 
-init_maps(new Array(objectGuid, objectSid, whenCreated, whenChanged, wildcard));
+return new Array(objectGuid, objectSid, whenCreated, whenChanged, wildcard);



More information about the samba-cvs mailing list