1 package net.argius.stew;
4 import java.nio.channels.*;
6 import java.util.regex.*;
9 * ConnectorConfiguration is a helper for ConnectorMap.
11 public final class ConnectorConfiguration {
13 static final String CONNECTOR_PROPERTIES_NAME = "connector.properties";
15 private static final Pattern idPattern = Pattern.compile("^([^\\.]+)\\.name *=");
18 * Loads configurations from a file.
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]);
35 * Loads configurations from a file.
40 public static ConnectorMap load(InputStream is) throws IOException {
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);
49 List<String> idList = new ArrayList<String>();
50 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
51 BufferedReader reader = new BufferedReader(new InputStreamReader(bis));
53 for (String line; (line = reader.readLine()) != null;) {
54 Matcher matcher = idPattern.matcher(line);
56 idList.add(matcher.group(1));
63 Properties props = new Properties();
64 props.load(new ByteArrayInputStream(bos.toByteArray()));
66 return new ConnectorMap(idList, props);
70 * Saves configurations to a file.
74 public static void save(ConnectorMap map) throws IOException {
75 ByteArrayOutputStream bos = new ByteArrayOutputStream();
77 byte[] bytes = bos.toByteArray();
78 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
79 FileOutputStream fos = new FileOutputStream(getPath());
81 fos.getChannel().transferFrom(Channels.newChannel(bis), 0, bytes.length);
88 * Saves configurations to a file.
93 public static void save(OutputStream os, ConnectorMap map) throws IOException {
95 ByteArrayOutputStream bos = new ByteArrayOutputStream();
96 map.toProperties().store(bos, "");
98 List<String> lines = new ArrayList<String>();
99 Scanner scanner = new Scanner(new ByteArrayInputStream(bos.toByteArray()));
101 while (scanner.hasNextLine()) {
102 String line = scanner.nextLine();
103 if (!line.trim().startsWith("#")) {
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);
115 for (String line : lines) {
124 public static long lastModified() {
125 return getPath().lastModified();
128 private static File getPath() {
129 return Bootstrap.getSystemFile(CONNECTOR_PROPERTIES_NAME);
132 private static final class ConnectorPropertyComparator implements
136 private final List<String> idList;
138 ConnectorPropertyComparator(List<String> idList) {
139 this.idList = idList;
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);
149 return index1 - index2;
152 private int getIdIndex(String s) {
153 String[] sa = s.split("\\.", 2);
154 if (sa.length >= 2) {
156 return idList.indexOf(id);