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