Skip to content

Commit

Permalink
Allow to configure the temporary folder used to store scripts for exe…
Browse files Browse the repository at this point in the history
…cution
  • Loading branch information
profesorfalken committed Apr 6, 2019
1 parent 28bcb69 commit aa894c0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Simple Java API that allows to interact with PowerShell console
.close();
```

#### New JPowerShell v3.0 is out
#### New JPowerShell v3 is out

The version 3 of JPowerShell includes an important revision and rewrite of most of the code that improves performance and stability.

Expand Down Expand Up @@ -103,6 +103,9 @@ The variables that can be configured in jPowerShell are:

*maxWait*: the maximum wait in ms for the command to execute. Default value is 10000

*tempFolder*: if you set this variable jPowerShell will use this folder in order to store temporary the scripts to execute.
By default the environment variable _java.io.tmpdir_ will be used.

## Advanced usage

### Setting the PowerShell executable path
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.profesorfalken</groupId>
<artifactId>jPowerShell</artifactId>
<version>3.0.4</version>
<version>3.1.0</version>
<packaging>jar</packaging>

<name>jPowerShell</name>
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/profesorfalken/jpowershell/PowerShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public class PowerShell implements AutoCloseable {
private static final String DEFAULT_LINUX_EXECUTABLE = "powershell";

// Config values
private int waitPause = 10;
private int waitPause = 5;
private long maxWait = 10000;
private File tempFolder = null;

// Variables used for script mode
private boolean scriptMode = false;
Expand Down Expand Up @@ -89,6 +90,8 @@ public PowerShell configuration(Map<String, String> config) {
: PowerShellConfig.getConfig().getProperty("waitPause"));
this.maxWait = Long.valueOf((config != null && config.get("maxWait") != null) ? config.get("maxWait")
: PowerShellConfig.getConfig().getProperty("maxWait"));
this.tempFolder = (config != null && config.get("tempFolder") != null) ? getTempFolder(config.get("tempFolder"))
: getTempFolder(PowerShellConfig.getConfig().getProperty("tempFolder"));
} catch (NumberFormatException nfe) {
logger.log(Level.SEVERE,
"Could not read configuration. Using default values.", nfe);
Expand Down Expand Up @@ -352,7 +355,7 @@ private File createWriteTempFile(BufferedReader srcReader) {
File tmpFile = null;

try {
tmpFile = File.createTempFile("psscript_" + new Date().getTime(), ".ps1");
tmpFile = File.createTempFile("psscript_" + new Date().getTime(), ".ps1", this.tempFolder);
if (!tmpFile.exists()) {
return null;
}
Expand Down Expand Up @@ -471,4 +474,15 @@ private long getPID() {

return -1;
}

//Return the temp folder File object or null if the path does not exist
private File getTempFolder(String tempPath) {
if (tempPath != null) {
File folder = new File(tempPath);
if (folder.exists()) {
return folder;
}
}
return null;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/jpowershell.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
waitPause=5
maxWait=10000
maxWait=10000
tempFolder=e:\\tmp

0 comments on commit aa894c0

Please sign in to comment.