View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.deploy;
15  
16  import java.io.FileNotFoundException;
17  import java.io.IOException;
18  import java.net.MalformedURLException;
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.eclipse.jetty.util.resource.Resource;
24  
25  /**
26   * FileConfigurationManager
27   * 
28   * Supplies properties defined in a file.
29   */
30  public class FileConfigurationManager implements ConfigurationManager
31  {
32      private Resource _file;
33      private Map<String,String> _map = new HashMap<String,String>();
34  
35      public FileConfigurationManager()
36      {
37      }
38  
39      public void setFile(String filename) throws MalformedURLException, IOException
40      {
41          _file = Resource.newResource(filename);
42      }
43  
44      /**
45       * @see org.eclipse.jetty.deploy.ConfigurationManager#getProperties()
46       */
47      public Map<String, String> getProperties()
48      {
49          try
50          {
51              loadProperties();
52              return _map;
53          }
54          catch (Exception e)
55          {
56              throw new RuntimeException(e);
57          }
58      }
59  
60      private void loadProperties() throws FileNotFoundException, IOException
61      {
62          if (_map.isEmpty() && _file!=null)
63          {
64              Properties properties = new Properties();
65              properties.load(_file.getInputStream());
66              for (Map.Entry<Object, Object> entry : properties.entrySet())
67                  _map.put(entry.getKey().toString(),String.valueOf(entry.getValue()));
68          }
69      }
70  }