Created
July 25, 2017 18:42
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.JarURLConnection; | |
import java.net.URL; | |
import java.util.Enumeration; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
import java.util.zip.ZipOutputStream; | |
import com.documentum.fc.common.DfException; | |
import com.documentum.thirdparty.javassist.ClassPool; | |
import com.documentum.thirdparty.javassist.CtClass; | |
import com.documentum.thirdparty.javassist.CtMethod; | |
/** | |
* @author Andrey B. Panfilov <andrey@panfilov.tel> | |
*/ | |
public class FixBofFile { | |
public static void main(String[] args) throws Exception { | |
ClassPool pool = ClassPool.getDefault(); | |
for (Class cls : new Class[] {File.class, DfException.class, | |
IOException.class, }) { | |
pool.importPackage(cls.getPackage().getName()); | |
} | |
String BF = "com.documentum.fc.client.impl.bof.cache.InboundModuleMetadata$BofFile"; | |
BF = BF.replace(".", "/") + ".class"; | |
URL resourceURL = FixBofFile.class.getResource("/" + BF); | |
JarURLConnection connection = (JarURLConnection) resourceURL | |
.openConnection(); | |
URL jarURL = connection.getJarFileURL(); | |
String fileName = jarURL.getFile(); | |
ZipFile zipFile = new ZipFile(fileName); | |
String out = fileName + ".patched"; | |
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out)); | |
for (Enumeration e = zipFile.entries(); e.hasMoreElements();) { | |
ZipEntry entryIn = (ZipEntry) e.nextElement(); | |
if (entryIn.getName().equals(BF)) { | |
zos.putNextEntry(new ZipEntry(BF)); | |
zos.write(getBFFix(pool)); | |
} else { | |
zos.putNextEntry(new ZipEntry(entryIn.getName())); | |
InputStream is = zipFile.getInputStream(entryIn); | |
byte[] buf = new byte[1024]; | |
int len; | |
while ((len = is.read(buf)) > 0) { | |
zos.write(buf, 0, len); | |
} | |
} | |
zos.closeEntry(); | |
} | |
zos.close(); | |
System.out.println(out); | |
} | |
private static byte[] getBFFix(ClassPool pool) throws Exception { | |
String cls = "com.documentum.fc.client.impl.bof.cache.InboundModuleMetadata$BofFile"; | |
CtClass cc = pool.get(cls); | |
CtMethod m = cc.getDeclaredMethod("persist", | |
new CtClass[] {pool.get(File.class.getName()) }); | |
StringBuilder body = new StringBuilder(); | |
body.append("{\n"); | |
body.append(" File temp = null;\n"); | |
body.append(" try {\n"); | |
body.append(" m_savedAs = !isArchive() ? constructNonJarName($1) : constructJarName($1);\n"); | |
body.append(" temp = File.createTempFile(m_sysObject.getObjectId().getId(), null, $1);\n"); | |
body.append(" m_sysObject.getFile(temp.getAbsolutePath());\n"); | |
body.append(" if(!temp.renameTo(new File(m_savedAs))) {\n"); | |
body.append(" throw new DfException(\"Unable to rename {0} to {1}\", new Object[] {temp, m_savedAs});\n"); | |
body.append(" }\n"); | |
body.append(" } catch (IOException ex) {\n"); | |
body.append(" throw new DfException(ex);\n"); | |
body.append(" } finally {\n"); | |
body.append(" if (temp != null && temp.exists()) {\n"); | |
body.append(" temp.delete();\n"); | |
body.append(" }\n"); | |
body.append(" }\n"); | |
body.append("}"); | |
m.setBody(body.toString()); | |
return cc.toBytecode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment