Package org.wso2.carbon.server.admin.internal

Source Code of org.wso2.carbon.server.admin.internal.ServerAdminServiceComponent

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.server.admin.internal;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.core.init.CarbonInitConstant;
import org.wso2.carbon.core.services.authentication.AuthenticationAdmin;
import org.wso2.carbon.core.services.authentication.CarbonServerAuthenticator;
import org.wso2.carbon.registry.core.dataaccess.DataAccessManager;
import org.wso2.carbon.registry.core.jdbc.dataaccess.JDBCDataAccessManager;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.server.admin.auth.AuthenticatorServerRegistry;
import org.wso2.carbon.server.admin.common.IServerAdmin;
import org.wso2.carbon.server.admin.service.ServerAdmin;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.carbon.utils.MBeanRegistrar;
import org.wso2.carbon.base.ServerConfiguration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Hashtable;

/**
* @scr.component name="serveradmin.service.component"" immediate="true"
* @scr.reference name="registry.service"
* interface="org.wso2.carbon.registry.core.service.RegistryService"
* cardinality="1..1" policy="dynamic"  bind="setRegistryService" unbind="unsetRegistryService"
* @scr.reference name="config.context.service"
* interface="org.wso2.carbon.utils.ConfigurationContextService"
* cardinality="1..1" policy="dynamic"  bind="setConfigurationContextService" unbind="unsetConfigurationContextService"
* @scr.reference name="server.configuration"
* interface="org.wso2.carbon.base.ServerConfiguration"
* cardinality="1..1" policy="dynamic" bind="setServerConfiguration" unbind="unsetServerConfiguration"
* @scr.reference name="user.realmservice.default" interface="org.wso2.carbon.user.core.service.RealmService"
* cardinality="1..1" policy="dynamic" bind="setRealmService"
* unbind="unsetRealmService"
*/
public class ServerAdminServiceComponent {

    private static final Log log = LogFactory.getLog(ServerAdminServiceComponent.class);

    private boolean registeredMBeans;
    public static final String SERVER_ADMIN_MODULE_NAME = "ServerAdminModule";
    private ConfigurationContext configContext;
    private ServerAdminDataHolder dataHolder = ServerAdminDataHolder.getInstance();

    protected void activate(ComponentContext ctxt) {
        try {
            dataHolder.
                    setRestartThreadContextClassloader(Thread.currentThread().getContextClassLoader());
            //Register ServerAdmin MBean
            registerMBeans(dataHolder.getServerConfig());
            // Engaging ServerAdmin as an global module
            configContext.getAxisConfiguration().engageModule(SERVER_ADMIN_MODULE_NAME);

            //setUserManagerDriver(userRealmDefault);
            setRegistryDriver(dataHolder.getRegistryService());
           
            ctxt.getBundleContext().registerService(IServerAdmin.class.getName(),
                                                    new ServerAdmin(),
                                                    null);
            ctxt.getBundleContext().registerService(CommandProvider.class.getName(),
                                                    new ServerAdminCommandProvider(),
                                                    null);
            AuthenticationAdmin authenticator = new AuthenticationAdmin();
            Hashtable<String, String> props = new Hashtable<String, String>();
            props.put(AuthenticatorServerRegistry.AUTHENTICATOR_TYPE, authenticator.getAuthenticatorName());
            ctxt.getBundleContext().registerService(CarbonServerAuthenticator.class.getName(), authenticator, props);

            AuthenticatorServerRegistry.init(ctxt.getBundleContext());
            log.debug("ServerAdmin bundle is activated");
        } catch (Throwable e) {
            log.error("Failed to activate ServerAdmin bundle", e);
        }
    }

    protected void deactivate(ComponentContext ctxt) {
        log.debug("ServerAdmin bundle is deactivated");
    }

    private void registerMBeans(ServerConfiguration serverConfig) {       
            if (registeredMBeans) return;
            MBeanRegistrar.registerMBean(new ServerAdmin());
            registeredMBeans = true;       
    }

    private void setRegistryDriver(RegistryService registry) {
        try {
            if (registry.getConfigSystemRegistry().getRegistryContext() != null &&
                registry.getConfigSystemRegistry().getRegistryContext().getDataAccessManager()
                        != null) {
                DataAccessManager dataAccessManager =
                        registry.getConfigSystemRegistry().getRegistryContext()
                                .getDataAccessManager();
                if (!(dataAccessManager instanceof JDBCDataAccessManager)) {
                    String msg = "Failed to obtain DB connection. Invalid data access manager.";
                    log.error(msg);
                }
                Connection dbConnection = null;
                try {
                    DataSource dataSource = ((JDBCDataAccessManager)dataAccessManager).getDataSource();
                    dbConnection = dataSource.getConnection();
                    dataHolder.setRegistryDBDriver(dbConnection.getMetaData().getDriverName());
                } finally {
                    if (dbConnection != null) {
                        dbConnection.close();
                    }
                }
            }
        } catch (Exception e) {
            String msg = "Cannot get registry driver";
            log.error(msg, e);
        }
    }

    protected void setRegistryService(RegistryService registryService) {
        dataHolder.setRegistryService(registryService);
    }

    protected void unsetRegistryService(RegistryService registryService) {
        dataHolder.setRegistryService(null);
        dataHolder.setRegistryDBDriver(null);
    }

    protected void setRealmService(RealmService realmService) {
        dataHolder.setRealmService(realmService);
    }

    protected void unsetRealmService(RealmService realmService) {
        dataHolder.setRealmService(null);
        dataHolder.setUserManagerDBDriver(null);
    }

    protected void setConfigurationContextService(ConfigurationContextService contextService) {
        this.configContext = contextService.getServerConfigContext();
        dataHolder.setConfigContext(contextService.getServerConfigContext());
    }

    protected void unsetConfigurationContextService(ConfigurationContextService contextService) {
        AxisConfiguration axisConf = configContext.getAxisConfiguration();
        AxisModule statModule = axisConf.getModule(ServerAdminServiceComponent.SERVER_ADMIN_MODULE_NAME);
        if (statModule != null) {
            try {
                axisConf.disengageModule(statModule);
            } catch (AxisFault axisFault) {
                log.error("Failed disengage module: " + ServerAdminServiceComponent.SERVER_ADMIN_MODULE_NAME);
            }
        }
        this.configContext = null;
        dataHolder.setConfigContext(null);
    }

    protected void setServerConfiguration(ServerConfiguration serverConfiguration) {
        dataHolder.setServerConfig(serverConfiguration);
    }

    protected void unsetServerConfiguration(ServerConfiguration serverConfiguration) {
        dataHolder.setServerConfig(null);
    }
}
TOP

Related Classes of org.wso2.carbon.server.admin.internal.ServerAdminServiceComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.