Domanda

Problem

I need to integrate AspectJ code into an existing application running on Tomcat, but I think I am not setting JAVA_OPTS correctly.

Our vendor has created some AspectJ code that passes logged in user id information to the CONTEXT_INFO() object within MSSQLServer Connection. This is so that within an audit database trigger that we created, we can capture the user id that made the change.

What I have done

  1. Added the following code to our database trigger

    DECLARE @appUserID INT

    SET @appUserID = ISNULL(REPLACE(CONVERT(VarChar(128), CONTEXT_INFO()),CHAR(0), ''), '0');

  2. Added aspectjrt.jar to the web application WEB-INF\lib folder.

  3. Added vendorAspectJCode.jar to the web application WEB-INF\lib folder.
  4. Added aspectjweaver.jar to tomcat's lib folder \tomcat7.0.27\lib
  5. Edited catalina.bat with the following:

there is a line of code that looks like this:

set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%

I have changed that to

 set JAVA_OPTS=”%JAVA_OPTS% %LOGGING_CONFIG% -javaagent:D:\tomcat\tomcat7.0.27\lib\aspectjweaver.jar"

but it did not seem to work.

So then I have tried setting it like that, adding a new set JAVA_OPTS:

set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%
set JAVA_OPTS="-javaagent:D:\tomcat\tomcat7.0.27\lib\aspectjweaver.jar"

but that did not seem to do the trick either

After making the following changes and running a test through the web application front end, the user id that was inserted into the database was 0, so that tells me that something has not been done right and the part that I feel less comfortable with all of the steps above was Step 5.

  1. Does anybody know if the syntax for setting JAVA_OPTS is correct?
  2. or whether there is another place to put it?
È stato utile?

Soluzione

After a lot of trial and error I found out how to integrate AspectJ in Tomcat running as a Service on a Windows server. I do not know why, but the bolded stuff was the cause to my problems.

Of course, as I mentioned in my question above you need the following prerequisites:

  1. Add aspectjrt.jar to the web application WEB-INF\lib folder.
  2. Add vendorAspectJCode.jar to the web application WEB-INF\lib folder.
  3. Add aspectjweaver.jar to tomcat's lib folder \tomcat7.0.27\lib

Setting -javaagent:PathToMyAspectjweaver\aspectjweaver.jar in the service.bat did not work. So I had to set it in the registry along with uninstalling/installing the Tomcat service for changes to be picked up by doing as follows:

  1. First I recommend turning UAC off and to make sure that you are an Administrator
  2. Stop the Tomcat service if running.
  3. Delete the tomcat service.
  4. Verify in Windows Services that the service is no longer there.
  5. Verify the Windows registry that everything related to the service got deleted. If not, do so manually.
  6. Install the Tomcat service.
  7. Verify in Windows Services that the service got created.
  8. Find the service in the registry and edit the variable Options apppending the following:

    -javaagent:PathToMyAspectjweaver\aspectjweaver.jar

I have created a couple of bat files for these steps. Steps 2 and 3 would look something similar to this below (TomcatServiceUninstall.bat):

echo OFF
ECHO Removing Tomcat Service...
sc stop YourServiceName
sc delete YourServiceName
ECHO Removing Registry Key containing config data for Tomcat7
REG DELETE "HKLM\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\YourServiceName" /f
REG DELETE "HKLM\SOFTWARE\Wow6432Node\Apache Software Foundation\Tomcat\7.0" /f
ECHO Uninstall Complete - File Directories remain intact. 

Step 6 would look like that (TomcatServiceInstall.bat)

ECHO OFF
ECHO Running Service.bat to install the Tomcat 7 - YourServiceName - Service
cd "C:\Path to your tomcat\tomcat7.0.27\bin"
service.bat install
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top