aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-maven-plugin
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-06-17 17:45:56 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-06-17 17:45:56 +0200
commit32ec8599d050066e41e12acd4b554febd3920197 (patch)
treef06a136a4e426e4b8ef8491627e1eb52fb1529c6 /sonar-maven-plugin
parenta07ebb829958f3aa566b6246e4643dad91b96b6c (diff)
downloadsonarqube-32ec8599d050066e41e12acd4b554febd3920197.tar.gz
sonarqube-32ec8599d050066e41e12acd4b554febd3920197.zip
Revert "SONAR-3979, SONAR-4047 Fix Sonar Maven goal to support Maven 3.1"
This reverts commit ceaddeb0db54d29fd67d11f23488f61f85041446.
Diffstat (limited to 'sonar-maven-plugin')
-rw-r--r--sonar-maven-plugin/pom.xml6
-rw-r--r--sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java32
-rw-r--r--sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java66
3 files changed, 29 insertions, 75 deletions
diff --git a/sonar-maven-plugin/pom.xml b/sonar-maven-plugin/pom.xml
index 4dadfc59428..6efe191c675 100644
--- a/sonar-maven-plugin/pom.xml
+++ b/sonar-maven-plugin/pom.xml
@@ -18,11 +18,7 @@
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
- </dependency>
- <dependency>
- <groupId>org.codehaus.sonar.runner</groupId>
- <artifactId>sonar-runner-api</artifactId>
- <version>2.2.2</version>
+ <version>1.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java
deleted file mode 100644
index 6ec6ea0704a..00000000000
--- a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMaven2ProjectBuilder.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package org.sonar.maven;
-
-import org.apache.maven.execution.MavenSession;
-import org.apache.maven.project.MavenProject;
-import org.sonar.api.batch.bootstrap.ProjectBuilder;
-import org.sonar.api.batch.bootstrap.ProjectBuilderContext;
-import org.sonar.batch.scan.maven.MavenProjectConverter;
-
-import java.util.List;
-
-public class SonarMaven2ProjectBuilder extends ProjectBuilder {
-
- private MavenSession session;
-
- public SonarMaven2ProjectBuilder(MavenSession session) {
- this.session = session;
- }
-
- @Override
- public void build(ProjectBuilderContext context) {
- List<MavenProject> sortedProjects = session.getSortedProjects();
- MavenProject topLevelProject = null;
- for (MavenProject project : sortedProjects) {
- if (project.isExecutionRoot()) {
- topLevelProject = project;
- break;
- }
- }
- MavenProjectConverter.configure(context.getProjectReactor().getRoot(), sortedProjects, topLevelProject);
- }
-
-}
diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java
index c5215e28a85..ffce333b4c3 100644
--- a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java
+++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java
@@ -19,8 +19,7 @@
*/
package org.sonar.maven;
-import org.apache.commons.lang.ObjectUtils;
-import org.apache.commons.lang.StringUtils;
+import com.google.common.collect.Maps;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -35,14 +34,12 @@ import org.apache.maven.plugin.PluginManager;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
-import org.sonar.api.batch.maven.MavenUtils;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.batch.scan.maven.MavenProjectConverter;
-import org.sonar.runner.api.EmbeddedRunner;
-import org.sonar.runner.api.RunnerProperties;
-import org.sonar.runner.api.ScanProperties;
-
-import java.util.Map.Entry;
-import java.util.Set;
+import org.sonar.batch.bootstrapper.Batch;
+import org.sonar.batch.bootstrapper.EnvironmentInformation;
+import org.sonar.batch.bootstrapper.LoggingConfiguration;
/**
* @goal sonar
@@ -137,39 +134,32 @@ public final class SonarMojo extends AbstractMojo {
private RuntimeInformation runtimeInformation;
public void execute() throws MojoExecutionException, MojoFailureException {
+ ProjectDefinition def = MavenProjectConverter.convert(session.getSortedProjects(), project);
+ ProjectReactor reactor = new ProjectReactor(def);
+
+ Batch batch = Batch.builder()
+ .setEnvironment(getEnvironmentInformation())
+ .setProjectReactor(reactor)
+ .addComponents(
+ session, getLog(), lifecycleExecutor, pluginManager, artifactFactory,
+ localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
+ projectBuilder, Maven2PluginExecutor.class)
+ .build();
+
+ configureLogging(batch.getLoggingConfiguration());
+ batch.execute();
+ }
- EmbeddedRunner runner = EmbeddedRunner.create()
- .setApp("Maven", getMavenVersion());
- // Workaround for SONARPLUGINS-2947
- // TODO remove when it will be fixed
- runner.setProperty("sonarRunner.userAgent", "Maven");
- runner.setProperty("sonarRunner.userAgentVersion", getMavenVersion());
- Set<Entry<Object, Object>> properties = project.getModel().getProperties().entrySet();
- for (Entry<Object, Object> entry : properties) {
- runner.setProperty(ObjectUtils.toString(entry.getKey()), ObjectUtils.toString(entry.getValue()));
- }
- String encoding = MavenUtils.getSourceEncoding(project);
- if (encoding != null) {
- runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, encoding);
- }
- runner.setProperty(ScanProperties.PROJECT_KEY, MavenProjectConverter.getSonarKey(project))
- .setProperty(RunnerProperties.WORK_DIR, MavenProjectConverter.getSonarWorkDir(project).getAbsolutePath())
- .setProperty(ScanProperties.PROJECT_BASEDIR, project.getBasedir().getAbsolutePath())
- .setProperty(ScanProperties.PROJECT_VERSION, StringUtils.defaultString(project.getVersion()))
- .setProperty(ScanProperties.PROJECT_NAME, StringUtils.defaultString(project.getName()))
- .setProperty(ScanProperties.PROJECT_DESCRIPTION, StringUtils.defaultString(project.getDescription()))
- .setProperty(ScanProperties.PROJECT_SOURCE_DIRS, ".")
- // Required to share ProjectBuilder extension between SonarMavenProjectBuilder and Sonar classloader
- .setUnmaskedPackages("org.sonar.api.batch.bootstrap")
- .addExtensions(session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector,
- dependencyTreeBuilder, projectBuilder, Maven2PluginExecutor.class, new SonarMaven2ProjectBuilder(session));
+ private void configureLogging(LoggingConfiguration logging) {
+ logging.setProperties(Maps.fromProperties(session.getExecutionProperties()));
+ logging.setFormat(LoggingConfiguration.FORMAT_MAVEN);
if (getLog().isDebugEnabled()) {
- runner.setProperty("sonar.verbose", "true");
+ logging.setVerbose(true);
}
- runner.execute();
}
- private String getMavenVersion() {
- return runtimeInformation.getApplicationVersion().toString();
+ private EnvironmentInformation getEnvironmentInformation() {
+ String mavenVersion = runtimeInformation.getApplicationVersion().toString();
+ return new EnvironmentInformation("Maven", mavenVersion);
}
}
option value='backport/48043/stable30'>backport/48043/stable30 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
summaryrefslogtreecommitdiffstats
path: root/lib/private/tempmanager.php
blob: b8cef8e036ec33fa10f04f7637e0cacf435d83c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
 * @author Lukas Reschke <lukas@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Olivier Paroz <github@oparoz.com>
 * @author Robin Appelman <icewind@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OC;

use OCP\ILogger;
use OCP\ITempManager;

class TempManager implements ITempManager {
	/** @var string[] Current temporary files and folders, used for cleanup */
	protected $current = [];
	/** @var string i.e. /tmp on linux systems */
	protected $tmpBaseDir;
	/** @var ILogger */
	protected $log;
	/** Prefix */
	const TMP_PREFIX = 'oc_tmp_';

	/**
	 * @param string $baseDir
	 * @param \OCP\ILogger $logger
	 */
	public function __construct($baseDir, ILogger $logger) {
		$this->tmpBaseDir = $baseDir;
		$this->log = $logger;
	}

	/**
	 * Builds the filename with suffix and removes potential dangerous characters
	 * such as directory separators.
	 *
	 * @param string $absolutePath Absolute path to the file / folder
	 * @param string $postFix Postfix appended to the temporary file name, may be user controlled
	 * @return string
	 */
	private function buildFileNameWithSuffix($absolutePath, $postFix = '') {
		if($postFix !== '') {
			$postFix = '.' . ltrim($postFix, '.');
			$postFix = str_replace(['\\', '/'], '', $postFix);
			$absolutePath .= '-';
		}

		return $absolutePath . $postFix;
	}

	/**
	 * Create a temporary file and return the path
	 *
	 * @param string $postFix Postfix appended to the temporary file name
	 * @return string
	 */
	public function getTemporaryFile($postFix = '') {
		if (is_writable($this->tmpBaseDir)) {
			// To create an unique file and prevent the risk of race conditions
			// or duplicated temporary files by other means such as collisions
			// we need to create the file using `tempnam` and append a possible
			// postfix to it later
			$file = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
			$this->current[] = $file;

			// If a postfix got specified sanitize it and create a postfixed
			// temporary file
			if($postFix !== '') {
				$fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix);
				touch($fileNameWithPostfix);
				chmod($fileNameWithPostfix, 0600);
				$this->current[] = $fileNameWithPostfix;
				return $fileNameWithPostfix;
			}

			return $file;
		} else {
			$this->log->warning(
				'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions',
				[
					'dir' => $this->tmpBaseDir,
				]
			);
			return false;
		}
	}

	/**
	 * Create a temporary folder and return the path
	 *
	 * @param string $postFix Postfix appended to the temporary folder name
	 * @return string
	 */
	public function getTemporaryFolder($postFix = '') {
		if (is_writable($this->tmpBaseDir)) {
			// To create an unique directory and prevent the risk of race conditions
			// or duplicated temporary files by other means such as collisions
			// we need to create the file using `tempnam` and append a possible
			// postfix to it later
			$uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX);
			$this->current[] = $uniqueFileName;

			// Build a name without postfix
			$path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix);
			mkdir($path, 0700);
			$this->current[] = $path;

			return $path . '/';
		} else {
			$this->log->warning(
				'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions',
				[
					'dir' => $this->tmpBaseDir,
				]
			);
			return false;
		}
	}

	/**
	 * Remove the temporary files and folders generated during this request
	 */
	public function clean() {
		$this->cleanFiles($this->current);
	}

	/**
	 * @param string[] $files
	 */
	protected function cleanFiles($files) {
		foreach ($files as $file) {
			if (file_exists($file)) {
				try {
					\OC_Helper::rmdirr($file);
				} catch (\UnexpectedValueException $ex) {
					$this->log->warning(
						"Error deleting temporary file/folder: {file} - Reason: {error}",
						[
							'file' => $file,
							'error' => $ex->getMessage(),
						]
					);
				}
			}
		}
	}

	/**
	 * Remove old temporary files and folders that were failed to be cleaned
	 */
	public function cleanOld() {
		$this->cleanFiles($this->getOldFiles());
	}

	/**
	 * Get all temporary files and folders generated by oc older than an hour
	 *
	 * @return string[]
	 */
	protected function getOldFiles() {
		$cutOfTime = time() - 3600;
		$files = [];
		$dh = opendir($this->tmpBaseDir);
		if ($dh) {
			while (($file = readdir($dh)) !== false) {
				if (substr($file, 0, 7) === self::TMP_PREFIX) {
					$path = $this->tmpBaseDir . '/' . $file;
					$mtime = filemtime($path);
					if ($mtime < $cutOfTime) {
						$files[] = $path;
					}
				}
			}
		}
		return $files;
	}
}