PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/testsuite/mixed-domain/src/test/java/org/jboss/as/test/integration/domain/mixed/util/OldVersionCopier.java

https://bitbucket.org/cprenzberg/wildfly
Java | 239 lines | 173 code | 28 blank | 38 comment | 30 complexity | 3ba40a68e0b16609f4906de8843e0185 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2012, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.test.integration.domain.mixed.util;
  23. import static org.junit.Assert.assertNotNull;
  24. import static org.junit.Assert.assertTrue;
  25. import java.io.BufferedInputStream;
  26. import java.io.BufferedOutputStream;
  27. import java.io.BufferedReader;
  28. import java.io.BufferedWriter;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.io.FileOutputStream;
  32. import java.io.FileReader;
  33. import java.io.FileWriter;
  34. import java.io.InputStream;
  35. import java.io.OutputStream;
  36. import java.net.URL;
  37. import java.util.Enumeration;
  38. import java.util.zip.ZipEntry;
  39. import java.util.zip.ZipFile;
  40. import org.xnio.IoUtils;
  41. /**
  42. *
  43. * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
  44. */
  45. public class OldVersionCopier {
  46. private static String OLD_VERSIONS_DIR = "jboss.test.mixed.domain.dir";
  47. private final File oldVersionsBaseDir;
  48. private final File targetOldVersions = new File("target/old-versions/");
  49. private OldVersionCopier(File oldVersionsBaseDir) {
  50. this.oldVersionsBaseDir = oldVersionsBaseDir;
  51. }
  52. static OldVersionCopier expandOldVersions() {
  53. OldVersionCopier copier = new OldVersionCopier(obtainOldVersionsDir());
  54. copier.expandAsInstances();
  55. return copier;
  56. }
  57. File getVersionDir(String version) {
  58. File file = new File(targetOldVersions, "jboss-as-" + version);
  59. if (!file.exists() || !file.isDirectory()) {
  60. throw new IllegalStateException("Could not find " + file.getAbsolutePath());
  61. }
  62. return file;
  63. }
  64. private static File obtainOldVersionsDir() {
  65. String error = "System property '" + OLD_VERSIONS_DIR + "' must be set to a directory containing old versions";
  66. String oldVersionsDir = System.getProperty(OLD_VERSIONS_DIR);
  67. if (oldVersionsDir == null) {
  68. throw new RuntimeException(error);
  69. }
  70. File file = new File(oldVersionsDir);
  71. if (!file.exists() || !file.isDirectory()) {
  72. throw new RuntimeException(error);
  73. }
  74. return file;
  75. }
  76. private void expandAsInstances() {
  77. if (targetOldVersions.exists()) {
  78. return;
  79. }
  80. if (!targetOldVersions.mkdirs() && targetOldVersions.exists()) {
  81. throw new RuntimeException("Could not create " + targetOldVersions);
  82. }
  83. for (File file : oldVersionsBaseDir.listFiles()) {
  84. if (file.getName().endsWith(".zip")) {
  85. try {
  86. expandAsInstance(file);
  87. // if (file.getName().equals("jboss-as-7.1.2.Final.zip")) {
  88. // patchBadRemoting("jboss-as-7.1.2.Final");
  89. // } else if (file.getName().equals("jboss-as-7.1.3.Final.zip")) {
  90. // patchBadRemoting("jboss-as-7.1.3.Final");
  91. // }
  92. } catch(Exception e) {
  93. throw new RuntimeException(e);
  94. }
  95. }
  96. }
  97. }
  98. private void patchBadRemoting(String name) throws Exception {
  99. File file = new File(targetOldVersions, name);
  100. File modulesDir = new File(file, "modules");
  101. if (!modulesDir.exists()) {
  102. throw new RuntimeException("No modules dir " + modulesDir);
  103. }
  104. // File modulesZip = new File(file, "modules.zip");
  105. // if (!modulesZip.exists()) {
  106. // throw new RuntimeException("No modules zip " + modulesZip);
  107. // }
  108. //org/jboss/xnio/main/
  109. File xnioDir = new File(modulesDir, "org");
  110. xnioDir = new File(xnioDir, "jboss");
  111. xnioDir = new File(xnioDir, "xnio");
  112. xnioDir = new File(xnioDir, "main");
  113. URL patchedXnioUrl = getClass().getResource("/patched-jars/xnio-api-3.0.7.GA.jar");
  114. patchModule(xnioDir, patchedXnioUrl);
  115. //org/jboss/remoting3/main
  116. File remotingDir = new File(modulesDir, "org");
  117. remotingDir = new File(remotingDir, "jboss");
  118. remotingDir = new File(remotingDir, "remoting3");
  119. remotingDir = new File(remotingDir, "main");
  120. URL patchedRemotingUrl = getClass().getResource("/patched-jars/jboss-remoting-3.2.13.GA.jar");
  121. patchModule(remotingDir, patchedRemotingUrl);
  122. }
  123. private void patchModule(File moduleMainDir, URL patchedJar) throws Exception {
  124. assertTrue(moduleMainDir.exists());
  125. assertNotNull(patchedJar);
  126. if (patchedJar == null) {
  127. throw new RuntimeException("Null url");
  128. }
  129. String oldJarName = null;
  130. for (String fileName : moduleMainDir.list()) {
  131. if (fileName.endsWith(".jar")) {
  132. if (oldJarName != null) {
  133. throw new RuntimeException("Could not determine old jar in " + moduleMainDir);
  134. }
  135. oldJarName = fileName;
  136. }
  137. }
  138. if (oldJarName == null) {
  139. throw new RuntimeException("Could not determine old jar in " + moduleMainDir);
  140. }
  141. File patchedJarFile = new File(patchedJar.toURI());
  142. //Copy the patched jar to the module main directory
  143. inputStreamToFile(new FileInputStream(patchedJarFile), new File(moduleMainDir, patchedJarFile.getName()));
  144. //Replace the name of the jar with the patched jar in module.xml
  145. File moduleXml = new File(moduleMainDir, "module.xml");
  146. StringBuffer moduleXmlContents = new StringBuffer();
  147. BufferedReader reader = new BufferedReader(new FileReader(moduleXml));
  148. try {
  149. String line = reader.readLine();
  150. while (line != null) {
  151. moduleXmlContents.append(line);
  152. moduleXmlContents.append("\n");
  153. line = reader.readLine();
  154. }
  155. } finally {
  156. IoUtils.safeClose(reader);
  157. }
  158. int index = moduleXmlContents.indexOf(oldJarName);
  159. moduleXmlContents.replace(index, index + oldJarName.length(), patchedJarFile.getName());
  160. moduleXml.delete();
  161. BufferedWriter writer = new BufferedWriter(new FileWriter(moduleXml));
  162. try {
  163. writer.write(moduleXmlContents.toString());
  164. } finally {
  165. IoUtils.safeClose(writer);
  166. }
  167. }
  168. private void expandAsInstance(final File file) throws Exception {
  169. ZipFile zipFile = null;
  170. try {
  171. zipFile = new ZipFile(file);
  172. for (Enumeration<? extends ZipEntry> en = zipFile.entries() ; en.hasMoreElements() ; ) {
  173. final ZipEntry entry = en.nextElement();
  174. final File output = new File(targetOldVersions, entry.getName());
  175. if (entry.isDirectory()) {
  176. if (!output.exists()) {
  177. if (!output.mkdirs() && !output.exists()) {
  178. throw new RuntimeException("Could not make dir " + output.getAbsolutePath());
  179. }
  180. }
  181. } else {
  182. inputStreamToFile(zipFile.getInputStream(entry), output);
  183. }
  184. }
  185. } finally {
  186. IoUtils.safeClose(zipFile);
  187. }
  188. }
  189. private void inputStreamToFile(InputStream input, File output) throws Exception {
  190. final InputStream in = new BufferedInputStream(input);
  191. try {
  192. final OutputStream out = new BufferedOutputStream(new FileOutputStream(output));
  193. try {
  194. byte[] buf = new byte[1024];
  195. int len = in.read(buf);
  196. while (len != -1) {
  197. out.write(buf, 0, len);
  198. len = in.read(buf);
  199. }
  200. } finally {
  201. IoUtils.safeClose(out);
  202. }
  203. } finally {
  204. IoUtils.safeClose(in);
  205. }
  206. }
  207. public static void main(String[] args) {
  208. System.setProperty(OLD_VERSIONS_DIR, "/Users/kabir/old-as7-releases/");
  209. OldVersionCopier.expandOldVersions();
  210. }
  211. }