Author: tchemit Date: 2008-08-09 09:31:56 +0000 (Sat, 09 Aug 2008) New Revision: 818 Added: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java Removed: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProvider.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionNameProvider.java Log: simplification du code : suppression du paquetage provider Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionNameProvider.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionNameProvider.java 2008-08-06 10:53:44 UTC (rev 817) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionNameProvider.java 2008-08-09 09:31:56 UTC (rev 818) @@ -16,6 +16,13 @@ /** * Contrat pour obtenir les noms d'une action de manière dynamique. + * <p/> + * Cette méthode sera appelé par un {@link ActionProvider} lorsque la clef dans le + * fichier de mapping est :fqn (où fqn correspond à une implantation de ce contrat). + * <p/> + * Ainsi on peut associer à une action donnée plusieurs instances avec des noms différents mais avec le même comportement. + * <p/> + * Par exemple, une changement de locale où seule la locale varie (et elle sera retrouvée à partir du nom de l'action). * * @author chemit */ Copied: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java (from rev 796, trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProvider.java) =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java (rev 0) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java 2008-08-09 09:31:56 UTC (rev 818) @@ -0,0 +1,35 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.jaxx.action; + +/** + * Contract to be realized by a provider of Actions. + * <p/> + * A provider of actions + * + * @author chemit + */ +public interface ActionProvider<A extends MyAbstractAction> { + + /** @return the name of the provider */ + String getName(); + + /** @return the base classe of provided actions */ + Class<A> getBaseClass(); + + /** @return the provided actions classes */ + java.util.Map<String, Class<? extends A>> getClasses(); + +} Copied: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java (from rev 817, trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java) =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java (rev 0) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java 2008-08-09 09:31:56 UTC (rev 818) @@ -0,0 +1,142 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.jaxx.action; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import static org.codelutin.i18n.I18n._; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; +import java.util.TreeMap; + +/** @author chemit */ +public class ActionProviderFromProperties<A extends MyAbstractAction> implements ActionProvider<A> { + + /** default prefix for an entryin mapping file. */ + protected static final String ACTION_KEY_PREFIX = "action."; + + protected String propertiesPath; + + protected static Log log = LogFactory.getLog(ActionProviderFromProperties.class); + + protected String name; + + protected Class<A> baseClass; + + protected Map<String, Class<? extends A>> actions; + + + protected ActionProviderFromProperties(String name, Class<A> baseClass, String propertiesPath) { + + this.name = name; + this.baseClass = baseClass; + + this.propertiesPath = propertiesPath; + this.actions = initCache(); + } + + public String getName() { + return name; + } + + public Class<A> getBaseClass() { + return baseClass; + } + + public Map<String, Class<? extends A>> getClasses() { + return actions; + } + + @Override + public String toString() { + return super.toString() + "<name:" + name + ", baseClass:" + baseClass.getSimpleName() + ">"; + } + + protected void clearCache() { + if (actions != null) { + actions.clear(); + actions = null; + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + clearCache(); + } + + @SuppressWarnings({"unchecked"}) + protected Map<String, Class<? extends A>> initCache() { + + InputStream inputStream = null; + + Properties properties = new Properties(); + + try { + inputStream = getClass().getResourceAsStream(propertiesPath); + if (inputStream == null) { + throw new NullPointerException("could not find action file " + propertiesPath); + } + log.info("load " + propertiesPath); + properties.load(inputStream); + } catch (IOException e) { + log.warn(_("jaxx.error.load.actions.file", e.getMessage())); + throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + log.warn(_("jaxx.error.close.actions.file", e.getMessage())); + //throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); + } + } + } + + Map<String, Class<? extends A>> cache = new TreeMap<String, Class<? extends A>>(); + int prefix = ACTION_KEY_PREFIX.length(); + for (Map.Entry<Object, Object> entry : properties.entrySet()) { + String key = entry.getKey() + ""; + String qfn = entry.getValue() + ""; + try { + Class<? extends A> implCass; + implCass = (Class<? extends A>) Class.forName(qfn); + String actionKey = key.substring(prefix); + if (actionKey.startsWith(":")) { + // this is a RuntimeActionNameProvider + Class<ActionNameProvider> klazz = (Class<ActionNameProvider>) Class.forName(actionKey.substring(1)); + for (String s : klazz.newInstance().getActionCommands()) { + log.debug("found action <" + s + " : " + implCass + ">"); + cache.put(s, implCass); + } + continue; + } + log.debug("found action <" + actionKey + " : " + implCass + ">"); + cache.put(actionKey, implCass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); + } catch (IllegalAccessException e) { + throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); + } catch (InstantiationException e) { + throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); + } + } + + return cache; + } +} Deleted: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProvider.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProvider.java 2008-08-06 10:53:44 UTC (rev 817) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProvider.java 2008-08-09 09:31:56 UTC (rev 818) @@ -1,37 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. This program 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 General Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * # #% - */ -package org.codelutin.jaxx.action.provider; - -import org.codelutin.jaxx.action.MyAbstractAction; - -/** - * Contract to be realized by a provider of Actions. - * <p/> - * A provider of actions - * - * @author chemit - */ -public interface ActionProvider<A extends MyAbstractAction> { - - /** @return the name of the provider */ - String getName(); - - /** @return the base classe of provided actions */ - Class<A> getBaseClass(); - - /** @return the provided actions classes */ - java.util.Map<String, Class<? extends A>> getClasses(); - -} Deleted: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java 2008-08-06 10:53:44 UTC (rev 817) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/provider/ActionProviderFromProperties.java 2008-08-09 09:31:56 UTC (rev 818) @@ -1,144 +0,0 @@ -/** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. This program 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 General Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * # #% - */ -package org.codelutin.jaxx.action.provider; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import static org.codelutin.i18n.I18n._; -import org.codelutin.jaxx.action.ActionNameProvider; -import org.codelutin.jaxx.action.MyAbstractAction; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Map; -import java.util.Properties; -import java.util.TreeMap; - -/** @author chemit */ -public class ActionProviderFromProperties<A extends MyAbstractAction> implements ActionProvider<A> { - - /** default prefix for an entryin mapping file. */ - protected static final String ACTION_KEY_PREFIX = "action."; - - protected String propertiesPath; - - protected static Log log = LogFactory.getLog(ActionProviderFromProperties.class); - - protected String name; - - protected Class<A> baseClass; - - protected Map<String, Class<? extends A>> actions; - - - protected ActionProviderFromProperties(String name, Class<A> baseClass, String propertiesPath) { - - this.name = name; - this.baseClass = baseClass; - - this.propertiesPath = propertiesPath; - this.actions = initCache(); - } - - public String getName() { - return name; - } - - public Class<A> getBaseClass() { - return baseClass; - } - - public Map<String, Class<? extends A>> getClasses() { - return actions; - } - - @Override - public String toString() { - return super.toString() + "<name:" + name + ", baseClass:" + baseClass.getSimpleName() + ">"; - } - - protected void clearCache() { - if (actions != null) { - actions.clear(); - actions = null; - } - } - - @Override - protected void finalize() throws Throwable { - super.finalize(); - clearCache(); - } - - @SuppressWarnings({"unchecked"}) - protected Map<String, Class<? extends A>> initCache() { - - InputStream inputStream = null; - - Properties properties = new Properties(); - - try { - inputStream = getClass().getResourceAsStream(propertiesPath); - if (inputStream == null) { - throw new NullPointerException("could not find action file " + propertiesPath); - } - log.info("load " + propertiesPath); - properties.load(inputStream); - } catch (IOException e) { - log.warn(_("jaxx.error.load.actions.file", e.getMessage())); - throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - log.warn(_("jaxx.error.close.actions.file", e.getMessage())); - //throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); - } - } - } - - Map<String, Class<? extends A>> cache = new TreeMap<String, Class<? extends A>>(); - int prefix = ACTION_KEY_PREFIX.length(); - for (Map.Entry<Object, Object> entry : properties.entrySet()) { - String key = entry.getKey() + ""; - String qfn = entry.getValue() + ""; - try { - Class<? extends A> implCass; - implCass = (Class<? extends A>) Class.forName(qfn); - String actionKey = key.substring(prefix); - if (actionKey.startsWith(":")) { - // this is a RuntimeActionNameProvider - Class<ActionNameProvider> klazz = (Class<ActionNameProvider>) Class.forName(actionKey.substring(1)); - for (String s : klazz.newInstance().getActionCommands()) { - log.debug("found action <" + s + " : " + implCass + ">"); - cache.put(s, implCass); - } - continue; - } - log.debug("found action <" + actionKey + " : " + implCass + ">"); - cache.put(actionKey, implCass); - } catch (ClassNotFoundException e) { - throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); - } catch (IllegalAccessException e) { - throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); - } catch (InstantiationException e) { - throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); - } - } - - return cache; - } -}
participants (1)
-
tchemit@users.labs.libre-entreprise.org