KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > RepositoryCommand


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 package org.netbeans.lib.cvsclient.command;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.netbeans.lib.cvsclient.*;
25 import org.netbeans.lib.cvsclient.admin.*;
26 import org.netbeans.lib.cvsclient.connection.*;
27 import org.netbeans.lib.cvsclient.event.*;
28 import org.netbeans.lib.cvsclient.request.*;
29
30 /**
31  * A class that provides common functionality for CVS commands that
32  * operate upon the repository.
33  *
34  * @author Martin Entlicher
35  */

36 public abstract class RepositoryCommand extends BuildableCommand {
37     /**
38      * The requests that are sent and processed.
39      */

40     protected List requests = new LinkedList();
41
42     /**
43      * The client services that are provided to this command.
44      */

45     protected ClientServices clientServices;
46
47     /**
48      * Whether to process recursively.
49      */

50     private boolean recursive = true;
51
52     /**
53      * The modules to process. These names are unexpanded and will be passed
54      * to a module-expansion request.
55      */

56     protected final List modules = new LinkedList();
57     
58     /**
59      * The expanded modules.
60      */

61     protected final List expandedModules = new LinkedList();
62
63     /**
64      * Gets the value of the recursive option.
65      * @return true if recursive, false if not
66      */

67     public boolean isRecursive() {
68         return recursive;
69     }
70
71     /**
72      * Sets the value of the recursive option.
73      * @param r true if the command should recurse, false otherwise
74      */

75     public void setRecursive(boolean recursive) {
76         this.recursive = recursive;
77     }
78
79     /**
80      * Add a module to process.
81      * @param module the name of the module to process
82      */

83     public void addModule(String JavaDoc module) {
84         modules.add(module);
85     }
86     
87     /**
88      * Set the modules to process.
89      * @param modules the names of the modules to process
90      */

91     public void setModules(String JavaDoc[] modules) {
92         clearModules();
93         if (modules == null) {
94             return;
95         }
96         for (int i = 0; i < modules.length; i++) {
97             String JavaDoc module = modules[i];
98             this.modules.add(module);
99         }
100     }
101     
102     /**
103      * Get the array of modules that are set to be processed.
104      */

105     public String JavaDoc[] getModules() {
106         String JavaDoc[] mods = new String JavaDoc[modules.size()];
107         mods = (String JavaDoc[])modules.toArray(mods);
108         return mods;
109     }
110     
111     /**
112      * Clear the list of modules.
113      */

114     public void clearModules() {
115         this.modules.clear();
116     }
117     
118     /**
119      * Add the argument requests. The argument requests are created using
120      * the expanded set of modules passed in. Subclasses of this
121      * class should call this method at the appropriate point in their
122      * postExpansionExecute() method. Note that arguments are appended to the list.
123      */

124     protected final void addArgumentRequests() {
125         if (expandedModules.size() == 0) {
126             return;
127         }
128
129         for (Iterator it = expandedModules.iterator(); it.hasNext(); ) {
130             final String JavaDoc module = (String JavaDoc) it.next();
131             addRequest(new ArgumentRequest(module));
132         }
133     }
134
135     /**
136      * This is called when the server has responded to an expand-modules
137      * request.
138      */

139     public final void moduleExpanded(ModuleExpansionEvent e) {
140         expandedModules.add(e.getModule());
141     }
142     
143     /**
144      * Execute this command. This method sends the ExpandModulesRequest in order
145      * to expand the modules that were set. The actual execution is performed by
146      * {@link #postExpansionExecute} method.
147      * @param client the client services object that provides any necessary
148      * services to this command, including the ability to actually process
149      * all the requests
150      */

151     public final void execute(ClientServices client, EventManager em)
152     throws CommandException, AuthenticationException {
153         
154         client.ensureConnection();
155         
156         requests.clear();
157         super.execute(client, em);
158         
159         clientServices = client;
160         
161         if (client.isFirstCommand()) {
162             requests.add(new RootRequest(client.getRepository()));
163         }
164         for (Iterator it = modules.iterator(); it.hasNext();) {
165             String JavaDoc module = (String JavaDoc)it.next();
166             requests.add(new ArgumentRequest(module));
167         }
168         expandedModules.clear();
169         requests.add(new DirectoryRequest(".", client.getRepository())); //NOI18N
170
requests.add(new ExpandModulesRequest());
171         try {
172             client.processRequests(requests);
173         }
174         catch (CommandException ex) {
175             throw ex;
176         }
177         catch (Exception JavaDoc ex) {
178             throw new CommandException(ex, ex.getLocalizedMessage());
179         }
180         requests.clear();
181         postExpansionExecute(client, em);
182     }
183     
184     /**
185      * Execute this command
186      * @param client the client services object that provides any necessary
187      * services to this command, including the ability to actually process
188      * all the requests
189      */

190     protected abstract void postExpansionExecute(ClientServices client, EventManager em)
191         throws CommandException, AuthenticationException;
192     
193     /**
194      * Adds the specified request to the request list.
195      */

196     protected final void addRequest(Request request) {
197         requests.add(request);
198     }
199
200     /**
201      * Adds the request for the current working directory.
202      */

203     protected final void addRequestForWorkingDirectory(ClientServices clientServices)
204             throws IOException {
205         addRequest(new DirectoryRequest(".", //NOI18N
206
clientServices.getRepositoryForDirectory(getLocalDirectory())));
207     }
208
209     /**
210      * If the specified value is true, add a ArgumentRequest for the specified
211      * argument.
212      */

213     protected final void addArgumentRequest(boolean value, String JavaDoc argument) {
214         if (!value) {
215             return;
216         }
217
218         addRequest(new ArgumentRequest(argument));
219     }
220
221     /**
222      * Appends the file's names to the specified buffer.
223      */

224     protected final void appendModuleArguments(StringBuffer JavaDoc buffer) {
225         if (expandedModules.size() == 0) {
226             return;
227         }
228
229         Iterator it = expandedModules.iterator();
230         buffer.append((String JavaDoc) it.next());
231         while (it.hasNext()) {
232             buffer.append(' ');
233             buffer.append((String JavaDoc) it.next());
234         }
235     }
236 }
237
238
Popular Tags