package nl.tudelft.simulation.naming; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.net.URI; import java.util.Hashtable; import javax.naming.Context; import javax.naming.spi.InitialContextFactory; import nl.tudelft.simulation.logger.Logger; /** * A factory for FileContext instances, automatically invoked by JNDI when the correct jndi.properties file has been * used. *

* (c) copyright 2002-2005 Delft University of Technology , the * Netherlands.
* See for project information www.simulation.tudelft.nl
* License of use: Lesser General Public License (LGPL) , no * warranty. * @author Peter Jacobs * @version 1.3 2004-03-24 * @since 1.5 */ public class FileContextFactory implements InitialContextFactory { /** context refers to the static JVMContext. */ private static FileContext context = null; /** {@inheritDoc} */ @Override public synchronized Context getInitialContext(final Hashtable environment) { if (context == null) { try { URI fileURI = new URI(environment.get("java.naming.provider.url").toString()); File file = new File(fileURI); if (file.exists()) { ObjectInputStream stream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); FileContextFactory.context = (FileContext) stream.readObject(); stream.close(); } else { FileContextFactory.context = new FileContext(file); } } catch (Exception exception) { Logger.warning(this, "getInitialContext", exception); } } return context; } }