@Echo Off
rem ---------------------------------------------------------------------------
rem ObjectWeb Area Management Framework
rem Copyright (C) 2005 INRIA - Jacquard Team
rem Contact: architecture@objectweb.org
rem Copyright (C) 1999-2008 Bull S.A.S.
rem Contact: jonas-team@objectweb.org
rem
rem This library is free software; you can redistribute it and/or
rem modify it under the terms of the GNU Lesser General Public
rem License as published by the Free Software Foundation; either
rem version 2.1 of the License, or any later version.
rem
rem This library is distributed in the hope that it will be useful,
rem but WITHOUT ANY WARRANTY; without even the implied warranty of
rem MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rem Lesser General Public License for more details.
rem
rem You should have received a copy of the GNU Lesser General Public
rem License along with this library; if not, write to the Free Software
rem Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
rem USA
rem
rem Initial developer(s): Philippe Merle.
rem Contributor(s): Nicolas Dolet.
rem        S. Ali Tokmen:
rem          - Automatic setting of JONAS_ROOT
rem          - Fixed issues when using this batch in a folder contaning spaces
rem ---------------------------------------------------------------------------

Rem  Check for JONAS_ROOT, if not set set it to the default
if "%JONAS_ROOT%"=="" Goto SetJONAS_ROOT
echo JONAS_ROOT is set to %JONAS_ROOT%
Goto ExecuteBatch

Rem %~dp0 is the directory in which this batch file is,
Rem therefore JONAS_ROOT\bin\nt in our case.
:SetJONAS_ROOT
pushd "%~dp0"
cd ..
cd ..
@set JONAS_ROOT=%CD%
@echo Setting JONAS_ROOT to %JONAS_ROOT%
popd

:ExecuteBatch
if ["%JAVA_HOME%"]==[""] goto setjava
set JAVA="%JAVA_HOME%\bin\java"
goto jarname

:setjava
echo JAVA_HOME is not set, please set it
goto :EOF

:jarname
rem ---------------------------------------------------------------------------
rem Jar name setup
rem ---------------------------------------------------------------------------
set THREAD_JAR=ow_thread.jar
set AREA_JAR=ow_area.jar
set THREAD_JONAS_JAR=ow_jonas_thread.jar
set JAVAMAIL_JAR=mail.jar
set MAILAPI_JAR=mailapi.jar
set JAF_JAR=activation.jar
set ASM_JAR=asm-2.1.jar

rem ---------------------------------------------------------------------------
rem Check argument
rem ---------------------------------------------------------------------------
pushd .
if [%1]==[enable]  goto enable
if [%1]==[disable] goto disable
goto usage

:enable
rem ---------------------------------------------------------------------------
rem                         Enable TMF
rem ---------------------------------------------------------------------------
rem Run AdaptThreadClass to build the adapted Thread class
rem ---------------------------------------------------------------------------

mkdir "%JONAS_ROOT%\lib\tmf\tmp\java\lang"
cd "%JONAS_ROOT%\lib\tmf\tmp"

jar xf "%JONAS_ROOT%\lib\tmf\%THREAD_JAR%"

@echo.
echo Run AdaptThreadClass:
@echo.

%JAVA% -classpath "%JONAS_ROOT%\lib\tmf\%THREAD_JAR%;%JONAS_ROOT%\lib\tmf\%ASM_JAR%" org.objectweb.area.thread.AdaptThreadClass %JONAS_ROOT%\lib\tmf\tmp\java\lang\


rem ---------------------------------------------------------------------------
rem Create jars into JONAS_ROOT\lib\endorsed and JONAS_ROOT\lib\commons\jonas
rem ---------------------------------------------------------------------------

cd "%JONAS_ROOT%\lib\endorsed"

@echo.
echo Create %THREAD_JAR% into %JONAS_ROOT%\lib\endorsed:

if exist "%JONAS_ROOT%\lib\tmf\tmp\java\lang\ThreadGroup.class" goto threadgroupjar
goto threadjar

:threadgroupjar
jar cf %THREAD_JAR% -C "%JONAS_ROOT%\lib\tmf\tmp" java/lang/ThreadGroup.class -C "%JONAS_ROOT%\lib\tmf\tmp" org/objectweb/area/thread/ThreadManager.class -C "%JONAS_ROOT%\lib\tmf\tmp" ow_thread.xml
goto deleteTmp

:threadjar
jar cf %THREAD_JAR% -C "%JONAS_ROOT%\lib\tmf\tmp" java/lang/Thread.class -C "%JONAS_ROOT%\lib\tmf\tmp" org/objectweb/area/thread/ThreadManager.class -C "%JONAS_ROOT%\lib\tmf\tmp" ow_thread.xml

:deleteTmp
rd /S /Q "%JONAS_ROOT%\lib\tmf\tmp"

echo Copy %AREA_JAR% into %JONAS_ROOT%\lib\endorsed:
xcopy /Y "%JONAS_ROOT%\lib\tmf\%AREA_JAR%"            "%JONAS_ROOT%\lib\endorsed"

echo Copy %THREAD_JONAS_JAR% into %JONAS_ROOT%\lib\commons\jonas:
xcopy /Y "%JONAS_ROOT%\lib\tmf\%THREAD_JONAS_JAR%"    "%JONAS_ROOT%\lib\commons\jonas"

echo Copy %JAF_JAR%, %JAVAMAIL_JAR% and %MAILAPI_JAR% into %JONAS_ROOT%\lib\endorsed:
xcopy /Y "%JONAS_ROOT%\lib\tmf\%JAF_JAR%"             "%JONAS_ROOT%\lib\endorsed"
xcopy /Y "%JONAS_ROOT%\lib\tmf\%JAVAMAIL_JAR%"        "%JONAS_ROOT%\lib\endorsed"
xcopy /Y "%JONAS_ROOT%\lib\tmf\%MAILAPI_JAR%"         "%JONAS_ROOT%\lib\endorsed"

cd "%JONAS_ROOT%"

@echo.
echo ThreadManagementFramework enabled !
echo Please check jonas.properties to enable the thread service.
@echo.
goto :popd

:disable

rem ---------------------------------------------------------------------------
rem                         Disable TMF
rem ---------------------------------------------------------------------------
rem Delete jars from JONAS_ROOT/lib/endorsed and JONAS_ROOT/lib/commons/jonas
rem ---------------------------------------------------------------------------

cd "%JONAS_ROOT%\lib\endorsed"

@echo.
echo Delete %THREAD_JAR%, %AREA_JAR%, %JAVAMAIL_JAR%, %MAILAPI_JAR% and %JAF_JAR% from %JONAS_ROOT%\lib\endorsed

del /Q "%JONAS_ROOT%\lib\endorsed\%THREAD_JAR%"
del /Q "%JONAS_ROOT%\lib\endorsed\%AREA_JAR%"
del /Q "%JONAS_ROOT%\lib\endorsed\%JAVAMAIL_JAR%"
del /Q "%JONAS_ROOT%\lib\endorsed\%MAILAPI_JAR%"
del /Q "%JONAS_ROOT%\lib\endorsed\%JAF_JAR%"
del /Q "%JONAS_ROOT%\lib\commons\jonas\%THREAD_JONAS_JAR%"

cd "%JONAS_ROOT%"

@echo.
echo ThreadManagementFramework disabled !
echo Please check jonas.properties to disable the thread service
@echo.

goto :popd

:usage
echo "Usage: controlTMF enable|disable"
goto :popd

:popd
popd
goto :EOF
