View Javadoc
1   /**
2    * Copyright 1&1 Internet AG, https://github.com/1and1/
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.oneandone.sushi;
17  
18  import net.oneandone.sushi.fs.Node;
19  import net.oneandone.sushi.fs.World;
20  import net.oneandone.sushi.metadata.Type;
21  import net.oneandone.sushi.metadata.reflect.ReflectSchema;
22  
23  import java.io.IOException;
24  
25  /**
26   * To work with property files, use create a Pojo representing all fields of the file, and use
27   * Sushi to map between instances and its file. This exsample demonstrates how to save and load
28   * "Config" instances to and from a property file.
29   */
30  public class PropertiesSample {
31      private static final World world = new World();
32  
33      /** Serialize object to xml and load the result back into an object */
34      public static void main(String[] args) throws IOException {
35          Node file;
36          Config config;
37  
38          file = world.getTemp().createTempFile();
39          config = new Config();
40          config.save(file);
41          System.out.println("default config created:");
42          System.out.println(file.readString());
43  
44          config.javaHome = "changed";
45          config.version = 2;
46          config.save(file);
47          System.out.println("saved changes:");
48          System.out.println(file.readString());
49  
50          config = Config.load(file);
51          System.out.println("loaded config: " + config);
52      }
53      
54      public static class Config {
55          private static final Type TYPE = new ReflectSchema(world).type(Config.class);
56  
57          public static Config load(Node file) throws IOException {
58              return (Config) TYPE.loadProperties(file.readProperties()).get();
59          }
60  
61          public String javaHome;
62          public int version;
63          public Complex complex;
64  
65          public Config() {
66              this("", 0, new Complex());
67          }
68  
69          public Config(String javaHome, int version, Complex complex) {
70              this.javaHome = javaHome;
71              this.version = version;
72              this.complex = complex;
73          }
74  
75          public void save(Node file) throws IOException {
76              file.writeProperties(TYPE.instance(this).toProperties());
77          }
78  
79          @Override
80          public String toString() {
81              return "javaHome=" + javaHome + ", version=" + version;
82          }
83      }
84  
85      public static class Complex {
86          public int left;
87          public int right;
88      }
89  }