OSDN Git Service

Upgrade to 4.2.0
[stew/Stew4.git] / src / net / argius / stew / ConnectorConfiguration.java
1 package net.argius.stew;
2
3 import java.io.*;
4 import java.nio.channels.*;
5 import java.util.*;
6 import java.util.regex.*;
7
8 /**
9  * ConnectorConfiguration is a helper for ConnectorMap.
10  */
11 public final class ConnectorConfiguration {
12
13     static final String CONNECTOR_PROPERTIES_NAME = "connector.properties";
14
15     private static final Pattern idPattern = Pattern.compile("^([^\\.]+)\\.name *=");
16
17     /**
18      * Loads configurations from a file.
19      * @return
20      * @throws IOException
21      */
22     public static ConnectorMap load() throws IOException {
23         final File f = getPath();
24         if (f.exists()) {
25             InputStream is = new FileInputStream(f);
26             try {
27                 return load(is);
28             } finally {
29                 is.close();
30             }
31         }
32         return new ConnectorMap();
33     }
34
35     /**
36      * Loads configurations from a file.
37      * @param is
38      * @return
39      * @throws IOException
40      */
41     public static ConnectorMap load(InputStream is) throws IOException {
42         // cache for reuse
43         ByteArrayOutputStream bos = new ByteArrayOutputStream();
44         byte[] buffer = new byte[4096];
45         for (int c; (c = is.read(buffer)) >= 0;) {
46             bos.write(buffer, 0, c);
47         }
48         bos.flush();
49         byte[] data = bos.toByteArray();
50         // create ID list
51         List<String> idList = new ArrayList<String>();
52         Scanner scanner = new Scanner(new ByteArrayInputStream(data));
53         try {
54             while (scanner.hasNextLine()) {
55                 final String line = scanner.nextLine();
56                 Matcher matcher = idPattern.matcher(line);
57                 if (matcher.find()) {
58                     idList.add(matcher.group(1));
59                 }
60             }
61         } finally {
62             scanner.close();
63         }
64         // read as Properties
65         Properties props = new Properties();
66         props.load(new ByteArrayInputStream(data));
67         // creates a instance
68         return new ConnectorMap(idList, props);
69     }
70
71     /**
72      * Saves configurations to a file.
73      * @param map
74      * @throws IOException
75      */
76     public static void save(ConnectorMap map) throws IOException {
77         ByteArrayOutputStream bos = new ByteArrayOutputStream();
78         save(bos, map);
79         byte[] bytes = bos.toByteArray();
80         ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
81         FileOutputStream fos = new FileOutputStream(getPath());
82         try {
83             fos.getChannel().transferFrom(Channels.newChannel(bis), 0, bytes.length);
84         } finally {
85             fos.close();
86         }
87     }
88
89     /**
90      * Saves configurations to a file.
91      * @param os
92      * @param map
93      * @throws IOException
94      */
95     public static void save(OutputStream os, ConnectorMap map) throws IOException {
96         // import using store
97         ByteArrayOutputStream bos = new ByteArrayOutputStream();
98         map.toProperties().store(bos, "");
99         // lines to elements
100         List<String> lines = new ArrayList<String>();
101         Scanner scanner = new Scanner(new ByteArrayInputStream(bos.toByteArray()));
102         try {
103             while (scanner.hasNextLine()) {
104                 String line = scanner.nextLine();
105                 if (!line.trim().startsWith("#")) {
106                     lines.add(line);
107                 }
108             }
109         } finally {
110             scanner.close();
111         }
112         // rewrites records sorted by ID
113         Comparator<String> c = new ConnectorPropertyComparator(new ArrayList<String>(map.keySet()));
114         Collections.sort(lines, c);
115         PrintWriter out = new PrintWriter(os);
116         try {
117             for (String line : lines) {
118                 out.println(line);
119             }
120             out.flush();
121         } finally {
122             out.close();
123         }
124     }
125
126     public static long lastModified() {
127         return getPath().lastModified();
128     }
129
130     private static File getPath() {
131         return Bootstrap.getSystemFile(CONNECTOR_PROPERTIES_NAME);
132     }
133
134     private static final class ConnectorPropertyComparator implements
135                                                           Comparator<String>,
136                                                           Serializable {
137
138         private final List<String> idList;
139
140         ConnectorPropertyComparator(List<String> idList) {
141             this.idList = idList;
142         }
143
144         @Override
145         public int compare(String s1, String s2) {
146             int index1 = getIdIndex(s1);
147             int index2 = getIdIndex(s2);
148             if (index1 == index2) {
149                 return s1.compareTo(s2);
150             }
151             return index1 - index2;
152         }
153
154         private int getIdIndex(String s) {
155             String[] sa = s.split("\\.", 2);
156             if (sa.length >= 2) {
157                 String id = sa[0];
158                 return idList.indexOf(id);
159             }
160             return -1;
161         }
162
163     }
164
165 }