You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2013/09/19 09:12:26 UTC
svn commit: r1524637 - in
/tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock:
Archive.java Files.java IO.java Join.java
Author: dblevins
Date: Thu Sep 19 07:12:25 2013
New Revision: 1524637
URL: http://svn.apache.org/r1524637
Log:
TOMEE-1033 - Fill out ZipLock Utilities
Added:
tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Archive.java (with props)
tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Files.java (with props)
tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Join.java (with props)
Modified:
tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/IO.java
Added: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Archive.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Archive.java?rev=1524637&view=auto
==============================================================================
--- tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Archive.java (added)
+++ tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Archive.java Thu Sep 19 07:12:25 2013
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ziplock;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class Archive {
+
+ final Map<String, String> manifest = new HashMap<String, String>();
+ final Map<String, byte[]> entries = new HashMap<String, byte[]>();
+
+ public static Archive archive() {
+ return new Archive();
+ }
+
+ public Archive manifest(String key, Object value) {
+ manifest.put(key, value.toString());
+ return this;
+ }
+
+ public Archive manifest(String key, Class value) {
+ manifest.put(key, value.getName());
+ return this;
+ }
+
+ public Archive copyTo(String path, File file) {
+ if (file.isDirectory()) {
+ for (File child : file.listFiles()) {
+ copyTo(path + "/" + child.getName(), child);
+ }
+ } else {
+ try {
+ entries.put(path, IO.readBytes(file));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ return this;
+ }
+
+ public Archive addTo(String path, Class<?> clazz) {
+ try {
+ final String name = clazz.getName().replace('.', '/') + ".class";
+
+ final URL resource = this.getClass().getClassLoader().getResource(name);
+
+ final InputStream from = new BufferedInputStream(resource.openStream());
+ final ByteArrayOutputStream to = new ByteArrayOutputStream();
+
+ final byte[] buffer = new byte[1024];
+ int length;
+ while ((length = from.read(buffer)) != -1) {
+ to.write(buffer, 0, length);
+ }
+ to.flush();
+
+ if (path != null && path.length() != 0) {
+ entries.put(path + "/" + name, to.toByteArray());
+ } else {
+ entries.put(name, to.toByteArray());
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+
+ return this;
+ }
+
+
+ public Archive add(Class<?> clazz) {
+ return addTo(null, clazz);
+ }
+
+ public File toJar() throws IOException {
+ return toJar("archive-", ".jar");
+ }
+
+ public File toJar(final String prefix, final String suffix) throws IOException {
+ final File file = File.createTempFile(prefix, suffix);
+ file.deleteOnExit();
+
+ // Create the ZIP file
+ final ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
+
+ for (Map.Entry<String, byte[]> entry : entries().entrySet()) {
+ out.putNextEntry(new ZipEntry(entry.getKey()));
+ out.write(entry.getValue());
+ }
+
+ // Complete the ZIP file
+ out.close();
+ return file;
+ }
+
+ public File asJar() {
+ try {
+ return toJar();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public File asJar(final String prefix, final String suffix) {
+ try {
+ return toJar(prefix, suffix);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public File toDir() throws IOException {
+
+ File classpath = Files.tmpdir();
+
+ for (Map.Entry<String, byte[]> entry : entries().entrySet()) {
+
+ final String key = entry.getKey().replace('/', File.separatorChar);
+
+ final File file = new File(classpath, key);
+
+ File d = file.getParentFile();
+
+ if (!d.exists()) {
+ if (!d.mkdirs()) {
+ throw new IllegalStateException("Cannot Make Directory: "+d.getAbsolutePath());
+ }
+ }
+
+ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
+
+ out.write(entry.getValue());
+
+ out.close();
+ }
+
+ return classpath;
+ }
+
+ public File asDir() {
+ try {
+ return toDir();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private HashMap<String, byte[]> entries() {
+ final HashMap<String, byte[]> entries = new HashMap<String, byte[]>(this.entries);
+ entries.put("META-INF/MANIFEST.MF", buildManifest().getBytes());
+ return entries;
+ }
+
+ private String buildManifest() {
+ return Join.join("\r\n", new Join.NameCallback<Map.Entry<String, String>>() {
+ @Override
+ public String getName(Map.Entry<String, String> entry) {
+ return entry.getKey() + ": " + entry.getValue();
+ }
+ }, manifest.entrySet());
+ }
+
+}
Propchange: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Archive.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Files.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Files.java?rev=1524637&view=auto
==============================================================================
--- tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Files.java (added)
+++ tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Files.java Thu Sep 19 07:12:25 2013
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ziplock;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class Files {
+
+ public static List<File> collect(final File dir, final String regex) {
+ return collect(dir, Pattern.compile(regex));
+ }
+
+ public static List<File> collect(final File dir, final Pattern pattern) {
+ return collect(dir, new FileFilter() {
+ @Override
+ public boolean accept(final File file) {
+ return pattern.matcher(file.getName()).matches();
+ }
+ });
+ }
+
+
+ public static List<File> collect(final File dir, final FileFilter filter) {
+ final List<File> accepted = new ArrayList<File>();
+ if (filter.accept(dir)) accepted.add(dir);
+
+ final File[] files = dir.listFiles();
+ if (files != null) for (final File file : files) {
+ accepted.addAll(collect(file, filter));
+ }
+
+ return accepted;
+ }
+
+ public static void remove(final File file) {
+ if (file == null) return;
+ if (!file.exists()) return;
+
+ if (file.isDirectory()) {
+ final File[] files = file.listFiles();
+ if (files != null) {
+ for (final File child : files) {
+ remove(child);
+ }
+ }
+ }
+ if (!file.delete()) {
+ throw new IllegalStateException("Could not delete file: " + file.getAbsolutePath());
+ }
+ }
+
+
+ public static File tmpdir() {
+ try {
+ File file = null;
+ try {
+ file = File.createTempFile("temp", "dir");
+ } catch (Throwable e) {
+ //Use a local tmp directory
+ final File tmp = new File("tmp");
+ if (!tmp.exists() && !tmp.mkdirs()) {
+ throw new IOException("Failed to create local tmp directory: " + tmp.getAbsolutePath());
+ }
+
+ file = File.createTempFile("temp", "dir", tmp);
+ }
+
+ if (!file.delete()) {
+ throw new IOException("Failed to create temp dir. Delete failed");
+ }
+
+ mkdir(file);
+ deleteOnExit(file);
+
+ return file;
+
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static File mkdir(final File file) {
+ if (file.exists()) return file;
+ if (!file.mkdirs()) throw new RuntimeException("Cannot mkdir: " + file.getAbsolutePath());
+ return file;
+ }
+
+ // Shutdown hook for recursive delete on tmp directories
+ static final List<String> delete = new ArrayList<String>();
+
+ static {
+ final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(Files.class.getClassLoader());
+ try {
+ final Thread deleteShutdownHook = new Thread() {
+ @Override
+ public void run() {
+ for (final String path : delete) {
+ try {
+ remove(new File(path));
+ } catch (Throwable e) {
+ System.err.println(e.getMessage());
+ }
+ }
+ }
+ };
+ try {
+ Runtime.getRuntime().addShutdownHook(deleteShutdownHook);
+ } catch (Throwable e) {
+ //Ignore
+ }
+ } finally {
+ Thread.currentThread().setContextClassLoader(loader);
+ }
+ }
+
+ public static void deleteOnExit(final File file) {
+ delete.add(file.getAbsolutePath());
+ }
+
+}
Propchange: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Files.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/IO.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/IO.java?rev=1524637&r1=1524636&r2=1524637&view=diff
==============================================================================
--- tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/IO.java (original)
+++ tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/IO.java Thu Sep 19 07:12:25 2013
@@ -67,21 +67,36 @@ public class IO {
}
public static String slurp(final File file) throws IOException {
+ final byte[] bytes = readBytes(file);
+ return new String(bytes);
+ }
+
+ public static byte[] readBytes(File file) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(file, out);
- return new String(out.toByteArray());
+ return out.toByteArray();
}
public static String slurp(final InputStream in) throws IOException {
+ final byte[] bytes = readBytes(in);
+ return new String(bytes, "UTF-8");
+ }
+
+ public static byte[] readBytes(InputStream in) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
copy(in, out);
- return new String(out.toByteArray(), "UTF-8");
+ return out.toByteArray();
}
public static String slurp(final URL url) throws IOException {
+ final byte[] bytes = readBytes(url);
+ return new String(bytes);
+ }
+
+ public static byte[] readBytes(URL url) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
- copy(url.openStream(), out);
- return new String(out.toByteArray());
+ copy(url, out);
+ return out.toByteArray();
}
public static void writeString(final File file, final String string) throws IOException {
@@ -108,6 +123,15 @@ public class IO {
}
}
+ public static void copy(final URL from, final OutputStream to) throws IOException {
+ final InputStream read = read(from);
+ try {
+ copy(read, to);
+ } finally {
+ close(read);
+ }
+ }
+
public static void copy(final InputStream from, final File to) throws IOException {
final OutputStream write = write(to);
try {
@@ -183,4 +207,8 @@ public class IO {
final InputStream in = new FileInputStream(source);
return new BufferedInputStream(in, 32768);
}
+ public static InputStream read(final URL source) throws IOException {
+ final InputStream in = source.openStream();
+ return new BufferedInputStream(in, 32768);
+ }
}
Added: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Join.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Join.java?rev=1524637&view=auto
==============================================================================
--- tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Join.java (added)
+++ tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Join.java Thu Sep 19 07:12:25 2013
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ziplock;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class Join {
+
+ public static final MethodCallback METHOD_CALLBACK = new MethodCallback();
+
+ public static final ClassCallback CLASS_CALLBACK = new ClassCallback();
+
+ public static String join(String delimiter, Collection collection) {
+ if (collection.size() == 0) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ for (Object obj : collection) {
+ sb.append(obj).append(delimiter);
+ }
+ return sb.substring(0, sb.length() - delimiter.length());
+ }
+
+ public static String join(String delimiter, Object... collection) {
+ if (collection.length == 0) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ for (Object obj : collection) {
+ sb.append(obj).append(delimiter);
+ }
+ return sb.substring(0, sb.length() - delimiter.length());
+ }
+
+ public static <T> String join(String delimiter, NameCallback<T> nameCallback, T... collection) {
+ if (collection.length == 0) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ for (T obj : collection) {
+ sb.append(nameCallback.getName(obj)).append(delimiter);
+ }
+ return sb.substring(0, sb.length() - delimiter.length());
+ }
+
+ public static <T> String join(String delimiter, NameCallback<T> nameCallback, Collection<T> collection) {
+ if (collection.size() == 0) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ for (T obj : collection) {
+ sb.append(nameCallback.getName(obj)).append(delimiter);
+ }
+ return sb.substring(0, sb.length() - delimiter.length());
+ }
+
+ public static <T> List<String> strings(Collection<T> collection, NameCallback<T> callback) {
+ final List<String> list = new ArrayList<String>();
+
+ for (T t : collection) {
+ final String name = callback.getName(t);
+ list.add(name);
+ }
+
+ return list;
+ }
+
+ public static interface NameCallback<T> {
+
+ public String getName(T object);
+ }
+
+ public static class FileCallback implements NameCallback<File> {
+
+ public String getName(File file) {
+ return file.getName();
+ }
+ }
+
+ public static class MethodCallback implements NameCallback<Method> {
+
+ public String getName(Method method) {
+ return method.getName();
+ }
+ }
+
+ public static class ClassCallback implements NameCallback<Class<?>> {
+
+ public String getName(Class<?> cls) {
+ return cls.getName();
+ }
+ }
+}
Propchange: tomee/tomee/trunk/arquillian/ziplock/src/main/java/org/apache/ziplock/Join.java
------------------------------------------------------------------------------
svn:eol-style = native