1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc; |
|
27 |
|
28 import java.io.*; |
|
29 |
|
30 /** |
|
31 * A generic configuration writing class, used for adding, removing, or |
|
32 * updating lines in a #-commented line-based configuration file while |
|
33 * maintaining existing comments. |
|
34 */ |
|
35 public abstract class ConfigWriter |
|
36 { |
|
37 /** |
|
38 * Called for each existing non-comment line if the default |
|
39 * {@link #processFile} implementation is used. Should be overridden by |
|
40 * subclasses that don't override {@link #processFile}. |
|
41 * |
|
42 * @param writer the writer to which new configuration should be emitted |
|
43 * @param line the input line being processed |
|
44 * @return {@code true} if the line was processed, {@code false} if the |
|
45 * caller should emit the line |
|
46 * @throws java.io.IOException |
|
47 */ |
|
48 protected boolean processLine(BufferedWriter writer, String line) |
|
49 throws IOException |
|
50 { |
|
51 return (false); |
|
52 } |
|
53 |
|
54 /** |
|
55 * Called when the end of the configuration file is reached if the |
|
56 * default {@link #processFile} implementation is used. Default |
|
57 * implementation does nothing. |
|
58 * |
|
59 * @param writer the writer to which new configuration should be emitted |
|
60 * @throws java.io.IOException |
|
61 */ |
|
62 protected void processEOF(BufferedWriter writer) |
|
63 throws IOException |
|
64 { |
|
65 } |
|
66 |
|
67 /** |
|
68 * Reads the configuration found in the old configuration file and writes |
|
69 * the updated configuration to the new configuration file. The default |
|
70 * implementation copies each comment line and line for which |
|
71 * {@link #processLine} returns {@code false}, and calls {@link #processEOF} |
|
72 * when the end of the input file is reached. |
|
73 * |
|
74 * @param reader the old configuration file |
|
75 * @param writer the new configuration file |
|
76 * @throws java.io.IOException |
|
77 */ |
|
78 protected void processFile(BufferedReader reader, BufferedWriter writer) |
|
79 throws IOException |
|
80 { |
|
81 String line; |
|
82 while ((line = reader.readLine()) != null) { |
|
83 if (line.startsWith("#") || !processLine(writer, line)) { |
|
84 writer.write(line); |
|
85 writer.newLine(); |
|
86 } |
|
87 } |
|
88 |
|
89 processEOF(writer); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Transforms the configuration of the specified file. Creates a |
|
94 * temporary file, passes it and the original to {@link #processFile}, |
|
95 * and renames it over the original. |
|
96 * |
|
97 * @param filename the file to transform |
|
98 * @throws java.io.IOException |
|
99 */ |
|
100 protected void writeConfig(String filename) throws IOException |
|
101 { |
|
102 /* |
|
103 * Assume we're dealing with a trusted location. |
|
104 */ |
|
105 String tmpfile = filename + ".tmp"; |
|
106 BufferedReader reader = new BufferedReader(new FileReader(filename)); |
|
107 BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile)); |
|
108 |
|
109 try { |
|
110 processFile(reader, writer); |
|
111 } finally { |
|
112 /* |
|
113 * Close files and move temporary file over the original. |
|
114 */ |
|
115 reader.close(); |
|
116 writer.close(); |
|
117 (new File(tmpfile)).renameTo(new File(filename)); |
|
118 } |
|
119 } |
|
120 } |
|