Debian Codegraph @

src/java/org/astrogrid/samp/hub/LockWriter.java

raw
1 package org.astrogrid.samp.hub;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10 import org.astrogrid.samp.Platform;
11
12 /**
13 * Writes records to a SAMP Standard Profile hub lockfile.
14 *
15 * @author Mark Taylor
16 * @since 15 Jul 2008
17 */
18 public class LockWriter {
19
20 private final OutputStream out_;
21 private static final byte[] linesep_ = getLineSeparator();
22 private static final String TOKEN_REGEX = "[a-zA-Z0-9\\-_\\.]+";
23 private static final Pattern TOKEN_PATTERN = Pattern.compile( TOKEN_REGEX );
24
25 /**
26 * Constructs a writer for writing to a given output stream.
27 *
28 * @param out output stream
29 */
30 public LockWriter( OutputStream out ) {
31 out_ = out;
32 }
33
34 /**
35 * Writes all the assignments in a given map to the lockfile.
36 *
37 * @param map assignment set to output
38 */
39 public void writeAssignments( Map map ) throws IOException {
40 for ( Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
41 Map.Entry entry = (Map.Entry) it.next();
42 writeAssignment( (String) entry.getKey(),
43 (String) entry.getValue() );
44 }
45 }
46
47 /**
48 * Writes a single assignment to the lockfile.
49 *
50 * @param name assignment key
51 * @param value assignment value
52 */
53 public void writeAssignment( String name, String value )
54 throws IOException {
55 if ( TOKEN_PATTERN.matcher( name ).matches() ) {
56 writeLine( name + "=" + value );
57 }
58 else {
59 throw new IllegalArgumentException( "Bad name sequence: " + name
60 + " !~" + TOKEN_REGEX );
61 }
62 }
63
64 /**
65 * Writes a comment line to the lockfile.
66 *
67 * @param comment comment text
68 */
69 public void writeComment( String comment ) throws IOException {
70 writeLine( "# " + comment );
71 }
72
73 /**
74 * Writes a blank line to the lockfile.
75 */
76 public void writeLine() throws IOException {
77 out_.write( linesep_ );
78 }
79
80 /**
81 * Writes a line of text to the lockfile, terminated with a line-end.
82 *
83 * @param line line to write
84 */
85 protected void writeLine( String line ) throws IOException {
86 byte[] bbuf = new byte[ line.length() ];
87 for ( int i = 0; i < line.length(); i++ ) {
88 int c = line.charAt( i );
89 if ( c < 0x20 || c > 0x7f ) {
90 throw new IllegalArgumentException( "Illegal character 0x" +
91 Integer.toHexString( c ) );
92 }
93 bbuf[ i ] = (byte) c;
94 }
95 out_.write( bbuf );
96 writeLine();
97 }
98
99 /**
100 * Closes the output stream.
101 * May be required to ensure that all data is written.
102 */
103 public void close() throws IOException {
104 out_.close();
105 }
106
107 /**
108 * Sets the permissions on a given file suitably for a SAMP Standard
109 * Profile lockfile. This means that nobody apart from the file's
110 * owner can read it.
111 *
112 * @param file file to set access permissions on
113 */
114 public static void setLockPermissions( File file ) throws IOException {
115 Platform.getPlatform().setPrivateRead( file );
116 }
117
118 /**
119 * Returns the platform-specific line separator sequence as an array of
120 * bytes.
121 *
122 * @return line separator sequence
123 */
124 private static final byte[] getLineSeparator() {
125 String linesep = System.getProperty( "line.separator" );
126 if ( linesep.matches( "[\\r\\n]+" ) ) {
127 byte[] lsbuf = new byte[ linesep.length() ];
128 for ( int i = 0; i < linesep.length(); i++ ) {
129 lsbuf[ i ] = (byte) linesep.charAt( i );
130 }
131 return lsbuf;
132 }
133 else {
134 return new byte[] { (byte) '\n' };
135 }
136 }
137 }