officecfg/registry/schema/org/openoffice/Office/Java.xcs |   19 ++
 stoc/source/javavm/javavm.cxx                            |  117 ++++++++++-----
 2 files changed, 101 insertions(+), 35 deletions(-)

New commits:
commit fabdac1af8d30f849a94defd516d6699945f908d
Author:     prrvchr <prrv...@gmail.com>
AuthorDate: Mon Mar 24 21:32:21 2025 +0100
Commit:     Stephan Bergmann <stephan.bergm...@allotropia.de>
CommitDate: Tue Mar 25 14:30:56 2025 +0100

    tdf#165774 Support for Java instrumentation
    
    Change-Id: I5323eaba60a1dbc6b872550dbbfed0fe989bcd11
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/183280
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <stephan.bergm...@allotropia.de>

diff --git a/officecfg/registry/schema/org/openoffice/Office/Java.xcs 
b/officecfg/registry/schema/org/openoffice/Office/Java.xcs
index e224edc1b251..1be106081b35 100644
--- a/officecfg/registry/schema/org/openoffice/Office/Java.xcs
+++ b/officecfg/registry/schema/org/openoffice/Office/Java.xcs
@@ -19,6 +19,19 @@
 <!DOCTYPE oor:component-schema SYSTEM "../../../../component-schema.dtd">
 <oor:component-schema xmlns:oor="http://openoffice.org/2001/registry"; 
xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; oor:name="Java" 
oor:package="org.openoffice.Office" xml:lang="en-US">
   <info/>
+  <templates>
+    <group oor:name="InstrumentationAgent">
+      <info>
+        <desc>Contains the Java instrumentation agents archives loaded at JVM 
startup.</desc>
+      </info>
+      <prop oor:name="URL" oor:type="xs:string" oor:nillable="false">
+        <info>
+          <desc>An extension can load its own Java instrumentation agent with 
the value: %origin%/lib/myagent.jar.</desc>
+          <label>Java instrumentation agent archive URL</label>
+        </info>
+      </prop>
+    </group>
+  </templates>
   <component>
     <group oor:name="VirtualMachine">
       <info>
@@ -63,6 +76,12 @@
         </info>
         <value>false</value>
       </prop>
+      <set oor:name="InstrumentationAgents" 
oor:node-type="InstrumentationAgent">
+        <info>
+          <desc>Specifies the Java archives from which the instrumentation 
agents will be loaded at JVM startup.</desc>
+          <label>Java instrumentation agent archives</label>
+        </info>
+      </set>
     </group>
   </component>
 </oor:component-schema>
diff --git a/stoc/source/javavm/javavm.cxx b/stoc/source/javavm/javavm.cxx
index 3aeb7c33ff3f..e86c98c9f22b 100644
--- a/stoc/source/javavm/javavm.cxx
+++ b/stoc/source/javavm/javavm.cxx
@@ -25,6 +25,7 @@
 
 #include <com/sun/star/beans/NamedValue.hpp>
 #include <com/sun/star/container/XContainer.hpp>
+#include <com/sun/star/container/XNameAccess.hpp>
 #include <com/sun/star/java/JavaNotFoundException.hpp>
 #include <com/sun/star/java/InvalidJavaSettingsException.hpp>
 #include <com/sun/star/java/RestartRequiredException.hpp>
@@ -54,6 +55,7 @@
 #include <jvmaccess/classpath.hxx>
 #include <jvmaccess/unovirtualmachine.hxx>
 #include <jvmaccess/virtualmachine.hxx>
+#include <osl/file.hxx>
 #include <rtl/process.h>
 #include <rtl/ustring.hxx>
 #include <sal/types.h>
@@ -321,34 +323,81 @@ void getDefaultLocaleFromConfig(
 }
 
 /// @throws css::uno::Exception
-void getJavaPropsFromSafetySettings(
+void getJavaPropsFromJavaSettings(
     stoc_javavm::JVM * pjvm,
-    const css::uno::Reference<css::lang::XMultiComponentFactory> & xSMgr,
     const css::uno::Reference<css::uno::XComponentContext> &xCtx)
 {
-    css::uno::Reference<css::uno::XInterface> xConfRegistry =
-        xSMgr->createInstanceWithContext(
-            u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
-            xCtx);
-    if(!xConfRegistry.is())
+    css::uno::Reference<css::lang::XMultiServiceFactory> xConfigProvider(
+        xCtx->getValueByName(
+            u"/singletons/com.sun.star.configuration.theDefaultProvider"_ustr),
+        css::uno::UNO_QUERY);
+
+    if (!xConfigProvider.is())
         throw css::uno::RuntimeException(
-            u"javavm.cxx: couldn't get ConfigurationRegistry"_ustr, nullptr);
+            u"javavm.cxx: couldn't get ConfigurationProvider"_ustr, nullptr);
 
-    css::uno::Reference<css::registry::XSimpleRegistry> xConfRegistry_simple(
-        xConfRegistry, css::uno::UNO_QUERY_THROW);
-    xConfRegistry_simple->open(
-        u"org.openoffice.Office.Java"_ustr,
-        true, false);
-    css::uno::Reference<css::registry::XRegistryKey> xRegistryRootKey =
-        xConfRegistry_simple->getRootKey();
+    css::beans::NamedValue aPath(u"nodepath"_ustr, 
css::uno::Any(u"org.openoffice.Office.Java/VirtualMachine"_ustr));
+    css::uno::Sequence<css::uno::Any> aArguments{ css::uno::Any(aPath) };
+
+    css::uno::Reference<css::container::XNameAccess> 
xConfigAccess(xConfigProvider->createInstanceWithArguments(
+            u"com.sun.star.configuration.ConfigurationAccess"_ustr,
+            aArguments),
+        css::uno::UNO_QUERY);
 
-    if (xRegistryRootKey.is())
+    if (!xConfigAccess.is())
+        throw css::uno::RuntimeException(
+            u"javavm.cxx: couldn't get ConfigurationAccess"_ustr, nullptr);
+
+    if (xConfigAccess->hasByName(u"InstrumentationAgents"_ustr))
     {
-        css::uno::Reference<css::registry::XRegistryKey> key_NetAccess= 
xRegistryRootKey->openKey(u"VirtualMachine/NetAccess"_ustr);
-        if (key_NetAccess.is()
-            && key_NetAccess->getValueType() != 
css::registry::RegistryValueType_NOT_DEFINED)
+        css::uno::Reference<css::container::XNameAccess> xAgentAccess;
+        xConfigAccess->getByName(u"InstrumentationAgents"_ustr) >>= 
xAgentAccess;
+        if (xAgentAccess.is() && xAgentAccess->hasElements())
+        {
+            OUString sScheme(u"vnd.sun.star.expand:"_ustr);
+            css::uno::Reference<css::util::XMacroExpander> exp = 
css::util::theMacroExpander::get(xCtx);
+            css::uno::Sequence<OUString> aAgents = 
xAgentAccess->getElementNames();
+            for (auto const & sAgent : aAgents)
+            {
+                css::uno::Reference<css::container::XNameAccess> xAgent;
+                xAgentAccess->getByName(sAgent) >>= xAgent;
+                if (!xAgent->hasByName(u"URL"_ustr))
+                {
+                    SAL_WARN("stoc.java", "Cant retrieve URL property from 
InstrumentationAgent: " << sAgent);
+                    continue;
+                }
+                OUString sUrl;
+                xAgent->getByName(u"URL"_ustr) >>= sUrl;
+                if (sUrl.isEmpty())
+                {
+                    SAL_WARN("stoc.java", "Cant use empty URL property from 
InstrumentationAgent: " << sAgent);
+                    continue;
+                }
+                if (sUrl.startsWithIgnoreAsciiCase(sScheme))
+                {
+                    try {
+                        sUrl = 
exp->expandMacros(sUrl.copy(sScheme.getLength()));
+                    } catch (css::lang::IllegalArgumentException & exception) {
+                        SAL_WARN("stoc.java", exception.Message);
+                        continue;
+                    }
+                }
+                OUString sPath = sUrl;
+                osl::FileBase::RC nError = 
osl::FileBase::getSystemPathFromFileURL(sUrl, sPath);
+                if (nError != osl::FileBase::E_None)
+                {
+                    SAL_WARN("stoc.java", "Cant convert url to system path: " 
<< sUrl);
+                    continue;
+                }
+                pjvm->pushProp("-javaagent:" + sPath);
+            }
+        }
+    }
+    if (xConfigAccess->hasByName(u"NetAccess"_ustr))
+    {
+        sal_Int32 val = 0;
+        if (xConfigAccess->getByName(u"NetAccess"_ustr) >>= val)
         {
-            sal_Int32 val= key_NetAccess->getLongValue();
             OUString sVal;
             switch( val)
             {
@@ -362,20 +411,18 @@ void getJavaPropsFromSafetySettings(
             OUString sProperty = "appletviewer.security.mode=" + sVal;
             pjvm->pushProp(sProperty);
         }
-        css::uno::Reference<css::registry::XRegistryKey> key_CheckSecurity= 
xRegistryRootKey->openKey(
-            u"VirtualMachine/Security"_ustr);
-        if( key_CheckSecurity.is())
-        {
-            bool val = static_cast<bool>(key_CheckSecurity->getLongValue());
-            OUString sProperty(u"stardiv.security.disableSecurity="_ustr);
-            if( val)
-                sProperty += "false";
-            else
-                sProperty += "true";
-            pjvm->pushProp( sProperty);
-        }
     }
-    xConfRegistry_simple->close();
+    if (xConfigAccess->hasByName(u"Security"_ustr))
+    {
+        bool val = true;
+        xConfigAccess->getByName(u"Security"_ustr) >>= val;
+        OUString sProperty(u"stardiv.security.disableSecurity="_ustr);
+        if( val)
+            sProperty += "false";
+        else
+            sProperty += "true";
+        pjvm->pushProp(sProperty);
+    }
 }
 
 void setTimeZone(stoc_javavm::JVM * pjvm) noexcept {
@@ -426,10 +473,10 @@ void initVMConfiguration(
 
     try
     {
-        getJavaPropsFromSafetySettings(&jvm, xSMgr, xCtx);
+        getJavaPropsFromJavaSettings(&jvm, xCtx);
     }
     catch(const css::uno::Exception & exception) {
-        SAL_INFO("stoc", "couldn't get safety settings because of " << 
exception);
+        SAL_INFO("stoc", "couldn't get Java settings because of " << 
exception);
     }
 
     *pjvm = std::move(jvm);

Reply via email to