shiro.ini獲取

  1. shiro 默認獲取shiro.ini的方式:

    1. org.apache.shiro.web.env.EnvironmentLoaderListener中的

       

    2.  protected WebEnvironment createEnvironment(ServletContext sc)
          {
              Class clazz = determineWebEnvironmentClass(sc);
              if(!org/apache/shiro/web/env/MutableWebEnvironment.isAssignableFrom(clazz))
                  throw new ConfigurationException((new StringBuilder()).append("Custom WebEnvironment class [").append(clazz.getName()).append("] is not of required type [").append(org/apache/shiro/web/env/WebEnvironment.getName()).append("]").toString());
              String configLocations = sc.getInitParameter("shiroConfigLocations");
              boolean configSpecified = StringUtils.hasText(configLocations);
              if(configSpecified && !org/apache/shiro/config/ResourceConfigurable.isAssignableFrom(clazz))
              {
                  String msg = (new StringBuilder()).append("WebEnvironment class [").append(clazz.getName()).append("] does not implement the ").append(org/apache/shiro/config/ResourceConfigurable.getName()).append("interface.  This is required to accept any ").append("configured ").append("shiroConfigLocations").append("value(s).").toString();
                  throw new ConfigurationException(msg);
              }
              MutableWebEnvironment environment = (MutableWebEnvironment)ClassUtils.newInstance(clazz);
              environment.setServletContext(sc);
              if(configSpecified && (environment instanceof ResourceConfigurable))
                  ((ResourceConfigurable)environment).setConfigLocations(configLocations);
              customizeEnvironment(environment);
              LifecycleUtils.init(environment);
              return environment;
          }

 通過調用LifecycleUtils.init(environment)


最終通過調用org.apache.shiro.web.env.IniWebEnvironment

  public void init()
    {
        Ini ini = getIni();
        String configLocations[] = getConfigLocations();
        if(log.isWarnEnabled() && !CollectionUtils.isEmpty(ini) && configLocations != null && configLocations.length > 0)
            log.warn("Explicit INI instance has been provided, but configuration locations have also been specified.  The {} implementation does not currently support multiple Ini config, but this may be supported in the future. Only the INI instance will be used for configuration.", org/apache/shiro/web/env/IniWebEnvironment.getName());
        if(CollectionUtils.isEmpty(ini))
        {
            log.debug("Checking any specified config locations.");
            ini = getSpecifiedIni(configLocations);
        }
        if(CollectionUtils.isEmpty(ini))
        {
            log.debug("No INI instance or config locations specified.  Trying default config locations.");
            ini = getDefaultIni();
        }
        if(CollectionUtils.isEmpty(ini))
        {
            String msg = "Shiro INI configuration was either not found or discovered to be empty/unconfigured.";
            throw new ConfigurationException(msg);
        } else
        {
            setIni(ini);
            configure();
            return;
        }
    }

   


1.顯示配置路徑:

 protected Ini getSpecifiedIni(String configLocations[])
        throws ConfigurationException
    {
        Ini ini = null;
        if(configLocations != null && configLocations.length > 0)
        {
            if(configLocations.length > 1)
                log.warn("More than one Shiro .ini config location has been specified.  Only the first will be used for configuration as the {} implementation does not currently support multiple files.  This may be supported in the future however.", org/apache/shiro/web/env/IniWebEnvironment.getName());
            ini = createIni(configLocations[0], true);
        }
        return ini;
    }

2.默認配置的路徑:

 protected Ini getDefaultIni()
    {
        Ini ini = null;
        String configLocations[] = getDefaultConfigLocations();
        if(configLocations != null)
        {
            String arr$[] = configLocations;
            int len$ = arr$.length;
            int i$ = 0;
            do
            {
                if(i$ >= len$)
                    break;
                String location = arr$[i$];
                ini = createIni(location, false);
                if(!CollectionUtils.isEmpty(ini))
                {
                    log.debug("Discovered non-empty INI configuration at location '{}'.  Using for configuration.", location);
                    break;
                }
                i$++;
            } while(true);
        }
        return ini;
    }

    
    
    protected String[] getDefaultConfigLocations()
    {
        return (new String[] {
            "/WEB-INF/shiro.ini", "classpath:shiro.ini"
        });
    }
    
    
      protected Ini createIni(String configLocation, boolean required)
        throws ConfigurationException
    {
        Ini ini = null;
        if(configLocation != null)
            ini = convertPathToIni(configLocation, required);
        if(required && CollectionUtils.isEmpty(ini))
        {
            String msg = (new StringBuilder()).append("Required configuration location '").append(configLocation).append("' does not exist or did not ").append("contain any INI configuration.").toString();
            throw new ConfigurationException(msg);
        } else
        {
            return ini;
        }
    }
    
    
    
    private Ini convertPathToIni(String path, boolean required)
    {
        Ini ini = null;
        if(StringUtils.hasText(path))
        {
            InputStream is = null;
            if(!ResourceUtils.hasResourcePrefix(path))
                is = getServletContextResourceStream(path);
            else
                try
                {
                    is = ResourceUtils.getInputStreamForPath(path);
                }
                catch(IOException e)
                {
                    if(required)
                        throw new ConfigurationException(e);
                    if(log.isDebugEnabled())
                        log.debug((new StringBuilder()).append("Unable to load optional path '").append(path).append("'.").toString(), e);
                }
            if(is != null)
            {
                ini = new Ini();
                ini.load(is);
            } else
            if(required)
                throw new ConfigurationException((new StringBuilder()).append("Unable to load resource path '").append(path).append("'").toString());
        }
        return ini;
    }

    private InputStream getServletContextResourceStream(String path)
    {
        InputStream is = null;
        path = WebUtils.normalize(path);
        ServletContext sc = getServletContext();
        if(sc != null)
            is = sc.getResourceAsStream(path);
        return is;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章