Author: tchemit Date: 2008-02-13 15:27:24 +0000 (Wed, 13 Feb 2008) New Revision: 926 Modified: trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java Log: initialisation des services Modified: trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java 2008-02-13 15:24:47 UTC (rev 925) +++ trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java 2008-02-13 15:27:24 UTC (rev 926) @@ -26,16 +26,42 @@ import fr.cemagref.simexplorer.is.service.SimExplorerServiceException; import fr.cemagref.simexplorer.is.service.StorageService; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import java.io.BufferedInputStream; import java.io.InputStream; +import java.net.URI; +import java.util.Properties; /** - * Une classe pour encapsuler les appels aux services Storage + * Une classe pour encapsuler les appels aux services Storage et leur initialisation. * * @author chemit */ public class StorageServiceHelper { + private final static String PROVIDER_URL = "java.naming.provider.url"; + private final static String FACTORY_INITIAL = "java.naming.factory.initial"; + private final static String FACTORY_URL_PKGS = "java.naming.factory.url.pkgs"; + + /** le service de données distant */ + protected static StorageService remoteStorageService; + + /** le service de données local */ + protected static StorageService storageService; + + /** + * @param context le context de l'application + * @param remote flag pour indiquer le type de service recherché (local ou remote) + * @return le service instancié et initialisé + */ + public static StorageService getService(SimExplorerContext context, boolean remote) { + StorageService service; + service = remote ? getRemoteStorageService(context) : getLocalStorageService(context); + return service; + } + public static int getCount(SimExplorerContext context, boolean remote, String query, boolean onlyLatest) { StorageService service = getService(context, remote); String token = context.getToken(); @@ -145,9 +171,79 @@ } } - protected static StorageService getService(SimExplorerContext context, boolean remote) { + /** + * @param context le context de l'application + * @return le service de storage locale + */ + protected static StorageService getLocalStorageService(SimExplorerContext context) { + if (storageService == null) { + storageService = initLocalStorageService(context.getConfig()); + } + return storageService; + } + + /** + * @param context le context de l'application + * @return le service de storage distant + */ + protected static StorageService getRemoteStorageService(SimExplorerContext context) { + if (remoteStorageService == null) { + remoteStorageService = initRemoteStorageService(context.getConfig()); + } + return remoteStorageService; + } + + /** + * Initialise le service local de Storage + * + * @param config la config de l'application + * @return le service local initialisé + * @throws SimExplorerRuntimeException si pb lors de l'init du service + */ + protected static StorageService initLocalStorageService(SimExplorerConfig config) throws SimExplorerRuntimeException { StorageService service; - service = context.getStorageService(remote); - return service; + try { + service = new MockStorageServiceImpl(); + return service; + } catch (Exception e) { + throw new SimExplorerRuntimeException(e); + } } + + /** + * Initialise le service distant de Storage + * + * @param config la config de l'application + * @return le service distant initialisé + * @throws SimExplorerRuntimeException si pb lors de l'init du service + */ + protected static StorageService initRemoteStorageService(SimExplorerConfig config) throws SimExplorerRuntimeException { + + try { + StorageService service; + Properties props = initProperties(config.getRemoteURI()); + Context context; + try { + context = new InitialContext(props); + } catch (Exception e) { + context = new InitialContext(); + } + service = (StorageService) context.lookup("StorageService"); + return service; + } catch (NamingException e) { + throw new SimExplorerRuntimeException(e); + } + } + + protected static Properties initProperties(URI uri) { + Properties props = (Properties) System.getProperties().clone(); + props.put(PROVIDER_URL, uri.toString()); + props.put(FACTORY_INITIAL, "org.jnp.interfaces.NamingContextFactory"); + props.put(FACTORY_URL_PKGS, "org.jnp.interfaces"); + return props; + } + + protected StorageServiceHelper() { + // no instance + } }
participants (1)
-
tchemit@users.labs.libre-entreprise.org