[tracker/libtracker-sparql: 5/44] libtracker-sparql: Added plugin loader class



commit 4342cc3783d7ce64e8f51b97e8932e6611291b21
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-sparql/Makefile.am                |   12 ++-
 src/libtracker-sparql/tracker-plugin-loader.vala |  135 ++++++++++++++++++++++
 src/libtracker-sparql/tracker-query.vala         |   27 +++++
 3 files changed, 171 insertions(+), 3 deletions(-)
---
diff --git a/src/libtracker-sparql/Makefile.am b/src/libtracker-sparql/Makefile.am
index 2800951..08b5db8 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,13 @@ 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)					\
+	$(GIO_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..7538e1a
--- /dev/null
+++ b/src/libtracker-sparql/tracker-plugin-loader.vala
@@ -0,0 +1,135 @@
+/*
+ * 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.
+ */
+
+private 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 ();
+
+	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 (dir_path, "tracker-bus-0.9");
+		bus = load_plugins_from_path (bus_path);
+
+		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 ();
+		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 ();
+
+		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 ();
+	}
+}



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