`
smartvessel
  • 浏览: 80685 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

tomcat 7 源码分析-1 关于读取properties及注册系统properties

阅读更多

Tomact的启动开始于Bootstrap.java,在其init()中,首先要做的就是

        setCatalinaHome();
        setCatalinaBase();
initClassLoaders();

 目的就是将tomcat启动的环境设置好,在进行classloader。

  private void setCatalinaHome() {

        if (System.getProperty("catalina.home") != null)
            return;
        File bootstrapJar = 
            new File(System.getProperty("user.dir"), "bootstrap.jar");
        if (bootstrapJar.exists()) {
            try {
                System.setProperty
                    ("catalina.home", 
                     (new File(System.getProperty("user.dir"), ".."))
                     .getCanonicalPath());
                
            } catch (Exception e) {
                // Ignore
                System.setProperty("catalina.home",
                                   System.getProperty("user.dir"));
            }
        } else {
            System.setProperty("catalina.home",
                               System.getProperty("user.dir"));
        }
    }

 判断user.dir\bootstrap.jar存在否来设置catalina.home。

 

在initClassLoaders();中调用createClassLoader。loader class之前又要读取properties,于是看String value = CatalinaProperties.getProperty(name + ".loader");

 

public class CatalinaProperties {


    // ------------------------------------------------------- Static Variables

    private static final org.apache.juli.logging.Log log=
        org.apache.juli.logging.LogFactory.getLog( CatalinaProperties.class );

    private static Properties properties = null;


    static {

        loadProperties();

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Return specified property value.
     */
    public static String getProperty(String name) {
	
        return properties.getProperty(name);

    }


    /**
     * Return specified property value.
     */
    public static String getProperty(String name, String defaultValue) {

        return properties.getProperty(name, defaultValue);

    }


    // --------------------------------------------------------- Public Methods


    /**
     * Load properties.
     */
    private static void loadProperties() {

        InputStream is = null;
        Throwable error = null;

        try {
            String configUrl = getConfigUrl();
            if (configUrl != null) {
                is = (new URL(configUrl)).openStream();
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
        }

        if (is == null) {
            try {
                File home = new File(getCatalinaBase());
                File conf = new File(home, "conf");
                File properties = new File(conf, "catalina.properties");
                is = new FileInputStream(properties);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
        }

        if (is == null) {
            try {
                is = CatalinaProperties.class.getResourceAsStream
                    ("/org/apache/catalina/startup/catalina.properties");
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
        }

        if (is != null) {
            try {
                properties = new Properties();
                properties.load(is);
                is.close();
            } catch (Throwable t) {
                error = t;
            }
        }

        if ((is == null) || (error != null)) {
            // Do something
            log.warn("Failed to load catalina.properties", error);
            // That's fine - we have reasonable defaults.
            properties=new Properties();
        }

        // Register the properties as system properties
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            String value = properties.getProperty(name);
            if (value != null) {
                System.setProperty(name, value);
            }
        }

    }


    /**
     * Get the value of the catalina.home environment variable.
     */
    private static String getCatalinaHome() {
        return System.getProperty("catalina.home",
                                  System.getProperty("user.dir"));
    }
    
    
    /**
     * Get the value of the catalina.base environment variable.
     */
    private static String getCatalinaBase() {
        return System.getProperty("catalina.base", getCatalinaHome());
    }


    /**
     * Get the value of the configuration URL.
     */
    private static String getConfigUrl() {
        return System.getProperty("catalina.config");
    }


}
 CatalinaProperties 的核心是静态函数loadProperties(),在这里读取真正意义上的properties并注册
分享到:
评论

相关推荐

    Linux简明教程.rar

    1.列出系统上的用户 2.另一种方法 3.推荐使用的 八、用户间通信-------------------------------------------------------------------------------------- 1.发送消息 2.接受消息和拒绝消息 九、发一封邮件--...

    Tomcat6.x+IIS6+jk2最新整合

    软件环境:Window server 2003+ IIS6.0 + Tomcat6.0 + JK2 整了大半天,不过直得整合的好处就不说了 1.导入注册表 iis+tomcat.reg 文件说明 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\...

    Tomcat6.x+IIS6+jk2 整合

    软件环境:Window server 2003+ IIS6.0 + Tomcat6.0 + JK2 整了大半天,不过直得整合的好处就不说了 1.导入注册表 iis+tomcat.reg 文件说明 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\...

    java注解源码-Spring-Framework-1:JavaSpring使用注释和Java源代码配置

    Properties-&gt; Java Build Path -&gt; Libraries -&gt; Add Jar- Apply 类及其功能 使用注释配置 AnnotationDemoApp是主要类,它将读取sport.properties文件并显示信息。 AnnotationBeanScopeDemoApp显示Bean的范围...

    带注释的Bootstrap.java

    //并将catalina.properties内的属性存为系统属性 //catalina.properties内common.loader="${catalina.base}/lib", //"${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar" //...

    SpringMVC-Mybatis-Shiro-redis-master 权限集成缓存中实例

    &lt;property name="maxAge" value="-1"/&gt; &lt;!-- 配置存储Session Cookie的domain为 一级域名 --&gt; 上面配置是去掉了 Session 的存储Key 的作用域,之前设置的.itboy.net ,是写到当前域名的 一级域名 下,...

    Spring MVC 入门实例

    读取 /WEB-INF/jdbc.properties 文件. 你可以在 list 标签中配置多个 value 标签. database.xml: 1 &lt;?xml version="1.0" encoding="UTF-8"?&gt; 2 &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ...

    SSH的jar包.rar

    1、客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2、这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫做ActionContextCleanUp的可选过滤器,这个过滤器对于Struts2和其他框架的集成很有帮助,...

    跨项目调用webservice接口

    *这里是我的参数放在了properties文件中,我在读取里面的参数,这里我们也可以通过方法传参数 *如 : testWebService(String url,String xMlStr)() 那么在调用的时候就可以直接传进来了 *url 是你访问的webservice 的...

    blade-fm:blade框架开发的音乐电台系统

    该项目是基于blade框架搭建的音乐电台系统,使用七牛云作为存储CDN。 项目含括技术点: blade框架使用 模板引擎使用 验证码技术 第三方云存储使用 web音乐播放技术 分片上传 配置文件读取 使用 建立blade_fm数据库,...

    elasticsearch整合分词、创建索引、搜索例子

    1 在dababase目录中导致相关的数据库文件,修改DBCOperation java文件数据库连接地址,修改elasticsearch.properties文件中相关链接 2 运行CreatIndexMysql文件,里面有main方法查询数据库生成相关的索引文件 3 ...

    封装给java使用的 web报表 rm webrm 报表1.0.0.1版正式版

    //1:设置报表路径及文件名称 public void AddClientDataSet(String dataSourceName,IRMDataSet rmd);//2:设置数据源名称以ocx使用,并把IRMDataSet实现的结果加入 public String CreateViewer();//3:生成在html...

    springboot常用框架

    ,包括springboot的注解分析、路径分析、取得内置对象、项目打包、配置环境属性、读取资源文件、Bean配置、模板渲染、 基于yml的多profile配置、基于properties的多profile、添加数据验证、配置错误页、全局异常处理...

    JAVA 范例大全 光盘 资源

    常见问题 读取Properties文件出现中文乱码 182 第9章 Java异常处理与反射机制 183 实例73 运用throws、throw、try与catch 183 实例74 throws声明异常的实例 185 实例75 自定义异常类 187 实例76 使用finally...

    jxl操作excel 修改,删除,生成,线程搬迁 手动打jar包

    jxl excel 修改,删除,生成 线程搬迁 手动打jar包 包含jxl操作excel 配置线程执行搬迁或者其他操作,需要手动添加一个文件getSZ.properties配置数据库 手动打包及一些常用的方法

    MicroService-converter

    微服务转换器 使用 Spring Boot 创建的 REST 接口。 ##Libs 在这个项目中使用:## Apache POI(用于读取 xslx ... Tomcat 配置为在端口 7000 上运行,您可以在 resources/application.properties 文件中更改此配置。

    messageCapsule:类似于消息工具的whatsapp,但具有地理位置功能

    messageCapsule 类似于...Leafletjs如何使用需要从Google注册GCM服务并更新相关代码使用pgAdmin读取src / skymsg01282013db中SQL转储文件需要在服务器和客户端更改硬代码值点(使用Properties和Preferences类和xml)

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

    Activiti6.0教程例子下载

    1. 初识Activiti 1.1. 工作流与工作流引擎 工作流(workflow)就是工作流程的计算模型,即将工作流程中的工作如何前后组织在一起的逻辑和规则在计算机中以恰当的模型进行表示并对其实施计算。它主要解决的是“使在...

Global site tag (gtag.js) - Google Analytics