Is there a possibility to programmatically set system properties (e.g. email server settings) from within an ivy application? Is there an API?

asked 27.12.2013 at 10:26

DanielGauch's gravatar image

DanielGauch
146369
accept rate: 100%

edited 15.05.2014 at 12:22

Flavio%20Sadeghi's gravatar image

Flavio Sadeghi ♦♦
1.8k5723


Dear Daniel,

We think we can do it via IApplicationConfigurationManager class

public static void setSystemProperty(final String propName, final String propValue) throws Exception {
    SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            IServer server = ServerFactory.getServer();
            IApplicationConfigurationManager appConfManager = server.getApplicationConfigurationManager();

            IProperty property = appConfManager.getSystemProperty(propName);

            if (property != null) {
                property.setValue(propValue);
            }

            return null;
        }
    });
}
link

answered 03.01.2014 at 11:20

anphunl's gravatar image

anphunl
76238
accept rate: 50%

Here i have complete class we're using for this purpose:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;

import ch.ivyteam.db.jdbc.ConnectionUrl;
import ch.ivyteam.db.jdbc.DatabaseConnectionConfiguration;
import ch.ivyteam.ivy.application.IApplication;
import ch.ivyteam.ivy.application.IApplicationConfigurationManager;
import ch.ivyteam.ivy.application.IExternalDatabaseConfiguration;
import ch.ivyteam.ivy.application.restricted.IEnvironment;
import ch.ivyteam.ivy.application.restricted.IGlobalVariable;
import ch.ivyteam.ivy.application.restricted.IWebService;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.security.IRole;
import ch.ivyteam.ivy.security.IUser;
import ch.ivyteam.ivy.security.SecurityManagerFactory;
import ch.ivyteam.ivy.server.IServer;
import ch.ivyteam.ivy.server.ServerFactory;
import ch.ivyteam.ivy.system.IProperty;
import ch.soreco.customers.xabs.DatabaseConfig;
import ch.soreco.customers.xabs.EmailServerConfig;


@SuppressWarnings("restriction")
public class XpertIvyServerUtil {
    public static IServer server;
    public static IApplicationConfigurationManager appConfManager;


    static {
        server = ServerFactory.getServer();
        appConfManager = server.getApplicationConfigurationManager();
    }

    public static IApplication getApplication(final String applicationName) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<IApplication>() {

            @Override
            public IApplication call() throws Exception {
                return appConfManager.findApplication(applicationName);
            }
        });
    }
    public static List<IApplication> getApplicationList() throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<List<IApplication>>() {

            @Override
            public List<IApplication> call() throws Exception {
                return appConfManager.getApplications();
            }
        });
    }

    public static List<IEnvironment> getEnvironmentList(final IApplication application) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<List<IEnvironment>>() {

            @Override
            public List<IEnvironment> call() throws Exception {
                return application.getEnvironments();
            }
        });
    }

    public static IEnvironment getEnvironment(final IApplication application, final String environmentName) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<IEnvironment>() {

            @Override
            public IEnvironment call() throws Exception {
                return application.findEnvironment(environmentName);
            }
        });
    }

    public static void setActiveEnvironment(final IApplication application,final String environmentName) throws Exception{
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                application.setActiveEnvironment(environmentName);
                return null;
            }
        });
    }

    public static String getActiveEnvironment(final IApplication application) throws Exception{
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return application.getActiveEnvironment();
            }
        });

    }

    public static List<IGlobalVariable> getGlobalVariableList(final IEnvironment environment) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<List<IGlobalVariable>>() {

            @Override
            public List<IGlobalVariable> call() throws Exception {

                return environment.getGlobalVariables();
            }
        });
    }


    public static List<IExternalDatabaseConfiguration> getDatabaseList(final IEnvironment environment) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<List<IExternalDatabaseConfiguration>>() {

            @Override
            public List<IExternalDatabaseConfiguration> call() throws Exception {
                return environment.getExternalDatabaseConfigurations();
            }
        });
    }

    public static List<IWebService> getWebServiceList(final IEnvironment environment) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<List<IWebService>>() {

            @Override
            public List<IWebService> call() throws Exception {
                return environment.getWebServices();
            }
        });
    }



    public static IExternalDatabaseConfiguration getDatabase(final IEnvironment environment, final String databaseName) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<IExternalDatabaseConfiguration>() {

            @Override
            public IExternalDatabaseConfiguration call() throws Exception {
                return environment.findExternalDatabaseConfiguration(databaseName);
            }
        });
    }


    public static DatabaseConfig getDatabaseConfig(final IEnvironment environment, final String databaseName) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<DatabaseConfig>() {

            @Override
            public DatabaseConfig call() throws Exception {
                IExternalDatabaseConfiguration exDB = environment.findExternalDatabaseConfiguration(databaseName);
                DatabaseConnectionConfiguration db = exDB.getDatabaseConnectionConfiguration();
                DatabaseConfig data = new DatabaseConfig();
                data.setUsernameDB(db.getUserName());
                data.setPasswordDB(db.getPassword());

                String connectUrl = db.getConnectionUrl();
                ConnectionUrl url;
                if(connectUrl.contains(";")) {
                    url = new ConnectionUrl(connectUrl.replaceFirst(";DatabaseName=","/"));
                } else {
                    url = new ConnectionUrl(connectUrl);
                }
                data.setPortDB(url.getDbPort());
                data.setIsPortDBDefault(false);
                if("".equals(data.getPortDB()) || data.getPortDB() == null) {
                    data.setIsPortDBDefault(true);
                }
                data.setHostDB(url.getDbHost());
                data.setDatabaseName(url.getDbName());
                return data;
            }
        });
    }

    public static List<IProperty> getSystemProperties() throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<List<IProperty>>() {

            @Override
            public List<IProperty> call() throws Exception {
                return appConfManager.getSystemProperties();
            }
        });
    }

    public static String getSystemProperty(final String propName) throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<String>() {

            @Override
            public String call() throws Exception {
                return appConfManager.getSystemProperty(propName).getValue();
            }
        });
    }


    public static void updateDatabaseConfig(final IEnvironment environment,final String databaseName,final DatabaseConfig input) throws Exception {
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                IExternalDatabaseConfiguration exDB = XpertIvyServerUtil.getDatabase(environment, databaseName);
                DatabaseConnectionConfiguration db = exDB.getDatabaseConnectionConfiguration();

                db.setConnectionUrl(input.getConnectDBUrl());
                db.setUserName(input.getUsernameDB());
                db.setPassword(input.getPasswordDB());

                exDB.setDatabaseConnectionConfiguration(db);
                return null;
            }
        });
    }


    public static IUser findUser(String userName, IApplication app)
    {
        List<IUser> users = app.getSecurityContext().getUsers();
        IUser foundUser = null;
        for (IUser user : users)
        {
            if (user.getName().equalsIgnoreCase(userName))
            {
                foundUser = user;
                break;
            }
        }
        return foundUser;
    }


    public static void createIUser(final String userName, final String email,
            final String fullUserName, final String password, final Locale eMailLanguage, final String externalSecuritySystemName) throws Exception {
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {

                Ivy.session().getSecurityContext().createUser(userName, fullUserName, password, eMailLanguage, email, externalSecuritySystemName);

                return null;
            }
        });
    }

    public static void addIRole(final IUser user, final IRole role) throws Exception {
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {

                List<IRole> ivyActualUserRoles = user.getRoles();

                if(!ivyActualUserRoles.contains(role.getName())){
                    user.addRole(role);
                }

                return null;
            }
        });
    }

    public static void removeIRole(final IUser user, final IRole role) throws Exception {
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {

                List<IRole> ivyActualUserRoles = user.getRoles();

                if(!ivyActualUserRoles.contains(role.getName())){
                    user.removeRole(role);
                }

                return null;
            }
        });
    }

    public static EmailServerConfig getEmailServerConfig() throws Exception {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<EmailServerConfig>() {
            @Override
            public EmailServerConfig call() throws Exception {
                EmailServerConfig config =  new EmailServerConfig();
                config.setHostEmailServer(appConfManager.getSystemProperty("EMail.Server.Host").getValue());
                config.setMailAddress(appConfManager.getSystemProperty("EMail.Server.MailAddress").getValue());
                config.setUserEmailServer(appConfManager.getSystemProperty("EMail.Server.User").getValue());
                config.setPasswordEmailServer(appConfManager.getSystemProperty("EMail.Server.Password").getValue());
                config.setPortEmailServer(appConfManager.getSystemProperty("EMail.Server.Port").getValue());
                if(config.getPortEmailServer() == null || config.getPortEmailServer().isEmpty() || config.getPortEmailServer().equals("-1")) {
                    config.setIsPortEmailServerDefault(true);
                }
                return config;
            }
        });
    }

    public static void updateEmailServerConfig(final EmailServerConfig input) throws Exception {
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                appConfManager.getSystemProperty("EMail.Server.Host").setValue(input.getHostEmailServer());
                appConfManager.getSystemProperty("EMail.Server.MailAddress").setValue(input.getMailAddress());
                appConfManager.getSystemProperty("EMail.Server.User").setValue(input.getUserEmailServer());
                appConfManager.getSystemProperty("EMail.Server.Password").setValue(input.getPasswordEmailServer());
                return null;
            }
        });
    }

    public static void updateGlobalVariableValue(final String globalName,final String globalValue,final IEnvironment environment) throws Exception {
        SecurityManagerFactory.getSecurityManager().executeAsSystem(new Callable<Void>() {
            @Override
            public Void call() throws Exception {

                IGlobalVariable variable = environment.findGlobalVariable(globalName);

                if (variable != null) {
                    if (variable.getDefaultGlobalVariable() != null) {
                        variable.setValue(globalValue);
                    } else {
                        environment.createGlobalVariable(globalName, variable.getDescription(), globalValue);
                    }
                }

                return null;
            }
        });
    }

    public static Boolean testConnection (DatabaseConfig input) {
        Boolean isSuccess = false;
        Connection conn;
        try {
            conn = DriverManager.getConnection(input.getConnectDBUrl(), input.getUsernameDB(), input.getPasswordDB());
            isSuccess = conn.isValid(10);// 10 sec
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }
}
link

answered 20.05.2014 at 04:53

trungdv's gravatar image

trungdv
440547290
accept rate: 52%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×29
×12

Asked: 27.12.2013 at 10:26

Seen: 1,630 times

Last updated: 20.05.2014 at 04:53