Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
pescuma committed Oct 15, 2013
1 parent ac9fb3b commit 2f2ce07
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 4 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
*.class

# Package Files #
*.jar
*.war
*.ear
8 changes: 8 additions & 0 deletions merge-jar-services/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/ant-1.7.0-jar.jar" sourcepath="lib/ant-1.7.0-source.jar"/>
<classpathentry kind="lib" path="lib/ant-launcher-1.7.0-jar.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 3 additions & 0 deletions merge-jar-services/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
build

17 changes: 17 additions & 0 deletions merge-jar-services/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>merge-jar-services</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions merge-jar-services/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Binary file added merge-jar-services/lib/ant-1.7.0-jar.jar
Binary file not shown.
Binary file added merge-jar-services/lib/ant-1.7.0-source.jar
Binary file not shown.
Binary file added merge-jar-services/lib/ant-launcher-1.7.0-jar.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.pescuma.mergeservices;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.FileResourceIterator;

public class MergeServicesTask extends Task {

private File dest;
private final List<FileSet> filesets = new ArrayList<FileSet>();

public void setDest(File dest) {
this.dest = dest;
}

public void addFileset(FileSet set) {
filesets.add(set);
}

@Override
public void execute() throws BuildException {
for (FileSet fileSet : filesets) {
process(fileSet);
}
}

private void process(FileSet fileSet) {
DirectoryScanner ds = fileSet.getDirectoryScanner();
Iterator<?> iter = new FileResourceIterator(fileSet.getDir(), ds.getIncludedFiles());

while (iter.hasNext()) {
FileResource resource = (FileResource) iter.next();
process(resource.getFile());
}

}

private void process(File file) {
try {

ZipFile zip = new ZipFile(file);
for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements();) {
ZipEntry entry = e.nextElement();
if (!entry.isDirectory() && entry.getName().startsWith("META-INF/services/"))
process(zip, entry);
}

} catch (IOException e) {
throw new BuildException(e);
}
}

private void process(ZipFile zip, ZipEntry entry) throws IOException {
File result = new File(dest, entry.getName());

if (!result.getParentFile().exists() && !result.getParentFile().mkdirs())
throw new BuildException("Could not create dir " + result.getParent());

InputStream in = null;
OutputStream out = null;
try {

in = zip.getInputStream(entry);
out = new FileOutputStream(result, true);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);

} finally {
close(out);
close(in);
}
}

private void close(Closeable writer) {
try {

if (writer != null)
writer.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
4 changes: 4 additions & 0 deletions merge-jar-services/src/org/pescuma/mergeservices/antlib.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<antlib>
<typedef name="merge-services" classname="org.pescuma.mergeservices.MergeServicesTask" />
</antlib>

0 comments on commit 2f2ce07

Please sign in to comment.