1@echo off
2
3REM -------------------------------------------------------------------------
4REM  WildFly Service Script for Windows
5REM    It has to reside in %JBOSS_HOME%\bin
6REM    It is expecting that prunsrv.exe reside in:
7REM      %JBOSS_HOME%\bin\service\
8REM Easiest way to make it work is to copy whole "service" directory to %JBOSS_HOME%\bin
9REM
10REM  v9 2016-02-16 customize for WildFly, fix working on paths with spaces (Tomaz Cerar)
11REM  v8 2016-01-20 customize for EAP 7 (Petr Sakar)
12REM  v7 2014-07-09 added /logpath /startup /config /hostconfig /base /debug
13REM                      simplified/corrected use of quotes
14REM
15REM  v6 was shipped with EAP 6.2.0 and supports all previous versions of 6.x as well.
16REM  v6 2013-08-21 added /name /desc
17REM                added /serviceuser /servicepass
18REM                extended directory checking for versions and locations
19REM                extended checking on option usage
20REM  v5	2013-06-10 adapted for EAP 6.1.0
21REM  v4	2012-10-03 Small changes to properly handles spaces in LogPath, StartPath,
22REM                and StopPath (George Rypysc)
23REM  v3	2012-09-14 fixed service log path
24REM                cmd line options for controller,domain host, loglevel,
25REM		   username,password
26REM  v2	2012-09-05 NOPAUSE support
27REM  v1	2012-08-20 initial edit
28REM
29REM Author: Tom Fonteyne (unless noted above)
30REM ========================================================
31setlocal EnableExtensions EnableDelayedExpansion
32
33set DEBUG=0
34if "%DEBUG%" == "1" (
35	echo "Debug info enabled"
36	echo on
37)
38
39set "DIRNAME=%~dp0%"
40if "%DEBUG%" == "1" (
41	echo DIRNAME "%DIRNAME%x"
42)
43
44if exist "%DIRNAME%..\jboss-modules.jar" (
45  REM we are in JBOSS_HOME/bin
46  set "WE=%DIRNAME%..\"
47  goto :WE_FOUND
48) else if exist "%DIRNAME%..\..\jboss-modules.jar" (
49  REM we are in bin\service in a WildFly installation
50  set "WE=%DIRNAME%..\..\"
51  goto :WE_FOUND
52) else if exist "%DIRNAME%..\..\..\jboss-modules.jar" (
53  REM we are in sbin in a 6.0.x installation
54  set "WE=%DIRNAME%..\..\..\"
55  goto :WE_FOUND
56) else (
57  REM we should be in sbin in 6.1 and up
58  set "WE=%DIRNAME%..\..\..\..\..\..\"
59)
60
61if "%DEBUG%" == "1" (
62	echo WE was not found, using "%WE%"
63)
64
65:WE_FOUND
66if "%DEBUG%" == "1" (
67	echo WE "%WE%"
68)
69pushd "%WE%"
70set "RESOLVED_JBOSS_HOME=%CD%"
71popd
72set WE=
73set DIRNAME=
74if "x%JBOSS_HOME%" == "x" (
75  set "JBOSS_HOME=%RESOLVED_JBOSS_HOME%"
76)
77
78pushd "%JBOSS_HOME%"
79set "SANITIZED_JBOSS_HOME=%CD%"
80popd
81
82if "%DEBUG%" == "1" (
83	echo SANITIZED_JBOSS_HOME="%SANITIZED_JBOSS_HOME%"
84	echo RESOLVED_JBOSS_HOME="%RESOLVED_JBOSS_HOME%"
85	echo JBOSS_HOME="%JBOSS_HOME%"
86)
87
88if not "%RESOLVED_JBOSS_HOME%x" == "%SANITIZED_JBOSS_HOME%x" (
89    echo WARNING JBOSS_HOME may be pointing to a different installation - unpredictable results may occur.
90    goto cmdEnd
91)
92
93rem Find jboss-modules.jar to check JBOSS_HOME
94if not exist "%JBOSS_HOME%\jboss-modules.jar" (
95  echo Could not locate "%JBOSS_HOME%\jboss-modules.jar"
96  goto cmdEnd
97)
98
99set PRUNSRV=
100if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
101  echo Using the X86-64bit version of prunsrv
102  set PRUNSRV="%JBOSS_HOME%\bin\service\amd64\wildfly-service"
103) else (
104  echo Using the X86-32bit version of prunsrv
105  set PRUNSRV="%JBOSS_HOME%\bin\service\wildfly-service"
106)
107
108if "%DEBUG%" == "1" (
109	echo PRUNSRV %PRUNSRV%
110)
111
112echo(
113
114rem defaults
115set SHORTNAME=Wildfly
116set DISPLAYNAME=WildFly
117rem NO quotes around the description here !
118set DESCRIPTION="WildFly Application Server"
119set CONTROLLER=localhost:9990
120set DC_HOST=master
121set IS_DOMAIN=false
122set LOGLEVEL=INFO
123set LOGPATH=
124set JBOSSUSER=
125set JBOSSPASS=
126set SERVICE_USER=
127set SERVICE_PASS=
128set STARTUP_MODE=manual
129set ISDEBUG=
130set CONFIG=
131set HOSTCONFIG=host.xml
132set BASE=
133
134set COMMAND=%1
135shift
136if /I "%COMMAND%" == "install"   goto cmdInstall
137if /I "%COMMAND%" == "uninstall" goto cmdUninstall
138if /I "%COMMAND%" == "start"     goto cmdStart
139if /I "%COMMAND%" == "stop"      goto cmdStop
140if /I "%COMMAND%" == "restart"   goto cmdRestart
141
142echo ERROR: invalid command
143
144:cmdUsage
145echo WildFly Service Script for Windows
146echo Usage:
147echo(
148echo   service install ^<options^>  , where the options are:
149echo(
150echo     /startup                  : Set the service to auto start
151echo                                 Not specifying sets the service to manual
152echo(
153echo     /jbossuser ^<username^>     : JBoss username to use for the shutdown command.
154echo     /jbosspass ^<password^>     : Password for /jbossuser
155echo(
156echo     /controller ^<host:port^>   : The host:port of the management interface.
157echo                                 default: %CONTROLLER%
158echo(
159echo     /host [^<domainhost^>]      : Indicates that domain mode is to be used with an
160echo                                 optional domain controller name.
161echo                                 default: %DC_HOST%
162echo                                 Not specifying /host will install JBoss in
163echo                                 standalone mode.
164echo(
165echo Options to use when multiple services or different accounts are needed:
166echo(
167echo     /name ^<servicename^>       : The name of the service
168echo(
169echo                                 default: %SHORTNAME%
170echo     /desc ^<description^>       : The description of the service, use double
171echo                                 quotes to allow spaces.
172echo                                 Maximum 1024 characters.
173echo                                 default: %DESCRIPTION%
174echo(
175echo     /serviceuser ^<username^>   : Specifies the name of the account under which
176echo                                 the service should run.
177echo                                 Use an account name in the form of
178echo                                 DomainName\UserName
179echo                                 default: not used, the service runs as
180echo                                 Local System Account.
181echo     /servicepass ^<password^>   : password for /serviceuser
182echo(
183echo Advanced options:
184echo(
185echo     /config ^<xmlfile^>         : The server-config to use
186echo                                 default: standalone.xml / domain.xml
187echo     /hostconfig ^<xmlfile^>     : domain mode only, the host config to use
188echo                                 default: host.xml
189echo(
190echo     /base ^<directory^>         : The base directory for server/domain content
191echo                                 Must be specified as a fully qualified path
192echo                                 default: %JBOSS_HOME%\standalone or
193echo                                          %JBOSS_HOME%\domain
194echo(
195echo     /loglevel ^<level^>         : The log level for the service:  Error, Info,
196echo                                 Warn or Debug ^(Case insensitive^)
197echo                                 default: %LOGLEVEL%
198echo     /logpath ^<path^>           : Path of the log
199echo                                 default depends on domain or standalone mode
200echo                                 /base applies when /logpath is not set.
201echo                                   %JBOSS_HOME%\domain\log
202echo                                   %JBOSS_HOME%\standalone\log
203echo(
204echo     /debug                    : run the service install in debug mode
205echo(
206echo Other commands:
207echo(
208echo   service uninstall [/name ^<servicename^>]
209echo   service start [/name ^<servicename^>]
210echo   service stop [/name ^<servicename^>]
211echo   service restart [/name ^<servicename^>]
212echo(
213echo     /name  ^<servicename^>      : Name of the service: should not contain spaces
214echo                                 default: %SHORTNAME%
215echo(
216goto endBatch
217
218:cmdInstall
219
220:LoopArgs
221if "%~1" == "" goto doInstall
222
223if /I "%~1"== "/debug" (
224  set ISDEBUG=true
225  shift
226  goto LoopArgs
227)
228if /I "%~1"== "/startup" (
229  set STARTUP_MODE=auto
230  shift
231  goto LoopArgs
232)
233if /I "%~1"== "/config" (
234  set CONFIG=
235  if not "%~2"=="" (
236    set T=%~2
237    if not "!T:~0,1!"=="/" (
238      set CONFIG=%~2
239    )
240  )
241  if "!CONFIG!" == "" (
242    echo ERROR: You need to specify a config name
243    goto endBatch
244  )
245  shift
246  shift
247  goto LoopArgs
248)
249if /I "%~1"== "/hostconfig" (
250  set HOSTCONFIG=
251  if not "%~2"=="" (
252    set T=%~2
253    if not "!T:~0,1!"=="/" (
254      set HOSTCONFIG=%~2
255    )
256  )
257  if "!HOSTCONFIG!" == "" (
258    echo ERROR: You need to specify a host-config name
259    goto endBatch
260  )
261  shift
262  shift
263  goto LoopArgs
264)
265if /I "%~1"== "/base" (
266  set BASE=
267  if not "%~2"=="" (
268    set T=%~2
269    if not "!T:~0,1!"=="/" (
270      set BASE=%~2
271    )
272  )
273  if "!BASE!" == "" (
274    echo ERROR: You need to specify a base directory name
275    goto endBatch
276  )
277  shift
278  shift
279  goto LoopArgs
280)
281if /I "%~1"== "/controller" (
282  set CONTROLLER=
283  if not "%~2"=="" (
284    set T=%~2
285    if not "!T:~0,1!"=="/" (
286      set CONTROLLER=%~2
287    )
288  )
289  if "!CONTROLLER!" == "" (
290    echo ERROR: The management interface should be specified in the format host:port, example:  127.0.0.1:9999
291    goto endBatch
292  )
293  shift
294  shift
295  goto LoopArgs
296)
297if /I "%~1"== "/name" (
298  set SHORTNAME=
299  if not "%~2"=="" (
300    set T=%~2
301    if not "!T:~0,1!"=="/" (
302      set SHORTNAME=%~2
303      set DISPLAYNAME=%~2
304    )
305  )
306  if "!SHORTNAME!" == "" (
307    echo ERROR: You need to specify a service name
308    goto endBatch
309  )
310  shift
311  shift
312  goto LoopArgs
313)
314if /I "%~1"== "/desc" (
315  set DESCRIPTION=
316  if not "%~2"=="" (
317    set T=%~2
318    if not "!T:~0,1!"=="/" (
319      set DESCRIPTION=%~2
320    )
321  )
322  if "!DESCRIPTION!" == "" (
323    echo ERROR: You need to specify a description, maximum of 1024 characters
324    goto endBatch
325  )
326  shift
327  shift
328  goto LoopArgs
329)
330if /I "%~1"== "/jbossuser" (
331  set JBOSSUSER=
332  if not "%~2"=="" (
333    set T=%~2
334    if not "!T:~0,1!"=="/" (
335      set JBOSSUSER=%~2
336    )
337  )
338  if "!JBOSSUSER!" == "" (
339    echo ERROR: You need to specify a username
340    goto endBatch
341  )
342  shift
343  shift
344  goto LoopArgs
345)
346if /I "%~1"== "/jbosspass" (
347  set JBOSSPASS=
348  if not "%~2"=="" (
349    set T=%~2
350    if not "!T:~0,1!"=="/" (
351      set JBOSSPASS=%~2
352    )
353  )
354  if "!JBOSSPASS!" == "" (
355    echo ERROR: You need to specify a password for /jbosspass
356    goto endBatch
357  )
358  shift
359  shift
360  goto LoopArgs
361)
362if /I "%~1"== "/serviceuser" (
363  set SERVICE_USER=
364  if not "%~2"=="" (
365    set T=%~2
366    if not "!T:~0,1!"=="/" (
367      set SERVICE_USER=%~2
368    )
369  )
370  if "!SERVICE_USER!" == "" (
371    echo ERROR: You need to specify a username in the format DOMAIN\USER, or .\USER for the local domain
372    goto endBatch
373  )
374  shift
375  shift
376  goto LoopArgs
377)
378if /I "%~1"== "/servicepass" (
379  set SERVICE_PASS=
380  if not "%~2"=="" (
381    set T=%~2
382    if not "!T:~0,1!"=="/" (
383      set SERVICE_PASS=%~2
384    )
385  )
386  if "!SERVICE_PASS!" == "" (
387    echo ERROR: You need to specify a password for /servicepass
388    goto endBatch
389  )
390  shift
391  shift
392  goto LoopArgs
393)
394rem the hostname is optional
395if /I "%~1"== "/host" (
396  set IS_DOMAIN=true
397  if not "%~2"=="" (
398    set T=%~2
399    if not "!T:~0,1!"=="/" (
400      set DC_HOST=%~2
401      shift
402    )
403  )
404  shift
405  goto LoopArgs
406)
407if /I "%~1"== "/loglevel" (
408  if /I not "%~2"=="Error" if /I not "%~2"=="Info" if /I not "%~2"=="Warn" if /I not "%~2"=="Debug" (
409    echo ERROR: /loglevel must be set to Error, Info, Warn or Debug ^(Case insensitive^)
410    goto endBatch
411  )
412  set LOGLEVEL=%~2
413  shift
414  shift
415  goto LoopArgs
416)
417if /I "%~1"== "/logpath" (
418  set LOGPATH=
419  if not "%~2"=="" (
420    set T=%~2
421    if not "!T:~0,1!"=="/" (
422      set LOGPATH=%~2
423	)
424  )
425  if "!LOGPATH!" == "" (
426    echo ERROR: You need to specify a path for the service log
427    goto endBatch
428  )
429  shift
430  shift
431  goto LoopArgs
432)
433echo ERROR: Unrecognised option: %1
434echo(
435goto cmdUsage
436
437:doInstall
438set CREDENTIALS=
439if not "%JBOSSUSER%" == "" (
440  if "%JBOSSPASS%" == "" (
441    echo When specifying a user, you need to specify the password
442    goto endBatch
443  )
444  set CREDENTIALS=--user=%JBOSSUSER% --password=%JBOSSPASS%
445)
446
447set RUNAS=
448if not "%SERVICE_USER%" == "" (
449  if "%SERVICE_PASS%" == "" (
450    echo When specifying a user, you need to specify the password
451    goto endBatch
452  )
453  set RUNAS=--ServiceUser=%SERVICE_USER% --ServicePassword=%SERVICE_PASS%
454)
455
456if "%STDOUT%"=="" set STDOUT=auto
457if "%STDERR%"=="" set STDERR=auto
458
459if "%START_PATH%"=="" set START_PATH="%JBOSS_HOME%\bin"
460if "%STOP_PATH%"=="" set STOP_PATH="%JBOSS_HOME%\bin"
461
462if "%STOP_SCRIPT%"=="" set STOP_SCRIPT=jboss-cli.bat
463
464if /I "%IS_DOMAIN%" == "true" (
465  if "%BASE%"=="" set BASE="%JBOSS_HOME%\domain"
466  if "%CONFIG%"=="" set CONFIG=domain.xml
467  if "%START_SCRIPT%"=="" set START_SCRIPT=domain.bat
468  set STARTPARAM="/c#set#NOPAUSE=Y#&&#!START_SCRIPT!#-Djboss.domain.base.dir=!BASE!#--domain-config=!CONFIG!#--host-config=!HOSTCONFIG!"
469  set STOPPARAM="/c %STOP_SCRIPT% --controller=%CONTROLLER% --connect %CREDENTIALS% --command=/host=!DC_HOST!:shutdown"
470) else (
471  if "%BASE%"=="" set "BASE=%JBOSS_HOME%\standalone"
472  if "%CONFIG%"=="" set CONFIG=standalone.xml
473  if "%START_SCRIPT%"=="" set START_SCRIPT=standalone.bat
474  set STARTPARAM="/c#set#NOPAUSE=Y#&&#!START_SCRIPT!#-Djboss.server.base.dir=!BASE!#--server-config=!CONFIG!"
475  set STOPPARAM="/c !STOP_SCRIPT! --controller=%CONTROLLER% --connect %CREDENTIALS% --command=:shutdown"
476)
477
478if "%LOGPATH%"=="" set LOGPATH="!BASE!\log"
479
480if not exist "%BASE%" (
481  echo The base directory does not exist: "%BASE%"
482  goto endBatch
483)
484
485if not exist "%BASE%\configuration\%CONFIG%" (
486  echo The configuration does not exist: "%BASE%\configuration\%CONFIG%"
487  goto endBatch
488)
489
490if /I "%ISDEBUG%" == "true" (
491  echo JBOSS_HOME="%JBOSS_HOME%"
492  echo RUNAS=%RUNAS%
493  echo SHORTNAME="%SHORTNAME%"
494  echo DESCRIPTION="%DESCRIPTION%"
495  echo STARTPARAM=%STARTPARAM%
496  echo STOPPARAM=%STOPPARAM%
497  echo LOGLEVEL=%LOGLEVEL%
498  echo LOGPATH=%LOGPATH%
499  echo CREDENTIALS=%CREDENTIALS%
500  echo BASE="%BASE%"
501  echo CONFIG="%CONFIG%"
502  echo START_SCRIPT=%START_SCRIPT%
503  echo START_PATH=%START_PATH%
504  echo STOP_SCRIPT=%STOP_SCRIPT%
505  echo STOP_PATH=%STOP_PATH%
506  echo STDOUT="%STDOUT%"
507  echo STDERR="%STDERR%"
508)
509if /I "%ISDEBUG%" == "true" (
510  @echo on
511)
512
513@rem quotes around the "%DESCRIPTION%" but nowhere else
514echo %PRUNSRV% install %SHORTNAME% %RUNAS% --DisplayName=%DISPLAYNAME% --Description="%DESCRIPTION%" --LogLevel=%LOGLEVEL% --LogPath=%LOGPATH% --LogPrefix=service --StdOutput=%STDOUT% --StdError=%STDERR% --StartMode=exe --Startup=%STARTUP_MODE% --StartImage=cmd.exe --StartPath=%START_PATH% ++StartParams=%STARTPARAM% --StopMode=exe --StopImage=cmd.exe --StopPath=%STOP_PATH%  ++StopParams=%STOPPARAM%
515
516%PRUNSRV% install %SHORTNAME% %RUNAS% --DisplayName=%DISPLAYNAME% --Description="%DESCRIPTION%" --LogLevel=%LOGLEVEL% --LogPath=%LOGPATH% --LogPrefix=service --StdOutput=%STDOUT% --StdError=%STDERR% --StartMode=exe --Startup=%STARTUP_MODE% --StartImage=cmd.exe --StartPath=%START_PATH% ++StartParams=%STARTPARAM% --StopMode=exe --StopImage=cmd.exe --StopPath=%STOP_PATH%  ++StopParams=%STOPPARAM%
517@rem %PRUNSRV% install "%SHORTNAME%" "%RUNAS%" --DisplayName="%DISPLAYNAME%" --Description="%DESCRIPTION%" --LogLevel="%LOGLEVEL%" --LogPath="%LOGPATH%" --LogPrefix=service --StdOutput="%STDOUT%" --StdError="%STDERR%" --StartMode=exe --Startup="%STARTUP_MODE%" --StartImage=cmd.exe --StartPath="%START_PATH%" ++StartParams="%STARTPARAM%" --StopMode=exe --StopImage=cmd.exe --StopPath="%STOP_PATH%"  ++StopParams="%STOPPARAM%"
518
519@if /I "%ISDEBUG%" == "true" (
520  @echo off
521)
522
523if errorlevel 8 (
524  echo ERROR: The service %SHORTNAME% already exists
525  goto endBatch
526)
527if errorlevel 0 (
528  echo Service %SHORTNAME% installed
529  goto endBatch
530)
531goto cmdEnd
532
533
534REM the other commands take a /name parameter - if there is no ^<servicename^> passed as second parameter,
535REM we silently ignore this and use the default SHORTNAME
536
537:cmdUninstall
538if /I "%~1"=="/name" (
539  if not "%~2"=="" (
540    set SHORTNAME="%~2"
541  )
542)
543%PRUNSRV% stop %SHORTNAME%
544if errorlevel 0 (
545  %PRUNSRV% delete %SHORTNAME%
546  if errorlevel 0 (
547    echo Service %SHORTNAME% uninstalled
548  )
549) else (
550  echo Unable to stop the service %SHORTNAME%
551)
552goto cmdEnd
553
554:cmdStart
555if /I "%~1"=="/name" (
556  if not "%~2"=="" (
557    set SHORTNAME="%~2"
558  )
559)
560%PRUNSRV% start %SHORTNAME%
561echo Service %SHORTNAME% starting...
562goto cmdEnd
563
564:cmdStop
565if /I "%~1"=="/name" (
566  if not "%~2"=="" (
567    set SHORTNAME="%~2"
568  )
569)
570%PRUNSRV% stop %SHORTNAME%
571echo Service %SHORTNAME% stopping...
572goto cmdEnd
573
574:cmdRestart
575if /I "%~1"=="/name" (
576  if not "%~2"=="" (
577    set SHORTNAME="%~2"
578  )
579)
580%PRUNSRV% stop %SHORTNAME%
581echo Service %SHORTNAME% stopping...
582if "%errorlevel%" == "0" (
583  %PRUNSRV% start %SHORTNAME%
584  echo Service %SHORTNAME% starting...
585) else (
586  echo Unable to stop the service %SHORTNAME%
587)
588goto cmdEnd
589
590
591:cmdEnd
592REM if there is a need to add other error messages, make sure to list higher numbers first !
593if errorlevel 2 (
594  echo ERROR: Failed to load service %SHORTNAME% configuration
595  goto endBatch
596)
597if errorlevel 0 (
598  goto endBatch
599)
600echo "Unforseen error=%errorlevel%"
601
602rem nothing below, exit
603:endBatch
604