View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.environment.sysprops;
2   
3   import org.apache.maven.plugin.logging.Log;
4   import org.codehaus.mojo.jaxb2.shared.environment.AbstractLogAwareFacet;
5   
6   /**
7    * EnvironmentFacet which saves the value of a system property for the duration
8    * of executing a tool. This may be required for tools (such as the XJC tool) which
9    * may overwrite property values for its own purpose.
10   * 
11   * Unlike {@link SystemPropertyChangeEnvironmentFacet}, this does not a set a new
12   * property value itself, just saves the old value and later restores or clears it.
13   *  
14   * This facet accepts the key of the property to save.
15   *
16   * @author <a href="https://github.com/shelgen">Svein Elgst&oslash;en</a>
17   * @since 2.5
18   */
19  public final class SystemPropertySaveEnvironmentFacet extends AbstractLogAwareFacet {
20      private final String key;
21      private final String originalValue;
22  
23      /**
24       * Creates a SystemPropertySave which will remember the original value of the
25       * supplied system property for the duration of this SystemPropertySave.
26       *
27       * @param key A non-null key.
28       * @param log The active Maven Log.
29       */
30      public SystemPropertySaveEnvironmentFacet(final String key, final Log log) {
31          // Delegate
32          super(log);
33  
34          // Assign internal state
35          this.key = key;
36          this.originalValue = System.getProperty(key);
37      }
38  
39      /**
40       * {@inheritDoc}
41       */
42      @Override
43      public void restore() {
44          if (originalValue != null) {
45              System.setProperty(key, originalValue);
46          } else {
47              System.clearProperty(key);
48          }
49  
50          if (log.isDebugEnabled()) {
51              log.debug("Restored " + toString());
52          }
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public void setup() {
60          // Do nothing, value is saved in constructor
61  
62          if (log.isDebugEnabled()) {
63              log.debug("Setup " + toString());
64          }
65      }
66  
67      @Override
68      public String toString() {
69          return "SysProp key [" + key + "], saved value: [" + originalValue + "]";
70      }
71  }