View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.annotation.ManagedAttribute;
29  import org.eclipse.jetty.util.annotation.ManagedObject;
30  import org.eclipse.jetty.util.annotation.ManagedOperation;
31  import org.eclipse.jetty.util.annotation.Name;
32  import org.eclipse.jetty.util.resource.Resource;
33  
34  /**
35   * FileConfigurationManager
36   * 
37   * Supplies properties defined in a file.
38   */
39  @ManagedObject("Configure deployed webapps via properties")
40  public class PropertiesConfigurationManager implements ConfigurationManager
41  {
42      private String _properties;
43      private final Map<String,String> _map = new HashMap<String,String>();
44  
45      public PropertiesConfigurationManager(String properties)
46      {
47      }
48      
49      public PropertiesConfigurationManager()
50      {
51      }
52  
53      @ManagedAttribute("A file or URL of properties")
54      public void setFile(String resource) throws MalformedURLException, IOException
55      {
56          _properties=resource;
57          _map.clear();
58          loadProperties(_properties);
59      }
60  
61      public String getFile()
62      {
63          return _properties;
64      }
65      
66      @ManagedOperation("Set a property")
67      public void put(@Name("name")String name, @Name("value")String value)
68      {
69          _map.put(name,value);
70      }
71      
72      /**
73       * @see org.eclipse.jetty.deploy.ConfigurationManager#getProperties()
74       */
75      @Override
76      public Map<String, String> getProperties()
77      {
78          return new HashMap<>(_map);
79      }
80  
81      private void loadProperties(String resource) throws FileNotFoundException, IOException
82      {   
83          Resource file=Resource.newResource(resource);
84          if (file!=null && file.exists())
85          {
86              Properties properties = new Properties();
87              properties.load(file.getInputStream());
88              for (Map.Entry<Object, Object> entry : properties.entrySet())
89                  _map.put(entry.getKey().toString(),String.valueOf(entry.getValue()));
90          }
91      }
92  }