KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > log > RLogExecutor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.actions.log;
21
22 import org.netbeans.modules.versioning.system.cvss.CvsVersioningSystem;
23 import org.netbeans.modules.versioning.system.cvss.ExecutorSupport;
24 import org.netbeans.modules.versioning.system.cvss.ClientRuntime;
25 import org.netbeans.modules.versioning.system.cvss.util.CommandDuplicator;
26 import org.netbeans.modules.versioning.system.cvss.util.Utils;
27 import org.netbeans.lib.cvsclient.command.GlobalOptions;
28 import org.netbeans.lib.cvsclient.command.log.RlogCommand;
29 import org.netbeans.lib.cvsclient.command.log.LogInformation;
30 import org.netbeans.lib.cvsclient.admin.AdminHandler;
31 import org.netbeans.lib.cvsclient.event.FileInfoEvent;
32 import org.netbeans.lib.cvsclient.event.MessageEvent;
33 import org.openide.ErrorManager;
34 import org.openide.util.NbBundle;
35
36 import java.util.*;
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Executes a given 'rlog' command.
42  *
43  * @author Maros Sandor
44  */

45 public class RLogExecutor extends ExecutorSupport {
46     
47     private final File JavaDoc localRoot;
48     private boolean failedOnSymbolicLink;
49
50     /**
51      * Splits the original command into more commands if the original
52      * command would execute on incompatible files.
53      * See {@link #prepareBasicCommand(org.netbeans.lib.cvsclient.command.BasicCommand)}
54      * for more information.
55      *
56      * @param cmd command o execute
57      * @param roots folders that represent remote repositories to operate on
58      * @param options global option for the command
59      * @return array of executors that will execute the command (or array of splitted commands)
60      */

61     public static RLogExecutor [] splitCommand(RlogCommand cmd, File JavaDoc [] roots, GlobalOptions options) {
62         if (cmd.getDisplayName() == null) cmd.setDisplayName(NbBundle.getMessage(RLogExecutor.class, "MSG_RLogExecutor_CmdDisplayName"));
63         if (options == null) options = CvsVersioningSystem.createGlobalOptions();
64         
65         CvsVersioningSystem cvs = CvsVersioningSystem.getInstance();
66         AdminHandler ah = cvs.getAdminHandler();
67         CommandDuplicator cloner = CommandDuplicator.getDuplicator(cmd);
68
69         List executors = new ArrayList();
70         try {
71             File JavaDoc [][] split = ExecutorSupport.splitByCvsRoot(roots);
72             for (int i = 0; i < split.length; i++) {
73                 File JavaDoc [] files = split[i];
74                 GlobalOptions currentOptions = (GlobalOptions) options.clone();
75                 currentOptions.setCVSRoot(Utils.getCVSRootFor(files[0]));
76                 String JavaDoc remoteRepository = null;
77                 File JavaDoc directory = null;
78                 for (int j = 0; j < files.length; j++) {
79                     File JavaDoc file = files[j];
80                     File JavaDoc dir = file.isDirectory() ? file : file.getParentFile();
81                     String JavaDoc repository = ah.getRepositoryForDirectory(dir.getAbsolutePath(), "").substring(1); // NOI18N
82
if (remoteRepository == null || remoteRepository.equals(repository)) {
83                         remoteRepository = repository;
84                         directory = dir;
85                     } else {
86                         RlogCommand command = (RlogCommand) cloner.duplicate();
87                         command.setModule(remoteRepository);
88                         command.setDisplayName(NbBundle.getMessage(RLogExecutor.class, "MSG_RLogExecutor_CmdContext", remoteRepository));
89                         RLogExecutor executor = new RLogExecutor(cvs, command, directory, currentOptions);
90                         executors.add(executor);
91                         remoteRepository = repository;
92                         directory = dir;
93                     }
94                 }
95                 RlogCommand command = (RlogCommand) cloner.duplicate();
96                 command.setModule(remoteRepository);
97                 command.setDisplayName(NbBundle.getMessage(RLogExecutor.class, "MSG_RLogExecutor_CmdContext", remoteRepository));
98                 RLogExecutor executor = new RLogExecutor(cvs, command, directory, currentOptions);
99                 executors.add(executor);
100             }
101         } catch (IOException JavaDoc e) {
102             ErrorManager.getDefault().notify(e);
103             return new RLogExecutor[0];
104         }
105         return (RLogExecutor[]) executors.toArray(new RLogExecutor[executors.size()]);
106     }
107
108     private RLogExecutor(CvsVersioningSystem cvs, RlogCommand cmd, File JavaDoc localRoot, GlobalOptions options) {
109         super(cvs, cmd, options);
110         this.localRoot = localRoot;
111     }
112     
113     public File JavaDoc getFile() {
114         return localRoot;
115     }
116
117     public void fileInfoGenerated(FileInfoEvent e) {
118         LogInformation information = (LogInformation) e.getInfoContainer();
119         String JavaDoc remotePath = information.getRepositoryFilename();
120         File JavaDoc f = remote2local(remotePath);
121         information.setFile(f);
122         super.fileInfoGenerated(e);
123     }
124
125     protected void commandFinished(ClientRuntime.Result result) {
126         // repository command, nothing to do here
127
}
128
129     /**
130      * Translates repository path of a file to local path in user workdir. This includes some heuristics
131      * because we cannot do not know the actual path to repository on server.
132      *
133      * @param remotePath remote path on server as reposted by the rlog command
134      * @return File local location of the remote file (may not exist if the file is not checked out)
135      */

136     private File JavaDoc remote2local(String JavaDoc remotePath) {
137         AdminHandler ah = CvsVersioningSystem.getInstance().getAdminHandler();
138         String JavaDoc repository;
139         try {
140             // /javacvs/cvsmodule
141
repository = ah.getRepositoryForDirectory(localRoot.getAbsolutePath(), "").substring(1); // NOI18N
142
} catch (IOException JavaDoc e) {
143             ErrorManager.getDefault().notify(e);
144             return null;
145         }
146         
147         int idx = remotePath.indexOf(repository);
148         if (idx == -1) {
149             return null;
150         }
151         idx = remotePath.indexOf('/', idx + repository.length());
152         if (idx == -1) {
153             return null;
154         }
155         String JavaDoc remoteRelativePath = remotePath.substring(idx);
156         
157         idx = remoteRelativePath.indexOf(',');
158         if (idx != -1) {
159             remoteRelativePath = remoteRelativePath.substring(0, idx);
160         }
161
162         idx = remoteRelativePath.lastIndexOf('/');
163         if (idx != -1 && idx >= 6) {
164             if ("/Attic".equals(remoteRelativePath.substring(idx - 6, idx))) { // NOI18N
165
remoteRelativePath = remoteRelativePath.substring(0, idx - 6) + remoteRelativePath.substring(idx);
166             }
167         }
168         
169         return new File JavaDoc(localRoot, remoteRelativePath);
170     }
171
172     public List getLogEntries() {
173         return toRefresh;
174     }
175
176     /**
177      * Overridden to detect failures due to symbolic link server misconfiguration.
178      *
179      * @param e a message
180      */

181     public void messageSent(MessageEvent e) {
182         super.messageSent(e);
183         if (!failedOnSymbolicLink && e.isError()) {
184             String JavaDoc msg = e.getMessage();
185             failedOnSymbolicLink = msg != null && msg.indexOf("failed assertion `strncmp (repository,") != -1;
186         }
187     }
188
189     public boolean hasFailedOnSymbolicLink() {
190         return failedOnSymbolicLink;
191     }
192     
193     /**
194      * Does not log anything by default.
195      *
196      * @return false
197      */

198     protected boolean logCommandOutput() {
199         return false;
200     }
201
202     /**
203      * Be quiet if the command failed due to symbolic link problems.
204      */

205     protected void report(String JavaDoc title, String JavaDoc prompt, List messages, int type) {
206         if (!failedOnSymbolicLink) super.report(title, prompt, messages, type);
207     }
208
209 }
210
Popular Tags