001: package tide.export;
002:
003: import snow.utils.storage.FileUtils;
004: import java.io.*;
005: import java.util.jar.*;
006: import java.util.*;
007:
008: import tide.editor.*;
009: import tide.project.*;
010: import snow.utils.*;
011:
012: /** Creates the processes to sign jar files and related ops.
013: */
014: public class JarSigner {
015:
016: /** Signs the jar file. May also work without tide.
017: */
018: public static Process signJarFile_(final File jarFile,
019: final File keystore, final char[] keyStorePassword,
020: final String keyStoreAlias) throws Exception {
021:
022: File jarSigner = MainEditorFrame.instance.getActualProject()
023: .getJarSigner_TOOL();
024:
025: if (!jarFile.exists()) {
026: throw new Exception("JarFile to sign not found: "
027: + jarFile.getAbsolutePath());
028: }
029:
030: if (!jarSigner.exists()) {
031: throw new Exception(
032: "The JarSigner tool path is not valid,\nplease check your project settings"
033: + jarSigner.getAbsolutePath());
034: }
035:
036: if (!keystore.exists()) {
037: throw new Exception("The keystore was not found in "
038: + keystore.getAbsolutePath());
039: }
040:
041: Process proc = Runtime.getRuntime().exec(
042: new String[] {
043: jarSigner.getAbsolutePath(),
044: "-J-Xmx800m", // ?? TODO: offers as parameter OR grew if out of memory detected
045: "-J-Duser.language=en", "-verbose",
046: "-keystore", keystore.getAbsolutePath(),
047: "-storepass", new String(keyStorePassword),
048: "-keypass", new String(keyStorePassword),
049: jarFile.getAbsolutePath(), keyStoreAlias });
050:
051: return proc;
052: }
053:
054: /** Reads all existing signatures from jar.
055: * @return lowercased list of aliases
056: */
057: public static List<String> readAllExistingSignatures(File jarFile)
058: throws Exception {
059:
060: List<String> rs = new ArrayList<String>();
061: JarFile jf = null;
062: try {
063: jf = new JarFile(jarFile);
064: Enumeration<JarEntry> entries = jf.entries();
065: while (entries.hasMoreElements()) {
066: JarEntry je = entries.nextElement();
067: String nameLow = je.getName().toLowerCase(
068: Locale.ENGLISH);
069: if (nameLow.startsWith("meta-inf/")
070: && nameLow.endsWith(".sf")) {
071: rs.add(StringUtils.removeBeforeIncluded(nameLow,
072: "/"));
073: }
074: }
075: } catch (Exception e) {
076: throw e;
077: } finally {
078: if (jf != null)
079: jf.close();
080: }
081: return rs;
082: }
083:
084: /** Rewrites the jarFile, removing the meta-inf\*.sf entries.
085: */
086: public static void removeAllExistingSignatures(File jarFile)
087: throws Exception {
088: JarFile jar = null;
089: JarOutputStream newJar = null;
090: File tempdest = null;
091: try {
092:
093: jar = new JarFile(jarFile);
094:
095: tempdest = File.createTempFile(jarFile.getName(), null);
096: tempdest.deleteOnExit();
097: //System.out.println(""+tempdest);
098:
099: newJar = new JarOutputStream(new FileOutputStream(tempdest));
100:
101: Enumeration<JarEntry> entries = jar.entries();
102: byte[] buffer = new byte[256];
103: int bytesRead = -1;
104: wl: while (entries.hasMoreElements()) {
105: JarEntry entry = entries.nextElement();
106: String nameLow = entry.getName().toLowerCase(
107: Locale.ENGLISH);
108: if (nameLow.startsWith("meta-inf/")
109: && nameLow.endsWith(".sf")) {
110: continue wl;
111: }
112:
113: InputStream is = jar.getInputStream(entry);
114: newJar.putNextEntry(entry);
115: while ((bytesRead = is.read(buffer)) != -1) {
116: newJar.write(buffer, 0, bytesRead);
117: }
118: }
119:
120: FileUtils.closeIgnoringExceptions(jar);
121: FileUtils.closeIgnoringExceptions(newJar);
122:
123: // rename
124: jarFile.delete();
125: //tempdest.renameTo(jarFile);
126: FileUtils.copy(tempdest, jarFile);
127: } catch (Exception e) {
128: throw e;
129: } finally {
130: FileUtils.closeIgnoringExceptions(jar);
131: FileUtils.closeIgnoringExceptions(newJar);
132:
133: }
134: }
135: /*test
136: public static void main(String[] arguments)
137: {
138: try{
139: File f = new File("c:/temp/mail.jar");
140: System.out.println("" + readAllExistingSignatures(f));
141: removeAllExistingSignatures(f);
142: }
143: catch(Exception e) {
144: e.printStackTrace();
145: }
146: finally{
147: }
148: } */
149:
150: }
|