View Javadoc

1   /*
2    * @(#)FileContextFactory.java Feb 1, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.naming;
11  
12  import java.io.BufferedInputStream;
13  import java.io.File;
14  import java.io.FileInputStream;
15  import java.io.ObjectInputStream;
16  import java.net.URI;
17  import java.util.Hashtable;
18  
19  import javax.naming.Context;
20  import javax.naming.spi.InitialContextFactory;
21  
22  import nl.tudelft.simulation.logger.Logger;
23  
24  /***
25   * A factory for FileContext instances, automatically invoked by JNDI when the
26   * correct jndi.properties file has been used.
27   * <p>
28   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
29   * University of Technology </a>, the Netherlands. <br>
30   * See for project information <a href="http://www.simulation.tudelft.nl">
31   * www.simulation.tudelft.nl </a> <br>
32   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
33   * License (GPL) </a>, no warranty <br>
34   * 
35   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36   *         Jacobs </a>
37   * @version 1.3 2004-03-24
38   * @since 1.0
39   */
40  public class FileContextFactory implements InitialContextFactory
41  {
42  	/*** context refers to the static JVMContext */
43  	private static FileContext context = null;
44  
45  	/***
46  	 * @see javax.naming.spi.InitialContextFactory#getInitialContext( Hashtable)
47  	 */
48  	public synchronized Context getInitialContext(final Hashtable environment)
49  	{
50  		if (context == null)
51  		{
52  			try
53  			{
54  				URI fileURI = new URI(environment.get(
55  						"java.naming.provider.url").toString());
56  				File file = new File(fileURI);
57  				if (file.exists())
58  				{
59  					ObjectInputStream stream = new ObjectInputStream(
60  							new BufferedInputStream(new FileInputStream(file)));
61  					FileContextFactory.context = (FileContext) stream
62  							.readObject();
63  					stream.close();
64  				} else
65  				{
66  					FileContextFactory.context = new FileContext(file);
67  				}
68  			} catch (Exception exception)
69  			{
70  				Logger.warning(this, "getInitialContext", exception);
71  			}
72  		}
73  		return context;
74  	}
75  }