You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2011/06/19 14:48:08 UTC
svn commit: r1137364 - in /openejb/trunk/sandbox/tools/src/main:
java/org/apache/openejb/tools/ java/org/apache/openejb/tools/examples/
resources/generate-index/velocity/
Author: rmannibucau
Date: Sun Jun 19 12:48:07 2011
New Revision: 1137364
URL: http://svn.apache.org/viewvc?rev=1137364&view=rev
Log:
adding zip download links
Added:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/ZipHelper.java
Modified:
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/Daily.java
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/FileHelper.java
openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/GenerateIndex.java
openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/example.vm
openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/glossary.vm
openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/index.vm
Modified: openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/Daily.java
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/Daily.java?rev=1137364&r1=1137363&r2=1137364&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/Daily.java (original)
+++ openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/Daily.java Sun Jun 19 12:48:07 2011
@@ -33,6 +33,6 @@ public class Daily {
private static final String EXAMPLE_WORKING_FOLDER = "/tmp/openejb";
public static void main(String[] args) {
- GenerateIndex.main(new String[] { EXAMPLE_ZIP_LOCATION, EXAMPLE_WORKING_FOLDER });
+ GenerateIndex.generate(EXAMPLE_ZIP_LOCATION, EXAMPLE_WORKING_FOLDER);
}
}
Modified: openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/FileHelper.java
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/FileHelper.java?rev=1137364&r1=1137363&r2=1137364&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/FileHelper.java (original)
+++ openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/FileHelper.java Sun Jun 19 12:48:07 2011
@@ -14,12 +14,12 @@ import org.apache.log4j.Logger;
* @author Romain Manni-Bucau
*/
public final class FileHelper {
- private static final Logger LOGGER = Logger.getLogger(FileHelper.class);
- private static final int BUFFER_SIZE = 1024;
- private static final List<String> EXCLUDED_FOLDERS = ListBuilder.newList(String.class)
+ public static final List<String> EXCLUDED_FOLDERS = ListBuilder.newList(String.class)
.add("examples").add(".svn").add("target").add(".git").add(".settings")
.list();
+ private static final Logger LOGGER = Logger.getLogger(FileHelper.class);
+
private FileHelper() {
// no-op
}
@@ -48,35 +48,6 @@ public final class FileHelper {
return examples;
}
- public static void extract(String filename, String output) {
- File extractHere = new File(output);
- mkdirp(extractHere);
-
- try {
- // we'll read everything so ZipFile is useless
- ZipInputStream zip = new ZipInputStream(new FileInputStream(filename));
- byte[] buf = new byte[BUFFER_SIZE];
- ZipEntry entry;
- while ((entry = zip.getNextEntry()) != null) {
- if (entry.isDirectory()) {
- mkdirp(new File(output + File.separator + entry.getName()));
- } else {
- int count;
- File file = new File(output + File.separator + entry.getName());
- FileOutputStream fos = new FileOutputStream(file);
- while ((count = zip.read(buf, 0, BUFFER_SIZE)) != -1) {
- fos.write(buf, 0, count);
- }
- fos.flush();
- fos.close();
- }
- }
- } catch (Exception e) {
- LOGGER.error("can't unzip examples", e);
- throw new RuntimeException("can't unzip " + filename);
- }
- }
-
public static void mkdirp(File file) {
if (!file.exists()) {
if (!file.mkdirs()) {
Modified: openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/GenerateIndex.java
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/GenerateIndex.java?rev=1137364&r1=1137363&r2=1137364&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/GenerateIndex.java (original)
+++ openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/GenerateIndex.java Sun Jun 19 12:48:07 2011
@@ -30,7 +30,6 @@ import java.util.TreeMap;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
-import static org.apache.openejb.tools.examples.FileHelper.extract;
import static org.apache.openejb.tools.examples.FileHelper.listFilesEndingWith;
import static org.apache.openejb.tools.examples.FileHelper.listFolders;
import static org.apache.openejb.tools.examples.FileHelper.mkdirp;
@@ -43,6 +42,8 @@ import static org.apache.openejb.tools.e
import static org.apache.openejb.tools.examples.ViewHelper.getExamplesClassesByApi;
import static org.apache.openejb.tools.examples.ViewHelper.getLink;
import static org.apache.openejb.tools.examples.ViewHelper.removePrefix;
+import static org.apache.openejb.tools.examples.ZipHelper.extract;
+import static org.apache.openejb.tools.examples.ZipHelper.zipDirectory;
/**
* Most the examples do not have any documentation.
@@ -76,16 +77,16 @@ public class GenerateIndex {
public static final String INDEX_HTML = "index.html";
public static final String GLOSSARY_HTML = "glossary.html";
public static final String README_MD = "README.md";
+
public static final String POM_XML = "pom.xml";
private static final MarkdownProcessor PROCESSOR = new MarkdownProcessor();
-
private static final String TEMPLATE_COMMON_PROPERTIES = "generate-index/config.properties";
private static final String MAIN_TEMPLATE = "index.vm";
private static final String DEFAULT_EXAMPLE_TEMPLATE = "example.vm";
private static final String EXTERNALE_TEMPLATE = "external.vm";
- private static final String GLOSSARY_TEMPLATE = "glossary.vm";
+ private static final String GLOSSARY_TEMPLATE = "glossary.vm";
private static final String TITLE = "title";
private static final String BASE = "base";
@@ -94,14 +95,10 @@ public class GenerateIndex {
* <p/>
* mvn clean install exec:java -Dexec.mainClass=org.apache.openejb.tools.examples.GenerateIndex
*
- * @param args zip-location work-folder
+ * @param examplesZip examples zip location
+ * @param workFolder work folder
*/
- public static void main(String[] args) {
- if (args.length < 2) {
- LOGGER.info("Usage: <main> <examples-zip-location> <work-folder>");
- return;
- }
-
+ public static void generate(String examplesZip, String workFolder) {
Properties properties = new Properties();
URL propertiesUrl = Thread.currentThread().getContextClassLoader().getResource(TEMPLATE_COMMON_PROPERTIES);
try {
@@ -114,15 +111,16 @@ public class GenerateIndex {
String base = properties.getProperty(BASE);
// working folder
- File extractedDir = new File(args[1], EXTRACTED_EXAMPLES);
- File generatedDir = new File(args[1], GENERATED_EXAMPLES);
+ File extractedDir = new File(workFolder, EXTRACTED_EXAMPLES);
+ File generatedDir = new File(workFolder, GENERATED_EXAMPLES);
// crack open the examples zip file
- extract(args[0], extractedDir.getPath());
+ extract(examplesZip, extractedDir.getPath());
// generate index.html by example
Map<String, Set<String>> exampleLinksByKeyword = new TreeMap<String, Set<String>>();
Map<String, String> nameByLink = new TreeMap<String, String>();
+ Map<String, String> zipLinks = new TreeMap<String, String>();
Collection<File> examples = listFolders(extractedDir, POM_XML);
for (File example : examples) {
// create a directory for each example
@@ -140,7 +138,18 @@ public class GenerateIndex {
}
File index = new File(generated, INDEX_HTML);
- nameByLink.put(getLink(generatedDir, index), example.getName());
+ String link = getLink(generatedDir, index);
+ nameByLink.put(link, example.getName());
+
+ File zip = zip = new File(generated, example.getName() + ".zip");
+ String zipLink = getLink(generatedDir, zip);
+ zipLinks.put(link, zipLink);
+
+ try {
+ zipDirectory(example, zip, example.getParent());
+ } catch (IOException e) {
+ LOGGER.error("can't zip example " + example.getName());
+ }
List<File> javaFiles = listFilesEndingWith(example, ".java");
Map<String, Integer> apiCount = getAndUpdateApis(javaFiles, exampleLinksByKeyword, generatedDir, index);
@@ -154,6 +163,7 @@ public class GenerateIndex {
.add(BASE, base)
.add(OpenEJBTemplate.USER_JAVASCRIPTS, newList(String.class).add("prettyprint.js").list())
.add("apis", apiCount)
+ .add("link", zip.getName())
.add("files", removePrefix(extractedDir, javaFiles))
.map(),
index.getPath());
@@ -180,6 +190,7 @@ public class GenerateIndex {
.add(BASE, base)
.add(USER_JAVASCRIPTS, newList(String.class).add("glossary.js").list())
.add("links", nameByLink)
+ .add("zipLinks", zipLinks)
.add("examples", nameByLink)
.add("classes", classesByApi)
.add("exampleByKeyword", exampleLinksByKeyword)
@@ -193,6 +204,7 @@ public class GenerateIndex {
.add(TITLE, "OpenEJB Example")
.add(BASE, base)
.add(USER_JAVASCRIPTS, newList(String.class).add("index.js").list())
+ .add("zipLinks", zipLinks)
.add("examples", nameByLink)
.add("classes", classesByApi)
.add("examplesClasses", examplesClassesByApi)
Added: openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/ZipHelper.java
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/ZipHelper.java?rev=1137364&view=auto
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/ZipHelper.java (added)
+++ openejb/trunk/sandbox/tools/src/main/java/org/apache/openejb/tools/examples/ZipHelper.java Sun Jun 19 12:48:07 2011
@@ -0,0 +1,99 @@
+package org.apache.openejb.tools.examples;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
+import org.apache.log4j.Logger;
+
+import static org.apache.openejb.tools.examples.FileHelper.mkdirp;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+public final class ZipHelper {
+ private static final Logger LOGGER = Logger.getLogger(ZipHelper.class);
+ private static final int BUFFER_SIZE = 1024;
+
+ private ZipHelper() {
+ // no-op
+ }
+
+ public static void extract(String filename, String output) {
+ File extractHere = new File(output);
+ mkdirp(extractHere);
+
+ try {
+ // we'll read everything so ZipFile is useless
+ ZipInputStream zip = new ZipInputStream(new FileInputStream(filename));
+ byte[] buf = new byte[BUFFER_SIZE];
+ ZipEntry entry;
+ while ((entry = zip.getNextEntry()) != null) {
+ if (entry.isDirectory()) {
+ mkdirp(new File(output + File.separator + entry.getName()));
+ } else {
+ int count;
+ File file = new File(output + File.separator + entry.getName());
+ FileOutputStream fos = new FileOutputStream(file);
+ while ((count = zip.read(buf, 0, BUFFER_SIZE)) != -1) {
+ fos.write(buf, 0, count);
+ }
+ fos.flush();
+ fos.close();
+ }
+ }
+ } catch (Exception e) {
+ LOGGER.error("can't unzip examples", e);
+ throw new RuntimeException("can't unzip " + filename);
+ }
+ }
+
+ public static void zipDirectory(File dir, File zipName, String skip) throws IOException, IllegalArgumentException {
+ if (!dir.isDirectory()) {
+ LOGGER.error(dir.getPath() + " is not a directory, skipping");
+ return;
+ }
+
+ String[] entries = dir.list();
+ ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipName));
+ for (int i = 0; i < entries.length; i++) {
+ File f = new File(dir, entries[i]);
+ zipFile(out, f, skip);
+ }
+ out.close();
+ }
+
+ private static void zipFile(ZipOutputStream out, File f, String skip) throws IOException {
+ if (f.isDirectory()) {
+ if (FileHelper.EXCLUDED_FOLDERS.contains(f.getName())) {
+ return;
+ } else {
+ for (File child : f.listFiles()) {
+ zipFile(out, child, skip);
+ }
+ }
+ } else {
+ byte[] buffer = new byte[BUFFER_SIZE];
+ int bytesRead;
+
+ String path = f.getPath();
+ if (path.startsWith(skip)) {
+ path = path.substring(skip.length(), path.length());
+ if (path.startsWith(File.separator)) {
+ path = path.substring(1);
+ }
+ }
+
+ FileInputStream in = new FileInputStream(f);
+ ZipEntry entry = new ZipEntry(path);
+ out.putNextEntry(entry);
+ while ((bytesRead = in.read(buffer)) != -1) {
+ out.write(buffer, 0, bytesRead);
+ }
+ in.close();
+ }
+ }
+}
Modified: openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/example.vm
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/example.vm?rev=1137364&r1=1137363&r2=1137364&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/example.vm (original)
+++ openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/example.vm Sun Jun 19 12:48:07 2011
@@ -1,6 +1,10 @@
#parse("generate-index/velocity/header.vm")
<h2>$title</h2>
+ <div id="download">
+ <a href="$link">Download as zip</a>
+ </div>
+
<div id="javaFiles">
<ul>
Files:
Modified: openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/glossary.vm
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/glossary.vm?rev=1137364&r1=1137363&r2=1137364&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/glossary.vm (original)
+++ openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/glossary.vm Sun Jun 19 12:48:07 2011
@@ -27,7 +27,7 @@
<li class="$classes.get($api.key)">$api.key
<ul>
#foreach( $link in $api.value )
- <li><a href="$link">$links.get($link)</a></li>
+ <li><a href="$link">$links.get($link)</a> <a href="$zipLinks.get($link)">Download zip</a></li>
#end
</ul>
</li>
@@ -38,7 +38,7 @@
<div id="aggregate">
<ul>
#foreach( $example in $links.entrySet() )
- <li class="$aggregatedClasses.get($example.key)"><a href="$example.key">$example.value</a></li>
+ <li class="$aggregatedClasses.get($example.key)"><a href="$example.key">$example.value</a> <a href="$zipLinks.get($example.key)">Download zip</a></li>
#end
</ul>
</div>
Modified: openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/index.vm
URL: http://svn.apache.org/viewvc/openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/index.vm?rev=1137364&r1=1137363&r2=1137364&view=diff
==============================================================================
--- openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/index.vm (original)
+++ openejb/trunk/sandbox/tools/src/main/resources/generate-index/velocity/index.vm Sun Jun 19 12:48:07 2011
@@ -16,7 +16,7 @@
<ul>
#foreach( $example in $examples.entrySet() )
- <li class="$aggregatedClasses.get($example.key)"><a href="$example.key">$example.value</a></li>
+ <li class="$aggregatedClasses.get($example.key)"><a href="$example.key">$example.value</a> <a href="$zipLinks.get($example.key)">Download zip</a></li>
#end
</ul>
</div>