Ejemplo n.º 1
0
  public static void setRequestAttributes(
      ServletRequest request, Delegator delegator, ServletContext servletContext) {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    // check if multi tenant is enabled
    boolean useMultitenant = EntityUtil.isMultiTenantEnabled();
    if (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 = getDelegator(servletContext);

        // Use base delegator for fetching data from entity of entityGroup org.ofbiz.tenant
        Delegator baseDelegator = DelegatorFactory.getDelegator(delegator.getDelegatorBaseName());
        GenericValue tenantDomainName =
            EntityQuery.use(baseDelegator)
                .from("TenantDomainName")
                .where("domainName", serverName)
                .queryOne();

        if (UtilValidate.isNotEmpty(tenantDomainName)) {
          String tenantId = tenantDomainName.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);
          servletContext.setAttribute("delegator", delegator);
        }

      } catch (GenericEntityException e) {
        Debug.logWarning(e, "Unable to get Tenant", module);
      }
    }

    // set the web context in the request for future use
    request.setAttribute("servletContext", httpRequest.getSession().getServletContext());
    request.setAttribute("delegator", delegator);

    // set the webSiteId in the session
    if (UtilValidate.isEmpty(httpRequest.getSession().getAttribute("webSiteId"))) {
      httpRequest
          .getSession()
          .setAttribute(
              "webSiteId", httpRequest.getSession().getServletContext().getAttribute("webSiteId"));
    }
  }
Ejemplo n.º 2
0
 public LabelReferences(Delegator delegator, LabelManagerFactory factory) {
   this.delegator = delegator;
   this.labels = factory.getLabels();
   DelegatorElement delegatorInfo = null;
   try {
     delegatorInfo = EntityConfigUtil.getDelegator(delegator.getDelegatorBaseName());
   } catch (GenericEntityConfException e) {
     Debug.logWarning(e, "Exception thrown while getting delegator config: ", module);
   }
   String modelName;
   if (delegatorInfo != null) {
     modelName = delegatorInfo.getEntityModelReader();
   } else {
     modelName = "main";
   }
   // since we do not associate a dispatcher to this DispatchContext, it is important to set a name
   // of an existing entity model reader:
   // in this way it will be possible to retrieve the service models from the cache
   this.dispatchContext = new DispatchContext(modelName, this.getClass().getClassLoader(), null);
   Collection<LabelInfo> infoList = this.labels.values();
   for (LabelInfo labelInfo : infoList) {
     this.labelSet.add(labelInfo.getLabelKey());
   }
   Collection<ComponentConfig> componentConfigs = ComponentConfig.getAllComponents();
   for (ComponentConfig config : componentConfigs) {
     String rootFolder = config.getRootLocation();
     rootFolder = rootFolder.replace('\\', '/');
     if (!rootFolder.endsWith("/")) {
       rootFolder = rootFolder + "/";
     }
     this.rootFolders.add(rootFolder);
   }
 }