001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileReader;
024: import java.io.FileWriter;
025: import java.io.IOException;
026: import java.io.PrintWriter;
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.Task;
030: import org.apache.tools.ant.util.StringUtils;
031:
032: /**
033: * Adds an new entry to a CVS password file.
034: *
035: *
036: * @since Ant 1.4
037: *
038: * @ant.task category="scm"
039: */
040: public class CVSPass extends Task {
041: /** CVS Root */
042: private String cvsRoot = null;
043: /** Password file to add password to */
044: private File passFile = null;
045: /** Password to add to file */
046: private String password = null;
047:
048: /** Array contain char conversion data */
049: private final char[] shifts = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
050: 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
051: 26, 27, 28, 29, 30, 31, 114, 120, 53, 79, 96, 109, 72, 108,
052: 70, 64, 76, 67, 116, 74, 68, 87, 111, 52, 75, 119, 49, 34,
053: 82, 81, 95, 65, 112, 86, 118, 110, 122, 105, 41, 57, 83,
054: 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91, 35, 125,
055: 55, 54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107,
056: 106, 56, 36, 121, 117, 104, 101, 100, 69, 73, 99, 63, 94,
057: 93, 39, 37, 61, 48, 58, 113, 32, 90, 44, 98, 60, 51, 33,
058: 97, 62, 77, 84, 80, 85, 223, 225, 216, 187, 166, 229, 189,
059: 222, 188, 141, 249, 148, 200, 184, 136, 248, 190, 199, 170,
060: 181, 204, 138, 232, 218, 183, 255, 234, 220, 247, 213, 203,
061: 226, 193, 174, 172, 228, 252, 217, 201, 131, 230, 197, 211,
062: 145, 238, 161, 179, 160, 212, 207, 221, 254, 173, 202, 146,
063: 224, 151, 140, 196, 205, 130, 135, 133, 143, 246, 192, 159,
064: 244, 239, 185, 168, 215, 144, 139, 165, 180, 157, 147, 186,
065: 214, 176, 227, 231, 219, 169, 175, 156, 206, 198, 129, 164,
066: 150, 210, 154, 177, 134, 127, 182, 128, 158, 208, 162, 132,
067: 167, 209, 149, 241, 153, 251, 237, 236, 171, 195, 243, 233,
068: 253, 240, 194, 250, 191, 155, 142, 137, 245, 235, 163, 242,
069: 178, 152 };
070:
071: /**
072: * Create a CVS task using the default cvspass file location.
073: */
074: public CVSPass() {
075: passFile = new File(System.getProperty("cygwin.user.home",
076: System.getProperty("user.home"))
077: + File.separatorChar + ".cvspass");
078: }
079:
080: /**
081: * Does the work.
082: *
083: * @exception BuildException if something goes wrong with the build
084: */
085: public final void execute() throws BuildException {
086: if (cvsRoot == null) {
087: throw new BuildException("cvsroot is required");
088: }
089: if (password == null) {
090: throw new BuildException("password is required");
091: }
092:
093: log("cvsRoot: " + cvsRoot, Project.MSG_DEBUG);
094: log("password: " + password, Project.MSG_DEBUG);
095: log("passFile: " + passFile, Project.MSG_DEBUG);
096:
097: BufferedReader reader = null;
098: PrintWriter writer = null;
099: try {
100: StringBuffer buf = new StringBuffer();
101:
102: if (passFile.exists()) {
103: reader = new BufferedReader(new FileReader(passFile));
104:
105: String line = null;
106:
107: while ((line = reader.readLine()) != null) {
108: if (!line.startsWith(cvsRoot)) {
109: buf.append(line).append(StringUtils.LINE_SEP);
110: }
111: }
112: }
113:
114: String pwdfile = buf.toString() + cvsRoot + " A"
115: + mangle(password);
116:
117: log("Writing -> " + pwdfile, Project.MSG_DEBUG);
118:
119: writer = new PrintWriter(new FileWriter(passFile));
120:
121: writer.println(pwdfile);
122: } catch (IOException e) {
123: throw new BuildException(e);
124: } finally {
125: if (reader != null) {
126: try {
127: reader.close();
128: } catch (IOException e) {
129: // ignore
130: }
131: }
132: if (writer != null) {
133: writer.close();
134: }
135: }
136: }
137:
138: private final String mangle(String password) {
139: StringBuffer buf = new StringBuffer();
140: for (int i = 0; i < password.length(); i++) {
141: buf.append(shifts[password.charAt(i)]);
142: }
143: return buf.toString();
144: }
145:
146: /**
147: * The CVS repository to add an entry for.
148: *
149: * @param cvsRoot the CVS repository
150: */
151: public void setCvsroot(String cvsRoot) {
152: this .cvsRoot = cvsRoot;
153: }
154:
155: /**
156: * Password file to add the entry to.
157: *
158: * @param passFile the password file.
159: */
160: public void setPassfile(File passFile) {
161: this .passFile = passFile;
162: }
163:
164: /**
165: * Password to be added to the password file.
166: *
167: * @param password the password.
168: */
169: public void setPassword(String password) {
170: this.password = password;
171: }
172:
173: }
|