2021SC@SDUSC
上篇文章对main下的类进行了分析,对ws下的IClusterWsMessage接口,main下的CalendarHelper类、CalendarPatterns类、ImportHelper类进行了分析。本篇将继续分析main下的类。
目录
ConnectionProperties类
public class ConnectionProperties implements Serializable {
private static final long serialVersionUID = 1L;
public enum DbType {
db2
, derby
, mssql
, mysql
, oracle
, postgresql
}
private String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private String url = "jdbc:derby:openmeetings";
private String login = "user";
private String password = "secret";
private DbType dbType = DbType.derby;
public String getDriver() {
return driver;
}
public void setDriver(String driverName) {
this.driver = driverName;
}
public String getURL() {
return url;
}
public void setURL(String connectionURL) {
this.url = connectionURL;
}
public String getLogin() {
return login;
}
public void setLogin(String connectionLogin) {
this.login = connectionLogin;
}
public String getPassword() {
return password;
}
public void setPassword(String connectionPass) {
this.password = connectionPass;
}
public DbType getDbType() {
return dbType;
}
public void setDbType(DbType dbType) {
this.dbType = dbType;
}
@Override
public String toString() {
return "ConnectionProperties [type=" + dbType + ", driver=" + driver + ", url=" + url + ", login=" + login + "]";
}
}
ConnectionProperties类主要是用来配置数据库文件,实现了Serializable接口用来实现序列化,序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。首先定义了一个枚举变量DbType用来表示所连接的数据库类型,入db2、mysql、oracle等,定义了连接数据库的属性driver、url、login、password以及DbType。然后通过get和set方法来获取和设置属性值。以及重写了toString方法返回字符串"ConnectionProperties [type=" + dbType + ", driver=" + driver + ", url=" + url + ", login=" + login + "]"以便调用。
NullStringer类
public class NullStringer extends JSONStringer {
public NullStringer() {
super();
}
public NullStringer(int indent) {
super(indent);
}
@Override
public JSONStringer entry(Entry<String, Object> entry) {
return this.key(entry.getKey()).value(entry.getValue());
}
}
该NullStringer类继承了JSONStringer类,JSONStringer是工具包org.json.jar下的类,该工具包是一个轻量级的java下的json构造和解析工具包,JSONStringer是JSONWriter的子类,用法跟JSONWtriter几乎一样,可以用来构建一个JSON格式的文本,便于传输和存储,可以直接new JSONStringer()创建对象,不用传入构造参数。
其中NullStringer()构造方法继承父类构造方法。
public JSONStringer entry(Entry<String, Object> entry) {
return this.key(entry.getKey()).value(entry.getValue());
}
entry方法将输入的键值对作为JSONStringer对象返回。
OMContextListener类
public class OMContextListener extends ContextLoggingListener {
@Override
public void contextInitialized(ServletContextEvent event) {
setWebAppRootKey(pathToName(event));
System.setProperty("current_openmeetings_context_name", getWebAppRootKey());
System.setProperty("webapp.contextPath", String.format("/%s", getWebAppRootKey()));
System.setProperty("logback.configurationFile", "logback-config.xml");
super.contextInitialized(event);
}
private static String pathToName(ServletContextEvent event) {
String contextName = event.getServletContext().getContextPath().replaceAll("/", "");
if ("".equals(contextName)) {
contextName = "root";
}
return contextName;
}
}
OMContextListener类继承了ContextLoggingListener类,即日志内容监听。
其中pathToName(ServletContextEvent event)来获取该web项目的绝对路径。
web.xml文件中webAppRootKey属性是web项目的绝对路径,默认值是webApp.root,可以通过System.getProperty(“webApp.root”)来获取属性值或者在配置文件中通过${webApp.root}获得。
contextInitialized(ServletContextEvent event)方法中第一行是设置web项目的绝对路径,然后利用System.setProperty()方法当该openmeetings项目初始化时设置属性。设置current_openmeetings_context_name为当前绝对路径,设置webapp.contextPath同样为当前绝对路径的字符串格式,设置logback的配置文件为logback-config.xml。
OmException类
public class OmException extends Exception {
private static final long serialVersionUID = 1L;
public static final OmException UNKNOWN = new OmException("error.unknown");
public static final OmException BAD_CREDENTIALS = new OmException("error.bad.credentials");
private final String key;
public OmException(String key) {
super();
this.key = key;
}
public OmException(Throwable cause) {
super(cause);
this.key = null;
}
public String getKey() {
return key;
}
}
Version类
public class Version {
private static final Logger log = Red5LoggerFactory.getLogger(Version.class, OpenmeetingsVariables.getWebAppRootKey());
private static final int TOTAL_LENGTH = 78;
private static String version = null;
private static String revision = null;
private static String buildDate = null;
static {
try {
Properties props = new Properties();
try (InputStream is = Version.class.getResourceAsStream("version.properties")) {
props.load(is);
}
version = props.getProperty("version");
revision = props.getProperty("revision");
buildDate = props.getProperty("date");
} catch (Exception e) {
log.error("Error", e);
}
}
private Version() {}
public static String getVersion() {
return version;
}
public static String getRevision() {
return revision;
}
public static String getBuildDate() {
return buildDate;
}
private static void getLine(StringBuilder sb, String text, char fill) {
sb.append("\t#");
int l = text.length();
int headLength = (TOTAL_LENGTH - l) / 2;
for (int i = 0; i < headLength; ++i) {
sb.append(fill);
}
sb.append(text);
for (int i = 0; i < (TOTAL_LENGTH - l - headLength); ++i) {
sb.append(fill);
}
sb.append("#\n");
}
public static void logOMStarted() {
StringBuilder sb = new StringBuilder("\n");
getLine(sb, "", '#');
getLine(sb, "Openmeetings is up", ' ');
getLine(sb, getVersion() + " " + getRevision() + " " + getBuildDate(), ' ');
getLine(sb, "and ready to use", ' ');
getLine(sb, "", '#');
log.debug(sb.toString());
}
}
Version类定义了Logger日志对象log,定义了一个常量TOTAL_LENGTH为78,version、revision、buildDate分别表示版本、修订版本和日期。其中三个get方法来获取这三个属性。然后getLine(StringBuilder sb,String text,char fill):获取传入的参数text的长度,然后添加一定长度的fill字符(一般是空格 符合)至缓冲区,然后在缓冲区加入text内容,再添加一定长度的fill字符加入至缓冲区。
logOMStarted()方法,即是当openmeetings启动时,将会打印如下内容:# Openmeetings is up 版本号+修改版本+日期 and ready to use #。
总结
本篇文章分析了main下的:ConnectionProperties类、NullStringer类、OMContextListener类、OmException类、Version类。后面将继续分析main下的类,还剩下OmFileHelper类、OpenmeetingsVariables类、StoredFile类、XmlExport类,则openmeetings-util包分析完毕。