1 if($PSVersionTable.PSVersion.Major -lt 2) {
2     Write-Warning "This script requires PowerShell 2.0 or better; you have version $($Host.Version)."
3     return
4 }
5 
6 $SCRIPTS_HOME = (Get-ChildItem $MyInvocation.MyCommand.Path).Directory.FullName
7 $RESOLVED_JBOSS_HOME = (Get-ChildItem $MyInvocation.MyCommand.Path).Directory.Parent.FullName
8 
9 
10 # A collection of functions that are used by the other scripts
11 
Set-Envnull12 Function Set-Env {
13   $key = $args[0]
14   $value = $args[1]
15   Set-Content -Path env:$key -Value $value
16 }
17 
Get-Envnull18 Function Get-Env {
19   $key = $args[0]
20   if( Test-Path env:$key ) {
21     return (Get-ChildItem env:$key).Value
22   }
23   return $args[1]
24 }
Get-Env-Booleannull25 Function Get-Env-Boolean{
26   $key = $args[0]
27   if( Test-Path env:$key ) {
28     return (Get-ChildItem env:$key).Value -eq 'true'
29   }
30   return $args[1]
31 }
32 
33 $global:SECMGR = Get-Env-Boolean SECMGR $false
34 $global:DEBUG_MODE=Get-Env DEBUG $false
35 $global:DEBUG_PORT=Get-Env DEBUG_PORT 8787
36 $global:RUN_IN_BACKGROUND=$false
37 $global:GC_LOG=$false
38 #module opts that are passed to jboss modules
39 $global:MODULE_OPTS = @()
40 
Get-String()41 Function Get-String {
42   $value = ''
43   foreach($k in $args) {
44     $value += $k
45   }
46   return $value
47 }
48 
String-To-Array($value)49 Function String-To-Array($value) {
50   $res = @()
51   if (!$value){
52   	return $res
53   }
54   $tmpArr = $value.split()
55 
56   foreach ($str in $tmpArr) {
57     if ($str) {
58 	  $res += $str
59 	}
60   }
61   return $res
62 }
63 
Display-Environmentnull64 Function Display-Environment {
65 $JAVA_OPTS = Get-Java-Opts
66 # Display our environment
67 Write-Host "================================================================================="
68 Write-Host ""
69 Write-Host "  JBoss Bootstrap Environment"
70 Write-Host ""
71 Write-Host "  JBOSS_HOME: $JBOSS_HOME"
72 Write-Host ""
73 Write-Host "  JAVA: $JAVA"
74 Write-Host ""
75 Write-Host "  MODULE_OPTS: $MODULE_OPTS"
76 Write-Host ""
77 Write-Host "  JAVA_OPTS: $JAVA_OPTS"
78 Write-Host ""
79 Write-Host "================================================================================="
80 Write-Host ""
81 
82 }
83 
84 #todo: bit funky at the moment, should probably be done via global variable
Get-Java-Opts()85 Function Get-Java-Opts {
86 	if($PRESERVE_JAVA_OPTS -ne 'true') { # if not perserve, then check for enviroment variable and use that
87 		if( (Test-Path env:JAVA_OPTS)) {
88 			$ops = Get-Env JAVA_OPTS
89 			# This is Powershell, so split the incoming string on a space into array
90 			return String-To-Array -value $ops
91 			Write-Host "JAVA_OPTS already set in environment; overriding default settings with values: $JAVA_OPTS"
92 		}
93 	}
94 	return $JAVA_OPTS
95 }
96 
Display-Array($array)97 Function Display-Array($array){
98 	for ($i=0; $i -lt $array.length; $i++) {
99 		$v =  "$i " + $array[$i]
100 		Write-Host $v
101 	}
102 }
103 
104 
Get-Java-Arguments()105 Function Get-Java-Arguments {
106 Param(
107    [Parameter(Mandatory=$true)]
108    [string]$entryModule,
109    [string]$logFileProperties = "$JBOSS_CONFIG_DIR/logging.properties",
110    [string]$logFile = "$JBOSS_LOG_DIR/server.log",
111    [string[]]$serverOpts
112 
113 
114 ) #end param
115   $JAVA_OPTS = Get-Java-Opts #takes care of looking at defind settings and/or using env:JAVA_OPTS
116 
117   $PROG_ARGS = @()
118   if ($JAVA_OPTS -ne $null){
119   	$PROG_ARGS += $JAVA_OPTS
120   }
121   if ($logFile){
122   	$PROG_ARGS += "-Dorg.jboss.boot.log.file=$logFile"
123   }
124   if ($logFileProperties){
125   	$PROG_ARGS += "-Dlogging.configuration=file:$logFileProperties"
126   }
127   $PROG_ARGS += "-Djboss.home.dir=$JBOSS_HOME"
128   $PROG_ARGS += "-Djboss.server.base.dir=$global:JBOSS_BASE_DIR"
129   $PROG_ARGS += "-Djboss.server.config.dir=$global:JBOSS_CONFIG_DIR"
130 
131   $PROG_ARGS += "-jar"
132   $PROG_ARGS += "$JBOSS_HOME\jboss-modules.jar"
133   if ($MODULE_OPTS -ne $null){
134   	$PROG_ARGS += $MODULE_OPTS
135   }
136   $PROG_ARGS += "-mp"
137   $PROG_ARGS += "$JBOSS_MODULEPATH"
138   $PROG_ARGS += $entryModule
139   if ($serverOpts -ne $null){
140   	$PROG_ARGS += $serverOpts
141   }
142   return $PROG_ARGS
143 }
144 
Process-Script-Parametersnull145 Function Process-Script-Parameters {
146 Param(
147    [Parameter(Mandatory=$false)]
148    [string[]]$Params
149 
150 ) #end param
151     $res = @()
152 	for($i=0; $i -lt $Params.Count; $i++){
153 		$arg = $Params[$i]
154 		if ($arg -eq '--debug'){
155 			$global:DEBUG_MODE=$true
156 			if ($args[$i+1] -match '\d+'){ #port number can only follow --debug
157 				$global:DEBUG_PORT = $Params[$i+1]
158 				$i++
159 				continue
160 			}
161 		}elseif ($arg -contains '-Djava.security.manager'){
162 			Write-Warning "ERROR: The use of -Djava.security.manager has been removed. Please use the -secmgr command line argument or SECMGR=true environment variable."
163 			exit
164 		}elseif ($arg -eq '-secmgr'){
165 			$global:SECMGR = $true
166 		}elseif ($arg -eq '--background'){
167 			$global:RUN_IN_BACKGROUND = $true
168 		}else{
169 			$res+=$arg
170 		}
171 	}
172 	return $res
173 }
174 
Start-WildFly-Process()175 Function Start-WildFly-Process {
176  Param(
177    [Parameter(Mandatory=$true)]
178    [string[]] $programArguments,
179    [boolean] $runInBackground = $false
180 
181 ) #end param
182 
183 	if(($JBOSS_PIDFILE -ne '') -and (Test-Path $JBOSS_PIDFILE)) {
184 		$processId = gc $JBOSS_PIDFILE
185 		if ($processId -ne $null){
186 			$proc = Get-Process -Id $processId -ErrorAction SilentlyContinue
187 		}
188 		if ($proc -ne $null){
189 			Write-Warning "Looks like a server process is already running. If it isn't then, remove the $JBOSS_PIDFILE and try again"
190 			return
191 		}else{
192 			Remove-Item $JBOSS_PIDFILE
193 		}
194 	}
195 
196 	if($runInBackground) {
197 		$process = Start-Process -FilePath $JAVA -ArgumentList $programArguments -NoNewWindow -RedirectStandardOutput $global:CONSOLE_LOG -WorkingDirectory $JBOSS_HOME -PassThru
198 		$processId = $process.Id;
199 		echo "Started process in background, process id: $processId"
200 		if ($JBOSS_PIDFILE -ne $null){
201 			$processId >> $JBOSS_PIDFILE
202 		}
203 	} else {
204 		try{
205 			pushd $JBOSS_HOME
206 			& $JAVA $programArguments
207 			if ($LastExitCode -eq 10){ # :shutdown(restart=true) was called
208 			    Write-Host "Restarting application server..."
209 				Start-WildFly-Process -programArguments $programArguments
210 			}
211 
212 		}finally{
213 			popd
214 		}
215 	}
216 	Env-Clean-Up
217 }
218 
Set-Global-Variables()219 Function Set-Global-Variables {
220 PARAM(
221 [Parameter(Mandatory=$true)]
222    [string]$baseDir
223 )
224 
225 	# determine the default base dir, if not set
226 	$global:JBOSS_BASE_DIR = $baseDir;
227 
228 	# determine the default log dir, if not set
229 	$global:JBOSS_LOG_DIR = Get-Env JBOSS_LOG_DIR $JBOSS_BASE_DIR\log
230 
231 	# determine the default configuration dir, if not set
232 	$global:JBOSS_CONFIG_DIR = Get-Env JBOSS_CONFIG_DIR $JBOSS_BASE_DIR\configuration
233 
234 	$global:CONSOLE_LOG = $JBOSS_LOG_DIR + '\console.log'
235 }
236 
Set-Global-Variables-Standalone()237 Function Set-Global-Variables-Standalone {
238 	$dir = Get-Env JBOSS_BASE_DIR $JBOSS_HOME\standalone
239 	Set-Global-Variables -baseDir $dir
240 }
241 
Set-Global-Variables-Domain()242 Function Set-Global-Variables-Domain {
243 	$dir = Get-Env JBOSS_BASE_DIR $JBOSS_HOME\domain
244 	Set-Global-Variables -baseDir $dir
245 }
246 
Env-Clean-Upnull247 Function Env-Clean-Up {
248 	[Environment]::SetEnvironmentVariable("JBOSS_HOME", $null, "Process")
249 }
250 
Rotate-GC-Logsnull251 Function Rotate-GC-Logs {
252 	mv -ErrorAction SilentlyContinue $JBOSS_LOG_DIR/gc.log.0 $JBOSS_LOG_DIR/backupgc.log.0
253 	mv -ErrorAction SilentlyContinue $JBOSS_LOG_DIR/gc.log.1 $JBOSS_LOG_DIR/backupgc.log.1
254 	mv -ErrorAction SilentlyContinue $JBOSS_LOG_DIR/gc.log.2 $JBOSS_LOG_DIR/backupgc.log.2
255 	mv -ErrorAction SilentlyContinue $JBOSS_LOG_DIR/gc.log.3 $JBOSS_LOG_DIR/backupgc.log.3
256 	mv -ErrorAction SilentlyContinue $JBOSS_LOG_DIR/gc.log.4 $JBOSS_LOG_DIR/backupgc.log.4
257 	mv -ErrorAction SilentlyContinue $JBOSS_LOG_DIR/gc.log.*.current $JBOSS_LOG_DIR/backupgc.log.current
258 }
259 
Check-For-GC-Log()260 Function Check-For-GC-Log {
261 	if ($global:GC_LOG){
262 		$args = (,'-verbose:gc',"-Xloggc:$JBOSS_LOG_DIR/gc.log","-XX:+PrintGCDetails","-XX:+PrintGCDateStamps","-XX:+UseGCLogFileRotation","-XX:NumberOfGCLogFiles=5","-XX:GCLogFileSize=3M","-XX:-TraceClassUnloading",'-version')
263 		$OutputVariable = (&$JAVA $args )  | Out-String
264 	}
265 }
266 
267 # Setup JBOSS_HOME
268 if((Test-Path env:JBOSS_HOME) -and (Test-Path (Get-Item env:JBOSS_HOME))) {# checks if env variable jboss is set and is valid folder
269   $SANITIZED_JBOSS_HOME = (Get-Item env:JBOSS_HOME).FullName
270   if($SANITIZED_JBOSS_HOME -ne $RESOLVED_JBOSS_HOME) {
271     echo "WARNING JBOSS_HOME may be pointing to a different installation - unpredictable results may occur."
272     echo ""
273   }
274   $JBOSS_HOME=$SANITIZED_JBOSS_HOME
275 } else {
276     # get the full path (without any relative bits)
277     $JBOSS_HOME=$RESOLVED_JBOSS_HOME
278 }
279 
280 # Setup the JVM
281 if (!(Test-Path env:JAVA)) {
282   if( Test-Path env:JAVA_HOME) {
283 	$JAVA_HOME = (Get-ChildItem env:JAVA_HOME).Value
284 	$JAVA = $JAVA_HOME + "\bin\java.exe"
285   } else {
286     $JAVA = 'java'
287   }
288 }
289 
290 # determine the default module path, if not set
291 $JBOSS_MODULEPATH = Get-Env JBOSS_MODULEPATH $JBOSS_HOME\modules
292 
293 Set-Global-Variables-Standalone
294 
295 # Determine the default JBoss PID file
296 $JBOSS_PIDFILE = Get-Env JBOSS_PIDFILE $SCRIPTS_HOME\process.pid
297 
298 [Environment]::SetEnvironmentVariable("JBOSS_HOME", $JBOSS_HOME, "Process")