[tracker/direct-access: 35/121] libtracker-sparql: Added plugin loader class



commit a393b76cf1edc797acc122e0716fc0031f7ccf9f
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Jul 13 12:23:09 2010 +0100

    libtracker-sparql: Added plugin loader class
    
    Now we can dynamically load the direct access library. For now, the
    environment variable TRACKER_SPARQL_MODULE_PATH must be set though.
    
    The Tracker.Sparql.Lookup class is named so because .Query is already
    taken for now.

 src/libtracker-direct/Makefile.am                |    2 +-
 src/libtracker-direct/tracker-connection.vala    |    5 +
 src/libtracker-sparql/Makefile.am                |   11 ++-
 src/libtracker-sparql/tracker-plugin-loader.vala |  134 ++++++++++++++++++++++
 src/libtracker-sparql/tracker-query.vala         |   27 +++++
 tests/functional-tests/direct-test.vala          |   24 +++--
 6 files changed, 190 insertions(+), 13 deletions(-)
---
diff --git a/src/libtracker-direct/Makefile.am b/src/libtracker-direct/Makefile.am
index 5c9d4cc..935c0fa 100644
--- a/src/libtracker-direct/Makefile.am
+++ b/src/libtracker-direct/Makefile.am
@@ -23,7 +23,7 @@ libtracker_direct_ TRACKER_API_VERSION@_la_SOURCES = 	\
 	$(libtracker_direct_la_VALASOURCES:.vala=.c)
 
 libtracker_direct_ TRACKER_API_VERSION@_la_LDFLAGS = 	\
-	-version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) 
+	-shared -fPIC -module -avoid-version
 
 #	-export-symbols-regex '^tracker_.*'
 
diff --git a/src/libtracker-direct/tracker-connection.vala b/src/libtracker-direct/tracker-connection.vala
index 444bb85..f3bbbb9 100644
--- a/src/libtracker-direct/tracker-connection.vala
+++ b/src/libtracker-direct/tracker-connection.vala
@@ -40,3 +40,8 @@ public class Tracker.Direct.Connection : Tracker.Sparql.Connection {
 		return cursor;
 	}
 }
+
+public Tracker.Sparql.Connection module_init (Tracker.Sparql.PluginLoader loader) {
+	Tracker.Sparql.Connection plugin = new Tracker.Direct.Connection ();
+	return plugin;
+}
diff --git a/src/libtracker-sparql/Makefile.am b/src/libtracker-sparql/Makefile.am
index 153be2f..7a3411b 100644
--- a/src/libtracker-sparql/Makefile.am
+++ b/src/libtracker-sparql/Makefile.am
@@ -13,9 +13,11 @@ lib_LTLIBRARIES = libtracker-sparql- TRACKER_API_VERSION@.la
 
 libtracker_sparqlincludedir = $(includedir)/tracker-$(TRACKER_API_VERSION)/libtracker-sparql
 
-libtracker_sparql_la_VALASOURCES = \
+libtracker_sparql_la_VALASOURCES = 			\
 	tracker-connection.vala				\
-	tracker-cursor.vala
+	tracker-cursor.vala             		\
+	tracker-query.vala              		\
+	tracker-plugin-loader.vala
 
 libtracker_sparql_ TRACKER_API_VERSION@_la_SOURCES = 	\
 	libtracker-sparql.vala.stamp			\
@@ -36,9 +38,12 @@ vapi_DATA =                     			\
 
 # Vala sources
 libtracker-sparql.vala.stamp: $(libtracker_sparql_la_VALASOURCES)
-	$(AM_V_GEN)$(VALAC) $(GCOV_VALAFLAGS) -C $(VALAFLAGS) --pkg gio-2.0 -H tracker-sparql.h --vapi tracker-sparql-$(TRACKER_API_VERSION).vapi $^
+	$(AM_V_GEN)$(VALAC) $(GCOV_VALAFLAGS) -C $(VALAFLAGS) --pkg gio-2.0 --pkg gmodule-2.0 -H tracker-sparql.h --vapi tracker-sparql-$(TRACKER_API_VERSION).vapi $^
 	$(AM_V_GEN)touch $@
 
+libtracker_sparql_ TRACKER_API_VERSION@_la_LIBADD = 	\
+	$(GLIB2_LIBS)
+
 BUILT_SOURCES = 					\
 	libtracker-sparql.vala.stamp
 
diff --git a/src/libtracker-sparql/tracker-plugin-loader.vala b/src/libtracker-sparql/tracker-plugin-loader.vala
new file mode 100644
index 0000000..73abbf6
--- /dev/null
+++ b/src/libtracker-sparql/tracker-plugin-loader.vala
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2010, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+public class Tracker.Sparql.PluginLoader : Object {
+	static bool initialized = false;
+	static Tracker.Sparql.Connection direct = null;
+	static Tracker.Sparql.Connection bus = null;
+
+	private delegate Tracker.Sparql.Connection ModuleInitFunc (PluginLoader loader);
+
+	public PluginLoader ()
+	requires (!initialized) {
+		if (!Module.supported ()) {
+		    return;
+		}
+
+		if (!load_plugins ()) {
+			initialized = false;
+			return;
+		}
+
+		initialized = true;
+	}
+
+	// Plugin loading functions
+	private bool load_plugins () {
+		string env_path = Environment.get_variable ("TRACKER_SPARQL_MODULE_PATH");
+		string path;
+		
+		if (env_path != null && env_path.length > 0) {
+			path = env_path; 
+		} else {
+			// FIXME: Get from config
+			path = "/tmp";
+		}
+
+		File dir = File.new_for_path (path);
+		string dir_path = dir.get_path ();
+		
+		debug ("Searching for modules in folder '%s' ..", dir_path);
+
+		// First get direct library details
+		string direct_path = Module.build_path (dir_path, "tracker-direct-0.9");
+		direct = load_plugins_from_path (direct_path);
+
+		// Second get bus library details
+		//string bus_path = Module.build_path (path, "libtracker-bus");
+
+		debug ("Finished searching for modules in folder '%s'", dir_path);
+
+		// FIXME: Finish this by checking for bus too!
+		return direct != null;
+	}
+
+	private Tracker.Sparql.Connection? load_plugins_from_path (string path) {
+		File file = File.new_for_path (path);
+		assert (file != null);
+
+		FileInfo info = null;
+
+		try {
+			string attributes = FILE_ATTRIBUTE_STANDARD_NAME + "," +
+			                    FILE_ATTRIBUTE_STANDARD_TYPE + "," +
+			                    FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
+
+			info = file.query_info (attributes,
+			                        FileQueryInfoFlags.NONE,
+			                        null);
+		} catch (Error e) {
+			warning ("Could not get GFileInfo for '%s'", path);
+			return null;
+		}
+
+		string content_type = info.get_content_type ();
+		weak string mime = g_content_type_get_mime_type (content_type);
+		string expected_mime = "application/x-sharedlib";
+		
+		if (mime != expected_mime) {
+			warning ("Could not load plugin, mime type was '%s', expected:'%s'", 
+			         mime,
+			         expected_mime);
+			return null;
+		}
+
+		Module module = Module.open (path, ModuleFlags.BIND_LOCAL);
+		if (module == null) {
+			warning ("Failed to load module from path '%s': %s",
+			         path,
+			         Module.error ());
+			return null;
+		}
+
+		void *function;
+
+		if (!module.symbol ("module_init", out function)) {
+			warning ("Failed to find entry point function '%s' in '%s': %s",
+			         "module_init",
+			         path,
+			         Module.error ());
+
+			return null;
+		}
+
+		ModuleInitFunc module_init = (ModuleInitFunc) function;
+		assert (module_init != null);
+
+		// We don't want our modules to ever unload
+		module.make_resident ();
+
+		// Call module init function
+		Tracker.Sparql.Connection c = module_init (this);
+
+		debug ("Loaded module source: '%s'", module.name ());
+
+		return c;
+	}
+}
+
diff --git a/src/libtracker-sparql/tracker-query.vala b/src/libtracker-sparql/tracker-query.vala
new file mode 100644
index 0000000..340441a
--- /dev/null
+++ b/src/libtracker-sparql/tracker-query.vala
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2010, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+public class Tracker.Sparql.Lookup : Object {
+	private PluginLoader plugin_loader;
+
+	public Lookup () {
+		debug ("Starting plugin loader");
+		plugin_loader = new PluginLoader ();
+	}
+}
diff --git a/tests/functional-tests/direct-test.vala b/tests/functional-tests/direct-test.vala
index 3269396..36c5243 100644
--- a/tests/functional-tests/direct-test.vala
+++ b/tests/functional-tests/direct-test.vala
@@ -5,18 +5,24 @@ using Tracker.Direct;
 int
 main( string[] args )
 {
-	Sparql.Connection con = new Direct.Connection ();
-	Cursor cursor = con.query ("SELECT ?u WHERE { ?u a rdfs:Class }");
+//	Sparql.Connection con = new Direct.Connection ();
+//	Cursor cursor = con.query ("SELECT ?u WHERE { ?u a rdfs:Class }");
 
-	while (cursor.iter_next()) {
-		int i;
+//	while (cursor.iter_next()) {
+//		int i;
 
-		for (i = 0; i < cursor.n_columns; i++) {
-			print ("%s%s", i != 0 ? ",":"", cursor.get_string (i));
-		}
+//		for (i = 0; i < cursor.n_columns; i++) {
+//			print ("%s%s", i != 0 ? ",":"", cursor.get_string (i));
+//		}
 
-		print ("\n");
-	}
+//		print ("\n");
+//	}
+	
+	// Testing new API with GModule
+	
+	print ("\n\n");
+
+	Lookup foo = new Lookup ();
 
 	return( 0 );
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]