1 package nl.tudelft.simulation.naming.context;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.ObjectInputStream;
7 import java.net.URI;
8 import java.util.Hashtable;
9
10 import org.djutils.logger.CategoryLogger;
11
12 /**
13 * A factory for FileContext instances, automatically invoked by JNDI when the correct jndi.properties file has been used.
14 * <p>
15 * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16 * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
17 * project is distributed under a three-clause BSD-style license, which can be found at
18 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
19 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
20 * </p>
21 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
22 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23 */
24 public class FileContextFactory implements ContextFactory
25 {
26 /** context refers to the static JVMContext. */
27 private static FileContext context = null;
28
29 /** {@inheritDoc} */
30 @Override
31 public synchronized ContextInterface getInitialContext(final Hashtable<?, ?> environment, final String atomicName)
32 {
33 if (context == null)
34 {
35 try
36 {
37 URI fileURI = new URI(environment.get("java.naming.provider.url").toString());
38 File file = new File(fileURI);
39 if (file.exists())
40 {
41 ObjectInputStream stream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
42 FileContextFactory.context = (FileContext) stream.readObject();
43 stream.close();
44 }
45 else
46 {
47 FileContextFactory.context = new FileContext(file, atomicName);
48 }
49 }
50 catch (Exception exception)
51 {
52 CategoryLogger.always().error(exception, "getInitialContext");
53 }
54 }
55 return context;
56 }
57 }