Java tutorial
/******************************************************************************* * * Copyright (C) 2008 JST-BIRD MassBank * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ******************************************************************************* * * o?[W??NX * * ver 1.0.5 2010.11.26 * ******************************************************************************/ package massbank.admin; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; import massbank.GetConfig; import massbank.MassBankEnv; import org.apache.commons.lang.math.NumberUtils; public class VersionManager { // R|?[lg(\?) public static final String[] COMPONENT_NAMES = { "Applet", "Common Lib", "JSP", "CGI", "Java Script", "CSS", "Admin Tool" }; // R|?[lgfBNg(\?) public static final String[] COMPONENT_DIR = { "applet", "WEB-INF/lib", "jsp", "cgi-bin", "script", "css", "mbadmin" }; // R|?[lgo?[Wi[? private static final int COMPONENT_APPLET = 0; private static final int COMPONENT_LIB = 1; private static final int COMPONENT_JSP = 2; private static final int COMPONENT_CGI = 3; private static final int COMPONENT_SCRIPT = 4; private static final int COMPONENT_CSS = 5; private static final int COMPONENT_ADMIN = 6; // JavaScript, CSSt@CfBNg private static final String OTHER_DIR[] = { "script", "css" }; // JavaScript, CSSt@Cgq private static final String OTHER_EXTENSION[] = { ".js", ".css" }; // JavaScript, CSSt@Ci[? private static final int OTHER_ARRAY_NUM[] = { COMPONENT_SCRIPT, COMPONENT_CSS }; // Admin ToolfBNg private static final String ADMIN_DIR[] = { "", "css/" }; // Admin Toolgq private static final String ADMIN_EXTENSION_REGEX[] = { ".*\\.(jsp|html)$", ".*\\.css$" }; // ?X?V?Ot@C private static final String EXCLUSION_LIST[][] = { { "JSP", "index.jsp" }, { "JSP", "BatchSearch.jsp" }, { "CGI", "BatchSender.cgi" } }; // Common Lib ?X?V?Ot@C private static final String EXCLUSION_COM_LIB[] = { "catalina-root.jar" }; // A?[JCu private static String ARCHIVE_NAME = "update"; // JSP private String jspName = ""; // ApacheMassBanpX private String massBankPath = ""; // TomcatROOTpX private String webRootPath = ""; // TomcatMassBankpX private String tomcatMbPath = ""; // CGIwb_?[ private String cgiHeader = ""; // vC}T?[oURL private String priServerUrl = ""; // T?[oURL private String myServerUrl = ""; // o?[W?i[z private List<VersionInfo>[] verInfoMyServer = null; private List<VersionInfo>[] verInfoPriServer = null; // ?X?V?t@CJEg private int oldCnt = 0; private int addCnt = 0; private int newCnt = 0; private int delCnt = 0; /** * RXgN^ */ public VersionManager(String reqUrl, String realPath) { // JSPt@CZbg this.jspName = reqUrl.substring(reqUrl.lastIndexOf("/") + 1); // x?[XURLZbg String baseUrl = MassBankEnv.get(MassBankEnv.KEY_BASE_URL); // ?t@C? GetConfig conf = new GetConfig(baseUrl); // URLXg String[] urlList = conf.getSiteUrl(); this.myServerUrl = urlList[GetConfig.MYSVR_INFO_NUM]; // vC}T?[oURL this.priServerUrl = MassBankEnv.get(MassBankEnv.KEY_PRIMARY_SERVER_URL); // ApacheMassBanpX this.massBankPath = MassBankEnv.get(MassBankEnv.KEY_APACHE_APPROOT_PATH); // TomcatROOTpXZbg this.webRootPath = MassBankEnv.get(MassBankEnv.KEY_TOMCAT_DOCROOT_PATH); // TomcatMassBankpXZbg this.tomcatMbPath = MassBankEnv.get(MassBankEnv.KEY_TOMCAT_APPROOT_PATH); // CGIwb_?[ AdminCommon admin = new AdminCommon(); this.cgiHeader = admin.getCgiHeader(); } /** * Abvf?[g???s * @return true:?? / false:? */ public boolean doUpdate(List copyFiles, List removeFiles) { // vC}T?[oA?[JCu??w if (!reqMakeArchive()) { return false; } boolean isOK = false; // vC}T?[oA?[JCu_E??[h String archiveName = ARCHIVE_NAME + ".tgz"; String srcUrl = priServerUrl + "temp/" + archiveName; String archivePath = tomcatMbPath + archiveName; FileUtil.downloadFile(srcUrl, archivePath); // A?[JCu boolean isPh1 = FileUtil.uncompress(archivePath, tomcatMbPath); // ?X?V?t@CRs?[ boolean isPh2 = true; for (int i = 0; i < copyFiles.size(); i++) { String relativePath = (String) copyFiles.get(i); String srcPath = tomcatMbPath + ARCHIVE_NAME + "/" + relativePath; String destPath = getAbsolutePath(relativePath) + relativePath; if (isOverwriteHeader(relativePath)) { overwriteHeader(srcPath); } if (!FileUtil.copyFile(srcPath, destPath)) { isPh2 = false; break; } } // ?X?V?t@C?? boolean isPh3 = true; if (isPh2) { for (int i = 0; i < removeFiles.size(); i++) { String relativePath = (String) removeFiles.get(i); String filePath = getAbsolutePath(relativePath) + relativePath; isOK = FileUtil.removeFile(filePath); if (!isOK) { isPh3 = false; break; } } } else { isPh3 = false; } // A?[JCu?? FileUtil.removeDir(tomcatMbPath + "update"); FileUtil.removeFile(archivePath); if (isPh3) { isOK = true; } return isOK; } /** * A?[JCu?????s * @return true:? / false:s */ public boolean doArchive() { String shellPath = "/MassBank/script/archiver.sh"; return FileUtil.executeShell(shellPath); } /** * o?[W`FbN???s * @param verInfoMyServer T?[oo?[W?Xg * @return true:?? / false:?(vC}T?[o???) */ public boolean doCheckVersion(List<VersionInfo>[] verInfoMyServer) { // T?[oo?[W?? this.verInfoMyServer = verInfoMyServer; // vC}T?[oo?[W? this.verInfoPriServer = getVerPriServer(); if (verInfoPriServer == null) { return false; } // o?[W?Xe?[^X? setVerStaus(); return true; } /** * vC}T?[oo?[W????s * @param verInfoServer T?[oo?[W?Xg * @return vC}T?[oo?[W??i?j */ public String doGetVerPServer(List<VersionInfo>[] verInfoServer) { StringBuffer res = new StringBuffer(); // o?[W?^u?` for (int i = 0; i < COMPONENT_NAMES.length; i++) { for (int j = 0; j < verInfoServer[i].size(); j++) { VersionInfo verInfo = (VersionInfo) verInfoServer[i].get(j); res.append(COMPONENT_NAMES[i] + "\t" + verInfo.getName() + "\t" + verInfo.getVersion() + "\t" + verInfo.getDate() + "\n"); } } return res.toString(); } /** * T?[oo?[W?Xg * @return T?[oo?[W?Xg */ public List<VersionInfo>[] getVerMyServer() { List<VersionInfo>[] verInfoMyServer = new ArrayList[COMPONENT_NAMES.length]; // Appleto?[W verInfoMyServer[COMPONENT_APPLET] = getVerApplet(); // Common Libo?[W verInfoMyServer[COMPONENT_LIB] = getVerComLib(); // JSPo?[W String jspPath = tomcatMbPath + COMPONENT_DIR[COMPONENT_JSP] + "/"; String extension = "jsp"; verInfoMyServer[COMPONENT_JSP] = getVerOther(jspPath, extension); // CGIo?[W verInfoMyServer[COMPONENT_CGI] = getVerCgi(); // CSS, Scripto?[W for (int i = 0; i < OTHER_DIR.length; i++) { String otherPath = massBankPath + OTHER_DIR[i] + "/"; verInfoMyServer[OTHER_ARRAY_NUM[i]] = getVerOther(otherPath, OTHER_EXTENSION[i]); } // Admin Tool o?[W verInfoMyServer[COMPONENT_ADMIN] = getVerAdminTool(); return verInfoMyServer; } /** * ?X?VL * @return true:?X?V / false:?X?V */ public boolean isUpdate() { if (oldCnt + addCnt + newCnt + delCnt == 0) { return false; } return true; } /** * Xe?[^X"OLD"JEg */ public int getOldCnt() { return oldCnt; } /** * Xe?[^X"ADD"JEg */ public int getAddCnt() { return addCnt; } /** * Xe?[^X"NEW"JEg */ public int getNewCnt() { return newCnt; } /** * Xe?[^X"DEL"JEg */ public int getDelCnt() { return delCnt; } /** * vC}T?[oA?[JCu??w * @return true:?? / false:? */ private boolean reqMakeArchive() { String res = ""; try { URL url = new URL(priServerUrl + "jsp/" + jspName + "?act=archive"); URLConnection con = url.openConnection(); if (con == null) { return false; } BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = ""; while ((line = in.readLine()) != null) { if (!line.trim().equals("")) { res += line.trim(); } } } catch (Exception e) { return false; } if (!res.equals("OK")) { return false; } return true; } /** * vC}T?[oo?[W? * @return vC}T?[oo?[W?Xg * (vC}T?[o???null) */ private List<VersionInfo>[] getVerPriServer() { List<VersionInfo>[] verInfoPriServer = new ArrayList[COMPONENT_NAMES.length]; for (int i = 0; i < COMPONENT_NAMES.length; i++) { verInfoPriServer[i] = new ArrayList(); } String strUrl = priServerUrl + "jsp/" + jspName + "?act=get"; try { URL url = new URL(strUrl); URLConnection con = url.openConnection(); if (con == null) { return null; } BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = ""; while ((line = in.readLine()) != null) { String data = line.trim(); if (data.equals("")) { continue; } String[] items = data.trim().split("\t"); String compoName = items[0]; String name = items[1]; String ver = items[2]; String date = items[3]; // ?Ot@CXg boolean isExclusion = false; for (int j = 0; j < EXCLUSION_LIST.length; j++) { if (compoName.equals(EXCLUSION_LIST[j][0]) && name.equals(EXCLUSION_LIST[j][1])) { isExclusion = true; break; } } if (isExclusion) { continue; } // o?[W?i[ for (int j = 0; j < COMPONENT_NAMES.length; j++) { VersionInfo verInfo = new VersionInfo(name, ver, date); if (compoName.equals(COMPONENT_NAMES[j])) { verInfoPriServer[j].add(verInfo); break; } } } in.close(); } catch (Exception e) { return null; } return verInfoPriServer; } /** * o?[W?Xe?[^X? */ private void setVerStaus() { int oldCnt = 0; int addCnt = 0; int newCnt = 0; int delCnt = 0; for (int i = 0; i < COMPONENT_NAMES.length; i++) { //------------------------------------------------------------- // T?[ot@CvC}T?[o?`FbN //------------------------------------------------------------- for (int l1 = 0; l1 < verInfoMyServer[i].size(); l1++) { boolean isFound = false; VersionInfo verInfo1 = (VersionInfo) verInfoMyServer[i].get(l1); VersionInfo verInfo2 = null; // T?[ot@C String name1 = verInfo1.getName(); // ?Ot@C`FbN?O boolean isExclusion = false; for (int j = 0; j < EXCLUSION_LIST.length; j++) { if (COMPONENT_NAMES[i].equals(EXCLUSION_LIST[j][0]) && name1.equals(EXCLUSION_LIST[j][1])) { isExclusion = true; break; } } if (isExclusion) { continue; } // ?`FbN for (int l2 = 0; l2 < verInfoPriServer[i].size(); l2++) { verInfo2 = (VersionInfo) verInfoPriServer[i].get(l2); String name2 = verInfo2.getName(); if (name2.equals(name1)) { isFound = true; break; } } //== t@C?? == if (isFound) { // o?[W?t String date1 = verInfo1.getDate(); String date2 = verInfo2.getDate(); // t0l if (date1.equals("-")) { date1 = "00.00.00"; } if (date2.equals("-")) { date2 = "00.00.00"; } // sIh?t String[] vals1 = date1.split("\\."); String[] vals2 = date2.split("\\."); // NsIhA String convDate1 = ""; String convDate2 = ""; for (int k = 0; k < vals1.length; k++) { // N2???2 if (k == 0) { vals1[k] = vals1[k].substring(vals1[k].length() - 2); vals2[k] = vals2[k].substring(vals2[k].length() - 2); } // N1???A0l2 convDate1 += "00".substring(vals1[k].length()) + vals1[k]; convDate2 += "00".substring(vals2[k].length()) + vals2[k]; } int iDate1 = Integer.parseInt(convDate1); int iDate2 = Integer.parseInt(convDate2); int status = 0; if (iDate1 < iDate2) { //** Xe?[^XZbg?u?v status = VersionInfo.STATUS_OLD; oldCnt++; } else if (iDate1 > iDate2) { //** Xe?[^XZbg?u?V?v status = VersionInfo.STATUS_NEW; newCnt++; } else { status = VersionInfo.STATUS_NON; } verInfo1.setStatus(status); } //== t@C?? == T?[o? else { //** Xe?[^XZbg?u???v verInfo1.setStatus(VersionInfo.STATUS_DEL); delCnt++; } } //------------------------------------------------------------- // vC}T?[o?t@C`FbN //------------------------------------------------------------- for (int l1 = 0; l1 < verInfoPriServer[i].size(); l1++) { VersionInfo verInfo1 = (VersionInfo) verInfoPriServer[i].get(l1); VersionInfo verInfo2 = null; // vC}T?[ot@C String name1 = verInfo1.getName(); boolean isFound = false; // ?`FbN for (int l2 = 0; l2 < verInfoMyServer[i].size(); l2++) { verInfo2 = (VersionInfo) verInfoMyServer[i].get(l2); String name2 = verInfo2.getName(); if (name2.equals(name1)) { isFound = true; break; } } if (!isFound) { VersionInfo verInfo3 = new VersionInfo(name1, "-", "-"); verInfoMyServer[i].add(verInfo3); //** Xe?[^XZbg?u?v verInfo3.setStatus(VersionInfo.STATUS_ADD); addCnt++; } } } this.oldCnt = oldCnt; this.addCnt = addCnt; this.newCnt = newCnt; this.delCnt = delCnt; } /** * Appleto?[W? * @return o?[W? */ private List<VersionInfo> getVerApplet() { List<VersionInfo> verInfoList = new ArrayList<VersionInfo>(); // appletfBNgt@CXg String appletPath = massBankPath + "applet/"; File file = new File(appletPath); String allList[] = file.list(); ArrayList<String> targetList = new ArrayList<String>(); for (int i = 0; i < allList.length; i++) { if (allList[i].indexOf(".jar") >= 0) { targetList.add(allList[i]); } } // t@C\?[g Collections.sort(targetList); for (int i = 0; i < targetList.size(); i++) { String fileName = targetList.get(i); VersionInfo verInfo = getVerJarFile(appletPath + fileName); // o?[W?i[ verInfo.setName(fileName); verInfoList.add(verInfo); } return verInfoList; } /** * Common Libo?[W? * @return o?[W? */ private List<VersionInfo> getVerComLib() { List<VersionInfo> verInfoList = new ArrayList<VersionInfo>(); // WEB-INF/libfBNgt@CXg String libPath = webRootPath + "WEB-INF/lib/"; File file = new File(libPath); String allList[] = file.list(); ArrayList<String> targetList = new ArrayList<String>(); for (int i = 0; i < allList.length; i++) { boolean isExclusion = false; for (int j = 0; j < EXCLUSION_COM_LIB.length; j++) { if (allList[i].equals(EXCLUSION_COM_LIB[j])) { isExclusion = true; break; } } if (!isExclusion && allList[i].indexOf(".jar") >= 0) { targetList.add(allList[i]); } } // t@C\?[g Collections.sort(targetList); for (int i = 0; i < targetList.size(); i++) { String fileName = targetList.get(i); VersionInfo verInfo = getVerJarFile(libPath + fileName); // o?[W?i[ verInfo.setName(fileName); verInfoList.add(verInfo); } return verInfoList; } /** * CGIo?[W? * @return o?[W? */ private List<VersionInfo> getVerCgi() { List<VersionInfo> verInfoList = new ArrayList<VersionInfo>(); // o?[W?CGI?s String strUrl = myServerUrl + "/cgi-bin/GetVersion.cgi"; try { URL url = new URL(strUrl); URLConnection con = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = ""; while ((line = in.readLine()) != null) { String data = line.trim(); if (data.equals("")) { continue; } String[] info = data.split("\t"); // Xy?[X?o?[Wt?o String name = info[0]; String[] item = info[1].trim().split(" "); String ver = item[0]; String date = "-"; for (int j = 1; j < item.length; j++) { if (!item[j].equals("")) { date = item[j]; break; } } // o?[W?i[ verInfoList.add(new VersionInfo(name, ver, date)); } in.close(); } catch (Exception e) { e.printStackTrace(); } return verInfoList; } /** * Admin Toolo?[W? * @return o?[W? */ private List<VersionInfo> getVerAdminTool() { List<VersionInfo> verInfoList = new ArrayList<VersionInfo>(); // t@CXg for (int i = 0; i < ADMIN_DIR.length; i++) { String path = tomcatMbPath + "mbadmin/" + ADMIN_DIR[i]; File file = new File(path); String allList[] = file.list(); List<String> targetList = new ArrayList<String>(); for (int j = 0; j < allList.length; j++) { if (allList[j].matches(ADMIN_EXTENSION_REGEX[i])) { targetList.add(allList[j]); } } // t@C\?[g Collections.sort(targetList); for (int j = 0; j < targetList.size(); j++) { String filName = targetList.get(j); File file2 = new File(path + filName); // fBNg if (file2.isDirectory()) { continue; } // eLXgt@Co?[W VersionInfo verInfo = getVerTextFile(path + filName); // o?[W?i[ verInfo.setName(ADMIN_DIR[i] + filName); verInfoList.add(verInfo); } } return verInfoList; } /** * (JSP,CSS, Script)o?[W? * @return o?[W? */ private List<VersionInfo> getVerOther(String path, String extension) { List<VersionInfo> verInfoList = new ArrayList<VersionInfo>(); // t@CXg File file1 = new File(path); String allList[] = file1.list(); List<String> targetList = new ArrayList<String>(); for (int i = 0; i < allList.length; i++) { if (allList[i].indexOf(extension) >= 0) { targetList.add(allList[i]); } } // t@C\?[g Collections.sort(targetList); for (int i = 0; i < targetList.size(); i++) { String fileName = targetList.get(i); File file2 = new File(path + fileName); // fBNg if (file2.isDirectory()) { continue; } // eLXgt@Co?[W VersionInfo verInfo = getVerTextFile(path + fileName); // o?[W?i[ verInfo.setName(fileName); verInfoList.add(verInfo); } return verInfoList; } /** * jart@Co?[W? * @param path t@CpX * @return o?[W? */ private VersionInfo getVerJarFile(String path) { VersionInfo verInfo = null; try { // }jtFXgo?[W? JarFile jar = new JarFile(new File(path)); Manifest manifest = jar.getManifest(); Attributes attributes = manifest.getMainAttributes(); String item1 = attributes.getValue("Implementation-Version"); if (item1 == null) { item1 = "-"; } // Xy?[X?o?[Wt?o String[] item2 = item1.trim().split(" "); String ver = item2[0]; String date = "-"; if (item2.length > 1) { date = item2[1]; } verInfo = new VersionInfo(null, ver, date); } catch (Exception e) { e.printStackTrace(); } return verInfo; } /* * eLXgt@Co?[W? * @param path t@CpX * @return o?[W? */ private VersionInfo getVerTextFile(String path) { VersionInfo verInfo = null; try { String info = "-"; final String FIND_STR[] = { "//**", "**/" }; BufferedReader in = new BufferedReader(new FileReader(path)); boolean isFound = false; boolean isEnd = false; String line = ""; while ((line = in.readLine()) != null) { if (isFound) { // wb_?[R?gJn?s? for (int i = 0; i < FIND_STR.length; i++) { int pos1 = line.indexOf(FIND_STR[i]); if (pos1 >= 0) { isEnd = true; break; } } if (isEnd) { break; } } // o?[W?L?q? String find2 = "ver"; int pos2 = line.indexOf(find2); if (pos2 >= 0) { info = line.substring(pos2 + find2.length() + 1).trim(); isFound = true; } } in.close(); // Xy?[X?o?[Wt?o String[] item = info.split(" "); // o?[WZbg String ver = "-"; String[] vals = null; for (int j = 0; j < item.length; j++) { if (!item[j].equals("")) { vals = item[j].split("\\."); if (NumberUtils.isNumber(vals[0]) && vals[0].length() < 4) { ver = item[0]; break; } } } // tZbg String date = "-"; for (int j = 1; j < item.length; j++) { // Xy?[X if (!item[j].equals("")) { vals = item[j].split("\\."); if (NumberUtils.isNumber(vals[0]) && Integer.parseInt(vals[0]) > 2000) { date = item[j]; break; } } } verInfo = new VersionInfo(null, ver, date); } catch (Exception e) { e.printStackTrace(); } return verInfo; } /** * t@C?pX * @param relativePath t@CpX * @return t@C?pX */ private String getAbsolutePath(String relativePath) { String absPath = ""; int pos = relativePath.lastIndexOf("/"); String dir = relativePath.substring(0, pos); if (dir.equals("jsp") || dir.equals("mbadmin") || dir.equals("mbadmin/css")) { // TomcatMassBankpXZbg absPath = tomcatMbPath; } else if (dir.equals("WEB-INF/lib")) { // TomcatAvP?[V?[gpXZbg absPath = webRootPath; } else { // ApacheMassBankpXZbg absPath = massBankPath; } return absPath; } /** * CGIwb_?[?v * @param relativePath t@CpX * @return true:v / false: */ private boolean isOverwriteHeader(String relativePath) { if (cgiHeader.equals("")) { return false; } int pos = relativePath.lastIndexOf("/"); String dir = relativePath.substring(0, pos); if (dir.equals("cgi-bin")) { return true; } return false; } /** * CGIwb_?[? * @param absPath t@CpX */ private void overwriteHeader(String absPath) { try { // t@C? InputStreamReader reader = new InputStreamReader(new FileInputStream(absPath), "UTF-8"); BufferedReader br = new BufferedReader(reader); StringBuffer text = new StringBuffer(""); String line = ""; while ((line = br.readLine()) != null) { int pos = line.indexOf("#!"); if (pos >= 0) { // wb_?[? text.append(cgiHeader + "\n"); } else { text.append(line + "\n"); } } br.close(); // t@C?? OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(absPath), "UTF-8"); BufferedWriter bw = new BufferedWriter(writer); bw.write(text.toString()); bw.flush(); bw.close(); } catch (Exception e) { e.printStackTrace(); } } }