<p>We are using WSO2 in VM and used below code into mediation to save data inside registry</p>
<pre><code>&lt;script language=&quot;js&quot;&gt;&lt;![CDATA[
                importPackage(Packages.org.apache.synapse.config);
                mc.getConfiguration().getRegistry().newResource(&quot;/keycloak/keycloakkey-kid&quot;,false);
                mc.getConfiguration().getRegistry().updateResource(&quot;/keycloak/keycloakkey-kid&quot;, mc.getProperty(&quot;keycloakkey-kid&quot;));
                mc.getConfiguration().getRegistry().newResource(&quot;/keycloak/keycloakkey-x5c&quot;,false);
                mc.getConfiguration().getRegistry().updateResource(&quot;/keycloak/keycloakkey-x5c&quot;, mc.getProperty(&quot;keycloakkey-x5c&quot;));
                mc.getConfiguration().getRegistry().newResource(&quot;/keycloak/keycloakkey-timestamp&quot;,false);
                mc.getConfiguration().getRegistry().updateResource(&quot;/keycloak/keycloakkey-timestamp&quot;, mc.getProperty(&quot;current-system-time&quot;));
                ]]&gt;
            &lt;/script&gt;
</code></pre>
<p>But now same code i deployed in Docker and Kubenete environment and this is not able to save the data</p>
<p>How to use the registry in the case of Kubernete environment ?</p>
<p>What i tried</p>
<p>in deployment.yaml i added volume mount</p>
<pre><code> volumeMounts:
            - mountPath: /keycloak/keycloakkey-kid
              name: keycloak
      volumes:
        - name: keycloak
</code></pre>
<p>and changed the js code as well</p>
<pre><code> &lt;script language=&quot;js&quot;&gt;&lt;![CDATA[
                importPackage(Packages.org.apache.synapse.config);
                mc.getConfiguration().getRegistry().newResource(&quot;/keycloak/keycloakkey-kid&quot;,false);
                mc.getConfiguration().getRegistry().updateResource(&quot;/keycloak/keycloakkey-kid&quot;, mc.getProperty(&quot;keycloakkey-kid&quot;));
                mc.getConfiguration().getRegistry().newResource(&quot;/keycloak/keycloakkey-x5c&quot;,false);
                mc.getConfiguration().getRegistry().updateResource(&quot;/keycloak/keycloakkey-x5c&quot;, mc.getProperty(&quot;keycloakkey-x5c&quot;));
                mc.getConfiguration().getRegistry().newResource(&quot;/keycloak/keycloakkey-timestamp&quot;,false);
                mc.getConfiguration().getRegistry().updateResource(&quot;/keycloak/keycloakkey-timestamp&quot;, mc.getProperty(&quot;current-system-time&quot;));
                ]]&gt;
            &lt;/script&gt;
</code></pre>
<p>But still failing with error</p>
<pre><code>2021-06-21 16:15:42,576] ERROR {MicroIntegratorRegistry} - Couldn't create resource: keycloakkey-kid
[2021-06-21 16:15:42,577] ERROR {MicroIntegratorRegistry} - Error when adding a new resource org.apache.synapse.SynapseException: Couldn't create resource: keycloakkey-kid
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.handleException(MicroIntegratorRegistry.java:655)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.createFile(MicroIntegratorRegistry.java:830)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.addResource(MicroIntegratorRegistry.java:813)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.newResource(MicroIntegratorRegistry.java:562)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
        at org.mozilla.javascript.gen._Unknown_Source__190._c_script_0(&lt;Unknown Source&gt;:3)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(&lt;Unknown Source&gt;)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(&lt;Unknown Source&gt;)
        at org.mozilla.javascript.gen._Unknown_Source__190.exec(&lt;Unknown Source&gt;)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        at javax.script.CompiledScript.eval(CompiledScript.java:92)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:395)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:290)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:258)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:37)
        at org.apache.synapse.config.xml.SwitchCase.mediate(SwitchCase.java:69)
        at org.apache.synapse.mediators.filters.SwitchMediator.mediate(SwitchMediator.java:134)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:214)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:239)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:244)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:267)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:809)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:309)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:583)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:197)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:285)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

[2021-06-21 16:15:42,581] ERROR {ScriptMediator} - {api:Orders} The script engine returned an error executing the inlined js script function mediate com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.WrappedException: Wrapped org.apache.synapse.SynapseException: Error when adding a new resource (&lt;Unknown Source&gt;#3) in &lt;Unknown Source&gt; at line number 3
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
        at javax.script.CompiledScript.eval(CompiledScript.java:92)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:395)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:290)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:258)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.config.xml.AnonymousListMediator.mediate(AnonymousListMediator.java:37)
        at org.apache.synapse.config.xml.SwitchCase.mediate(SwitchCase.java:69)
        at org.apache.synapse.mediators.filters.SwitchMediator.mediate(SwitchMediator.java:134)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:71)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:158)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:214)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:109)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:239)
        at org.apache.synapse.mediators.filters.FilterMediator.mediate(FilterMediator.java:244)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:267)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:809)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:309)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:583)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:197)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:285)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: org.mozilla.javascript.WrappedException: Wrapped org.apache.synapse.SynapseException: Error when adding a new resource (&lt;Unknown Source&gt;#3)
        at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
        at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:42)
        at org.mozilla.javascript.gen._Unknown_Source__190._c_script_0(&lt;Unknown Source&gt;:3)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(&lt;Unknown Source&gt;)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen._Unknown_Source__190.call(&lt;Unknown Source&gt;)
        at org.mozilla.javascript.gen._Unknown_Source__190.exec(&lt;Unknown Source&gt;)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        ... 27 more
Caused by: org.apache.synapse.SynapseException: Error when adding a new resource
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.handleException(MicroIntegratorRegistry.java:650)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.newResource(MicroIntegratorRegistry.java:564)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
        ... 36 more
Caused by: org.apache.synapse.SynapseException: Couldn't create resource: keycloakkey-kid
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.handleException(MicroIntegratorRegistry.java:655)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.createFile(MicroIntegratorRegistry.java:830)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.addResource(MicroIntegratorRegistry.java:813)
        at org.wso2.micro.integrator.registry.MicroIntegratorRegistry.newResource(MicroIntegratorRegistry.java:562)
        ... 41 more
</code></pre>

Subodh Joshi
<p>The above error happens when you run the <strong>newResource</strong> method in <strong>MicroIntegratorRegistry</strong> class more than once with the same resource path. You should be able to reproduce this error in a VM environment as well if you try out this same mediation flow for the second time. The reason is that <strong>newResource</strong> method in MicroIntegratorRegistry throws this error if the file already exists at the target location.</p>
<p>As a solution, you may use <strong>newNonEmptyResource</strong> method [1] in MicroIntegratorRegistry as follows to fulfill your requirement.</p>
<pre><code>&lt;script language=&quot;js&quot;&gt;&lt;![CDATA[
    importPackage(Packages.org.apache.synapse.config);
    mc.getConfiguration().getRegistry().newNonEmptyResource(&quot;/keycloak/keycloakkey-kid&quot;, false, null, mc.getProperty(&quot;keycloakkey-kid&quot;), null);
    mc.getConfiguration().getRegistry().newNonEmptyResource(&quot;/keycloak/keycloakkey-x5c&quot;, false, null, mc.getProperty(&quot;keycloakkey-x5c&quot;), null);
    mc.getConfiguration().getRegistry().newNonEmptyResource(&quot;/keycloak/keycloakkey-timestamp&quot;, false, null, mc.getProperty(&quot;current-system-time&quot;), null);
    ]]&gt;
&lt;/script&gt;
</code></pre>
<p>The method signature would be like below.</p>
<pre><code>public void newNonEmptyResource(String path, boolean isDirectory, String mediaType, String content,
                                  String propertyName);
</code></pre>
<p>Also, I was able to successfully mount the registry in the Kubernetes environment using the following configuration in pod definition file.</p>
<pre><code>spec:
  containers:
  - image: ...
    ....
    volumeMounts:
    - mountPath: &quot;/home/wso2carbon/wso2mi-4.0.0/registry&quot;
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /registry
      type: DirectoryOrCreate
</code></pre>
<p>or else with Docker, with below configuration in docker-compose.yml file -&gt;</p>
<pre><code>volumes:
      - ./registry:/home/wso2carbon/wso2mi-4.0.0/registry
</code></pre>
<p><strong>Note:</strong> Make sure you give the absolute path to the MI-4.0.0 registry folder as the mount path (in my case it was &quot;<em>/home/wso2carbon/wso2mi-4.0.0/registry</em>&quot; inside the MI-4.0.0 docker container)</p>
<p>Also, I had to change the ownership of my mount path (/registry) as below to start the container successfully inside the pod.</p>
<pre><code>user: wso2carbon
user id: 802
user group: wso2
user group id: 802
</code></pre>
<p>Command:</p>
<pre><code>groupadd --system -g 802 wso2 \
&amp;&amp; useradd --system --home /home/wso2carbon -g 802 -u 802 wso2carbon
</code></pre>
<p>[1] - <a href="https://github.com/wso2/micro-integrator/blob/26d25fbe2f2535de2861b80525646ff28035f2f0/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java#L573" rel="nofollow noreferrer">https://github.com/wso2/micro-integrator/blob/26d25fbe2f2535de2861b80525646ff28035f2f0/components/mediation/registry/org.wso2.micro.integrator.registry/src/main/java/org/wso2/micro/integrator/registry/MicroIntegratorRegistry.java#L573</a></p>
