Author: gcrieloue Date: 2010-01-23 15:27:12 +0100 (Sat, 23 Jan 2010) New Revision: 12 Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/ trunk/src/main/java/org/nuiton/mapstoragemanager/core/Core.java trunk/src/main/java/org/nuiton/mapstoragemanager/core/MainTestCore.java trunk/src/main/java/org/nuiton/mapstoragemanager/core/Messages.java trunk/src/main/java/org/nuiton/mapstoragemanager/core/PluginLoader.java trunk/src/main/java/org/nuiton/mapstoragemanager/core/messages.properties trunk/src/main/java/org/nuiton/mapstoragemanager/core/package-info.java trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/ trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/BigTable.java trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/package-info.java Log: ajouts de l'architecture ?\195?\160 plugins (echec lors du commit pr?\195?\169c?\195?\169dent) Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/Core.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/core/Core.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/core/Core.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,92 @@ +package org.nuiton.mapstoragemanager.core; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; + +import org.nuiton.mapstoragemanager.plugins.BigTable; + +public class Core { + /** + * A map "name of the plugin" - "plugin instance". + */ + private HashMap < String, BigTable > bases = + new HashMap < String, BigTable > (); + + /** + * The plugin loader. + */ + private PluginLoader pluginLoader = null; + + /** + * Class constructor. + */ + public Core() { + try { + File directory = new File( + Messages.getString("Config.url")); + pluginLoader = new PluginLoader( + new URL[]{directory.toURI().toURL()}, + this.getClass().getClassLoader()); + } catch (Exception e1) { + e1.printStackTrace(); + } + try { + loadAvailableBases(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Loads all the plugins (IBigTable implementations). + * @throws IOException if the classes are not loaded correctly + */ + private void loadAvailableBases() throws IOException { + for (Class < ? > tmpClass : pluginLoader.getPluginsClasses()) { + for (Class < ? > i : tmpClass.getInterfaces()) { + /* + * If the class implements the IBigTable + * interface, an instance is created + * and added to the list + */ + if (i.getName().equals("plugins.IBigTable")) { + try { + bases.put(tmpClass.getName(), + (BigTable) tmpClass.newInstance()); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } + } + + /** + * Returns an ArrayList of all the plugins instances. + * @return an ArrayList of all the plugins instances + */ + public final ArrayList < BigTable > getAvailableBases() { + ArrayList < BigTable > bigTables = new ArrayList < BigTable > (); + for (BigTable bigTable : bases.values()) { + bigTables.add(bigTable); + } + return bigTables; + } + + /** + * Returns an ArrayList of all the plugins class names. + * @return an ArrayList of all the plugins class names + */ + public final ArrayList < String > listPlugins() { + ArrayList < String > s = new ArrayList<String>(); + for (String plugin : this.bases.keySet()) { + s.add(plugin); + } + return s; + } +} Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/MainTestCore.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/core/MainTestCore.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/core/MainTestCore.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,29 @@ +package org.nuiton.mapstoragemanager.core; + +import org.nuiton.mapstoragemanager.plugins.BigTable; + +public class MainTestCore { + + + /** + * test for plugins loading and basic functions. + */ + public static void main(String[] args) { + + Core core = new Core(); + + System.out.println("Plugins disponibles"); + for (String base : core.listPlugins()) { + System.out.println("* " + base); + } + + /* Test du plugins */ + + for (BigTable bigTable : core.getAvailableBases()) { + bigTable.put("clé", "valeur"); + System.out.println(bigTable.get("clé")); + } + + } + +} Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/Messages.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/core/Messages.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/core/Messages.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,42 @@ +package org.nuiton.mapstoragemanager.core; + +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +/** + * Get strings from an external file. + * @author Crieloue Gilles + * + */ +public final class Messages { + + /** + * bundle name. + */ + private static final String BUNDLE_NAME = "core.messages"; //$NON-NLS-1$ + + /** + * ressource bundle. + */ + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle + .getBundle(BUNDLE_NAME); + + /** + * Constructor. + */ + private Messages() { + } + + /** + * Returns a result according to a key. + * @param key the key + * @return the matching result + */ + public static String getString(final String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } +} Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/PluginLoader.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/core/PluginLoader.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/core/PluginLoader.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,161 @@ +package org.nuiton.mapstoragemanager.core; + +import java.io.File; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.logging.Logger; + +/** + * This class loads the plugins. + * @author Crieloue Gilles + * + */ +public class PluginLoader extends URLClassLoader { + + /** + * A logger. + */ + private Logger logger = Logger.getLogger(this.getClass().getName()); + + /** + * The plugin classes. + */ + private ArrayList < Class < ? > > classes = + new ArrayList < Class < ? > > (); + + /** + * Class constructor. + * @param urls for the class loader + * @param classLoader the parent ClassLoader + * @throws Exception if the directory URL doesn't exist + */ + public PluginLoader(final URL[] urls, final ClassLoader classLoader) + throws Exception { + super(urls, classLoader); + + File directory = new File(urls[0].getFile()); + if (!directory.exists()) { + throw new Exception("No directory " + urls[0]); + } + + // adds all the directory jars to the ClassLoader + for (File file : directory.listFiles()) { + if (file.getAbsolutePath().endsWith(".jar")) { + try { + this.addURL(file.toURI().toURL()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + } + + + File libs = new File("plugins/libs"); + if (!libs.exists()) { + throw new Exception("No directory " + libs.getAbsolutePath()); + } + + // adds all the libs jars to the ClassLoader + for (File file : libs.listFiles()) { + if (file.getAbsolutePath().endsWith(".jar")) { + try { + logger.info("== LIB " + file.getAbsolutePath() + "..."); + this.addURL(file.toURI().toURL()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + } + + // loads the jars classes + for (File file : directory.listFiles()) { + if (file.getAbsolutePath().endsWith(".jar")) { + logger.info("== JAR " + file.getAbsolutePath() + "..."); + loadJarClasses(file.getAbsolutePath()); + } + } + } + + /** + * Loads all the classes of a jar. + * (adds the path to the classLoader + * and adds the class to the classes list) + * @param jarPath the jar path + */ + private void loadJarClasses(final String jarPath) { + + File file = new File(jarPath); + logger.info("Loading " + jarPath + "..."); + + JarFile jar = null; + try { + jar = new JarFile(file.getAbsolutePath()); + } catch (IOException e) { + e.printStackTrace(); + } + +// Enumeration < JarEntry > entries2 = jar.entries(); +// while (entries2.hasMoreElements()) { +// JarEntry entry = entries2.nextElement(); +// if (entry.getName().startsWith("libs/")) { +// try { +// String fileName = entry.getName(); +// String entryURL = "jar:jar:file:" +// + jarPath + "!/" + fileName + "!/"; +// logger.info("== LIB " + fileName +// + " (" + entryURL + ") ..."); +// this.addURL(new URL(entryURL)); +// } catch (MalformedURLException e) { +// e.printStackTrace(); +// } +// } +// } + + Enumeration < JarEntry > entries = jar.entries(); + + // adds the classes from the jar to the classes list + while (entries.hasMoreElements()) { + Class < ? > tmpClass = null; + JarEntry entry = entries.nextElement(); + String fileName = entry.toString(); + String extension = ".class"; + + if (fileName.endsWith(extension)) { + logger.info("Loading " + fileName + "..."); + + // formating file name + fileName = fileName.substring(0, fileName.length() + - extension.length()); + fileName = fileName.replaceAll("/", "."); + + // loading class + try { + tmpClass = this.loadClass(fileName); + //Class.forName(fileName, true, this); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + classes.add(tmpClass); + } + } + + + } + + /** + * Returns the plugin classes. + * @return the plugin classes + */ + public final ArrayList < Class < ? > > getPluginsClasses() { + return classes; + } + +} Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/messages.properties =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/core/messages.properties (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/core/messages.properties 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1 @@ +Config.url=plugins/ Added: trunk/src/main/java/org/nuiton/mapstoragemanager/core/package-info.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/core/package-info.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/core/package-info.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,5 @@ +/** + * The core architecture. + * It deals with plugins discovery, loading, and such. + */ +package org.nuiton.mapstoragemanager.core; Added: trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/BigTable.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/BigTable.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/BigTable.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,47 @@ +package org.nuiton.mapstoragemanager.plugins; + +import java.util.Set; + +/** + * The interface for all the BigTable implementations. + * @author Crieloue Gilles + * + */ +public interface BigTable { + + /** + * Connects to the database. + * @param host the server + * @param base the base name + * @param username the user login + * @param password the user password + */ + void connect(String host, String base, String username, String password); + + /** + * Selects the table. + * @param table the table name + */ + void selectTable(String table); + + /** + * Get the value matching a key. + * @param key the key + * @return the value + */ + String get(String key); + + /** + * put a value matching the key. + * @param key the key + * @param value the value + */ + void put(String key, String value); + + /** + * Returns the keys set. + * @return the keys set + */ + Set < String > getKeys(); + +} Added: trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/package-info.java =================================================================== --- trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/package-info.java (rev 0) +++ trunk/src/main/java/org/nuiton/mapstoragemanager/plugins/package-info.java 2010-01-23 14:27:12 UTC (rev 12) @@ -0,0 +1,5 @@ +/** + * The plugins interfaces. + */ +package org.nuiton.mapstoragemanager.plugins; +