001: /*
002: * Created on 20 juin 2005
003: * SalomeTMF is a Test Managment Framework
004: * Copyright (C) 2005 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Contact: mikael.marche@rd.francetelecom.com
021: */
022: package plugins;
023:
024: import java.io.BufferedOutputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.util.Enumeration;
032: import java.util.Properties;
033: import java.util.StringTokenizer;
034: import java.util.zip.ZipEntry;
035: import java.util.zip.ZipFile;
036:
037: /**
038: * @author marchemi
039: *
040: */
041:
042: public class PluginInstaller {
043: static String plgName;
044: static String plgFile;
045: static String propertiesFile = "CfgPlugins.properties";
046: static String avaiblePugin = "";
047: static Properties pluginProperties = new Properties();
048:
049: static private void printHelp() {
050: System.out.println("PluginInstaller arguments are :");
051: System.out
052: .println("\tpluginarchive.zip (compressed archive of the plugin)");
053: System.out
054: .println("\tSalomé-TMF plugin properties file (default CfgPlugins.properties)");
055:
056: }
057:
058: public static final void copyInputStream(InputStream in,
059: OutputStream out) throws IOException {
060: byte[] buffer = new byte[1024];
061: int len;
062: while ((len = in.read(buffer)) >= 0)
063: out.write(buffer, 0, len);
064:
065: in.close();
066: out.close();
067: }
068:
069: public static void main(String[] args) {
070: if (args.length < 1 || args.length > 2) {
071: printHelp();
072: System.exit(1);
073: }
074: plgFile = args[0];
075: if (args.length > 1) {
076: propertiesFile = args[1];
077: }
078:
079: try {
080: loadPluginPropertie();
081: uncompressPlugin();
082: savePluginPropertie();
083: } catch (Exception e) {
084: e.printStackTrace();
085: System.out.println("Plugin " + plgName
086: + " installation fail, try manualy");
087: System.exit(1);
088: }
089: System.out.println("Plugin " + plgName
090: + " successfully installed");
091: System.exit(0);
092: }
093:
094: static private void loadPluginPropertie() throws Exception {
095: FileInputStream ins = new FileInputStream(propertiesFile);
096: pluginProperties.load(ins);
097: ins.close();
098: avaiblePugin = pluginProperties.getProperty("pluginsList",
099: "core");
100: }
101:
102: static private void savePluginPropertie() throws Exception {
103: FileOutputStream out = new FileOutputStream(propertiesFile);
104: pluginProperties.store(out, "");
105: out.close();
106: }
107:
108: static private void uncompressPlugin() throws Exception {
109: boolean firstDir = true;
110: boolean toWrite = true;
111: ZipFile pZipFile = new ZipFile(plgFile);
112: Enumeration pZipEntries = pZipFile.entries();
113: while (pZipEntries.hasMoreElements()) {
114: ZipEntry pEntry = (ZipEntry) pZipEntries.nextElement();
115: //System.out.println("Found entry : " + pEntry.getName());
116: if (pEntry.isDirectory()) {
117: if (firstDir) {
118: firstDir = false;
119: plgName = pEntry.getName();
120: if (plgName.endsWith("/") || plgName.endsWith("\\")) {
121: plgName = plgName.substring(0,
122: plgName.length() - 1);
123: }
124: StringTokenizer st = new StringTokenizer(
125: avaiblePugin, ",");
126: while (st.hasMoreTokens() && toWrite) {
127: String token = st.nextToken().trim();
128: if (token.equals(plgName)) {
129: toWrite = false;
130: }
131: }
132: if (toWrite) {
133: avaiblePugin += ", " + plgName;
134: pluginProperties.setProperty("pluginsList",
135: avaiblePugin);
136: }
137: }
138: System.out.println("Extracting directory: "
139: + pEntry.getName());
140: (new File(pEntry.getName())).mkdir();
141: } else {
142: System.out.println("Extracting file: "
143: + pEntry.getName());
144: copyInputStream(pZipFile.getInputStream(pEntry),
145: new BufferedOutputStream(new FileOutputStream(
146: pEntry.getName())));
147:
148: }
149: }
150: pZipFile.close();
151: }
152: }
|