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
14
15
16
17
18
19
20
21
22
23
24 public class FileContextFactory implements ContextFactory
25 {
26
27 private static FileContext context = null;
28
29
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 }