Thank you Hans,

please see inline:

On Nov 19, 2012, at 3:22 PM, Hans Bakker wrote:

> Hi Jacopo,
> 
> when a request for a certain domain name comes in the tenant is selected and 
> the login screen without tenant input field is shown when login is required. 
> Otherwise the login screen is skipped.

Ok, I think it is inline with my understanding after my review of the code; so 
we have two independent mechanisms to select a tenant for the logged in user:

* by domain name: the tenant is selected automatically based on the URL of the 
server
* selection by the user at login: the user selects a tenant entering its name 
in the login screen

> 
> A problem here is that only a single domain name can be specified,

Do you mean a single domain per tenant, right? Otherwise I don't see how this 
feature could be of any use...

Thanks,

Jacopo

> Here locally we already extended that and we should commit that...but it is 
> an entity change which probably need a lot of discussion.....
> 
> Regards,
> Hans
> 
> On 11/19/2012 08:31 PM, Jacopo Cappellato wrote:
>> Hi Hans, Chattree,
>> 
>> I have a question about the changes to ContextFilter.java in this commit 
>> related to the selection of the Tenant record matching the domainname: does 
>> it mean that if a matching record is found then the tenant selected by the 
>> user at login is automatically overridden?
>> Is this the intended behavior?
>> 
>> Thanks,
>> 
>> Jacopo
>> 
>> On Nov 8, 2010, at 7:54 AM, hans...@apache.org wrote:
>> 
>>> Author: hansbak
>>> Date: Mon Nov  8 06:54:17 2010
>>> New Revision: 1032472
>>> 
>>> URL: http://svn.apache.org/viewvc?rev=1032472&view=rev
>>> Log:
>>> next to setting of the tenent id, now also the initial mountpoint if 
>>> entered also refactoring:
>>> - set delegator, dispatcher and security from session, servlet context or 
>>> request's attribute to app context of birt engine before render and send 
>>> email
>>> - add the initialPath field to Tenant entity
>>> - create the tenant context which as a default servlet using 
>>> org.ofbiz.webapp.control.TenantServlet in the catalina container and check 
>>> the multitenant property for initial the tenant context with root mount /
>>> - move the changing a multi tenant delegator statement from ControlServlet 
>>> to ContextFilter that could apply to every servlets
>>> (implementation by Chattree Richard)
>>> 
>>> Added:
>>>    
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/TenantServlet.java
>>> Modified:
>>>    ofbiz/trunk/framework/birt/src/org/ofbiz/birt/BirtWorker.java
>>>    
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/email/BirtEmailServices.java
>>>    
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtEngineServlet.java
>>>    
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtViewerServlet.java
>>>    
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/webapp/view/BirtViewHandler.java
>>>    
>>> ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
>>>    ofbiz/trunk/framework/common/webcommon/login.ftl
>>>    ofbiz/trunk/framework/entity/entitydef/entitymodel.xml
>>>    
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>>>    
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java
>>> 
>>> Modified: ofbiz/trunk/framework/birt/src/org/ofbiz/birt/BirtWorker.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/birt/src/org/ofbiz/birt/BirtWorker.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- ofbiz/trunk/framework/birt/src/org/ofbiz/birt/BirtWorker.java (original)
>>> +++ ofbiz/trunk/framework/birt/src/org/ofbiz/birt/BirtWorker.java Mon Nov  
>>> 8 06:54:17 2010
>>> @@ -23,6 +23,11 @@ import java.sql.SQLException;
>>> import java.util.Locale;
>>> import java.util.Map;
>>> 
>>> +import javax.servlet.ServletContext;
>>> +import javax.servlet.http.HttpServletRequest;
>>> +import javax.servlet.http.HttpServletResponse;
>>> +import javax.servlet.http.HttpSession;
>>> +
>>> import org.eclipse.birt.report.engine.api.EXCELRenderOption;
>>> import org.eclipse.birt.report.engine.api.EngineException;
>>> import org.eclipse.birt.report.engine.api.HTMLRenderOption;
>>> @@ -36,7 +41,11 @@ import org.eclipse.birt.report.engine.ap
>>> import org.ofbiz.base.util.Debug;
>>> import org.ofbiz.base.util.GeneralException;
>>> import org.ofbiz.base.util.UtilGenerics;
>>> +import org.ofbiz.base.util.UtilValidate;
>>> import org.ofbiz.birt.container.BirtContainer;
>>> +import org.ofbiz.entity.Delegator;
>>> +import org.ofbiz.security.Security;
>>> +import org.ofbiz.service.LocalDispatcher;
>>> 
>>> public class BirtWorker {
>>> 
>>> @@ -138,4 +147,45 @@ public class BirtWorker {
>>>         task.run();
>>>         task.close();
>>>     }
>>> +
>>> +    public static void setWebContextObjects(IReportEngine engine, 
>>> HttpServletRequest request, HttpServletResponse response) {
>>> +        HttpSession session = request.getSession();
>>> +        ServletContext servletContext = session.getServletContext();
>>> +
>>> +        // set delegator
>>> +        Delegator delegator = (Delegator) 
>>> session.getAttribute("delegator");
>>> +        if (UtilValidate.isEmpty(delegator)) {
>>> +            delegator = (Delegator) 
>>> servletContext.getAttribute("delegator");
>>> +        }
>>> +        if (UtilValidate.isEmpty(delegator)) {
>>> +            delegator = (Delegator) request.getAttribute("delegator");
>>> +        }
>>> +        if (UtilValidate.isNotEmpty(delegator)) {
>>> +            engine.getConfig().getAppContext().put("delegator", delegator);
>>> +        }
>>> +
>>> +        // set delegator
>>> +        LocalDispatcher dispatcher = (LocalDispatcher) 
>>> session.getAttribute("dispatcher");
>>> +        if (UtilValidate.isEmpty(dispatcher)) {
>>> +            dispatcher = (LocalDispatcher) 
>>> servletContext.getAttribute("dispatcher");
>>> +        }
>>> +        if (UtilValidate.isEmpty(dispatcher)) {
>>> +            dispatcher = (LocalDispatcher) 
>>> request.getAttribute("dispatcher");
>>> +        }
>>> +        if (UtilValidate.isNotEmpty(dispatcher)) {
>>> +            engine.getConfig().getAppContext().put("dispatcher", 
>>> dispatcher);
>>> +        }
>>> +
>>> +        // set security
>>> +        Security security = (Security) session.getAttribute("security");
>>> +        if (UtilValidate.isEmpty(security)) {
>>> +            security = (Security) servletContext.getAttribute("security");
>>> +        }
>>> +        if (UtilValidate.isEmpty(security)) {
>>> +            security = (Security) request.getAttribute("security");
>>> +        }
>>> +        if (UtilValidate.isNotEmpty(security)) {
>>> +            engine.getConfig().getAppContext().put("security", dispatcher);
>>> +        }
>>> +    }
>>> }
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/email/BirtEmailServices.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/birt/src/org/ofbiz/birt/email/BirtEmailServices.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/email/BirtEmailServices.java 
>>> (original)
>>> +++ 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/email/BirtEmailServices.java 
>>> Mon Nov  8 06:54:17 2010
>>> @@ -46,6 +46,8 @@ import org.ofbiz.base.util.string.Flexib
>>> import org.ofbiz.birt.BirtWorker;
>>> import org.ofbiz.birt.container.BirtContainer;
>>> import org.ofbiz.common.email.NotificationServices;
>>> +import org.ofbiz.entity.Delegator;
>>> +import org.ofbiz.security.Security;
>>> import org.ofbiz.service.DispatchContext;
>>> import org.ofbiz.service.LocalDispatcher;
>>> import org.ofbiz.service.ServiceUtil;
>>> @@ -69,7 +71,10 @@ public class BirtEmailServices {
>>>      */
>>>     public static Map<String, Object> sendBirtMail(DispatchContext ctx, 
>>> Map<String, ? extends Object> context) {
>>>         Map<String, Object> serviceContext = 
>>> UtilMisc.makeMapWritable(context);
>>> +        Delegator delegator = ctx.getDelegator();
>>>         LocalDispatcher dispatcher = ctx.getDispatcher();
>>> +        Security security = ctx.getSecurity();
>>> +
>>>         String webSiteId = (String) serviceContext.remove("webSiteId");
>>>         String bodyText = (String) serviceContext.remove("bodyText");
>>>         String bodyScreenUri = (String) 
>>> serviceContext.remove("bodyScreenUri");
>>> @@ -153,11 +158,14 @@ public class BirtEmailServices {
>>>                     birtContentType = "application/pdf";
>>>                 }
>>>                 IReportEngine engine = BirtContainer.getReportEngine();
>>> +                engine.getConfig().getAppContext().put("delegator", 
>>> delegator);
>>> +                engine.getConfig().getAppContext().put("dispatcher", 
>>> dispatcher);
>>> +                engine.getConfig().getAppContext().put("security", 
>>> security);
>>> +
>>>                 InputStream reportInputStream = 
>>> BirtFactory.getReportInputStreamFromLocation(birtReportLocation);
>>>                 IReportRunnable design = 
>>> engine.openReportDesign(reportInputStream);
>>> -                     Debug.logInfo("Export report as content type:" + 
>>> birtContentType, module);
>>> -                     BirtWorker.exportReport(design, context, 
>>> birtContentType, baos);
>>> -                // and generate the PDF
>>> +                Debug.logInfo("Export report as content type:" + 
>>> birtContentType, module);
>>> +                BirtWorker.exportReport(design, context, birtContentType, 
>>> baos);
>>>                 baos.flush();
>>>                 baos.close();
>>> 
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtEngineServlet.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtEngineServlet.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtEngineServlet.java
>>>  (original)
>>> +++ 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtEngineServlet.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -24,9 +24,12 @@ import javax.servlet.http.HttpServletRes
>>> 
>>> import org.eclipse.birt.core.exception.BirtException;
>>> import org.eclipse.birt.report.context.IContext;
>>> +import org.eclipse.birt.report.engine.api.IReportEngine;
>>> import 
>>> org.eclipse.birt.report.presentation.aggregation.layout.EngineFragment;
>>> import 
>>> org.eclipse.birt.report.presentation.aggregation.layout.RequesterFragment;
>>> import org.eclipse.birt.report.service.BirtReportServiceFactory;
>>> +import org.ofbiz.birt.BirtWorker;
>>> +import org.ofbiz.birt.container.BirtContainer;
>>> import org.ofbiz.birt.report.context.OFBizBirtContext;
>>> import org.ofbiz.birt.report.service.OFBizBirtViewerReportService;
>>> 
>>> @@ -52,6 +55,9 @@ public class BirtEngineServlet extends o
>>>     protected IContext __getContext( HttpServletRequest request,
>>>             HttpServletResponse response ) throws BirtException
>>>     {
>>> +        IReportEngine reportEngine = BirtContainer.getReportEngine();
>>> +        BirtWorker.setWebContextObjects(reportEngine, request, response);
>>> +
>>>         BirtReportServiceFactory.getReportService( ).setContext(
>>>                 getServletContext( ), null );
>>>         return new OFBizBirtContext( request, response );
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtViewerServlet.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtViewerServlet.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtViewerServlet.java
>>>  (original)
>>> +++ 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/report/servlet/BirtViewerServlet.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -24,10 +24,13 @@ import javax.servlet.http.HttpServletRes
>>> 
>>> import org.eclipse.birt.core.exception.BirtException;
>>> import org.eclipse.birt.report.context.IContext;
>>> +import org.eclipse.birt.report.engine.api.IReportEngine;
>>> import 
>>> org.eclipse.birt.report.presentation.aggregation.layout.FramesetFragment;
>>> import org.eclipse.birt.report.presentation.aggregation.layout.RunFragment;
>>> import org.eclipse.birt.report.service.BirtReportServiceFactory;
>>> import org.eclipse.birt.report.servlet.ViewerServlet;
>>> +import org.ofbiz.birt.BirtWorker;
>>> +import org.ofbiz.birt.container.BirtContainer;
>>> import org.ofbiz.birt.report.context.OFBizBirtContext;
>>> import org.ofbiz.birt.report.service.OFBizBirtViewerReportService;
>>> 
>>> @@ -56,6 +59,9 @@ public class BirtViewerServlet extends V
>>>     protected IContext __getContext( HttpServletRequest request,
>>>             HttpServletResponse response ) throws BirtException
>>>     {
>>> +        IReportEngine reportEngine = BirtContainer.getReportEngine();
>>> +        BirtWorker.setWebContextObjects(reportEngine, request, response);
>>> +
>>>         BirtReportServiceFactory.getReportService( ).setContext(
>>>                 getServletContext( ), null );
>>>         return new OFBizBirtContext( request, response );
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/webapp/view/BirtViewHandler.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/birt/src/org/ofbiz/birt/webapp/view/BirtViewHandler.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/webapp/view/BirtViewHandler.java
>>>  (original)
>>> +++ 
>>> ofbiz/trunk/framework/birt/src/org/ofbiz/birt/webapp/view/BirtViewHandler.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -70,6 +70,7 @@ public class BirtViewHandler implements
>>>     public void render(String name, String page, String info,
>>>             String contentType, String encoding, HttpServletRequest request,
>>>             HttpServletResponse response) throws ViewHandlerException {
>>> +
>>>         try {
>>>             IReportEngine engine = BirtContainer.getReportEngine();
>>>             // open report design
>>> @@ -80,6 +81,8 @@ public class BirtViewHandler implements
>>>             } else {
>>>                 design = 
>>> engine.openReportDesign(servletContext.getRealPath(page));
>>>             }
>>> +
>>> +            BirtWorker.setWebContextObjects(engine, request, response);
>>> 
>>>             Map<String, Object> context = FastMap.newInstance();
>>>             // set parameters from request
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
>>>  (original)
>>> +++ 
>>> ofbiz/trunk/framework/catalina/src/org/ofbiz/catalina/container/CatalinaContainer.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -67,6 +67,7 @@ import org.ofbiz.base.container.Containe
>>> import org.ofbiz.base.container.ContainerConfig.Container.Property;
>>> import org.ofbiz.base.util.Debug;
>>> import org.ofbiz.base.util.SSLUtil;
>>> +import org.ofbiz.base.util.UtilProperties;
>>> import org.ofbiz.base.util.UtilURL;
>>> import org.ofbiz.base.util.UtilValidate;
>>> import org.ofbiz.base.util.UtilXml;
>>> @@ -620,6 +621,43 @@ public class CatalinaContainer implement
>>>         return context;
>>>     }
>>> 
>>> +    protected Context createTenantContext() throws ContainerException {
>>> +        String server = "default-server";
>>> +        Engine engine = engines.get(server);
>>> +        if (engine == null) {
>>> +            Debug.logWarning("Server with name [" + server + "] not 
>>> found;", module);
>>> +            return null;
>>> +        }
>>> +
>>> +        // create the web application context
>>> +        StandardContext context = (StandardContext) 
>>> embedded.createContext("/", System.getProperty("ofbiz.home"));
>>> +        context.setJ2EEApplication(J2EE_APP);
>>> +        context.setJ2EEServer(J2EE_SERVER);
>>> +        
>>> context.setLoader(embedded.createLoader(ClassLoaderContainer.getClassLoader()));
>>> +        context.setReloadable(contextReloadable);
>>> +        context.setDistributable(distribute);
>>> +        context.setCrossContext(crossContext);
>>> +
>>> +
>>> +        // create the Default Servlet instance to mount
>>> +        StandardWrapper defaultServlet = new StandardWrapper();
>>> +        
>>> defaultServlet.setServletClass("org.ofbiz.webapp.control.TenantServlet");
>>> +        defaultServlet.setServletName("default");
>>> +        defaultServlet.setLoadOnStartup(1);
>>> +        defaultServlet.addInitParameter("debug", "0");
>>> +        defaultServlet.addInitParameter("listing", "true");
>>> +        defaultServlet.addMapping("/");
>>> +        context.addChild(defaultServlet);
>>> +        context.addServletMapping("/", "default");
>>> +
>>> +        Host host = hosts.get(engine.getName() + "._DEFAULT");
>>> +        context.setRealm(host.getRealm());
>>> +        host.addChild(context);
>>> +        context.getMapper().setDefaultHostName(host.getName());
>>> +
>>> +        return context;
>>> +    }
>>> +
>>>     protected void loadComponents() throws ContainerException {
>>>         if (embedded == null) {
>>>             throw new ContainerException("Cannot load web applications 
>>> without Embedded instance!");
>>> @@ -641,6 +679,12 @@ public class CatalinaContainer implement
>>>                 }
>>>             }
>>>         }
>>> +
>>> +        // if the multitenant is enabled then create the tenant context
>>> +        String useMultitenant = 
>>> UtilProperties.getPropertyValue("general.properties", "multitenant");
>>> +        if ("Y".equals(useMultitenant) && 
>>> UtilValidate.isEmpty(delegator.getDelegatorTenantId())) {
>>> +            createTenantContext();
>>> +        }
>>>     }
>>> 
>>>     public void stop() throws ContainerException {
>>> 
>>> Modified: ofbiz/trunk/framework/common/webcommon/login.ftl
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/common/webcommon/login.ftl?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- ofbiz/trunk/framework/common/webcommon/login.ftl (original)
>>> +++ ofbiz/trunk/framework/common/webcommon/login.ftl Mon Nov  8 06:54:17 
>>> 2010
>>> @@ -42,13 +42,15 @@ under the License.
>>>             <td class="label">${uiLabelMap.CommonPassword}</td>
>>>             <td><input type="password" name="PASSWORD" value="" 
>>> size="20"/></td>
>>>           </tr>
>>> -          <#if ("Y" == useMultitenant && 
>>> !sessionAttributes.tenantId?exists) >
>>> -            <tr>
>>> -              <td class="label">${uiLabelMap.CommonTenantId}</td>
>>> -              <td><input type="text" name="tenantId" 
>>> value="${parameters.tenantId?if_exists}" size="20"/></td>
>>> -            </tr>
>>> -          <#elseif ("Y" == useMultitenant && 
>>> sessionAttributes.tenantId?exists) >
>>> -              <input type="hidden" name="tenantId" 
>>> value="${sessionAttributes.tenantId?if_exists}"/>
>>> +          <#if ("Y" == useMultitenant) >
>>> +              <#if !requestAttributes.tenantId?exists>
>>> +                  <tr>
>>> +                      <td class="label">${uiLabelMap.CommonTenantId}</td>
>>> +                      <td><input type="text" name="tenantId" 
>>> value="${parameters.tenantId?if_exists}" size="20"/></td>
>>> +                  </tr>
>>> +              <#else>
>>> +                  <input type="hidden" name="tenantId" 
>>> value="${requestAttributes.tenantId?if_exists}"/>
>>> +              </#if>
>>>           </#if>
>>>           <tr>
>>>             <td colspan="2" align="center">
>>> 
>>> Modified: ofbiz/trunk/framework/entity/entitydef/entitymodel.xml
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/entitydef/entitymodel.xml?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- ofbiz/trunk/framework/entity/entitydef/entitymodel.xml (original)
>>> +++ ofbiz/trunk/framework/entity/entitydef/entitymodel.xml Mon Nov  8 
>>> 06:54:17 2010
>>> @@ -67,6 +67,7 @@ under the License.
>>>         <field name="tenantId" type="id-ne"/>
>>>         <field name="tenantName" type="name"/>
>>>         <field name="domainName" type="long-varchar"/>
>>> +        <field name="initialPath" type="value"/>
>>>         <field name="disabled" type="indicator"><description>Disabled if 
>>> 'Y', defaults to 'N' (not disabled).</description></field>
>>>         <prim-key field="tenantId"/>
>>>     </entity>
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>>>  (original)
>>> +++ 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ContextFilter.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -50,10 +50,16 @@ import org.ofbiz.base.util.Debug;
>>> import org.ofbiz.base.util.StringUtil;
>>> import org.ofbiz.base.util.UtilGenerics;
>>> import org.ofbiz.base.util.UtilHttp;
>>> +import org.ofbiz.base.util.UtilMisc;
>>> import org.ofbiz.base.util.UtilObject;
>>> +import org.ofbiz.base.util.UtilProperties;
>>> import org.ofbiz.base.util.UtilValidate;
>>> import org.ofbiz.entity.Delegator;
>>> import org.ofbiz.entity.DelegatorFactory;
>>> +import org.ofbiz.entity.GenericEntityException;
>>> +import org.ofbiz.entity.GenericValue;
>>> +import org.ofbiz.entity.condition.EntityCondition;
>>> +import org.ofbiz.entity.util.EntityUtil;
>>> import org.ofbiz.security.Security;
>>> import org.ofbiz.security.SecurityConfigurationException;
>>> import org.ofbiz.security.SecurityFactory;
>>> @@ -266,6 +272,52 @@ public class ContextFilter implements Fi
>>>                 return;
>>>             }
>>>         }
>>> +
>>> +        // check if multi tenant is enabled
>>> +        String useMultitenant = 
>>> UtilProperties.getPropertyValue("general.properties", "multitenant");
>>> +        if ("Y".equals(useMultitenant)) {
>>> +            // get tenant delegator by domain name
>>> +            String serverName = request.getServerName();
>>> +            try {
>>> +                // if tenant was specified, replace delegator with the new 
>>> per-tenant delegator and set tenantId to session attribute
>>> +                Delegator delegator = 
>>> getDelegator(config.getServletContext());
>>> +                List<EntityCondition> conds = FastList.newInstance();
>>> +                conds.add(EntityCondition.makeCondition("domainName", 
>>> serverName));
>>> +                List<GenericValue> tenants = delegator.findList("Tenant", 
>>> EntityCondition.makeCondition(conds), null, 
>>> UtilMisc.toList("-createdStamp"), null, false);
>>> +                if (UtilValidate.isNotEmpty(tenants)) {
>>> +                    GenericValue tenant = EntityUtil.getFirst(tenants);
>>> +                    String tenantId = tenant.getString("tenantId");
>>> +
>>> +                    // make that tenant active, setup a new delegator and 
>>> a new dispatcher
>>> +                    String tenantDelegatorName = 
>>> delegator.getDelegatorBaseName() + "#" + tenantId;
>>> +                    httpRequest.getSession().setAttribute("delegatorName", 
>>> tenantDelegatorName);
>>> +
>>> +                    // after this line the delegator is replaced with the 
>>> new per-tenant delegator
>>> +                    delegator = 
>>> DelegatorFactory.getDelegator(tenantDelegatorName);
>>> +                    config.getServletContext().setAttribute("delegator", 
>>> delegator);
>>> +
>>> +                    // clear web context objects
>>> +                    
>>> config.getServletContext().setAttribute("authorization", null);
>>> +                    config.getServletContext().setAttribute("security", 
>>> null);
>>> +                    config.getServletContext().setAttribute("dispatcher", 
>>> null);
>>> +
>>> +                    // initialize authorizer
>>> +                    getAuthz();
>>> +                    // initialize security
>>> +                    Security security = getSecurity();
>>> +                    // initialize the services dispatcher
>>> +                    LocalDispatcher dispatcher = 
>>> getDispatcher(config.getServletContext());
>>> +
>>> +                    // set web context objects
>>> +                    httpRequest.getSession().setAttribute("dispatcher", 
>>> dispatcher);
>>> +                    httpRequest.getSession().setAttribute("security", 
>>> security);
>>> +
>>> +                    request.setAttribute("tenantId", tenantId);
>>> +                }
>>> +            } catch (GenericEntityException e) {
>>> +                Debug.logWarning(e, "Unable to get Tenant", module);
>>> +            }
>>> +        }
>>> 
>>>         // we're done checking; continue on
>>>         chain.doFilter(request, response);
>>> 
>>> Modified: 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java?rev=1032472&r1=1032471&r2=1032472&view=diff
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java
>>>  (original)
>>> +++ 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/ControlServlet.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -226,39 +226,6 @@ public class ControlServlet extends Http
>>> 
>>>         String errorPage = null;
>>>         try {
>>> -            String useMultitenant = 
>>> UtilProperties.getPropertyValue("general.properties", "multitenant");
>>> -            if ("Y".equals(useMultitenant) && 
>>> UtilValidate.isEmpty(delegator.getDelegatorTenantId())) {
>>> -                // get tenant delegator by domain name
>>> -                try {
>>> -                    // if a domain name was specified for tenant, replace 
>>> delegator with the new per-tenant delegator and set tenantId to session 
>>> attribute
>>> -                    List<GenericValue> tenants = 
>>> delegator.findList("Tenant", EntityCondition.makeCondition("domainName", 
>>> request.getServerName()), null, UtilMisc.toList("-createdStamp"), null, 
>>> false);
>>> -                    if (UtilValidate.isNotEmpty(tenants)) {
>>> -                        GenericValue tenant = EntityUtil.getFirst(tenants);
>>> -                        String tenantId = tenant.getString("tenantId");
>>> -
>>> -                        // make that tenant active, setup a new delegator 
>>> and a new dispatcher
>>> -                        String tenantDelegatorName = 
>>> delegator.getDelegatorBaseName() + "#" + tenantId;
>>> -
>>> -                        // after this line the delegator is replaced with 
>>> the new per-tenant delegator
>>> -                        delegator = 
>>> DelegatorFactory.getDelegator(tenantDelegatorName);
>>> -                        session.setAttribute("tenantId", tenantId);
>>> -                        session.setAttribute("delegatorName", 
>>> tenantDelegatorName);
>>> -                    }
>>> -                } catch (GenericEntityException e) {
>>> -                    String errMsg = "Error getting tenant by domain name: 
>>> " + request.getServerName();
>>> -                    Debug.logError(e, errMsg, module);
>>> -                    throw new RequestHandlerException(errMsg, e);
>>> -                }
>>> -            }
>>> -            if ("Y".equals(useMultitenant) && 
>>> UtilValidate.isNotEmpty(delegator.getDelegatorTenantId())) {
>>> -                // re-make dispatcher from tenant delegator and change 
>>> delegator of security to use tanent delegator
>>> -                dispatcher = 
>>> ContextFilter.makeWebappDispatcher(session.getServletContext(), delegator);
>>> -                security.setDelegator(delegator);
>>> -
>>> -                request.setAttribute("delegator", delegator);
>>> -                request.setAttribute("dispatcher", dispatcher);
>>> -            }
>>> -
>>>             // the ServerHitBin call for the event is done inside the 
>>> doRequest method
>>>             requestHandler.doRequest(request, response, null, userLogin, 
>>> delegator);
>>>         } catch (RequestHandlerException e) {
>>> 
>>> Added: 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/TenantServlet.java
>>> URL: 
>>> http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/TenantServlet.java?rev=1032472&view=auto
>>> ==============================================================================
>>> --- 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/TenantServlet.java
>>>  (added)
>>> +++ 
>>> ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/control/TenantServlet.java
>>>  Mon Nov  8 06:54:17 2010
>>> @@ -0,0 +1,68 @@
>>> +/*******************************************************************************
>>> + * Licensed to the Apache Software Foundation (ASF) under one
>>> + * or more contributor license agreements.  See the NOTICE file
>>> + * distributed with this work for additional information
>>> + * regarding copyright ownership.  The ASF 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.ofbiz.webapp.control;
>>> +
>>> +import java.io.IOException;
>>> +import java.util.List;
>>> +
>>> +import javax.servlet.ServletException;
>>> +import javax.servlet.http.HttpServlet;
>>> +import javax.servlet.http.HttpServletRequest;
>>> +import javax.servlet.http.HttpServletResponse;
>>> +
>>> +import org.ofbiz.base.util.Debug;
>>> +import org.ofbiz.base.util.UtilMisc;
>>> +import org.ofbiz.base.util.UtilValidate;
>>> +import org.ofbiz.entity.Delegator;
>>> +import org.ofbiz.entity.DelegatorFactory;
>>> +import org.ofbiz.entity.GenericEntityException;
>>> +import org.ofbiz.entity.GenericValue;
>>> +import org.ofbiz.entity.condition.EntityCondition;
>>> +import org.ofbiz.entity.util.EntityUtil;
>>> +
>>> +
>>> +/**
>>> + * TenantServlet.java - Tenant servlet for the web application.
>>> + */
>>> +@SuppressWarnings("serial")
>>> +public class TenantServlet extends HttpServlet {
>>> +
>>> +    public static String module = TenantServlet.class.getName();
>>> +
>>> +    @Override
>>> +    public void doGet(HttpServletRequest request, HttpServletResponse 
>>> response)
>>> +            throws ServletException, IOException {
>>> +
>>> +        // get default delegator
>>> +        Delegator delegator = DelegatorFactory.getDelegator("default");
>>> +        try {
>>> +            // if a domain name was specified for tenant, redirect to 
>>> initial path
>>> +            List<GenericValue> tenants = delegator.findList("Tenant", 
>>> EntityCondition.makeCondition("domainName", request.getServerName()), null, 
>>> UtilMisc.toList("-createdStamp"), null, false);
>>> +            if (UtilValidate.isNotEmpty(tenants)) {
>>> +                GenericValue tenant = EntityUtil.getFirst(tenants);
>>> +                String initialPath = tenant.getString("initialPath");
>>> +                response.sendRedirect(initialPath);
>>> +            }
>>> +        } catch (GenericEntityException e) {
>>> +            String errMsg = "Error getting tenant by domain name: " + 
>>> request.getServerName();
>>> +            Debug.logError(e, errMsg, module);
>>> +            throw new ServletException(errMsg, e);
>>> +        }
>>> +    }
>>> +}
>>> 
>>> 
> 

Reply via email to