Index: topia/src/java/org/codelutin/topia/AbstractTopiaPersistenceService.java diff -u topia/src/java/org/codelutin/topia/AbstractTopiaPersistenceService.java:1.6 topia/src/java/org/codelutin/topia/AbstractTopiaPersistenceService.java:1.7 --- topia/src/java/org/codelutin/topia/AbstractTopiaPersistenceService.java:1.6 Sun Aug 15 13:56:22 2004 +++ topia/src/java/org/codelutin/topia/AbstractTopiaPersistenceService.java Tue Jul 19 13:15:13 2005 @@ -23,17 +23,22 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Mise a jour: $Date: 2004/08/15 13:56:22 $ - * par : $Author: pineau $ + * Mise a jour: $Date: 2005/07/19 13:15:13 $ + * par : $Author: bpoussin $ */ package org.codelutin.topia; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; -public abstract class AbstractTopiaPersistenceService extends AbstractTopiaService implements TopiaPersistenceService { // AbstractTopiaPersistenceService +public abstract class AbstractTopiaPersistenceService extends AbstractTopiaService implements TopiaPersistenceService { // AbstractTopiaPersistenceService + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.AbstractTopiaPersistenceService"); /** * Adds a new TopiaEntityListener to the subscribers list. @@ -58,39 +63,38 @@ * * @return the transient transferable TopiaEntity. */ - public TopiaEntity create() throws TopiaException { - TopiaEntity result = null; - result = getContext().getPersistenceHelper().create(getEntityClass()); - return result; - } - - public TopiaEntity makePersistent(TopiaEntity topiaEntity)throws TopiaException{ - TopiaEntity result = getContext().getPersistenceHelper().makePersistent(topiaEntity); + public Entity create() throws TopiaException { + Entity result = null; + result = (Entity)getContext().getPersistenceHelper().create(getEntityClass()); try { getContext().getListeners().fire(this, "entityAdded", new TopiaEntityEvent(this, result)); - } catch (Exception e) { - e.printStackTrace(); - // No trace that would pollute logs + } catch (Exception eee) { + log.log(Level.FINE, "Erreur durant l'envoie de l'event de creation d'entity", eee); } return result; } - public TopiaEntity update(TopiaEntity topiaEntity) throws TopiaException{ - TopiaEntity result = getContext().getPersistenceHelper().update(topiaEntity); + + public Entity makePersistent(Entity topiaEntity)throws TopiaException{ + Entity result = (Entity)getContext().getPersistenceHelper().makePersistent(topiaEntity); + return result; + } + + public Entity update(TopiaEntity topiaEntity) throws TopiaException{ + Entity result = (Entity)getContext().getPersistenceHelper().update(topiaEntity); try { getContext().getListeners().fire(this, "entityModified", new TopiaEntityEvent(this, result)); - } catch (Exception e) { - e.printStackTrace(); - // No trace that would pollute logs + } catch (Exception eee) { + log.log(Level.FINE, "Erreur durant l'envoie de l'event de modification d'entity", eee); } return result; } - public void delete(TopiaEntity topiaEntity) throws TopiaException{ + + public void delete(Entity topiaEntity) throws TopiaException{ getContext().getPersistenceHelper().delete(topiaEntity); try { getContext().getListeners().fire(this, "entityRemoved", new TopiaEntityEvent(this, topiaEntity)); - } catch (Exception e) { - e.printStackTrace(); - // No trace that would pollute logs + } catch (Exception eee) { + log.log(Level.FINE, "Erreur durant l'envoie de l'event de suppression d'entity", eee); } } @@ -99,7 +103,7 @@ * * @return a List containing all TopiaEntity related to this CRUD. */ - public List findAll() throws TopiaException { + public List findAll() throws TopiaException { return find(null); } @@ -112,7 +116,7 @@ * * @return a List containing all TopiaEntity related to this CRUD according to the given TopiaQuery. */ - public List find(TopiaQuery query) throws TopiaException{ + public List find(TopiaQuery query) throws TopiaException{ return getContext().getPersistenceHelper().find(normalizeQuery(query)); } @@ -142,6 +146,7 @@ TopiaQuery result = new TopiaQuery(); return result.select("*").from(getEntityClass().getName()); } + /** * Retourne une nouvelle requete complete. La requete passée en paramètre * ne contient ne contient pas la clause select ni from. Cette methode Index: topia/src/java/org/codelutin/topia/ContextHelper.java diff -u topia/src/java/org/codelutin/topia/ContextHelper.java:1.2 topia/src/java/org/codelutin/topia/ContextHelper.java:1.3 --- topia/src/java/org/codelutin/topia/ContextHelper.java:1.2 Fri Aug 6 17:48:52 2004 +++ topia/src/java/org/codelutin/topia/ContextHelper.java Tue Jul 19 13:15:13 2005 @@ -23,9 +23,9 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Mise a jour: $Date: 2004/08/06 17:48:52 $ + * Mise a jour: $Date: 2005/07/19 13:15:13 $ * par : $Author: bpoussin $ */ @@ -48,12 +48,18 @@ protected TopiaContext context = null; protected Properties properties = null; + protected HashMap singleton = new HashMap(); public ContextHelper(TopiaContext context, Properties properties){ this.context = context; this.properties = properties; } + public boolean haveProperties(String prop){ + String val = properties.getProperty(prop); + return val != null && !"".equals(val); + } + /** * Un helper doit avoir un constructeur prenant en paramètre un * {@link TopiaContext} et un objet {@link java.util.Properties} @@ -64,6 +70,23 @@ * permettant de retrouver la classe a utiliser. */ public Object getHelper(String propertyId) throws TopiaNotFoundException { + return getHelper(properties, propertyId, false); + } + + public Object getHelperSingleton(String propertyId) throws TopiaNotFoundException { + return getHelper(properties, propertyId, true); + } + + /** + * Un helper doit avoir un constructeur prenant en paramètre un + * {@link TopiaContext} et un objet {@link java.util.Properties} + * Un objet {@link java.util.Properties} est créé et est chargé avec + * le fichier de propriété définie dans la configuration du context. + * L'ancètre de cet objet de propriété est l'objet de propriété du context + * @param propertyId l'id dans le fichier de propriété du helper + * permettant de retrouver la classe a utiliser. + */ + public Object getHelper(Properties properties, String propertyId, boolean singleton) throws TopiaNotFoundException { String className = properties.getProperty(propertyId); if (className == null) { throw new TopiaNotFoundException( @@ -71,7 +94,7 @@ } // load properties for helper - String propertiesFilename = properties.getProperty(propertyId+".properties.file"); + String propertiesFilename = properties.getProperty(propertyId+".properties"); Properties properties = new Properties(this.properties); if (propertiesFilename != null && !propertiesFilename.equals("")) { try { @@ -91,9 +114,23 @@ try { // Warning : don't replace that for a Class.forName(...) has it does NOT behave the same way ! mappedClass = getClass().forName(className); - Constructor constructor = mappedClass.getDeclaredConstructor(new Class[] {TopiaContext.class, Properties.class}); - constructor.setAccessible(true); - return constructor.newInstance(new Object[] {context, properties}); + Object result = null; + if(singleton){ + // on ajoute dans les properties la class pour discreminer les singletons + properties.put(className, className); + result = singleton.get(properties); + if(result == null){ + Constructor constructor = mappedClass.getDeclaredConstructor(new Class[] {Properties.class}); + constructor.setAccessible(true); + result = constructor.newInstance(new Object[] {properties}); + singleton.put(properties, result); + } + } else { + Constructor constructor = mappedClass.getDeclaredConstructor(new Class[] {TopiaContext.class, Properties.class}); + constructor.setAccessible(true); + result = constructor.newInstance(new Object[] {context, properties}); + } + return result; } catch (Exception eee) { throw new TopiaNotFoundException( "Persistence Helper can't be instanciated: " @@ -104,27 +141,13 @@ } } - protected HashMap singleton = new HashMap(); - /** - * Retourne un objet deja instancier de la classe passé en paremetre. Si - * aucun objet de cette classe n'existe, alors un nouveau est instancier. - */ - public Object getSingletonObject(Class clazz) throws TopiaException { - Object result = singleton.get(clazz); - if(result == null){ - result = getInstance(clazz); - singleton.put(clazz, result); - } - return result; - } - /** * Instancie un objet de la classe passé en paramètre. Si l'objet instancier * implante {@link TopiaElement} alors un {@link TopiaElement#setContext} est * appele. */ - public Object getInstance(Class clazz) throws TopiaException{ - Object result = Util.getInstance(clazz); + public A getInstance(Class clazz) throws TopiaException{ + A result = Util.getInstance(clazz); if (result instanceof TopiaElement) { ((TopiaElement) result).setContext(context); } Index: topia/src/java/org/codelutin/topia/DefaultEntitiesHelper.java diff -u topia/src/java/org/codelutin/topia/DefaultEntitiesHelper.java:1.1 topia/src/java/org/codelutin/topia/DefaultEntitiesHelper.java:1.2 --- topia/src/java/org/codelutin/topia/DefaultEntitiesHelper.java:1.1 Wed Jul 6 22:36:20 2005 +++ topia/src/java/org/codelutin/topia/DefaultEntitiesHelper.java Tue Jul 19 13:15:13 2005 @@ -23,9 +23,9 @@ * Created: 6 juillet 2005 16:34:59 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Last update: $Date: 2005/07/06 22:36:20 $ + * Last update: $Date: 2005/07/19 13:15:13 $ * by : $Author: bpoussin $ */ @@ -33,6 +33,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import java.util.Properties; import org.apache.commons.lang.ObjectUtils; public class DefaultEntitiesHelper { // DefaultEntitiesHelper @@ -40,10 +41,18 @@ /** to use log facility, just put in your code: log.info(\"...\"); */ static private Logger log = Logger.getLogger("org.codelutin.topia.DefaultEntitiesHelper"); - public static String toString(Object o){ + protected TopiaContext context = null; + protected Properties properties = null; + + public DefaultEntitiesHelper(TopiaContext context, Properties properties){ + this.context = context; + this.properties = properties; + } + + public String toString(Object o){ return ObjectUtils.identityToString(o); } - public static String toXML(Object o){ + public String toXML(Object o){ return "<" + ((o!=null)?o.getClass().getName():"null") + "/>"; } Index: topia/src/java/org/codelutin/topia/TopiaConst.java diff -u topia/src/java/org/codelutin/topia/TopiaConst.java:1.2 topia/src/java/org/codelutin/topia/TopiaConst.java:1.3 --- topia/src/java/org/codelutin/topia/TopiaConst.java:1.2 Sat Feb 5 10:47:09 2005 +++ topia/src/java/org/codelutin/topia/TopiaConst.java Tue Jul 19 13:15:13 2005 @@ -23,10 +23,10 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Mise a jour: $Date: 2005/02/05 10:47:09 $ - * par : $Author: pineau $ + * Mise a jour: $Date: 2005/07/19 13:15:13 $ + * par : $Author: bpoussin $ */ package org.codelutin.topia; @@ -43,8 +43,21 @@ "context.helper.distribution"; public final static String HELPER_PERSISTENCE = "context.helper.persistence"; + public final static String HELPER_ENTIIES = + "context.helper.entities"; public final static String HELPER_HOOK = "context.helper.hook"; + public final static String HELPER_SECURITY = + "context.helper.security"; + public final static String HELPER_TRANSACTION = + "context.helper.transaction"; + + + public final static String PERSISTENCE_CACHE_CLASS = + "persistence.cache.class"; + public final static String PERSISTENCE_STORAGE_CLASS = + "persistence.storage.class"; + public final static String MAPPING_IMPLEMENTATION = "mapping.implementation"; Index: topia/src/java/org/codelutin/topia/TopiaContext.java diff -u topia/src/java/org/codelutin/topia/TopiaContext.java:1.37 topia/src/java/org/codelutin/topia/TopiaContext.java:1.38 --- topia/src/java/org/codelutin/topia/TopiaContext.java:1.37 Wed Jun 15 16:52:29 2005 +++ topia/src/java/org/codelutin/topia/TopiaContext.java Tue Jul 19 13:15:13 2005 @@ -1,33 +1,33 @@ /* *##% - * Copyright (C) 2002, 2003 Code Lutin - * - * 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. - *##%*/ +* Copyright (C) 2002, 2003 Code Lutin +* +* 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. +*##%*/ /* * - * TopiaContext.java - * - * Created: 3 juil. 2004 - * - * @author Benjamin Poussin - * Copyright Code Lutin - * @version $Revision: 1.37 $ - * - * Mise a jour: $Date: 2005/06/15 16:52:29 $ - * par : $Author: thimel $ - */ +* TopiaContext.java +* +* Created: 3 juil. 2004 +* +* @author Benjamin Poussin +* Copyright Code Lutin +* @version $Revision: 1.38 $ +* +* Mise a jour: $Date: 2005/07/19 13:15:13 $ +* par : $Author: bpoussin $ +*/ package org.codelutin.topia; @@ -38,13 +38,15 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Enumeration; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.Properties; import java.util.StringTokenizer; import java.util.Vector; - -import org.codelutin.topia.distribution.AbstractDistributionHelper; +import org.codelutin.topia.distribution.DistributionHelper; import org.codelutin.topia.hook.HookHelper; import org.codelutin.topia.persistence.PersistenceHelper; import org.codelutin.topia.security.TopiaAccessController; @@ -58,99 +60,142 @@ import org.codelutin.util.HashMapMultiKey; import org.codelutin.util.ListenerSet; -public abstract class TopiaContext { // TopiaContext +public abstract class TopiaContext { // TopiaContext + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.TopiaContext"); + + protected TopiaContextChild parentContext = null; + + protected PersistenceHelper persistenceHelper = null; + + protected EntitiesHelper entitiesHelper = null; + + protected DistributionHelper distributionHelper = null; + + protected HookHelper hookHelper = null; - protected PersistenceHelper persistenceHelper = null; + protected ContextHelper contextHelper = null; - protected AbstractDistributionHelper distributionHelper = null; + protected TopiaSecurityHelper securityHelper = null; - protected HookHelper hookHelper = null; + protected TransactionHelper transactionHelper = null; - protected ContextHelper contextHelper = null; + protected TopiaTransaction transaction = null; - protected CategorisedListenerSet listeners = new CategorisedListenerSet( - TopiaEntityListener.class); + protected CategorisedListenerSet listeners = new CategorisedListenerSet( + TopiaEntityListener.class); - protected ListenerSet permissionListeners = new ListenerSet( - TopiaPermissionListener.class); - protected HashMapMultiKey permissions; - - protected boolean permFileModified = false; + protected ListenerSet permissionListeners = new ListenerSet( + TopiaPermissionListener.class); - /** Toutes les propriétés qui permette de paramètrer le context */ - protected Properties properties = null; + /** Toutes les propriétés qui permette de paramètrer le context */ + protected Properties properties = null; - /** + protected HashMap cacheService = new HashMap(); + + /** * Constructeur du context. Il est protégé car les contexts doivent-être * construit par {@TopiaContextFactory} */ - protected TopiaContext(Properties properties) { - this.properties = properties; - contextHelper = new ContextHelper(this, properties); - permissions = new HashMapMultiKey(); - } + protected TopiaContext(Properties properties) { + this.properties = properties; + contextHelper = new ContextHelper(this, properties); + } + + /** + * Constructeur utilisé pour l'ouverture de transaction + */ + protected TopiaContext(TopiaContextChild parentContext){ + this(parentContext.getProperties()); + this.parentContext = parentContext; + } - /** + /** * Retourne tous les listeners */ - public CategorisedListenerSet getListeners() { - return listeners; - } + public CategorisedListenerSet getListeners() { + return listeners; + } - /** + /** * Adds a new TopiaEntityListener to the subscribers list. * @param topiaEntityListener - the TopiaEntityListener to add to the subscribers list. */ - public void addTopiaEntityListener(TopiaEntityListener l) { - getListeners().add(this, l); - } + public void addTopiaEntityListener(TopiaEntityListener l) { + getListeners().add(this, l); + } - /** + /** * Removes a TopiaEntityListener from the subscribers list. * @param topiaEntityListener - the TopiaEntityListener to remove from the subscribers. */ - public void removeTopiaEntityListener(TopiaEntityListener l) { - getListeners().remove(this, l); - } + public void removeTopiaEntityListener(TopiaEntityListener l) { + getListeners().remove(this, l); + } + + public TopiaContextChild beginTransaction() throws TopiaException { + TopiaContextChild result = new TopiaContextChild(this); + TopiaTransaction tt = null; + if(getTransaction() == null){ + tt = getTransactionHelper().newTranstaction(); + } else { + tt = getTransaction().newSubTranstaction(); + } + result.setTransaction(tt); + result.getPersistenceHelper().beginTransaction(); + return result; + } + + public void commitTransaction() throws TopiaException { + if(getTransaction() == null){ + throw new TopiaException("No transaction found"); + } + TopiaTransaction newtt = getPersistenceHelper().commitTransaction(); + setTransaction(newtt); + } + + public void rollbackTransaction() throws TopiaException { + if(getTransaction() == null){ + throw new TopiaException("No transaction found"); + } + TopiaTransaction newtt = getPersistenceHelper().rollbackTransaction(); + setTransaction(newtt); + } + + public TopiaTransaction getTransaction(){ + return transaction; + } + + public void setTransaction(TopiaTransaction transaction){ + this.transaction = transaction; + } + + /** + * Retourne le service demandé + * @see #getService(Class, boolean) + */ + public A getService(Class serviceInterfacez) + throws TopiaException { + return getService(serviceInterfacez, false); + } - /** + /** * Retourne le service de persistance de l'entity demandé. Par defaut * le service de persitence de l'entity se nome de la meme façon que * l'entity avec PersistenceServiceImpl en plus * @param entityClass la class de l'entité dont on souhaite le persistence * service */ - public TopiaPersistenceService getPersistenceService(Class entityClass) - throws TopiaException { - String className = entityClass.getName() + "PersistenceService"; - Class interfacez = Util.getClazz(className); - Object result = getService(interfacez, true); - return (TopiaPersistenceService) result; - - // - // - // className = entityClass.getName() + "PersistenceServiceImpl"; - // Class clazz = Util.getClazz(className); - // - // Object result = contextHelper.getSingletonObject(clazz); - // result = getHookHelper().addHookSupport(result, new Class[]{interfacez}); - // listeners.addCategory(this, result); - // return (TopiaPersistenceService)result; - } + public TopiaPersistenceService getPersistenceService(Class entityClass) + throws TopiaException { + String className = entityClass.getName() + "PersistenceService"; + Class interfacez = Util.getClazz(className); + Object result = getService(interfacez, true); + return (TopiaPersistenceService) result; + } - /** - * Retourne l'objet implantant les operations de l'entity demandé. Par defaut - * cette classe se nome de la meme façon que - * l'entity avec Operation en plus - * @param entityClass la class de l'entité dont on souhaite les operations - * @see #getService(Class, boolean) - */ - public TopiaEntityOperation getEntityOperation(Class entityClass) - throws TopiaException { - return getEntityOperation(entityClass, false); - } - - /** + /** * Retourne l'objet implantant les operations de l'entity demandé. Par defaut * cette classe se nome de la meme façon que * l'entity avec Operation en plus @@ -159,397 +204,113 @@ * lieu du Dist * @see #getService(Class, boolean) */ - public TopiaEntityOperation getEntityOperation(Class entityClass, - boolean local) throws TopiaException { - String className = entityClass.getName() + "Operation"; - Class interfacez = Util.getClazz(className); - return (TopiaEntityOperation) getService(interfacez, local); - } + public TopiaEntityOperation getEntityOperation(Class entityClass) throws TopiaException { + String className = entityClass.getName() + "Operation"; + Class interfacez = Util.getClazz(className); + return (TopiaEntityOperation) getService(interfacez, false); + } - /** + /** * Retourne le service demandé. * @param local si vrai alors donne l'implantation local du service au * lieu du Dist */ - public TopiaService getService(Class serviceInterfacez, boolean local) - throws TopiaException { - Class clazz = null; - if (local) { - try { - clazz = contextHelper.getImplementationClass(serviceInterfacez); - } catch (TopiaNotFoundException eee) { - // if can't find mapping, try with default dist class - clazz = Util.getClazz(serviceInterfacez.getName() + "Impl"); - } - } else { - try { - clazz = contextHelper.getDistributionClass(serviceInterfacez); - } catch (TopiaNotFoundException eee) { - // if can't find mapping, try with default dist class - clazz = Util.getClazz(serviceInterfacez.getName() + "Dist"); - } - } - Object result = contextHelper.getSingletonObject(clazz); - result = getHookHelper().addHookSupport(result, - new Class[] { serviceInterfacez }); - - if (result instanceof TopiaPersistenceService) { - listeners.addCategory(this, result); - } + public A getService(Class serviceInterfacez, boolean local) + throws TopiaException { + if(getTransaction() == null){ + throw new TopiaException("You must begin transaction to used Context"); + } + + A result = (A)cacheService.get(serviceInterfacez); + if(result == null){ + Class clazz = null; + if (local || getDistributionHelper() == null) { + try { + clazz = contextHelper.getImplementationClass(serviceInterfacez); + } catch (TopiaNotFoundException eee) { + // if can't find mapping, try with default dist class + clazz = Util.getClazz(serviceInterfacez.getName() + "Impl"); + } + result = contextHelper.getInstance(clazz); + } else { + result = getDistributionHelper().createProxy(serviceInterfacez); + } - return (TopiaService) result; - } + result = getHookHelper().addHookSupport(result, + new Class[] { serviceInterfacez }); - /** - * Retourne le service Dist demandé - * @see #getService(Class, boolean) - */ - public TopiaService getService(Class serviceInterfacez) - throws TopiaException { - return getService(serviceInterfacez, false); - } + if (result instanceof TopiaPersistenceService) { + listeners.addCategory(this, result); + } + cacheService.put(serviceInterfacez, result); + } + return (TopiaService) result; + } - /** + /** * Retourne l'objet responsable de la persistance dans l'application */ - public PersistenceHelper getPersistenceHelper() - throws TopiaNotFoundException { - if (persistenceHelper == null) { - persistenceHelper = (PersistenceHelper) contextHelper.getHelper(TopiaConst.HELPER_PERSISTENCE); - } - return persistenceHelper; - } + public PersistenceHelper getPersistenceHelper() + throws TopiaNotFoundException { + if (persistenceHelper == null) { + persistenceHelper = (PersistenceHelper) contextHelper.getHelper(TopiaConst.HELPER_PERSISTENCE); + } + return persistenceHelper; + } - /** + /** * Retourne l'objet permettant d'appeler des méthodes distantes - */ - public AbstractDistributionHelper getDistributionHelper() - throws TopiaNotFoundException { - if (distributionHelper == null) { - distributionHelper = (AbstractDistributionHelper) contextHelper.getHelper(TopiaConst.HELPER_DISTRIBUTION); - } - return distributionHelper; - - } + * Si le context est configuré pour ne pas faire d'appels distant, + * retourne null. + * @return l'objet permettant de faire la distribution, ou null si pas + * de distribution + */ + public DistributionHelper getDistributionHelper() + throws TopiaNotFoundException { + if (distributionHelper == null + && contextHelper.haveProperties(TopiaConst.HELPER_DISTRIBUTION)) { + distributionHelper = (DistributionHelper) + contextHelper.getHelper(TopiaConst.HELPER_DISTRIBUTION); + } + return distributionHelper; + } - /** + /** * Retourne l'objet permettant d'ajouter des hooks aux objets */ - public HookHelper getHookHelper() throws TopiaNotFoundException { - if (hookHelper == null) { - hookHelper = (HookHelper) contextHelper.getHelper(TopiaConst.HELPER_HOOK); - } - return hookHelper; - - } - - /** - * Cette methode permet de creer une nouvelle entity d'un certain type. - * La methode a une visibilite package car seul le framework doit l'utiliser - * et ceci au travers de AbstractTopiaPersistenceService.create() - */ - TopiaEntity createEntity(Class entityClass) throws TopiaException { - return (TopiaEntity) contextHelper.getInstance(entityClass); - } - - ///////////////////////////////////////////////////// - // Security // - ///////////////////////////////////////////////////// - - - /** - * Authentifie l'utilisateur en fonction des paramètres du fichier de propriétés - * @param login - * @param password - * @return la liste des TopiaPrincipal de l'utilisateur - * @throws TopiaSecurityException - */ - public List authenticate(String login, String password) - throws TopiaSecurityException { - String authType = properties.getProperty("topia.auth.type"); - if ("simple".equalsIgnoreCase(authType)) { - return simpleAuthentication(login, password); - } else if ("ldap".equalsIgnoreCase(authType)) { - return ldapAuthentication(login, password); - } else if ("xmi".equalsIgnoreCase(authType)) { - return xmiAuthentication(login, password); - } - throw new TopiaSecurityException("Invalid auth type : " + authType); - } - - /** - * Authentification basée sur les fichiers textes - * @param login - * @param password - * @return la liste des TopiaPrincipal de l'utilisateur - * @throws TopiaSecurityException - */ - protected List simpleAuthentication(String login, String password) - throws TopiaSecurityException { - Properties props = new Properties(); - String fileName = properties.getProperty("topia.auth.simple.file.login"); - if (fileName == null) - throw new TopiaSecurityException( - "Authentication filename must be specified"); - try { - props.load(new FileInputStream(fileName)); - } catch (FileNotFoundException e) { - throw new TopiaSecurityException("Invalid authentication file : " - + fileName); - } catch (IOException ioe) { - throw new TopiaSecurityException( - "Unable to read authentication file : " + fileName); - } - String hashMode = properties.getProperty("topia.auth.simple.hash"); - if (hashMode != null) { - try { - password = new String(MessageDigest.getInstance(hashMode).digest( - password.getBytes())); - } catch (NoSuchAlgorithmException nsaE) { - throw new TopiaSecurityException("Invalid hash algorithm : " - + hashMode); - } - } - if (!password.equals(props.getProperty(login))) - throw new TopiaSecurityException("Wrong Login/Password"); - Vector principals = new Vector(); - principals.addElement(new TopiaUserPrincipal(login)); - - String groupsFileName = properties.getProperty("topia.auth.simple.file.groups"); - props = new Properties(); - try { - props.load(new FileInputStream(groupsFileName)); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); - } catch (IOException e1) { - e1.printStackTrace(); - } - for (Enumeration e = props.keys(); e.hasMoreElements();) { - String key = (String) e.nextElement(); - - for (StringTokenizer sTK = new StringTokenizer(props.getProperty(key), - ","); sTK.hasMoreTokens();) { - if (login.equals(sTK.nextToken().trim())) { - principals.addElement(new TopiaGroupPrincipal(key)); - break; - } - } - } - return principals; - } - - /** - * Authntification basée sur un ldap - * @param login - * @param password - * @return la liste des TopiaPrincipal de l'utilisateur - * @throws TopiaSecurityException - */ - protected List ldapAuthentication(String login, String password) - throws TopiaSecurityException { - throw new TopiaSecurityException("ldapAuthentication not supported"); - //TODO Arno ;) - } - - /** - * Authentification basée sur l'utilisation de la BD de l'application - * @param login - * @param password - * @return la liste des TopiaPrincipal de l'utilisateur - * @throws TopiaSecurityException - */ - protected abstract List xmiAuthentication(String login, String password) - throws TopiaSecurityException; - - /** - * Charge en mémoire les fichiers contenant les permissions - */ - public void loadPermissions() { - loadPermissions("readPerm.file", "read"); - loadPermissions("writePerm.file", "write"); - loadPermissions("adminPerm.file", "admin"); - permFileModified = false; - } - - /** - * Charge en mémoire le fichier (fileName) contenant les permissions - * associées à l'action action - * @param fileName - * @param action - */ - private void loadPermissions(String fileName, String action) { - Properties props = new Properties(); - try { - props.load(new FileInputStream(fileName)); - HashMapMultiKey.Key multiKey = new HashMapMultiKey.Key().add(action); - for (Enumeration en = props.keys(); en.hasMoreElements();) { - String key = (String) en.nextElement(); - Vector propValues = new Vector(); - String principals = props.getProperty(key).replaceAll(" +", " "); - StringTokenizer sTK = new StringTokenizer(principals, ";"); - while (sTK.hasMoreTokens()) - try { - principals = sTK.nextToken(); - TopiaPermission perm = - new TopiaPermission(key + " " + principals, action); - addNoSecurityPermission(perm, true); - propValues.addElement(perm); - } catch (TopiaSecurityException e1) { - e1.printStackTrace(); - } - permissions.put(multiKey.add(key), propValues); - } - } catch (FileNotFoundException e) { - //Aucune permission n'est chargée puisque le fichier n'existe pas... - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * Sauvegarde les permissions dans les fichiers - * - */ - public void storePermissions() { - if (permFileModified) { - storePermissions("readPerm.file", "read"); - storePermissions("writePerm.file", "write"); - storePermissions("adminPerm.file", "admin"); - permFileModified = false; - } - } - - /** - * Sauvegardes les permissions de type "action" dans le fichier fileName - * @param fileName - * @param action - */ - private void storePermissions(String fileName, String action) { - Properties props = new Properties(); - List list = permissions.getKeys(action); - for (Iterator it = list.iterator(); it.hasNext(); ) { - HashMapMultiKey.Key key = (HashMapMultiKey.Key)it.next(); - String propKey = (String)key.get(1); - String value = ""; - Vector values = (Vector)permissions.get(key); - if (values == null || values.size()==0) - continue; - for (Enumeration en = values.elements(); en.hasMoreElements(); ) { - TopiaPermission perm = (TopiaPermission)en.nextElement(); - value += perm.principalsToString() + ";"; - } - props.put(propKey, value.substring(0, value.lastIndexOf(";"))); - } - try { - props.store(new FileOutputStream(fileName), null); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - /** - * Ajoute à l'application la Permission perm - * @param perm - * @throws TopiaSecurityException - */ - public void addPermission(TopiaPermission perm) - throws TopiaSecurityException { - TopiaAccessController.checkPermission("perm", "admin"); - addNoSecurityPermission(perm, true); - } - - /** - * Ajoute à l'application la Permission perm sans control de sécurité - * @param perm - * @param informListeners si true, informe les listeners concernés - * @throws TopiaSecurityException - */ - private void addNoSecurityPermission(TopiaPermission perm, boolean informListeners) - throws TopiaSecurityException { - HashMapMultiKey.Key key; - key = new HashMapMultiKey.Key().add(perm.getActions()).add(perm.getId()); - Vector perms = (Vector)permissions.get(key); - if (perms == null) - perms = new Vector(); - if (!perms.contains(perm)) - perms.add(perm); - permissions.put(key, perms); - permFileModified = true; - if (informListeners) { - try { - permissionListeners.fire("permissionAdded", new TopiaPermissionEvent( - this, perm)); - } catch (Exception e) { - throw new TopiaSecurityException("Error while adding permission " - + perm, e); - } - } - } - - /** - * Modifie la Permission perm - * @param perm - * @throws TopiaSecurityException - */ - public void modifyPermission(TopiaPermission perm) - throws TopiaSecurityException { - TopiaAccessController.checkPermission("perm", "admin"); - List list = permissions.getKeys(perm.getId()); - boolean found = false; - for (Iterator it = list.iterator(); !found && it.hasNext(); ) { - HashMapMultiKey.Key key = (HashMapMultiKey.Key)it.next(); - Vector perms = (Vector)permissions.get(key); - for (Enumeration en = perms.elements(); !found && en.hasMoreElements(); ) { - TopiaPermission topiaPerm = (TopiaPermission)en.nextElement(); - if (topiaPerm.getPrincipals().equals(perm.getPrincipals())) { - found = true; - if (!topiaPerm.getActions().equals(perm.getActions())) { - perms.remove(topiaPerm); - if (perms.isEmpty()) - permissions.remove(key); - addNoSecurityPermission(perm, false); - } + public HookHelper getHookHelper() throws TopiaNotFoundException { + if (hookHelper == null) { + hookHelper = (HookHelper) contextHelper.getHelper(TopiaConst.HELPER_HOOK); + } + return hookHelper; + } + + public TopiaSecurityHelper getSecurityHelper(){ + if(securityHelper == null){ + securityHelper = (TopiaSecurityHelper)contextHelper.getHelper(TopiaConst.HELPER_SECURITY); + } + return securityHelper; + } + + public TransactionHelper getTransactionHelper(){ + if(transactionHelper == null){ + transactionHelper = (TransactionHelper)contextHelper.getHelperSingleton(TopiaConst.HELPER_TRANSACTION); + } + return transactionHelper; + } + + public EntitiesHelper getEntitiesHelper(){ + if(entitiesHelper == null){ + entitiesHelper = (EntitiesHelper)contextHelper.getHelper(TopiaConst.HELPER_ENTIIES); + if(entitiesHelper == null){ + entitiesHelper = new DefaultEntitiesHelper(this, getProperties()); } - } - } - - permFileModified = true; - try { - permissionListeners.fire("permissionModified", - new TopiaPermissionEvent(this, perm)); - } catch (Exception e) { - throw new TopiaSecurityException("Error while modifying permission " - + perm, e); - } - } - - /** - * Retire la permission perm - * @param perm - * @throws TopiaSecurityException - */ - public void removePermission(TopiaPermission perm) - throws TopiaSecurityException { - TopiaAccessController.checkPermission("perm", "admin"); - HashMapMultiKey.Key key; - key = new HashMapMultiKey.Key().add(perm.getActions()).add(perm.getId()); - Vector perms = (Vector)permissions.get(key); - if ((perms != null) && (perms.contains(perm))) { - permFileModified = true; - perms.remove(perm); - if (perms.isEmpty()) - permissions.remove(key); - } - try { - permissionListeners.fire("permissionRemoved", - new TopiaPermissionEvent(this, perm)); - } catch (Exception e) { - throw new TopiaSecurityException("Error while removing permission " - + perm, e); - } - } + } + return entitiesHelper; + } + + public ContextHelper getContextHelper(){ + return contextHelper; + } } // TopiaContext Index: topia/src/java/org/codelutin/topia/TopiaContextFactory.java diff -u topia/src/java/org/codelutin/topia/TopiaContextFactory.java:1.6 topia/src/java/org/codelutin/topia/TopiaContextFactory.java:1.7 --- topia/src/java/org/codelutin/topia/TopiaContextFactory.java:1.6 Wed Jun 15 15:14:54 2005 +++ topia/src/java/org/codelutin/topia/TopiaContextFactory.java Tue Jul 19 13:15:13 2005 @@ -23,10 +23,10 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Mise a jour: $Date: 2005/06/15 15:14:54 $ - * par : $Author: thimel $ + * Mise a jour: $Date: 2005/07/19 13:15:13 $ + * par : $Author: bpoussin $ */ package org.codelutin.topia; @@ -139,16 +139,22 @@ Level.FINEST, "Class used for context is " + contextClassName); try { contextClass = Class.forName(contextClassName); - Constructor constructor = contextClass - .getDeclaredConstructor(new Class[] { Properties.class }); - constructor.setAccessible(true); - context = (TopiaContext) constructor - .newInstance(new Object[] { properties }); + context = createContext(contextClassName, properties); } catch (Exception eee) { throw new TopiaNotFoundException( "TopiaContext can't be instanciated: " + contextClassName + " concret : " + contextClass, eee); } + return context; + } + + protected static > A createContext( + Class contextClass, Properties properties) + throws Exception { + Constructor constructor = contextClass.getDeclaredConstructor( + Properties.class); + constructor.setAccessible(true); + A context = constructor.newInstance(properties); return context; } Index: topia/src/java/org/codelutin/topia/TopiaPersistenceService.java diff -u topia/src/java/org/codelutin/topia/TopiaPersistenceService.java:1.8 topia/src/java/org/codelutin/topia/TopiaPersistenceService.java:1.9 --- topia/src/java/org/codelutin/topia/TopiaPersistenceService.java:1.8 Fri Dec 17 16:13:51 2004 +++ topia/src/java/org/codelutin/topia/TopiaPersistenceService.java Tue Jul 19 13:15:13 2005 @@ -23,10 +23,10 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ * - * Mise a jour: $Date: 2004/12/17 16:13:51 $ - * par : $Author: pineau $ + * Mise a jour: $Date: 2005/07/19 13:15:13 $ + * par : $Author: bpoussin $ */ package org.codelutin.topia; @@ -37,7 +37,7 @@ * Common interface to all CRUD services (CRUD stands for Creation, Research, Update and Deletion) * Note : Implementation class are required to have a public empty constructor. */ -public interface TopiaPersistenceService extends TopiaService { +public interface TopiaPersistenceService extends TopiaService { /** * Adds a new TopiaEntityListener to the subscribers list. @@ -56,7 +56,7 @@ * * @return the transient transferable TopiaEntity. */ - public TopiaEntity create() throws TopiaException; + public Entity create() throws TopiaException; /** * Creates that is make persistent the given TopiaEntity. @@ -64,22 +64,23 @@ * @param topiaEntity - the TopiaEntity to persist. * * @return the persistent TopiaEntity. + * @deprecated create method make the entity persistence directly now */ - public TopiaEntity makePersistent(TopiaEntity topiaEntity) + public Entity makePersistent(Entity topiaEntity) throws TopiaException; /** * Updates that is make change on the given TopiaEntity persistent. * @param topiaEntity - the TopiaEntity whose changes must be made persistent. */ - public TopiaEntity update(TopiaEntity topiaEntity) throws TopiaException; + public Entity update(Entity topiaEntity) throws TopiaException; /** * Returns all TopiaEntity related to this CRUD. * * @return a List containing all TopiaEntity related to this CRUD. */ - public List findAll() throws TopiaException; + public List findAll() throws TopiaException; /** * Returns all TopiaEntity related to this CRUD according to the given TopiaQuery. @@ -90,7 +91,7 @@ * * @return a List containing all TopiaEntity related to this CRUD according to the given TopiaQuery. */ - public List find(TopiaQuery query) throws TopiaException; + public List find(TopiaQuery query) throws TopiaException; /** * Returns the number of TopiaEntity related to this CRUD. @@ -114,7 +115,7 @@ * Delete that is remove from persistence layer the given TopiaEntity. * @param topiaEntity - the TopiaEntity to delete. */ - public void delete(TopiaEntity topiaEntity) throws TopiaException; + public void delete(Entity topiaEntity) throws TopiaException; /** * Retourne une TopiaQuery ou le select et le from sont convenablement Index: topia/src/java/org/codelutin/topia/Util.java diff -u topia/src/java/org/codelutin/topia/Util.java:1.12 topia/src/java/org/codelutin/topia/Util.java:1.13 --- topia/src/java/org/codelutin/topia/Util.java:1.12 Wed Jul 6 22:35:20 2005 +++ topia/src/java/org/codelutin/topia/Util.java Tue Jul 19 13:15:13 2005 @@ -23,9 +23,9 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.12 $ + * @version $Revision: 1.13 $ * - * Mise a jour: $Date: 2005/07/06 22:35:20 $ + * Mise a jour: $Date: 2005/07/19 13:15:13 $ * par : $Author: bpoussin $ */ @@ -82,8 +82,8 @@ /** * Instancie un objet de la classe passé en paramètre. */ - public static Object getInstance(Class clazz) throws TopiaNotFoundException{ - Object result = null; + public static A getInstance(Class clazz) throws TopiaNotFoundException{ + A result = null; try{ result = clazz.newInstance(); } catch (Exception eee) { @@ -94,11 +94,11 @@ return result; } - public static Object getInstance(Class clazz, Object [] args) throws TopiaNotFoundException{ + public static A getInstance(Class clazz, Object [] args) throws TopiaNotFoundException{ Object result = null; try{ Expression e = new Expression(clazz, "new", args); - result = e.getValue(); + result = (A)e.getValue(); } catch (Exception eee) { throw new TopiaNotFoundException( "Requested class can't be instanciated: " + clazz.getName(), Index: topia/src/java/org/codelutin/topia/EntitiesHelper.java diff -u /dev/null topia/src/java/org/codelutin/topia/EntitiesHelper.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/EntitiesHelper.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,43 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * 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. + *##%*/ + +/* * + * EntitiesHelper.java + * + * Created: 18 juillet 2005 12:47:31 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public interface EntitiesHelper { // EntitiesHelper + + public String toString(Object o); + public String toXML(Object o); + +} // EntitiesHelper + Index: topia/src/java/org/codelutin/topia/TopiaSecurityHelper.java diff -u /dev/null topia/src/java/org/codelutin/topia/TopiaSecurityHelper.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/TopiaSecurityHelper.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,348 @@ +/* *##% + * Copyright (C) 2005 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * 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. + *##%*/ + +/* * + * TopiaSecurityHelper.java + * + * Created: 15 juillet 2005 17:39:35 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public class TopiaSecurityHelper { // TopiaSecurityHelper + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.TopiaSecurityHelper"); + + protected TopiaContext context = null; + + protected HashMapMultiKey permissions = new HashMapMultiKey(); + + protected boolean permFileModified = false; + + public TopiaSecurityHelper(TopiaContext context, Properties config){ + this.context = context; + } + + /** + * Authentifie l'utilisateur en fonction des paramètres du fichier de propriétés + * @param login + * @param password + * @return la liste des TopiaPrincipal de l'utilisateur + * @throws TopiaSecurityException + */ + public List authenticate(String login, String password) + throws TopiaSecurityException { + String authType = properties.getProperty("topia.auth.type"); + if ("simple".equalsIgnoreCase(authType)) { + return simpleAuthentication(login, password); + } else if ("ldap".equalsIgnoreCase(authType)) { + return ldapAuthentication(login, password); + } else if ("xmi".equalsIgnoreCase(authType)) { + return xmiAuthentication(login, password); + } + throw new TopiaSecurityException("Invalid auth type : " + authType); + } + + /** + * Authentification basée sur les fichiers textes + * @param login + * @param password + * @return la liste des TopiaPrincipal de l'utilisateur + * @throws TopiaSecurityException + */ + protected List simpleAuthentication(String login, String password) + throws TopiaSecurityException { + Properties props = new Properties(); + String fileName = properties.getProperty("topia.auth.simple.file.login"); + if (fileName == null) + throw new TopiaSecurityException( + "Authentication filename must be specified"); + try { + props.load(new FileInputStream(fileName)); + } catch (FileNotFoundException e) { + throw new TopiaSecurityException("Invalid authentication file : " + + fileName); + } catch (IOException ioe) { + throw new TopiaSecurityException( + "Unable to read authentication file : " + fileName); + } + String hashMode = properties.getProperty("topia.auth.simple.hash"); + if (hashMode != null) { + try { + password = new String(MessageDigest.getInstance(hashMode).digest( + password.getBytes())); + } catch (NoSuchAlgorithmException nsaE) { + throw new TopiaSecurityException("Invalid hash algorithm : " + + hashMode); + } + } + if (!password.equals(props.getProperty(login))) + throw new TopiaSecurityException("Wrong Login/Password"); + Vector principals = new Vector(); + principals.addElement(new TopiaUserPrincipal(login)); + + String groupsFileName = properties.getProperty("topia.auth.simple.file.groups"); + props = new Properties(); + try { + props.load(new FileInputStream(groupsFileName)); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } catch (IOException e1) { + e1.printStackTrace(); + } + for (Enumeration e = props.keys(); e.hasMoreElements();) { + String key = (String) e.nextElement(); + + for (StringTokenizer sTK = new StringTokenizer(props.getProperty(key), + ","); sTK.hasMoreTokens();) { + if (login.equals(sTK.nextToken().trim())) { + principals.addElement(new TopiaGroupPrincipal(key)); + break; + } + } + } + return principals; + } + + /** + * Authntification basée sur un ldap + * @param login + * @param password + * @return la liste des TopiaPrincipal de l'utilisateur + * @throws TopiaSecurityException + */ + protected List ldapAuthentication(String login, String password) + throws TopiaSecurityException { + throw new TopiaSecurityException("ldapAuthentication not supported"); + //TODO Arno ;) + } + + /** + * Authentification basée sur l'utilisation de la BD de l'application + * @param login + * @param password + * @return la liste des TopiaPrincipal de l'utilisateur + * @throws TopiaSecurityException + */ + protected abstract List xmiAuthentication(String login, String password) + throws TopiaSecurityException; + + /** + * Charge en mémoire les fichiers contenant les permissions + */ + public void loadPermissions() { + loadPermissions("readPerm.file", "read"); + loadPermissions("writePerm.file", "write"); + loadPermissions("adminPerm.file", "admin"); + permFileModified = false; + } + + /** + * Charge en mémoire le fichier (fileName) contenant les permissions + * associées à l'action action + * @param fileName + * @param action + */ + private void loadPermissions(String fileName, String action) { + Properties props = new Properties(); + try { + props.load(new FileInputStream(fileName)); + HashMapMultiKey.Key multiKey = new HashMapMultiKey.Key().add(action); + for (Enumeration en = props.keys(); en.hasMoreElements();) { + String key = (String) en.nextElement(); + Vector propValues = new Vector(); + String principals = props.getProperty(key).replaceAll(" +", " "); + StringTokenizer sTK = new StringTokenizer(principals, ";"); + while (sTK.hasMoreTokens()) + try { + principals = sTK.nextToken(); + TopiaPermission perm = + new TopiaPermission(key + " " + principals, action); + addNoSecurityPermission(perm, true); + propValues.addElement(perm); + } catch (TopiaSecurityException e1) { + e1.printStackTrace(); + } + permissions.put(multiKey.add(key), propValues); + } + } catch (FileNotFoundException e) { + //Aucune permission n'est chargée puisque le fichier n'existe pas... + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Sauvegarde les permissions dans les fichiers + * + */ + public void storePermissions() { + if (permFileModified) { + storePermissions("readPerm.file", "read"); + storePermissions("writePerm.file", "write"); + storePermissions("adminPerm.file", "admin"); + permFileModified = false; + } + } + + /** + * Sauvegardes les permissions de type "action" dans le fichier fileName + * @param fileName + * @param action + */ + private void storePermissions(String fileName, String action) { + Properties props = new Properties(); + List list = permissions.getKeys(action); + for (Iterator it = list.iterator(); it.hasNext(); ) { + HashMapMultiKey.Key key = (HashMapMultiKey.Key)it.next(); + String propKey = (String)key.get(1); + String value = ""; + Vector values = (Vector)permissions.get(key); + if (values == null || values.size()==0) + continue; + for (Enumeration en = values.elements(); en.hasMoreElements(); ) { + TopiaPermission perm = (TopiaPermission)en.nextElement(); + value += perm.principalsToString() + ";"; + } + props.put(propKey, value.substring(0, value.lastIndexOf(";"))); + } + try { + props.store(new FileOutputStream(fileName), null); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + /** + * Ajoute à l'application la Permission perm + * @param perm + * @throws TopiaSecurityException + */ + public void addPermission(TopiaPermission perm) + throws TopiaSecurityException { + TopiaAccessController.checkPermission("perm", "admin"); + addNoSecurityPermission(perm, true); + } + + /** + * Ajoute à l'application la Permission perm sans control de sécurité + * @param perm + * @param informListeners si true, informe les listeners concernés + * @throws TopiaSecurityException + */ + private void addNoSecurityPermission(TopiaPermission perm, boolean informListeners) + throws TopiaSecurityException { + HashMapMultiKey.Key key; + key = new HashMapMultiKey.Key().add(perm.getActions()).add(perm.getId()); + Vector perms = (Vector)permissions.get(key); + if (perms == null) + perms = new Vector(); + if (!perms.contains(perm)) + perms.add(perm); + permissions.put(key, perms); + permFileModified = true; + if (informListeners) { + try { + permissionListeners.fire("permissionAdded", new TopiaPermissionEvent( + this, perm)); + } catch (Exception e) { + throw new TopiaSecurityException("Error while adding permission " + + perm, e); + } + } + } + + /** + * Modifie la Permission perm + * @param perm + * @throws TopiaSecurityException + */ + public void modifyPermission(TopiaPermission perm) + throws TopiaSecurityException { + TopiaAccessController.checkPermission("perm", "admin"); + List list = permissions.getKeys(perm.getId()); + boolean found = false; + for (Iterator it = list.iterator(); !found && it.hasNext(); ) { + HashMapMultiKey.Key key = (HashMapMultiKey.Key)it.next(); + Vector perms = (Vector)permissions.get(key); + for (Enumeration en = perms.elements(); !found && en.hasMoreElements(); ) { + TopiaPermission topiaPerm = (TopiaPermission)en.nextElement(); + if (topiaPerm.getPrincipals().equals(perm.getPrincipals())) { + found = true; + if (!topiaPerm.getActions().equals(perm.getActions())) { + perms.remove(topiaPerm); + if (perms.isEmpty()) + permissions.remove(key); + addNoSecurityPermission(perm, false); + } + } + } + } + + permFileModified = true; + try { + permissionListeners.fire("permissionModified", + new TopiaPermissionEvent(this, perm)); + } catch (Exception e) { + throw new TopiaSecurityException("Error while modifying permission " + + perm, e); + } + } + + /** + * Retire la permission perm + * @param perm + * @throws TopiaSecurityException + */ + public void removePermission(TopiaPermission perm) + throws TopiaSecurityException { + TopiaAccessController.checkPermission("perm", "admin"); + HashMapMultiKey.Key key; + key = new HashMapMultiKey.Key().add(perm.getActions()).add(perm.getId()); + Vector perms = (Vector)permissions.get(key); + if ((perms != null) && (perms.contains(perm))) { + permFileModified = true; + perms.remove(perm); + if (perms.isEmpty()) + permissions.remove(key); + } + try { + permissionListeners.fire("permissionRemoved", + new TopiaPermissionEvent(this, perm)); + } catch (Exception e) { + throw new TopiaSecurityException("Error while removing permission " + + perm, e); + } + } + +} // TopiaSecurityHelper +