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();
25 InputStream is = new FileInputStream(f);
32 return new ConnectorMap();
36 * Loads configurations from a file.
41 public static ConnectorMap load(InputStream is) throws IOException {
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);
49 byte[] data = bos.toByteArray();
51 List<String> idList = new ArrayList<String>();
52 Scanner scanner = new Scanner(new ByteArrayInputStream(data));
54 while (scanner.hasNextLine()) {
55 final String line = scanner.nextLine();
56 Matcher matcher = idPattern.matcher(line);
58 idList.add(matcher.group(1));
65 Properties props = new Properties();
66 props.load(new ByteArrayInputStream(data));
68 return new ConnectorMap(idList, props);
72 * Saves configurations to a file.
76 public static void save(ConnectorMap map) throws IOException {
77 ByteArrayOutputStream bos = new ByteArrayOutputStream();
79 byte[] bytes = bos.toByteArray();
80 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
81 FileOutputStream fos = new FileOutputStream(getPath());
83 fos.getChannel().transferFrom(Channels.newChannel(bis), 0, bytes.length);
90 * Saves configurations to a file.
95 public static void save(OutputStream os, ConnectorMap map) throws IOException {
97 ByteArrayOutputStream bos = new ByteArrayOutputStream();
98 map.toProperties().store(bos, "");
100 List<String> lines = new ArrayList<String>();
101 Scanner scanner = new Scanner(new ByteArrayInputStream(bos.toByteArray()));
103 while (scanner.hasNextLine()) {
104 String line = scanner.nextLine();
105 if (!line.trim().startsWith("#")) {
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);
117 for (String line : lines) {
126 public static long lastModified() {
127 return getPath().lastModified();
130 private static File getPath() {
131 return Bootstrap.getSystemFile(CONNECTOR_PROPERTIES_NAME);
134 private static final class ConnectorPropertyComparator implements
138 private final List<String> idList;
140 ConnectorPropertyComparator(List<String> idList) {
141 this.idList = idList;
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);
151 return index1 - index2;
154 private int getIdIndex(String s) {
155 String[] sa = s.split("\\.", 2);
156 if (sa.length >= 2) {
158 return idList.indexOf(id);