Hi,
$Subject can be found in https://wso2.org/crucible/cru/COMPONENT-51.
Moreover, given below is the steps to set the tenantId for your threads,
such that the logging component will get the tenant id for the respective
component/thread.

If you are starting a new thread, you have to add this below code segment,
for the specific thread, to start the tenant flow, and finally end it.

Forwarded message from Amani attached below.


Basically what we need to do is to set the tenant id in that component
specific thread...

So you need to create a class extending
AbstractAxis2ConfigurationContextObserver (check admin console o event
component) and get the tenant id
  int
tenantId=SuperTenantCarbonContext.getCurrentContext(configurationContext).getTenantId();

and set the tenant id

try {
            SuperTenantCarbonContext.startTenantFlow()

SuperTenantCarbonContext.getCurrentContext().setTenantId(tenantID);

SuperTenantCarbonContext.getCurrentContext().getTenantDomain(true);

            if (!loadedTenants.contains(tenantDomain.trim())) {
                eventBroker.initializeTenant();
                loadedTenants.add(tenantDomain.trim());
            }

        } catch (Exception e) {
            log.error("Error in setting tenant information", e);
        } finally {
            SuperTenantCarbonContext.endTenantFlow();
        }

Please go through the attached files to get a clear idea on how exactly u
should do it.



Thank you.
Regards,
Pradeeban.


-- 
Kathiravelu Pradeeban.
Software Engineer.
WSO2 Inc.

Blog: [Llovizna] http://kkpradeeban.blogspot.com/
/*
 *  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.dataservices.core.internal;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;
import org.wso2.carbon.utils.AbstractAxis2ConfigurationContextObserver;

public class DSAxis2ConfigurationContextObserver extends AbstractAxis2ConfigurationContextObserver {

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

    public void createdConfigurationContext(ConfigurationContext configurationContext) {
        int tenantId = SuperTenantCarbonContext.getCurrentContext(
                configurationContext).getTenantId();
        try {
            SuperTenantCarbonContext.startTenantFlow();
            SuperTenantCarbonContext.getCurrentContext().setTenantId(tenantId);

        } catch (Exception e) {
            log.error("Error in setting tenant details ", e);
        } finally {
            SuperTenantCarbonContext.endTenantFlow();
        }
    }
    
}
/*
 *  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.dataservices.core.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.core.multitenancy.SuperTenantCarbonContext;
import org.wso2.carbon.dataservices.core.listeners.CarbonDataSourceServiceListener;
import org.wso2.carbon.dataservices.core.listeners.EventBrokerServiceListener;
import org.wso2.carbon.dataservices.core.listeners.RegistryServiceListener;
import org.wso2.carbon.datasource.DataSourceInformationRepositoryService;
import org.wso2.carbon.event.core.EventBroker;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;

import java.util.ArrayList;
import java.util.List;

/**
 * @scr.component name="dataservices.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="user.realmservice.default" interface="org.wso2.carbon.user.core.service.RealmService"
 * cardinality="1..1" policy="dynamic" bind="setRealmService" unbind="unsetRealmService"
 * @scr.reference name="org.wso2.carbon.datasource.DataSourceInformationRepositoryService" interface="org.wso2.carbon.datasource.DataSourceInformationRepositoryService"
 * cardinality="0..1" policy="dynamic" bind="setCarbonDataSourceService" unbind="unsetCarbonDataSourceService"
 * @scr.reference name="eventbrokerbuilder.component" interface="org.wso2.carbon.event.core.EventBroker"
 * cardinality="1..1" policy="dynamic" bind="setEventBroker" unbind="unsetEventBroker"
 */
public class DataServicesDSComponent {

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

    private static RegistryService registryService = null;

    private static RealmService realmService = null;

    private static DataSourceInformationRepositoryService dataSourceService;

    private static EventBroker eventBroker;

    private static List<CarbonDataSourceServiceListener> carbonDataSourceServiceListeners = new ArrayList<CarbonDataSourceServiceListener>();

    private static List<RegistryServiceListener> registryServiceListeners = new ArrayList<RegistryServiceListener>();

    private static List<EventBrokerServiceListener> eventBrokerServiceListeners = new ArrayList<EventBrokerServiceListener>();

    private static Object dsComponentLock = new Object(); /* class level lock for controlling synchronized access to static variables */

    public DataServicesDSComponent() {
    }

    protected void activate(ComponentContext ctxt) {
        log.debug("Data Services bundle is activated ");
        try {
            Activator activator = new Activator();
            activator.start(ctxt.getBundleContext());

            BundleContext bundleContext = ctxt.getBundleContext();
            bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
                    new DSAxis2ConfigurationContextObserver(), null);
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
            /* don't throw exception */
        }
    }

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

    protected void setRegistryService(RegistryService registryService) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Setting the Registry Service");
            }
            DataServicesDSComponent.registryService = registryService;
            for (RegistryServiceListener listener : registryServiceListeners) {
            	SuperTenantCarbonContext.getCurrentContext().setTenantId(listener.getTenantId());
                listener.setRegistryService(registryService);
            }
            registryServiceListeners.clear();
        }
    }

    protected void unsetRegistryService(RegistryService registryService) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Unsetting the Registry Service");
            }
            DataServicesDSComponent.registryService = null;
        }
    }

    protected void setRealmService(RealmService realmService) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Setting the Realm Service");
            }
            DataServicesDSComponent.realmService = realmService;
            if (DataServicesDSComponent.eventBroker != null) {
            	this.doEventRealmInitiliased();
            }
        }
    }

    protected void unsetRealmService(RealmService realmService) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Unsetting the Realm Service");
            }
            DataServicesDSComponent.realmService = null;
        }
    }

    public static RegistryService getRegistryService() {
        return registryService;
    }

    public static RealmService getRealmService() {
        return realmService;
    }

    protected void setCarbonDataSourceService(
            DataSourceInformationRepositoryService dataSourceService) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Setting the Carbon Data Sources Service");
            }
            DataServicesDSComponent.dataSourceService = dataSourceService;
            for (CarbonDataSourceServiceListener listener : carbonDataSourceServiceListeners) {
            	SuperTenantCarbonContext.getCurrentContext().setTenantId(listener.getTenantId());
                listener.setCarbonDataSourceService(dataSourceService);
            }
            carbonDataSourceServiceListeners.clear();
        }
    }

    protected void unsetCarbonDataSourceService(
            DataSourceInformationRepositoryService dataSourceService) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Unsetting the Carbon Data Sources Service");
            }
            DataServicesDSComponent.dataSourceService = null;
        }
    }

    public static DataSourceInformationRepositoryService getCarbonDataSourceService() {
        return dataSourceService;
    }

    protected void setEventBroker(EventBroker eventBroker) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Setting the Event Broker Service");
            }
            DataServicesDSComponent.eventBroker = eventBroker;
            /* event functionality depends on realm service */
            if (DataServicesDSComponent.realmService != null) {
            	this.doEventRealmInitiliased();
            }
        }
    }
    
    private void doEventRealmInitiliased() {
    	RealmService realmService = DataServicesDSComponent.getRealmService();
    	try {
            SuperTenantCarbonContext.getCurrentContext().setUserRealm(realmService.getBootstrapRealm());
            SuperTenantCarbonContext.getCurrentContext().setUsername(CarbonConstants.REGISTRY_SYSTEM_USERNAME);
    	} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
    	this.notifyEventServiceListeners();
    }
    
    private void notifyEventServiceListeners() {
    	for (EventBrokerServiceListener listener : eventBrokerServiceListeners) {
    		SuperTenantCarbonContext.getCurrentContext().setTenantId(listener.getTenantId());
            listener.setEventBroker(eventBroker);
        }
        eventBrokerServiceListeners.clear();
    }

    protected void unsetEventBroker(EventBroker eventBroker) {
        synchronized (dsComponentLock) {
            if (log.isDebugEnabled()) {
                log.debug("Unsetting the Event Broker Service");
            }
            DataServicesDSComponent.eventBroker = null;
        }
    }

    public static EventBroker getEventBroker() {
        return eventBroker;
    }

    public static void registerEventBrokerServiceListener(
            EventBrokerServiceListener listener) {
        synchronized (dsComponentLock) {
            EventBroker eventBroker = getEventBroker();
            if (eventBroker == null) {
                eventBrokerServiceListeners.add(listener);
            } else {
                listener.setEventBroker(eventBroker);
            }
        }
    }

    public static void registerCarbonDataSourceServiceListener(
            CarbonDataSourceServiceListener listener) {
        synchronized (dsComponentLock) {
            DataSourceInformationRepositoryService dataSourceService = getCarbonDataSourceService();
            if (dataSourceService == null) {
                carbonDataSourceServiceListeners.add(listener);
            } else {
                listener.setCarbonDataSourceService(dataSourceService);
            }
        }
    }

    public static void registerRegistryServiceListener(RegistryServiceListener listener) {
        synchronized (dsComponentLock) {
            RegistryService registryService = getRegistryService();
            if (registryService == null) {
                registryServiceListeners.add(listener);
            } else {
                listener.setRegistryService(registryService);
            }
        }
    }

}
_______________________________________________
Carbon-dev mailing list
[email protected]
http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev

Reply via email to