karaf.bat 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. @echo off
  2. rem
  3. rem
  4. rem Licensed to the Apache Software Foundation (ASF) under one or more
  5. rem contributor license agreements. See the NOTICE file distributed with
  6. rem this work for additional information regarding copyright ownership.
  7. rem The ASF licenses this file to You under the Apache License, Version 2.0
  8. rem (the "License"); you may not use this file except in compliance with
  9. rem the License. You may obtain a copy of the License at
  10. rem
  11. rem http://www.apache.org/licenses/LICENSE-2.0
  12. rem
  13. rem Unless required by applicable law or agreed to in writing, software
  14. rem distributed under the License is distributed on an "AS IS" BASIS,
  15. rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. rem See the License for the specific language governing permissions and
  17. rem limitations under the License.
  18. rem
  19. if not "%ECHO%" == "" echo %ECHO%
  20. setlocal
  21. set DIRNAME=%~dp0%
  22. set PROGNAME=%~nx0%
  23. set ARGS=%*
  24. rem Sourcing environment settings for karaf similar to tomcats setenv
  25. SET KARAF_SCRIPT="karaf.bat"
  26. if exist "%DIRNAME%setenv.bat" (
  27. call "%DIRNAME%setenv.bat"
  28. )
  29. rem Check console window title. Set to Karaf by default
  30. if not "%KARAF_TITLE%" == "" (
  31. title %KARAF_TITLE%
  32. ) else (
  33. title Karaf
  34. )
  35. rem Check/Set up some easily accessible MIN/MAX params for JVM mem usage
  36. if "%JAVA_MIN_MEM%" == "" (
  37. set JAVA_MIN_MEM=128M
  38. )
  39. if "%JAVA_MAX_MEM%" == "" (
  40. set JAVA_MAX_MEM=512M
  41. )
  42. goto BEGIN
  43. :warn
  44. echo %PROGNAME%: %*
  45. goto :EOF
  46. :BEGIN
  47. rem # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  48. if not "%KARAF_HOME%" == "" (
  49. call :warn Ignoring predefined value for KARAF_HOME
  50. )
  51. set KARAF_HOME=%DIRNAME%..
  52. if not exist "%KARAF_HOME%" (
  53. call :warn KARAF_HOME is not valid: "%KARAF_HOME%"
  54. goto END
  55. )
  56. if not "%KARAF_BASE%" == "" (
  57. if not exist "%KARAF_BASE%" (
  58. call :warn KARAF_BASE is not valid: "%KARAF_BASE%"
  59. goto END
  60. )
  61. )
  62. if "%KARAF_BASE%" == "" (
  63. set "KARAF_BASE=%KARAF_HOME%"
  64. )
  65. if not "%KARAF_DATA%" == "" (
  66. if not exist "%KARAF_DATA%" (
  67. call :warn KARAF_DATA is not valid: "%KARAF_DATA%"
  68. call :warn Creating %KARAF_DATA%
  69. mkdir %KARAF_DATA%
  70. )
  71. )
  72. if "%KARAF_DATA%" == "" (
  73. set "KARAF_DATA=%KARAF_BASE%\data"
  74. )
  75. if not "%KARAF_ETC%" == "" (
  76. if not exist "%KARAF_ETC%" (
  77. call :warn KARAF_ETC is not valid: "%KARAF_ETC%"
  78. goto END
  79. )
  80. )
  81. if "%KARAF_ETC%" == "" (
  82. set "KARAF_ETC=%KARAF_BASE%\etc"
  83. )
  84. set LOCAL_CLASSPATH=%CLASSPATH%
  85. set JAVA_MODE=-server
  86. set CLASSPATH=%LOCAL_CLASSPATH%;%KARAF_BASE%\conf
  87. set DEFAULT_JAVA_DEBUG_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
  88. if "%LOCAL_CLASSPATH%" == "" goto :KARAF_CLASSPATH_EMPTY
  89. set CLASSPATH=%LOCAL_CLASSPATH%;%KARAF_BASE%\conf
  90. goto :KARAF_CLASSPATH_END
  91. :KARAF_CLASSPATH_EMPTY
  92. set CLASSPATH=%KARAF_BASE%\conf
  93. :KARAF_CLASSPATH_END
  94. rem Setup Karaf Home
  95. if exist "%KARAF_HOME%\conf\karaf-rc.cmd" call %KARAF_HOME%\conf\karaf-rc.cmd
  96. if exist "%HOME%\karaf-rc.cmd" call %HOME%\karaf-rc.cmd
  97. rem Support for loading native libraries
  98. set PATH=%PATH%;%KARAF_BASE%\lib;%KARAF_HOME%\lib
  99. rem Setup the Java Virtual Machine
  100. if not "%JAVA%" == "" goto :Check_JAVA_END
  101. if not "%JAVA_HOME%" == "" goto :TryJDKEnd
  102. call :warn JAVA_HOME not set; results may vary
  103. :TryJRE
  104. start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment"
  105. if not exist __reg1.txt goto :TryJDK
  106. type __reg1.txt | find "CurrentVersion" > __reg2.txt
  107. if errorlevel 1 goto :TryJDK
  108. for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JavaTemp=%%~x
  109. if errorlevel 1 goto :TryJDK
  110. set JavaTemp=%JavaTemp%##
  111. set JavaTemp=%JavaTemp: ##=##%
  112. set JavaTemp=%JavaTemp: ##=##%
  113. set JavaTemp=%JavaTemp: ##=##%
  114. set JavaTemp=%JavaTemp: ##=##%
  115. set JavaTemp=%JavaTemp: ##=##%
  116. set JavaTemp=%JavaTemp:##=%
  117. del __reg1.txt
  118. del __reg2.txt
  119. start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\%JavaTemp%"
  120. if not exist __reg1.txt goto :TryJDK
  121. type __reg1.txt | find "JavaHome" > __reg2.txt
  122. if errorlevel 1 goto :TryJDK
  123. for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JAVA_HOME=%%~x
  124. if errorlevel 1 goto :TryJDK
  125. del __reg1.txt
  126. del __reg2.txt
  127. goto TryJDKEnd
  128. :TryJDK
  129. start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
  130. if not exist __reg1.txt (
  131. goto TryRegJRE
  132. )
  133. type __reg1.txt | find "CurrentVersion" > __reg2.txt
  134. if errorlevel 1 (
  135. goto TryRegJRE
  136. )
  137. for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JavaTemp=%%~x
  138. if errorlevel 1 (
  139. goto TryRegJRE
  140. )
  141. set JavaTemp=%JavaTemp%##
  142. set JavaTemp=%JavaTemp: ##=##%
  143. set JavaTemp=%JavaTemp: ##=##%
  144. set JavaTemp=%JavaTemp: ##=##%
  145. set JavaTemp=%JavaTemp: ##=##%
  146. set JavaTemp=%JavaTemp: ##=##%
  147. set JavaTemp=%JavaTemp:##=%
  148. del __reg1.txt
  149. del __reg2.txt
  150. start /w regedit /e __reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\%JavaTemp%"
  151. if not exist __reg1.txt (
  152. goto TryRegJRE
  153. )
  154. type __reg1.txt | find "JavaHome" > __reg2.txt
  155. if errorlevel 1 (
  156. goto TryRegJRE
  157. )
  158. for /f "tokens=2 delims==" %%x in (__reg2.txt) do set JAVA_HOME=%%~x
  159. if errorlevel 1 (
  160. goto TryRegJRE
  161. )
  162. del __reg1.txt
  163. del __reg2.txt
  164. :TryRegJRE
  165. rem try getting the JAVA_HOME from registry
  166. FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment" /v CurrentVersion`) DO (
  167. set JAVA_VERSION=%%A
  168. )
  169. FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKLM\Software\JavaSoft\Java Runtime Environment\%JAVA_VERSION%" /v JavaHome`) DO (
  170. set JAVA_HOME=%%A %%B
  171. )
  172. if not exist "%JAVA_HOME%" (
  173. goto TryRegJDK
  174. )
  175. goto TryJDKEnd
  176. :TryRegJDK
  177. rem try getting the JAVA_HOME from registry
  178. FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKLM\Software\JavaSoft\Java Development Kit" /v CurrentVersion`) DO (
  179. set JAVA_VERSION=%%A
  180. )
  181. FOR /F "usebackq tokens=3*" %%A IN (`REG QUERY "HKLM\Software\JavaSoft\Java Development Kit\%JAVA_VERSION%" /v JavaHome`) DO (
  182. set JAVA_HOME=%%A %%B
  183. )
  184. if not exist "%JAVA_HOME%" (
  185. call :warn Unable to retrieve JAVA_HOME from Registry
  186. )
  187. goto TryJDKEnd
  188. :TryJDKEnd
  189. if not exist "%JAVA_HOME%" (
  190. call :warn JAVA_HOME is not valid: "%JAVA_HOME%"
  191. goto END
  192. )
  193. set JAVA=%JAVA_HOME%\bin\java
  194. :Check_JAVA_END
  195. :CheckRootInstance
  196. set ROOT_INSTANCE_RUNNING=false
  197. if exist "%KARAF_HOME%\instances\instance.properties" (
  198. for /f "delims=" %%x in ( 'findstr "item.0.pid" "%KARAF_HOME%\instances\instance.properties" ' ) do @set pid=%%x
  199. for /f "delims=" %%i in ( 'findstr "item.0.name" "%KARAF_HOME%\instances\instance.properties" ' ) do @set name=%%i
  200. )
  201. set ROOT_INSTANCE_PID=%pid:~13%
  202. set ROOT_INSTANCE_NAME=%name:~14%
  203. SET CHECK_RUNNING_CONDITION=true
  204. if "%ROOT_INSTANCE_PID%" == "~13" SET CHECK_RUNNING_CONDITION=false
  205. if "%ROOT_INSTANCE_PID%" == "0" SET CHECK_RUNNING_CONDITION=false
  206. if "%CHECK_RUNNING_CONDITION%" == "true" (
  207. tasklist /fi "PID eq %ROOT_INSTANCE_PID%" | find ":" > NUL
  208. if errorlevel 1 set ROOT_INSTANCE_RUNNING=true
  209. )
  210. if not exist "%JAVA_HOME%\bin\server\jvm.dll" (
  211. if not exist "%JAVA_HOME%\jre\bin\server\jvm.dll" (
  212. echo WARNING: Running Karaf on a Java HotSpot Client VM because server-mode is not available.
  213. echo Install Java Developer Kit to fix this.
  214. echo For more details see http://java.sun.com/products/hotspot/whitepaper.html#client
  215. set JAVA_MODE=-client
  216. )
  217. )
  218. set DEFAULT_JAVA_OPTS=%JAVA_MODE% -Xms%JAVA_MIN_MEM% -Xmx%JAVA_MAX_MEM% -Dcom.sun.management.jmxremote -XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass
  219. rem Check some easily accessible MIN/MAX params for JVM mem usage
  220. if not "%JAVA_PERM_MEM%" == "" (
  221. set DEFAULT_JAVA_OPTS=%DEFAULT_JAVA_OPTS% -XX:PermSize=%JAVA_PERM_MEM%
  222. )
  223. if not "%JAVA_MAX_PERM_MEM%" == "" (
  224. set DEFAULT_JAVA_OPTS=%DEFAULT_JAVA_OPTS% -XX:MaxPermSize=%JAVA_MAX_PERM_MEM%
  225. )
  226. if "%JAVA_OPTS%" == "" set JAVA_OPTS=%DEFAULT_JAVA_OPTS%
  227. if "%EXTRA_JAVA_OPTS%" == "" goto :KARAF_EXTRA_JAVA_OPTS_END
  228. set JAVA_OPTS=%JAVA_OPTS% %EXTRA_JAVA_OPTS%
  229. :KARAF_EXTRA_JAVA_OPTS_END
  230. if "%KARAF_DEBUG%" == "" goto :KARAF_DEBUG_END
  231. if "%1" == "stop" goto :KARAF_DEBUG_END
  232. if "%1" == "client" goto :KARAF_DEBUG_END
  233. if "%1" == "status" goto :KARAF_DEBUG_END
  234. rem Use the defaults if JAVA_DEBUG_OPTS was not set
  235. if "%JAVA_DEBUG_OPTS%" == "" set JAVA_DEBUG_OPTS=%DEFAULT_JAVA_DEBUG_OPTS%
  236. set JAVA_OPTS=%JAVA_DEBUG_OPTS% %JAVA_OPTS%
  237. call :warn Enabling Java debug options: %JAVA_DEBUG_OPTS%
  238. :KARAF_DEBUG_END
  239. if "%KARAF_PROFILER%" == "" goto :KARAF_PROFILER_END
  240. set KARAF_PROFILER_SCRIPT=%KARAF_HOME%\conf\profiler\%KARAF_PROFILER%.cmd
  241. if exist "%KARAF_PROFILER_SCRIPT%" goto :KARAF_PROFILER_END
  242. call :warn Missing configuration for profiler '%KARAF_PROFILER%': %KARAF_PROFILER_SCRIPT%
  243. goto END
  244. :KARAF_PROFILER_END
  245. rem Setup the classpath
  246. pushd "%KARAF_HOME%\lib\boot"
  247. for %%G in (*.jar) do call:APPEND_TO_CLASSPATH %%G
  248. popd
  249. goto CLASSPATH_END
  250. : APPEND_TO_CLASSPATH
  251. set filename=%~1
  252. set suffix=%filename:~-4%
  253. if %suffix% equ .jar set CLASSPATH=%CLASSPATH%;%KARAF_HOME%\lib\boot\%filename%
  254. goto :EOF
  255. :CLASSPATH_END
  256. SET CHECK_ROOT_INSTANCE_RUNNING=true
  257. rem Execute the JVM or the load the profiler
  258. if "%KARAF_PROFILER%" == "" goto :RUN
  259. rem Execute the profiler if it has been configured
  260. call :warn Loading profiler script: %KARAF_PROFILER_SCRIPT%
  261. call %KARAF_PROFILER_SCRIPT%
  262. :RUN
  263. SET OPTS=-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=true
  264. SET MAIN=org.apache.karaf.main.Main
  265. SET SHIFT=false
  266. :RUN_LOOP
  267. if "%1" == "stop" goto :EXECUTE_STOP
  268. if "%1" == "status" goto :EXECUTE_STATUS
  269. if "%1" == "console" goto :EXECUTE_CONSOLE
  270. if "%1" == "server" goto :EXECUTE_SERVER
  271. if "%1" == "daemon" goto :EXECUTE_DAEMON
  272. if "%1" == "client" goto :EXECUTE_CLIENT
  273. if "%1" == "clean" goto :EXECUTE_CLEAN
  274. if "%1" == "debug" goto :EXECUTE_DEBUG
  275. goto :EXECUTE
  276. :EXECUTE_STOP
  277. SET MAIN=org.apache.karaf.main.Stop
  278. SET CHECK_ROOT_INSTANCE_RUNNING=false
  279. shift
  280. goto :RUN_LOOP
  281. :EXECUTE_STATUS
  282. SET MAIN=org.apache.karaf.main.Status
  283. SET CHECK_ROOT_INSTANCE_RUNNING=false
  284. shift
  285. goto :RUN_LOOP
  286. :EXECUTE_CONSOLE
  287. SET CHECK_ROOT_INSTANCE_RUNNING=false
  288. shift
  289. goto :RUN_LOOP
  290. :EXECUTE_SERVER
  291. SET OPTS=-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true
  292. shift
  293. goto :RUN_LOOP
  294. :EXECUTE_DAEMON
  295. SET OPTS=-Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true
  296. SET KARAF_DAEMON=true
  297. shift
  298. goto :RUN_LOOP
  299. :EXECUTE_CLIENT
  300. SET OPTS=-Dkaraf.startLocalConsole=true -Dkaraf.startRemoteShell=false
  301. SET CHECK_ROOT_INSTANCE_RUNNING=false
  302. shift
  303. goto :RUN_LOOP
  304. :EXECUTE_CLEAN
  305. pushd "%KARAF_DATA%" && (rmdir /S /Q "%KARAF_DATA%" 2>nul & popd)
  306. shift
  307. goto :RUN_LOOP
  308. :EXECUTE_DEBUG
  309. if "%JAVA_DEBUG_OPTS%" == "" set JAVA_DEBUG_OPTS=%DEFAULT_JAVA_DEBUG_OPTS%
  310. set JAVA_OPTS=%JAVA_DEBUG_OPTS% %JAVA_OPTS%
  311. shift
  312. goto :RUN_LOOP
  313. :EXECUTE
  314. SET ARGS=%1 %2 %3 %4 %5 %6 %7 %8
  315. rem Execute the Java Virtual Machine
  316. cd "%KARAF_BASE%"
  317. rem When users want to update the lib version of, they just need to create
  318. rem a lib.next directory and on the new restart, it will replace the current lib directory.
  319. if exist "%KARAF_HOME%\lib.next" (
  320. echo Updating libs...
  321. RD /S /Q "%KARAF_HOME%\lib"
  322. MOVE /Y "%KARAF_HOME%\lib.next" "%KARAF_HOME%\lib"
  323. )
  324. SET IS_RUNNABLE=false
  325. if "%ROOT_INSTANCE_RUNNING%" == "false" SET IS_RUNNABLE=true
  326. if "%CHECK_ROOT_INSTANCE_RUNNING%" == "false" SET IS_RUNNABLE=true
  327. if "%IS_RUNNABLE%" == "true" (
  328. "%JAVA%" %JAVA_OPTS% %OPTS% ^
  329. -classpath "%CLASSPATH%" ^
  330. -Djava.endorsed.dirs="%JAVA_HOME%\jre\lib\endorsed;%JAVA_HOME%\lib\endorsed;%KARAF_HOME%\lib\endorsed" ^
  331. -Djava.ext.dirs="%JAVA_HOME%\jre\lib\ext;%JAVA_HOME%\lib\ext;%KARAF_HOME%\lib\ext" ^
  332. -Dkaraf.instances="%KARAF_HOME%\instances" ^
  333. -Dkaraf.home="%KARAF_HOME%" ^
  334. -Dkaraf.base="%KARAF_BASE%" ^
  335. -Dkaraf.etc="%KARAF_ETC%" ^
  336. -Dkaraf.restart.jvm.supported=true ^
  337. -Djava.io.tmpdir="%KARAF_DATA%\tmp" ^
  338. -Dkaraf.data="%KARAF_DATA%" ^
  339. -Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties" ^
  340. %KARAF_OPTS% %MAIN% %ARGS%
  341. ) else (
  342. echo There is a Root instance already running with name %ROOT_INSTANCE_NAME% and pid %ROOT_INSTANCE_PID%
  343. goto :END
  344. )
  345. rem If KARAF_DAEMON is defined, auto-restart is bypassed and control given
  346. rem back to the operating system
  347. if defined "%KARAF_DAEMON%" (
  348. rem If Karaf has been started by winsw, the process can be restarted
  349. rem by executing KARAF_DAEMON% restart!
  350. rem https://github.com/kohsuke/winsw#restarting-service-from-itself
  351. if defined "%WINSW_EXECUTABLE%" (
  352. if ERRORLEVEL 10 (
  353. echo Restarting ...
  354. %KARAF_DAEMON% restart!
  355. )
  356. ) else (
  357. if ERRORLEVEL 10 (
  358. echo Restarting JVM...
  359. goto EXECUTE
  360. )
  361. )
  362. )
  363. rem # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  364. :END
  365. endlocal
  366. if not "%PAUSE%" == "" pause
  367. :END_NO_PAUSE
  368. EXIT /B %ERRORLEVEL%