OpenSSO源码分析之配置篇(SystemProperties类分析)

这个类提供了允许访问单点登录系统有关的系统属性。

这个系统属性能够通过几个方式进行设置:

以编程方式调用 initializeProperties方法,或者通过static 方法从AMConfig.properties文件中加载。

该类中有以下几个属性用来存储属性信息:

  • Map attributeMap  //存储serverAttributeMap.properties文件中的属性,Property name to service attribute schema name mapping
  • Properties props  //存储AMConfig.properties文件中的属性
  • Map mapTagswap = new HashMap(); //KEY是某个System的属性的别名,value为某个System的属性名,这样可以避免在程序中直接引      用System的属性名
  • Map tagswapValues   //key保存着某个System的属性的别名,value保存着System的属性的值

主要对外暴露出以下方法:

  • public static String get(String key)

//查询系统属性(如果是server模式,SMS services中查找,然后在props中查找,否则只在props中查找)

//如果查找到的属性值中有System的属性的别名占位符的话,通过tagswapValues变量进行替换

  • public static String get(String key, String def)
  • public static Properties getProperties()
  • public static Properties getAll()
  • public static synchronized void initializeProperties(String file)
  • public static synchronized void initializeProperties(Properties properties)
  • public static synchronized void initializeProperties(Properties properties,boolean reset)  //将被OpenSSO调用
  • public static synchronized void initializeProperties(Properties properties,boolean reset,boolean withDefaults)
  • public static synchronized void initializeProperties(String propertyName,String propertyValue)
  • public static boolean isServerMode()
  • public static Map getAttributeMap()

在该类中有两个static代码块,用来加载属性信息

View Code
 1 private static void initAttributeMapping() {
 2  ResourceBundle rb = ResourceBundle.getBundle("serverAttributeMap");
 3      for (Enumeration e = rb.getKeys(); e.hasMoreElements(); ) {
 4             String propertyName = (String)e.nextElement();
 5             attributeMap.put(propertyName, new AttributeStruct(
 6                 rb.getString(propertyName)));
 7      }
 8 }
 9 
10 //加载serverAttributeMap.properties文件中的属性到attributeMap中
View Code
 1 static {
 2         mapTagswap.put("%SERVER_PORT%", Constants.AM_SERVER_PORT);
 3         mapTagswap.put("%SERVER_URI%", Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR);
 4         mapTagswap.put("%SERVER_HOST%", Constants.AM_SERVER_HOST);
 5         mapTagswap.put("%SERVER_PROTO%", Constants.AM_SERVER_PROTOCOL);
 6         mapTagswap.put("%BASE_DIR%", CONFIG_PATH);
 7         
 8         try {
 9             // Initialize properties
10             props = new Properties();
11 
12             // Load properties from file
13             String serverName = System.getProperty(SERVER_NAME_PROPERTY);
14             // 如果配置文件名的属性不存在,默认为AMConfig.properties
15             String configName = System.getProperty(CONFIG_NAME_PROPERTY,AMCONFIG_FILE_NAME);
16             String fname = null;
17             FileInputStream fis = null;
18             if (serverName != null) { //如果有多个服务实例,则每个实例会访问各自的配置文件
19                 serverName = serverName.replace('.', '_');
20                 fname = configName + "-" + serverName;
21             } else {
22                 fname = configName;
23             }
24              //把AMConfig.properties中的属性追加到props变量中,并修改最后编辑时间
25             initializeProperties(fname);
26 
27             // Get the location of the new configuration file in case
28             // of single war deployment
29              //以下提供了扩展配置的机制,如果需要,得先在AMConfig.properties属性中
30 //配置com.sun.identity.configFilePath属性
31             try {
32                 String newConfigFileLoc = props.getProperty(Constants.AM_NEW_CONFIGFILE_PATH);
33                 if ((newConfigFileLoc != null) && (newConfigFileLoc.length() > 0) && 
34                     !newConfigFileLoc.equals(NEWCONFDIR) ) {
35                     String hostName = InetAddress.getLocalHost().getHostName().toLowerCase();
36                     String serverURI = props.getProperty( Constants.AM_SERVICES_DEPLOYMENT_DESCRIPTOR);
37                     serverURI = serverURI.replace('/', '_').toLowerCase();
38                     StringBuffer fileName = new StringBuffer();
39                     fileName.append(newConfigFileLoc).append("/").append(
40                             AMCONFIG_FILE_NAME).append(serverURI).append(
41                             hostName).append(props.getProperty(Constants.AM_SERVER_PORT))
42                             .append(".").append(PROPERTIES);
43                     Properties modProp = new Properties();
44                     try {
45                         fis = new FileInputStream(fileName.toString());
46                         modProp.load(fis);
47                         props.putAll(modProp);
48                     } catch (IOException ioe) {
49                         StringBuffer fileNameOrig = new StringBuffer();
50                         fileNameOrig.append(newConfigFileLoc).append("/")
51                                 .append(AMCONFIG_FILE_NAME).append(".").append(
52                                         PROPERTIES);
53                         try {
54                             fis = new FileInputStream(fileNameOrig.toString());
55                             modProp.load(fis);
56                             props.putAll(modProp);
57                         } catch (IOException ioexp) {
58                             saveException(ioexp);
59                         }
60                     } finally {
61                         if (fis != null) {
62                             fis.close();
63                         }
64                     }
65                 }
66             } catch (Exception ex) {
67                 saveException(ex);
68             }
69         } catch (MissingResourceException e) {
70             // Can't print the message to debug due to dependency
71             // Save it as a String and provide when requested.
72             ByteArrayOutputStream baos = new ByteArrayOutputStream();
73             e.printStackTrace(new PrintStream(baos));
74             initError = baos.toString();
75             try {
76                 baos.close();
77             } catch (IOException ioe) {
78                 // Should not happend, ignore the exception
79             }
80         }
81     }

 

 

posted @ 2012-12-19 20:05  杭州胡欣  阅读(603)  评论(0编辑  收藏  举报