How to configure osgi config xml under config folder programatically? | Community
Skip to main content
New Participant
June 30, 2019
Solved

How to configure osgi config xml under config folder programatically?

  • June 30, 2019
  • 11 replies
  • 8866 views

Hi,

I have a requirement to set osgi config properties @programatically. For instance i have created a xml file under config file(email service) with below values

<?xml version="1.0" encoding="UTF-8"?>

<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"

    jcr:primaryType="sling:OsgiConfig"

    debug.email="{Boolean}true"

    from.address=""

    smtp.host="smtp.gmail.com"

    smtp.password="password"

    smtp.port="465"

    smtp.ssl="{Boolean}true"

    smtp.user="mymail@gmail.com"/>

Arun PatidarJoerg Hohsmacdonald2008

I wasnt to set all these values programatically

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

I tried with as MessageGatewayService well, it is taking from address from osgi config but Name is working.

e.g.

email.setFrom("abcd.efg@gmail.com", "Arun Patidar");

aem63app-repo/HTMLEmailServlet.java at master · arunpatidar02/aem63app-repo · GitHub

I am not sure, if there is any other configuration to work above but if you really want to go with config update then you can use below  code :

-------------------------------------------------------------

Configuration configuration = configAdmin.getConfiguration("com.day.cq.mailer.DefaultMailService");

if (configuration != null) {

// get properties

Dictionary<String, Object> properties = configuration.getProperties();

Dictionary<String, Object> newProperties = properties;

// update properties

        if (properties == null) {

        newProperties = new Hashtable<String, Object>();

        }

       

        newProperties.put("smtp.user", "new user");

        newProperties.put("smtp.password","new pass");

        newProperties.put("from.address", "new from");

configuration.update(newProperties);

// send mail

// revert to old property

configuration.update(properties);

}

11 replies

joerghoh
Employee
June 30, 2019

Why do you want to set this within Code? eMail configuration should be very static and known upfront. It's a property of the AEM instance (because it relates to the environment the application is running in) and should not be set by code.

What is your usecaes that you want to set these properties by code?

New Participant
July 20, 2020
You could use the existing service for this , and provide all the properties from xml like this