}
}
}
if (pluginWithDescriptor != null && currentFile != null && currentFile.exists()) {
ServerPlugin dbPlugin = new ServerPlugin(name, path);
dbPlugin.setType(pluginType.stringify());
dbPlugin.setMd5(md5);
dbPlugin.setVersion(version);
dbPlugin.setMtime(mtime);
ServerPlugin obsoletePlugin = ServerPluginDescriptorUtil.determineObsoletePlugin(dbPlugin,
pluginWithDescriptor.plugin);
if (obsoletePlugin == pluginWithDescriptor.plugin) { // yes use == for reference equality!
StringBuilder logMsg = new StringBuilder();
logMsg.append("Found server plugin [").append(name);
logMsg.append("] in the DB that is newer than the one on the filesystem: ");
logMsg.append("DB path=[").append(path);
logMsg.append("]; file path=[").append(currentFile.getName());
logMsg.append("]; DB MD5=[").append(md5);
logMsg.append("]; file MD5=[").append(pluginWithDescriptor.plugin.getMd5());
logMsg.append("]; DB version=[").append(version);
logMsg.append("]; file version=[").append(pluginWithDescriptor.plugin.getVersion());
logMsg.append("]; DB timestamp=[").append(new Date(mtime));
logMsg.append("]; file timestamp=[").append(new Date(pluginWithDescriptor.plugin.getMtime()));
logMsg.append("]");
log.info(logMsg.toString());
updatedPlugins.add(dbPlugin);
if (currentFile.delete()) {
log.info("Deleted the obsolete server plugin file to be updated: " + currentFile);
this.serverPluginsOnFilesystem.remove(currentFile);
} else {
log.warn("Failed to delete the obsolete (to-be-updated) server plugin: " + currentFile);
}
} else if (obsoletePlugin == null) {
// the db is up-to-date, but update the cache so we don't check MD5 or parse the descriptor again
boolean succeeded = currentFile.setLastModified(mtime);
if (!succeeded && log.isDebugEnabled()) {
log.debug("Failed to set mtime to [" + new Date(mtime) + "] on file [" + currentFile + "].");
}
pluginWithDescriptor.plugin.setMtime(mtime);
pluginWithDescriptor.plugin.setVersion(version);
pluginWithDescriptor.plugin.setMd5(md5);
} else {
log.info("It appears that the server plugin [" + dbPlugin
+ "] in the database may be obsolete. If so, it will be updated later.");
}
} else {
log.info("Found server plugin in the DB that we do not yet have: " + name);
ServerPlugin plugin = new ServerPlugin(name, path, md5);
plugin.setType(pluginType.stringify());
plugin.setMtime(mtime);
plugin.setVersion(version);
updatedPlugins.add(plugin);
this.serverPluginsOnFilesystem.remove(expectedFile); // paranoia, make sure the cache doesn't have this
}
}
// write all our updated plugins to the file system
if (!updatedPlugins.isEmpty()) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
DataSource ds = LookupUtil.getDataSource();
conn = ds.getConnection();
ps = conn.prepareStatement("SELECT CONTENT FROM " + ServerPlugin.TABLE_NAME
+ " WHERE DEPLOYMENT = 'SERVER' AND STATUS = 'INSTALLED' AND NAME = ? AND PTYPE = ?");
for (ServerPlugin plugin : updatedPlugins) {
File file = new File(this.getServerPluginDir(), plugin.getPath());
ps.setString(1, plugin.getName());
ps.setString(2, plugin.getType());
rs = ps.executeQuery();
rs.next();
InputStream content = rs.getBinaryStream(1);
StreamUtil.copy(content, new FileOutputStream(file));
rs.close();
boolean succeeded = file.setLastModified(plugin.getMtime());// so our file matches the database mtime
if (!succeeded && log.isDebugEnabled()) {
log.debug("Failed to set mtime to [" + plugin.getMtime() + "] on file [" + file + "].");
}
updatedFiles.add(file);
// we are writing a new file to the filesystem, cache it since we know about it now
cacheFilesystemServerPluginJar(file, null);