OSDN Git Service

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