Subversion Repositories XServices

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 brianR 1
package net.brutex.xservices.ws.rs;
2
 
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.List;
6
import java.util.StringTokenizer;
7
 
8
import javax.ws.rs.core.GenericEntity;
9
import javax.ws.rs.core.HttpHeaders;
10
import javax.ws.rs.core.Response;
11
 
12
import org.apache.commons.configuration.ConfigurationException;
13
import org.apache.jcs.JCS;
14
import org.apache.jcs.access.exception.CacheException;
15
import org.apache.log4j.Logger;
16
import org.netbeans.lib.cvsclient.Client;
17
import org.netbeans.lib.cvsclient.command.CommandAbortedException;
18
import org.netbeans.lib.cvsclient.command.CommandException;
19
import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand;
20
import org.netbeans.lib.cvsclient.command.checkout.ModuleListInformation;
21
import org.netbeans.lib.cvsclient.command.log.LogInformation;
22
import org.netbeans.lib.cvsclient.command.log.RlogCommand;
23
import org.netbeans.lib.cvsclient.connection.AuthenticationException;
24
import org.netbeans.lib.cvsclient.event.FileInfoEvent;
25
 
26
import net.brutex.xservices.types.scm.ModuleType;
27
import net.brutex.xservices.types.scm.FileType;
28
import net.brutex.xservices.types.scm.Revision;
29
import net.brutex.xservices.util.BasicCVSListener;
30
import net.brutex.xservices.util.CVSClient;
31
 
32
/**
33
 * @author Brian Rosenberger
34
 * @since 0.5.0-20120824
35
 *
36
 */
37
public class CVSInfoImpl implements CVSInfo {
38
 
39
	final Logger logger = Logger.getLogger(CVSInfoImpl.class);
40
 
41
	public Response getRepositoryFiles(HttpHeaders h, File f, String modules,
42
			boolean showRevisions,
43
			boolean forceNoCache) {
44
 
45
		final List<FileType> list = new ArrayList<FileType>();
46
 
47
		String cachekey = "getFiles" + f.toURI().toString();
48
		logger.debug("forceNoCache="+forceNoCache);
49
		List<FileType> cacheresult = (List<FileType>) getCacheInstance().get(
50
				cachekey);
51
 
52
		if (!forceNoCache && cacheresult != null) {
53
			// Cache hit
54
			list.addAll(cacheresult);
55
		} else {
56
			// Cache miss
57
			try {
58
				CVSClient cvsclient = new CVSClient(f);
59
				Client client = cvsclient.client;
60
 
61
				client.getEventManager().addCVSListener(new BasicCVSListener() {
62
					@Override
63
					public void fileInfoGenerated(FileInfoEvent arg0) {
64
						LogInformation info = (LogInformation) arg0
65
								.getInfoContainer();
66
						FileType cvsfile = new FileType(info.getFile(), info
67
								.getRepositoryFilename(), info.getDescription());
68
						cvsfile.setHeadRevision(info.getHeadRevision());
69
						cvsfile.setBranch(info.getBranch());
70
						cvsfile.setTotalRevisions(info.getTotalRevisions());
71
						for (LogInformation.Revision r : info.getRevisionList()) {
72
							cvsfile.addRevision(new Revision(r.getNumber(), r
73
									.getMessage()));
74
						}
75
						list.add(cvsfile);
76
					}
77
				});
78
 
79
				RlogCommand rlog = new RlogCommand();
80
				StringTokenizer tk = new StringTokenizer(modules, ",");
81
				while (tk.hasMoreTokens()) {
82
					rlog.setModule(tk.nextToken());
83
				}
84
				if (rlog.getModules().length == 0)
85
					rlog.setModule("");
86
 
87
				rlog.setDefaultBranch(true); // -b Print information about the
88
												// revisions on the default
89
												// branch,
90
												// normally the highest branch
91
												// on
92
												// the trunk.
93
				rlog.setNoTags(true); // -N Do not print the list of tags for
94
										// this
95
										// file. This option can be very useful
96
										// when
97
										// your site uses a lot of tags, so
98
										// rather
99
										// than "more"'ing over 3 pages of tag
100
										// information, the log information is
101
										// presented without tags at all.
102
				rlog.setHeaderAndDescOnly(false); // -t Print only the name of
103
													// the
104
													// rcs file, name of the
105
													// file in
106
													// the working directory,
107
													// head,
108
													// default branch, access
109
													// list,
110
													// locks, symbolic names,
111
													// and
112
													// suffix. + description
113
				// rlog.setRevisionFilter("1.1.");
114
				// rlog.setSuppressHeader(true); // -S Supress log output when
115
				// no
116
				// revisions are selected within a file.
117
				client.executeCommand(rlog, cvsclient.getGlobalOptions());
118
				logger.info("Execute CVS command '" + rlog.getCVSCommand() + "' against '"+cvsclient.getRoot().host+"@" + cvsclient.getRoot().repository+"'");
119
				//need to put a new list into cache as we will filter the result
120
				//afterwards
121
				getCacheInstance().put(cachekey, new ArrayList<FileType>(list));
122
 
123
 
124
			} catch (ConfigurationException e) {
125
				logger.error("CVS Configuration File '"
126
						+ f.getAbsolutePath() + f.getName() + "'not found.", e);
127
 
128
			} catch (CommandAbortedException e) {
129
				// TODO Auto-generated catch block
130
				e.printStackTrace();
131
			} catch (AuthenticationException e) {
132
				// TODO Auto-generated catch block
133
				e.printStackTrace();
134
			} catch (CommandException e) {
135
				// TODO Auto-generated catch block
136
				e.printStackTrace();
137
			} catch (CacheException e) {
138
				// TODO Auto-generated catch block
139
				e.printStackTrace();
140
			}
141
 
142
		}
143
 
144
 
145
		// prepare output after everything is cached
146
		if (!showRevisions) {
147
			for (FileType t : list) {
148
				t.clearRevisionList();
149
			}
150
		}
151
 
152
		GenericEntity entity = new GenericEntity<List<FileType>>(list) {};
153
		return Response.ok(entity).build();
154
	}
155
 
156
	@Override
157
	public Response getModules(HttpHeaders h, File f, boolean forceNoCache) {
158
 
159
		// Try to deliver from Cache
160
		String cachekey = "Modules" + f.toURI().toString();
161
		logger.debug("forceNoCache="+forceNoCache);
162
		List<ModuleType> response = (List<ModuleType>) getCacheInstance().get(
163
				cachekey);
164
		if (!forceNoCache && response != null) {
165
			GenericEntity entity = new GenericEntity<List<ModuleType>>(response) {
166
			};
167
			return Response.ok(entity).build();
168
		}
169
		// -------------------------
170
 
171
		try {
172
			CVSClient cvsclient = new CVSClient(f);
173
			Client client = cvsclient.client;
174
 
175
			final List<ModuleType> list = new ArrayList<ModuleType>();
176
 
177
			client.getEventManager().addCVSListener(new BasicCVSListener() {
178
				public void fileInfoGenerated(FileInfoEvent e) {
179
					ModuleListInformation info = (ModuleListInformation) e
180
							.getInfoContainer();
181
 
182
					list.add(new ModuleType(info.getModuleName(), info
183
							.getModuleStatus(), info.getPaths(), info.getType()));
184
				}
185
			});
186
 
187
			CheckoutCommand co = new CheckoutCommand();
188
			co.setShowModulesWithStatus(true);
189
 
190
			client.executeCommand(co, cvsclient.getGlobalOptions());
191
			logger.info("Execute CVS command '" + co.getCVSCommand() + "' against '"+cvsclient.getRoot().host+"@" + cvsclient.getRoot().repository+"'");
192
			if(list.size()==0) {
193
				logger.warn("Repository '" + cvsclient.getRoot().repository + "' does not have modules");
194
				list.add(new ModuleType("","","",""));
195
			}
196
 
197
			GenericEntity entity = new GenericEntity<List<ModuleType>>(list) {
198
			};
199
			getCacheInstance().put(cachekey, list);
200
			return Response.ok(entity).build();
201
		} catch (Exception e) {
202
			e.printStackTrace();
203
		}
204
		return Response.serverError().build();
205
	}
206
 
207
	/**
208
	 * Get the caching manager for CVS objects
209
	 *
210
	 * @return The CVSCaching JCS region
211
	 */
212
	public JCS getCacheInstance() {
213
		JCS jcs = null;
214
		final String cacheinstance = "CVSCache";
215
		try {
216
			logger.debug("Getting cache instance named '"+cacheinstance+"'" );
217
			jcs = JCS.getInstance(cacheinstance);
218
		} catch (CacheException e) {
219
			logger.error("Failed to get cache instance", e);
220
			e.printStackTrace();
221
		}
222
		return jcs;
223
	}
224
 
225
}