日期:2014-05-16  浏览次数:20460 次

openfire(2)数据库脚本执行

当XMPPServer启动的时候,会调用其start()方法,

??? public void start() {
??????? try {
??????????? initialize();

??????????? startDate = new Date();
??????????? // Store server info
??????????? xmppServerInfo = new XMPPServerInfoImpl(name, host, version, startDate, getConnectionManager());

??????????? // Create PluginManager now (but don't start it) so that modules may use it
??????????? File pluginDir = new File(openfireHome, "plugins");
??????????? pluginManager = new PluginManager(pluginDir);

??????????? // If the server has already been setup then we can start all the server's modules
??????????? if (!setupMode) {
??????????????? verifyDataSource();
??????????????? // First load all the modules so that modules may access other modules while
??????????????? // being initialized
??????????????? loadModules();
??????????????? // Initize all the modules
??????????????? initModules();
??????????????? // Start all the modules
??????????????? startModules();
??????????? }
??????????? // Initialize statistics
??????????? ServerTrafficCounter.initStatistics();

??????????? // Load plugins (when in setup mode only the admin console will be loaded)
??????????? pluginManager.start();

??????????? // Log that the server has been started
??????????? String startupBanner = LocaleUtils.getLocalizedString("short.title") + " " + version.getVersionString() +
??????????????????? " [" + JiveGlobals.formatDateTime(new Date()) + "]";
??????????? Log.info(startupBanner);
??????????? System.out.println(startupBanner);

??????????? started = true;
???????????
??????????? // Notify server listeners that the server has been started
??????????? for (XMPPServerListener listener : listeners) {
??????????????? listener.serverStarted();
??????????? }
??????? }
??????? catch (Exception e) {
??????????? e.printStackTrace();
??????????? Log.error(e.getMessage(), e);
??????????? System.out.println(LocaleUtils.getLocalizedString("startup.error"));
??????????? shutdownServer();
??????? }
??? }

?

start方法中干的事情太多了,这里主要介绍数据库方面,其他的暂时不做介绍。

initialize();会读取openfire.xml中的setup的值,如果为true,就不会做数据的验证操作。

openfire所有的自动化执行简表脚本都在此方法中verifyDataSource();

?

private void verifyDataSource() {
??????? Connection con = null;
??????? PreparedStatement pstmt = null;
??????? ResultSet rs = null;
??????? try {
??????????? con = DbConnectionManager.getConnection();在此做了很多的操作。
??????????? pstmt = con.prepareStatement("SELECT count(*) FROM ofID");
??????????? rs = pstmt.executeQuery();
??????????? rs.next();
??????? }
??????? catch (Exception e) {
??????????? System.err.println("Database setup or configuration error: " +
??????????????????? "Please verify your database settings and check the " +
??????????????????? "logs/error.log file for detailed error messages.");
??????????? Log.error("Database could not be accessed", e);
??????????? throw new IllegalArgumentException(e);
??????? }
??????? finally {
??????????? DbConnectionManager.closeConnection(rs, pstmt, con);
??????? }
??? }

?

DbConnectionManager.getConnection();:

在获取数据库链接的时候会做如下操作:

public static Connection getConnection() throws SQLException {
??????? if (connectionProvider == null) {
??????????? synchronized (providerLock) {
??????????????? if (connectionProvider == null) {
??????????????????? // Attempt to load the connection provider classname as
??????????????????? // a Jive property.
??????????????????? String className = JiveGlobals.getXMLProperty("connectionProvider.className");
??????????????????? if (clas