001: package com.ice.jcvsweb.schedule;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.PrintWriter;
007: import java.text.SimpleDateFormat;
008: import java.util.Date;
009: import javax.servlet.ServletContext;
010:
011: import com.ice.cvsc.*;
012: import com.ice.jcvsweb.bean.*;
013: import com.ice.jcvsweb.helper.ContextHelper;
014: import com.ice.jcvsweb.manager.JCVSConfigManager;
015:
016: public class ScheduledCheckout extends ScheduledEvent {
017: private JCVSProjectView view = null;
018:
019: public ScheduledCheckout(ServletContext ctx, JCVSProjectView view) {
020: super (ctx);
021: this .view = view;
022: this .view.setState(JCVSProjectView.PS_CHKOUT_SCHED);
023: this .view
024: .setReason("The project is scheduled to be checked out.");
025: }
026:
027: public void run() {
028: this .getServletContext().log("ScheduledCheckout: " + this .view);
029:
030: try {
031: this .checkoutProjectView();
032: } catch (IOException ex) {
033: // UNDONE HIGH
034: this .getServletContext().log("error checking out", ex);
035: }
036:
037: this .getServletContext().log("ScheduledCheckout finished.");
038: }
039:
040: private void checkoutProjectView() throws IOException {
041: CVSClient cvsClient;
042: CVSProject cvsProject;
043: CVSRequest cvsRequest;
044: CVSResponse cvsResponse;
045: ServletContext ctx = this .getServletContext();
046:
047: ctx.log("[SCHEDCO] checkoutProjectView() '" + this .view + "'");
048:
049: JCVSProject project = this .view.getProject();
050: JCVSProjectDef def = project.getDef();
051:
052: if (this .view != null) {
053: this .view.setState(JCVSProjectView.PS_CHKOUT);
054: this .view
055: .setReason("The project is currently being checked out.");
056:
057: CVSEntryVector entries = new CVSEntryVector();
058: CVSArgumentVector arguments = new CVSArgumentVector();
059:
060: String coDir = this .view.getCodir();
061:
062: ctx.log("[SCHEDCO] coDirStr = " + coDir);
063:
064: if (coDir != null && coDir.length() > 0) {
065: arguments.appendArgument("-d");
066: arguments.appendArgument(coDir);
067: }
068:
069: arguments.appendArgument("-r");
070: arguments.appendArgument(this .view.getCvsTag());
071:
072: boolean dbgCheckout = def.getDebugCheckout();
073: ctx.log("[SCHEDCO] dbgCheckout = " + dbgCheckout);
074:
075: arguments.appendArgument(this .view.getCvsModule());
076:
077: String viewDir = this .view.getViewDirectory();
078: ctx.log("[SCHEDCO] Local = " + viewDir);
079:
080: String coDirPath = ContextHelper.getRealAbsolutePath(ctx,
081: viewDir);
082: ctx.log("[SCHEDCO] coDirPath = " + coDirPath);
083:
084: File coDirF = new File(coDirPath);
085: ctx.log("[SCHEDCO] coDirF = " + coDirF);
086:
087: if (!coDirF.exists()) {
088: if (!coDirF.mkdirs()) {
089: throw new IOException(
090: "could not create working directory '"
091: + coDirF.getPath() + "'.");
092: }
093: }
094:
095: int cvsPort = def.getCvsPort();
096: int connMethod = def.getCvsMethod();
097: boolean isPServer = (connMethod == CVSRequest.METHOD_INETD);
098:
099: ctx.log("[SCHEDCO] Connection Port = " + cvsPort);
100: ctx.log("[SCHEDCO] Connection Method = " + connMethod
101: + ", '" + CVSRequest.getConnMethodName(connMethod)
102: + "'");
103: ctx.log("[SCHEDCO] Connection isPServer = " + isPServer);
104:
105: cvsClient = new CVSClient(def.getCvsHost(), cvsPort);
106:
107: cvsProject = new CVSProject(cvsClient);
108:
109: CVSProjectDef cvsProjectDef = new CVSProjectDef(connMethod,
110: isPServer, false, def.getCvsHost(), def
111: .getCvsLogin(), def.getCvsRoot(), this .view
112: .getCvsModule());
113:
114: cvsProject.setProjectDef(cvsProjectDef);
115:
116: String passwd = (connMethod != CVSRequest.METHOD_INETD ? def
117: .getCvsPassword()
118: : CVSScramble.scramblePassword(
119: def.getCvsPassword(), 'A'));
120:
121: cvsProject.setUserName(def.getCvsLogin());
122: cvsProject.setPassword(passwd);
123:
124: cvsProject.setRepository(this .view.getCvsModule());
125:
126: String tempDir = def.getTempDirectory();
127: String tempDirPath = ContextHelper.getRealAbsolutePath(this
128: .getServletContext(), tempDir);
129: ctx.log("[SCHEDCO] tempDirPath = " + tempDirPath);
130: cvsProject.setTempDirectory(tempDirPath);
131:
132: cvsProject.setRootDirectory(def.getCvsRoot());
133: cvsProject.setLocalRootDirectory(coDirF.getPath());
134: cvsProject.setPServer(isPServer);
135: cvsProject.setConnectionMethod(connMethod);
136: cvsProject.setAllowsGzipFileMode(false);
137:
138: // We must make sure that the Project has its root entry, as
139: // CVSProject will not be able to create it from the context
140: // that the server will send with the checkout.
141:
142: cvsProject.establishRootEntry(def.getCvsRoot());
143:
144: cvsRequest = new CVSRequest();
145:
146: cvsRequest.setPServer(isPServer);
147: cvsRequest.setUserName(cvsProject.getUserName());
148: cvsRequest.setPassword(cvsProject.getPassword());
149:
150: cvsRequest.setConnectionMethod(cvsProject
151: .getConnectionMethod());
152: cvsRequest.setServerCommand(cvsProject.getServerCommand());
153: cvsRequest.setRshProcess(cvsProject.getRshProcess());
154:
155: cvsRequest.setPort(cvsPort);
156: cvsRequest.setHostName(def.getCvsHost());
157:
158: cvsRequest.setRepository(this .view.getCvsModule());
159: cvsRequest.setLocalDirectory(cvsProject
160: .getLocalRootDirectory());
161: cvsRequest.setRootDirectory(cvsProject.getRootDirectory());
162: cvsRequest.setRootRepository(cvsProject.getRootDirectory());
163:
164: cvsRequest.setSetVariables(cvsProject.getSetVariables());
165:
166: cvsRequest.setCommand("co");
167:
168: cvsRequest.sendModule = false;
169: cvsRequest.sendArguments = true;
170: cvsRequest.sendEntries = false;
171: cvsRequest.handleUpdated = true;
172: cvsRequest.allowOverWrites = true;
173: cvsRequest.queueResponse = false;
174: cvsRequest.responseHandler = cvsProject;
175: cvsRequest.includeNotifies = false;
176: cvsRequest.handleEntries = true;
177:
178: cvsRequest.traceRequest = dbgCheckout;
179: cvsRequest.traceResponse = dbgCheckout;
180: cvsRequest.traceTCPData = dbgCheckout;
181: cvsRequest.traceProcessing = dbgCheckout;
182:
183: cvsRequest.allowGzipFileMode = false;
184: cvsProject.allowsGzipFileMode();
185:
186: cvsRequest.setEntries(entries);
187:
188: cvsRequest.appendArguments(arguments);
189:
190: CVSBufferedUI cvsUI = new CVSBufferedUI();
191: cvsRequest.setUserInterface(cvsUI);
192:
193: // UNDONE Better log names, including view, and in subdirs!
194: //
195: SimpleDateFormat sdf = new SimpleDateFormat(
196: "yyyyMMdd-HHmmss");
197: Date tStamp = new Date();
198:
199: String logDir = this .view.getLogDirectory();
200: String logPath = ContextHelper.getRealAbsolutePath(this
201: .getServletContext(), logDir);
202: File logDirF = new File(logPath);
203:
204: ctx.log("[SCHEDCO] logDirF = " + logDirF);
205:
206: if (!logDirF.exists()) {
207: logDirF.mkdirs();
208: }
209:
210: String logFileName = "co-" + def.getKey() + "-"
211: + this .view.getKey() + "-" + sdf.format(tStamp)
212: + ".log";
213:
214: File logFile = new File(logDirF, logFileName);
215:
216: PrintWriter redir = new PrintWriter(new FileWriter(logFile));
217:
218: cvsRequest.setRedirectWriter(redir);
219:
220: ctx.log("[SCHEDCO] Sending CVS command");
221:
222: cvsResponse = cvsClient.processCVSRequest(cvsRequest);
223:
224: cvsProject.processCVSResponse(cvsRequest, cvsResponse);
225:
226: ctx.log("[SCHEDCO] Processed CVS command.");
227:
228: if (cvsResponse != null && !cvsRequest.saveTempFiles) {
229: cvsResponse.deleteTempFiles();
230: }
231:
232: if (cvsResponse.getStatus() != CVSResponse.OK) {
233: redir.println("*** Checkout Failed:");
234: redir.println(cvsResponse.getDisplayResults());
235: }
236:
237: redir.close();
238:
239: if (cvsResponse.getStatus() == CVSResponse.OK) {
240: // UNDONE
241: // Shouldn't the state be "synchronized" access?!?!
242: this .view.setState(JCVSProjectView.PS_INIT);
243: this .view.setReason("View checkout success. Log: "
244: + logFileName);
245: } else {
246: this .view.setState(JCVSProjectView.PS_BROKEN);
247: this .view.setReason("View checkout failed. Log: "
248: + logFileName);
249: }
250: }
251:
252: Runtime rt = Runtime.getRuntime();
253: ctx.log("[SCHEDCO] Checked out " + this .view.getViewName());
254: ctx.log("[SCHEDCO] Memory Free " + rt.freeMemory() + " Total "
255: + rt.totalMemory());
256: }
257:
258: }
|