package com.onpositive.appwrench.buildpath;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import com.google.gdt.eclipse.core.WebAppUtilities;
@SuppressWarnings("restriction")
public class CompilationParticipant extends
org.eclipse.jdt.core.compiler.CompilationParticipant {
public CompilationParticipant() {
}
@Override
public boolean isActive(IJavaProject project) {
return true;
}
@Override
public void buildFinished(IJavaProject project) {
super.buildFinished(project);
IJavaProject[] javaProjects;
try {
javaProjects = project.getJavaModel().getJavaProjects();
for (IJavaProject p : javaProjects) {
IProject project2 = p.getProject();
try {
if (project2
.hasNature("com.google.appengine.eclipse.core.gaeNature")) {
IPackageFragmentRoot[] packageFragmentRoots = p
.getAllPackageFragmentRoots();
String persistentProperty = project2
.getPersistentProperty(Activator.PACK_REFERENCED);
if (persistentProperty == null) {
continue;
}
if (!Boolean.parseBoolean(persistentProperty)) {
continue;
}
IFolder webInfLib = WebAppUtilities.getWebInfLib(project2);
for (IPackageFragmentRoot r : packageFragmentRoots) {
if (r.getKind() == IPackageFragmentRoot.K_SOURCE) {
IJavaElement parent = r.getParent();
if (parent instanceof IJavaProject) {
IJavaProject m = (IJavaProject) parent;
if (!m.equals(p) && m.equals(project)) {
doWork(project2, project);
}
if (!m.equals(p) && !webInfLib.getFile(parent.getElementName()+".jar").exists()){
doWork(project2, m);
}
}
}
}
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
} catch (JavaModelException e1) {
throw new IllegalStateException(e1);
}
}
void doWork(IProject project2,IJavaProject project) throws Exception{
IFolder webInfLib = WebAppUtilities.getWebInfLib(project2);
IFile file = webInfLib.getFile(project.getElementName() + ".jar");
File createTempFile = File.createTempFile(project.getElementName(),
".jar");
final JarOutputStream d = new JarOutputStream(new FileOutputStream(
createTempFile));
try{
final BufferedOutputStream b = new BufferedOutputStream(d);
final IFolder folder = project.getProject().getParent().getFolder(
project.getOutputLocation());
jarFolder(d, b, folder);
b.flush();
try{
d.closeEntry();
}catch (Exception e) {
//silently ignore it
}
}finally{
d.close();
}
if (file.exists()) {
file.setContents(new FileInputStream(createTempFile), true, false,
new NullProgressMonitor());
} else {
file.create(new FileInputStream(createTempFile), true,
new NullProgressMonitor());
}
boolean delete = createTempFile.delete();
if (!delete) {
System.err.println("Error");
}
}
private void jarFolder(final JarOutputStream d,
final BufferedOutputStream b, final IFolder folder)
throws CoreException {
folder.accept(new IResourceVisitor() {
@Override
public boolean visit(IResource resource) throws CoreException {
if (resource.getType() == IResource.FILE) {
IPath removeFirstSegments = resource
.getProjectRelativePath().removeFirstSegments(
folder.getProjectRelativePath()
.segmentCount());
try {
d.putNextEntry(new ZipEntry(removeFirstSegments
.toPortableString()));
} catch (IOException e) {
e.printStackTrace();
}
IFile fl = (IFile) resource;
InputStream contents = new BufferedInputStream(fl
.getContents(true));
try {
while (contents.available() >= 0) {
int read = contents.read();
if (read != -1) {
b.write(read);
} else {
break;
}
}
b.flush();
System.out.println(fl.getName());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
contents.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
}
});
}
}