PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/action/admin/vcs/RepositoryActionSupport.java

https://bitbucket.org/atlassian/jira-cvs-plugin
Java | 454 lines | 385 code | 54 blank | 15 comment | 25 complexity | 971b9f3a97886cf125c6fecce9c12e69 MD5 | raw file
  1. /*
  2. * Copyright (c) 2002-2004
  3. * All rights reserved.
  4. */
  5. package com.atlassian.jira.action.admin.vcs;
  6. import com.atlassian.jira.util.LockException;
  7. import com.atlassian.jira.vcs.Repository;
  8. import com.atlassian.jira.vcs.RepositoryBrowser;
  9. import com.atlassian.jira.vcs.RepositoryManager;
  10. import com.atlassian.jira.vcs.cvsimpl.CvsRepository;
  11. import com.atlassian.jira.vcs.cvsimpl.CvsRepositoryUtil;
  12. import com.atlassian.jira.vcs.cvsimpl.ValidationException;
  13. import com.atlassian.jira.vcs.viewcvs.ViewCvsBrowser;
  14. import com.atlassian.jira.web.action.ActionViewData;
  15. import com.atlassian.jira.web.action.JiraWebActionSupport;
  16. import com.opensymphony.util.TextUtils;
  17. import net.sf.statcvs.input.LogSyntaxException;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.netbeans.lib.cvsclient.command.CommandException;
  20. import org.netbeans.lib.cvsclient.connection.AuthenticationException;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.net.MalformedURLException;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.concurrent.TimeUnit;
  27. public class RepositoryActionSupport extends JiraWebActionSupport
  28. {
  29. protected Long id;
  30. private String type;
  31. private String name = null;
  32. private String description;
  33. private String logFilePath;
  34. private String cvsRoot;
  35. private String moduleName;
  36. private String password;
  37. private boolean fetchLog;
  38. private String timeout;
  39. private long timeoutMS = CvsRepository.CVS_OPERATION_TIMEOUT_DEFAULT;
  40. private String repositoryBrowserURL;
  41. private final RepositoryManager repositoryManager;
  42. private final CvsRepositoryUtil cvsRepositoryUtil;
  43. private String repositoryBrowserRootParam;
  44. public RepositoryActionSupport(RepositoryManager repositoryManager, CvsRepositoryUtil cvsRepositoryUtil)
  45. {
  46. this.repositoryManager = repositoryManager;
  47. this.cvsRepositoryUtil = cvsRepositoryUtil;
  48. this.timeout = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(CvsRepository.CVS_OPERATION_TIMEOUT_DEFAULT));
  49. }
  50. public String doView()
  51. {
  52. return SUCCESS;
  53. }
  54. public String doDefault() throws Exception
  55. {
  56. if (id == null || id.longValue() <= 0)
  57. {
  58. return getRedirect("ViewRepositories.jspa");
  59. }
  60. // Retrieve the cvs repository
  61. final Repository repository = getRepositoryManager().getRepository(id);
  62. if (!(repository instanceof CvsRepository))
  63. {
  64. addErrorMessage(getText("admin.errors.repository.with.id.not.cvs.repo", id.toString()));
  65. return ERROR;
  66. }
  67. CvsRepository cvsRepository = (CvsRepository) repository;
  68. // Initialize the values
  69. setType(RepositoryManager.CVS_TYPE);
  70. setName(cvsRepository.getName());
  71. setDescription(cvsRepository.getDescription());
  72. setLogFilePath(cvsRepository.getCvsLogFilePath());
  73. setCvsRoot(cvsRepository.getCvsRoot());
  74. setModuleName(cvsRepository.getModuleName());
  75. setPassword(cvsRepository.getPassword());
  76. setFetchLog(cvsRepository.fetchLog());
  77. setTimeoutMillis(cvsRepository.getCvsTimeout());
  78. // If the repository has the repository browser, set the base url
  79. if (cvsRepository.getRepositoryBrowser() != null)
  80. {
  81. if (RepositoryBrowser.VIEW_CVS_TYPE.equals(cvsRepository.getRepositoryBrowser().getType()))
  82. {
  83. final ViewCvsBrowser viewCvsBrowser = (ViewCvsBrowser) cvsRepository.getRepositoryBrowser();
  84. setRepositoryBrowserURL(viewCvsBrowser.getBaseURL());
  85. setRepositoryBrowserRootParam(viewCvsBrowser.getRootParameter());
  86. }
  87. }
  88. return INPUT;
  89. }
  90. @ActionViewData
  91. public Long getId()
  92. {
  93. return id;
  94. }
  95. public void setId(Long id)
  96. {
  97. this.id = id;
  98. }
  99. @ActionViewData
  100. public String getName()
  101. {
  102. return name;
  103. }
  104. public void setName(String name)
  105. {
  106. this.name = name;
  107. }
  108. @ActionViewData
  109. public String getDescription()
  110. {
  111. return description;
  112. }
  113. public void setDescription(String description)
  114. {
  115. this.description = description;
  116. }
  117. @ActionViewData
  118. public String getType()
  119. {
  120. return type;
  121. }
  122. public void setType(String type)
  123. {
  124. if (getRepositoryManager().isValidType(type))
  125. {
  126. this.type = type;
  127. }
  128. else
  129. {
  130. this.type = null;
  131. }
  132. }
  133. public Collection<String> getTypes()
  134. {
  135. return RepositoryManager.VCS_TYPES;
  136. }
  137. protected String getPassword()
  138. {
  139. return password;
  140. }
  141. public void setPassword(String password)
  142. {
  143. this.password = password;
  144. }
  145. @ActionViewData
  146. public String getLogFilePath()
  147. {
  148. return logFilePath;
  149. }
  150. public void setLogFilePath(String logFilePath)
  151. {
  152. this.logFilePath = logFilePath;
  153. }
  154. @ActionViewData
  155. public String getCvsRoot()
  156. {
  157. return cvsRoot;
  158. }
  159. public void setCvsRoot(String cvsRoot)
  160. {
  161. this.cvsRoot = cvsRoot;
  162. }
  163. @ActionViewData
  164. public String getModuleName()
  165. {
  166. return moduleName;
  167. }
  168. public void setModuleName(String moduleName)
  169. {
  170. this.moduleName = moduleName;
  171. }
  172. public boolean isFetchLog()
  173. {
  174. return fetchLog;
  175. }
  176. public void setFetchLog(boolean fetchLog)
  177. {
  178. this.fetchLog = fetchLog;
  179. }
  180. @ActionViewData
  181. public String getRepositoryBrowserURL()
  182. {
  183. return repositoryBrowserURL;
  184. }
  185. public void setRepositoryBrowserURL(String repositoryBrowserURL)
  186. {
  187. this.repositoryBrowserURL = repositoryBrowserURL;
  188. }
  189. @ActionViewData
  190. public String getRepositoryBrowserRootParam()
  191. {
  192. return repositoryBrowserRootParam;
  193. }
  194. public void setRepositoryBrowserRootParam(String repositoryBrowserRootParam)
  195. {
  196. this.repositoryBrowserRootParam = repositoryBrowserRootParam;
  197. }
  198. protected RepositoryManager getRepositoryManager()
  199. {
  200. return repositoryManager;
  201. }
  202. @ActionViewData
  203. public String getTimeout()
  204. {
  205. return timeout;
  206. }
  207. public long getTimeoutMillis()
  208. {
  209. return timeoutMS;
  210. }
  211. private void setTimeoutMillis(long timeoutMs)
  212. {
  213. if (timeoutMs <= 0)
  214. {
  215. this.timeoutMS = CvsRepository.CVS_OPERATION_TIMEOUT_DEFAULT;
  216. }
  217. else
  218. {
  219. this.timeoutMS = timeoutMs;
  220. }
  221. this.timeout = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(this.timeoutMS));
  222. }
  223. public void setTimeout(String timeout)
  224. {
  225. this.timeout = timeout;
  226. if (StringUtils.isBlank(timeout))
  227. {
  228. timeoutMS = CvsRepository.CVS_OPERATION_TIMEOUT_DEFAULT;
  229. }
  230. else
  231. {
  232. try
  233. {
  234. final long ms = Long.parseLong(timeout);
  235. timeoutMS = ms > 0 ? TimeUnit.SECONDS.toMillis(ms) : CvsRepository.CVS_OPERATION_TIMEOUT_DEFAULT;
  236. }
  237. catch (NumberFormatException e)
  238. {
  239. timeoutMS = CvsRepository.CVS_OPERATION_TIMEOUT_DEFAULT;
  240. }
  241. }
  242. }
  243. protected String checkRepository(String repositoryName, String logFilePath, String cvsRoot, String moduleName, String password, long cvsTimeout, boolean fetchLog)
  244. {
  245. try
  246. {
  247. testRepository(repositoryName, logFilePath, cvsRoot, moduleName, password, cvsTimeout, fetchLog);
  248. }
  249. catch (AuthenticationException e)
  250. {
  251. final String message = "Error authenticating with the repository: " + e.getLocalizedMessage();
  252. log.error(message, e);
  253. Throwable cause = e.getUnderlyingThrowable();
  254. if (cause != null)
  255. {
  256. log.error("Caused by: " + cause.getMessage(), cause);
  257. }
  258. return getText("admin.errors.authentication.repository", e.getLocalizedMessage());
  259. }
  260. catch (LockException e)
  261. {
  262. final String message = "Error obtaining lock: " + e.getMessage();
  263. log.error(message, e);
  264. return getText("admin.errors.cvs.obtaining.lock", e.getMessage());
  265. }
  266. catch (IOException e)
  267. {
  268. final String message = e.getMessage();
  269. log.error(message, e);
  270. return message;
  271. }
  272. catch (CommandException e)
  273. {
  274. log.error("Error occured while retrieving cvs log.", e);
  275. return getText("admin.errors.cvs.retrieving.log", e.getMessage());
  276. }
  277. catch (LogSyntaxException e)
  278. {
  279. log.error("Error occured while parsing cvs log.", e);
  280. return getText("admin.errors.cvs.parsing.log", e.getMessage());
  281. }
  282. catch (Exception unexpected)
  283. {
  284. log.error("Error occurred while obtaining cvs log or parsing the cvs log.", unexpected);
  285. return getText("admin.errors.cvs.parsing.or.obtaining.log");
  286. }
  287. return null;
  288. }
  289. protected void testRepository(String repositoryName, String logFilePath, String cvsRoot, String moduleName, String password, long cvsTimeout, boolean fetchLog)
  290. throws AuthenticationException, IOException, CommandException, LogSyntaxException, LockException
  291. {
  292. File logFile = new File(logFilePath);
  293. // Only check if we can get the log if the user wants us to do it
  294. if (fetchLog)
  295. {
  296. // Check that we can get the cvs log
  297. cvsRepositoryUtil.updateCvs(logFile, cvsRoot, moduleName, password, cvsTimeout);
  298. }
  299. else
  300. {
  301. if (!logFile.exists())
  302. {
  303. addErrorMessage(getText("admin.errors.cvs.log.file.not.found", logFile.getPath()));
  304. return;
  305. }
  306. }
  307. // Check if we can parse the log (if we got here no exception was thrown and hence updating the logs went ok)
  308. cvsRepositoryUtil.parseCvsLogs(logFile, moduleName, getCvsRepositoryUtil().parseCvsRoot(cvsRoot).getRepository(), repositoryName);
  309. }
  310. protected CvsRepositoryUtil getCvsRepositoryUtil()
  311. {
  312. return cvsRepositoryUtil;
  313. }
  314. // This is public for testing purposes
  315. public void validateRepositoryParameters()
  316. {
  317. if (!TextUtils.stringSet(getName()))
  318. {
  319. addError("name", getText("admin.errors.you.must.specify.a.name.for.the.repository"));
  320. }
  321. if (!TextUtils.stringSet(getCvsRoot()))
  322. {
  323. addError("cvsRoot", getText("admin.errors.you.must.specify.the.cvs.root.of.the.module"));
  324. }
  325. if (!TextUtils.stringSet(getModuleName()))
  326. {
  327. addError("moduleName", getText("admin.errors.you.must.specify.the.name.of.the.cvs.module"));
  328. }
  329. try
  330. {
  331. if (timeout == null || Long.parseLong(timeout) < 1)
  332. {
  333. addError("timeout", getText("admin.errors.cvs.invalid.timeout"));
  334. }
  335. }
  336. catch (NumberFormatException e)
  337. {
  338. addError("timeout", getText("admin.errors.cvs.invalid.timeout"));
  339. }
  340. if (!TextUtils.stringSet(getLogFilePath()))
  341. {
  342. addError("logFilePath", getText("admin.errors.you.must.specify.the.full.path.to.the.cvs.log.file"));
  343. }
  344. else
  345. {
  346. checkPathsAndRepository();
  347. }
  348. // If the ViewCVS URL is specified, check that it is in correct format
  349. if (TextUtils.stringSet(getRepositoryBrowserURL()))
  350. {
  351. try
  352. {
  353. new ViewCvsBrowser(getRepositoryBrowserURL(), Collections.EMPTY_MAP);
  354. }
  355. catch (MalformedURLException e)
  356. {
  357. addError("repositoryBrowserURL", getText("admin.errors.invalid.url.format"));
  358. }
  359. }
  360. }
  361. private void checkPathsAndRepository()
  362. {
  363. // Check LogFilePath validity
  364. checkLogFile();
  365. // Check CvsRoot validity
  366. checkCvsRoot();
  367. // Only go ahead with testing the repository if no problems were found
  368. if (!invalidInput())
  369. {
  370. final String message = checkRepository(getName(), getLogFilePath(), getCvsRoot(), getModuleName(), getPassword(), getTimeoutMillis(), isFetchLog());
  371. if (message != null)
  372. {
  373. addErrorMessage(message);
  374. }
  375. }
  376. }
  377. private void checkCvsRoot()
  378. {
  379. try
  380. {
  381. getCvsRepositoryUtil().checkCvsRoot(getCvsRoot());
  382. }
  383. catch (ValidationException e)
  384. {
  385. addError("cvsRoot", e.getMessage());
  386. }
  387. }
  388. private void checkLogFile()
  389. {
  390. try
  391. {
  392. File file = new File(getLogFilePath());
  393. getCvsRepositoryUtil().checkLogFilePath(file, isFetchLog());
  394. }
  395. catch (ValidationException e)
  396. {
  397. addError("logFilePath", e.getMessage());
  398. }
  399. }
  400. }