Author: tchemit Date: 2008-02-08 09:41:21 +0000 (Fri, 08 Feb 2008) New Revision: 739 Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/ContentTypeFactory.java Log: utilisation d'exception de type runtime sur les factories. ces exceptions ne peuvent provenir que d'erreurs de dev, et doivent dans ce cadre ?\195?\170tre de type Runtime. Java propose quelques exceptions de type Runtime ?\195?\160 utiliser (ou surcharger...) : NullPointerException IllegalArgumentException IllegalStateException... Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/ContentTypeFactory.java =================================================================== --- trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/ContentTypeFactory.java 2008-02-08 09:39:38 UTC (rev 738) +++ trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/ContentTypeFactory.java 2008-02-08 09:41:21 UTC (rev 739) @@ -22,7 +22,7 @@ /** * Cache content types. - * + * * @author landais */ public class ContentTypeFactory { @@ -30,36 +30,49 @@ /** Content type cache. */ private static Map<String, ContentType> contentTypes = null; + /** Constant defining where are implementations */ + private static final String PACKAGE_IMPLS = ContentTypeFactory.class.getPackage().getName(); + /** - * Retrieve a content type instance. - * - * @param contentTypeClassSimpleName - * Class required - * - * @return Instance - * - * @throws Exception - * the exception + * Retrieve a content type instance from the cache. + * <p/> + * If no such instance is found, then create it and store it in cache. + * + * @param contentTypeClassSimpleName Class required + * @return the Instance + * @throws IllegalArgumentException if could not found the class or has + * problem while instanciation */ - public static ContentType getContentTypeInstance( - String contentTypeClassSimpleName) throws Exception { - - // Create cache if doesn't exist - if (contentTypes == null) { - contentTypes = new HashMap<String, ContentType>(); - } + public static ContentType getContentTypeInstance(String contentTypeClassSimpleName) throws IllegalArgumentException { + Map<String, ContentType> contentTypes = getContentTypes(); // Check cache - ContentType result = contentTypes.get(contentTypeClassSimpleName); - // Create instance if doesn't exist, and puts it in cache + ContentType result = getContentTypes().get(contentTypeClassSimpleName); + // Create instance if doesn't exist, and put it in cache if (result == null) { - Class<?> contentTypeClass = Class.forName(ContentTypeFactory.class - .getPackage().getName() - + "." + contentTypeClassSimpleName); - result = (ContentType) contentTypeClass.newInstance(); - contentTypes.put(contentTypeClass.getSimpleName(), result); + try { + Class<?> impl; + impl = Class.forName(PACKAGE_IMPLS + "." + contentTypeClassSimpleName); + result = (ContentType) impl.newInstance(); + contentTypes.put(contentTypeClassSimpleName, result); + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(e); + } catch (IllegalAccessException e) { + throw new IllegalArgumentException(e); + } catch (InstantiationException e) { + throw new IllegalArgumentException(e); + } } return result; + } + protected static Map<String, ContentType> getContentTypes() { + if (contentTypes == null) { + contentTypes = new HashMap<String, ContentType>(); + } + return contentTypes; } + protected ContentTypeFactory() { + // no instance + } }
participants (1)
-
tchemit@users.labs.libre-entreprise.org