Author: tchemit Date: 2008-07-20 22:16:56 +0000 (Sun, 20 Jul 2008) New Revision: 744 Added: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionFactoryFromProvider.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/AbstractActionProvider.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/SimpleActionFactoryImpl.java Log: restore this deprecated factory (to make it compiliance with projects...) add a new factory with provider to detect actions Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/AbstractActionProvider.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/AbstractActionProvider.java 2008-07-20 22:01:49 UTC (rev 743) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/AbstractActionProvider.java 2008-07-20 22:16:56 UTC (rev 744) @@ -14,24 +14,31 @@ */ package org.codelutin.jaxx.action; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import javax.swing.AbstractAction; import java.util.Map; /** @author chemit */ public abstract class AbstractActionProvider<A extends AbstractAction> implements ActionProvider<A> { - protected final String name; + protected static Log log = LogFactory.getLog(AbstractActionProvider.class); - protected final Class<A> baseClass; + protected String name; + protected Class<A> baseClass; + protected Map<String, Class<? extends A>> actions; protected abstract Map<String, Class<? extends A>> initCache(); - protected AbstractActionProvider(String name, Class<A> baseClass) { + protected AbstractActionProvider(String name, Class<A> baseClass, boolean doInitCache) { this.name = name; this.baseClass = baseClass; - this.actions = initCache(); + if (doInitCache) { + this.actions = initCache(); + } } public String getName() { Copied: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionFactoryFromProvider.java (from rev 740, trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/SimpleActionFactoryImpl.java) =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionFactoryFromProvider.java (rev 0) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionFactoryFromProvider.java 2008-07-20 22:16:56 UTC (rev 744) @@ -0,0 +1,60 @@ +/* +* ##% Copyright (C) 2007, 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 javax.swing.AbstractAction; +import java.util.Map; +import java.util.ServiceLoader; +import java.util.TreeMap; + +/** + * A simple implementation of {@link ActionFactory} using a properties file to + * load action mapping. + * <p/> + * An entry is in that form : <code>action.actionName=fqn</code> where + * <p/> + * <code>actionName</code> is the key of action used in factory, and + * <code>fqn</code> is the fully qualified name of the implemented action class. + * + * @author chemit + */ +public class ActionFactoryFromProvider extends ActionFactory { + + /** real file where to load actions mapping */ + protected String actionFilePath; + + public ActionFactoryFromProvider() { + super(); + } + + + protected Map<String, Class<? extends AbstractAction>> init() { + // obtain a ServiceLoader on ActionProvider + ServiceLoader<ActionProvider> loader = ServiceLoader.load(ActionProvider.class); + Map<String, Class<? extends AbstractAction>> cache = new TreeMap<String, Class<? extends AbstractAction>>(); + + for (ActionProvider<?> actionProvider : loader) { + if (log.isDebugEnabled()) { + log.debug("load action provider " + actionProvider); + } + cache.putAll(actionProvider.getClasses()); + } + return cache; + } + +} \ No newline at end of file Added: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/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-07-20 22:16:56 UTC (rev 744) @@ -0,0 +1,82 @@ +/** + * # #% 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 static org.codelutin.i18n.I18n._; + +import javax.swing.AbstractAction; +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 AbstractAction> extends AbstractActionProvider<A> { + + /** default prefix for an entryin mapping file. */ + protected static final String ACTION_KEY_PREFIX = "action."; + + protected String propertiesPath; + + protected ActionProviderFromProperties(String name, Class<A> baseClass, String propertiesPath) { + super(name, baseClass, false); + this.propertiesPath = propertiesPath; + this.actions = initCache(); + } + + @SuppressWarnings({"unchecked"}) + protected Map<String, Class<? extends A>> initCache() { + + InputStream inputStream = null; + + Properties properties = new Properties(); + + try { + inputStream = getClass().getResourceAsStream(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> objectObjectEntry : properties.entrySet()) { + String key = objectObjectEntry.getKey() + ""; + String qfn = objectObjectEntry.getValue() + ""; + try { + Class<? extends A> implCass; + implCass = (Class<? extends A>) Class.forName(qfn); + log.debug("found action " + implCass); + cache.put(key.substring(prefix), implCass); + } catch (ClassNotFoundException e) { + throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn)); + } + } + + return cache; + } +} Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/SimpleActionFactoryImpl.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/SimpleActionFactoryImpl.java 2008-07-20 22:01:49 UTC (rev 743) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/SimpleActionFactoryImpl.java 2008-07-20 22:16:56 UTC (rev 744) @@ -27,7 +27,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.ServiceLoader; import java.util.TreeMap; /** @@ -40,6 +39,7 @@ * <code>fqn</code> is the fully qualified name of the implemented action class. * * @author chemit + * @deprecated prefer use {@link org.codelutin.jaxx.action.ActionFactoryFromProvider} */ public class SimpleActionFactoryImpl extends ActionFactory { /** default file where to load action mapping */ @@ -61,18 +61,6 @@ @SuppressWarnings({"unchecked"}) protected Map<String, Class<? extends AbstractAction>> init() { - ServiceLoader<ActionProvider> loader = ServiceLoader.load(ActionProvider.class); - Map<String, Class<? extends AbstractAction>> cache = new TreeMap<String, Class<? extends AbstractAction>>(); - - for (ActionProvider<? extends AbstractAction> actionProvider : loader) { - log.info("load action provider " + actionProvider); - cache.putAll(actionProvider.getClasses()); - } - return cache; - } - - @SuppressWarnings({"unchecked"}) - protected Map<String, Class<? extends AbstractAction>> initOld() { Properties properties = new Properties(); List<URL> urls = Resource.getURLs(".*jaxx/.+-actions\\.properties"); for (URL url : urls) {