package com.onpositive.gae.tools.deploy;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.ui.util.CoreUtility;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.osgi.framework.Bundle;
import com.onpositive.gae.tools.Activator;
import com.onpositive.gae.tools.Constants;
import com.onpositive.gae.tools.GaeBridge;
import com.onpositive.gae.tools.core.WebRootDirProvider;
@SuppressWarnings("restriction")
public class PrepareGaeProject extends Job {
private IJavaProject fCurrJProject;
public PrepareGaeProject(String name, IJavaProject project) {
super(name);
this.fCurrJProject = project;
}
protected IStatus copyServlets(IProgressMonitor monitor, Bundle bundle,
String string) throws CoreException {
// IPackageFragmentRoot packageFragmentRoot = fCurrJProject
// .getPackageFragmentRoot(fCurrJProject.getProject().getFolder(
// Constants.SOURCE_FOLDER_NAME));
// IPackageFragment packageFragment = packageFragmentRoot
// .getPackageFragment(Constants.ONPOSITIVE_SERVLETS_PACKAGE_NAME);
// Enumeration<?> entryPaths = bundle.getEntryPaths(string);
// IFolder folder = (IFolder) packageFragment.getResource();
// while (entryPaths!=null&&entryPaths.hasMoreElements()) {
// String path = (String) entryPaths.nextElement();
// if (path.endsWith("/.svn/")){
// continue;
// }
// URL entry = bundle.getEntry(path);
// String name = new File(path).getName();
// IFile file = folder.getFile(name);
// if (!file.exists()) {
// try {
// file.create(entry.openStream(), true, monitor);
// } catch (IOException e) {
// return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
// IStatus.ERROR, e.getMessage(), e);
// }
// }
// }
return Status.OK_STATUS;
}
// protected IStatus removeServlets(IProgressMonitor monitor, Bundle bundle,
// String string) {
// IPackageFragmentRoot packageFragmentRoot = fCurrJProject
// .getPackageFragmentRoot(fCurrJProject.getProject().getFolder(
// Constants.SOURCE_FOLDER_NAME));
// IPackageFragment packageFragment = packageFragmentRoot
// .getPackageFragment(Constants.ONPOSITIVE_SERVLETS_PACKAGE_NAME);
// Enumeration<?> entryPaths = bundle.getEntryPaths(string);
// IFolder folder = (IFolder) packageFragment.getResource();
// while (entryPaths.hasMoreElements()) {
// String path = (String) entryPaths.nextElement();
//
// String name = new File(path).getName();
// IFile file = folder.getFile(name);
// if (file.exists()) {
// try {
// file.delete(true, monitor);
// } catch (CoreException e) {
// return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
// IStatus.ERROR, e.getMessage(), e);
// }
// }
// }
// try {
// if (packageFragment.getCompilationUnits().length == 0) {
// packageFragment.delete(true, monitor);
// }
// if (notJavaUnits(packageFragmentRoot)) {
// packageFragmentRoot.delete(0,
// IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH,
// monitor);
// }
// } catch (JavaModelException e) {
// return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
// IStatus.ERROR, e.getMessage(), e);
// }
// return Status.OK_STATUS;
// }
// private boolean notJavaUnits(IPackageFragmentRoot packageFragmentRoot)
// throws JavaModelException {
// IJavaElement[] children = packageFragmentRoot.getChildren();
// for (IJavaElement e:children){
// if (e instanceof IPackageFragment){
// IPackageFragment p=(IPackageFragment) e;
// if (p.exists()){
// if (p.getCompilationUnits().length>0){
// return false;
// }
// }
// }
// }
// return true;
// }
protected boolean checkServlets(Bundle bundle, String string)
throws CoreException {
// IPackageFragmentRoot packageFragmentRoot = fCurrJProject
// .getPackageFragmentRoot(fCurrJProject.getProject().getFolder(
// Constants.SOURCE_FOLDER_NAME));
// IPackageFragment packageFragment = packageFragmentRoot
// .getPackageFragment(Constants.ONPOSITIVE_SERVLETS_PACKAGE_NAME);
// Enumeration<?> entryPaths = bundle.getEntryPaths(string);
// IFolder folder = (IFolder) packageFragment.getResource();
// while (entryPaths!=null&&entryPaths.hasMoreElements()) {
// String path = (String) entryPaths.nextElement();
// String name = new File(path).getName();
// IFile file = folder.getFile(name);
// if (file.getName().equals(".svn")){
// continue;
// }
// if (!file.exists()) {
// return false;
// }
// }
return true;
}
protected IStatus run(IProgressMonitor monitor) {
try {
// IPackageFragmentRoot createPackageFragmentRoot =
// createPackageFragmentRoot(monitor);
// IPackageFragment createPackageFragment =
// createPackageFragmentRoot
// .createPackageFragment(
// Constants.ONPOSITIVE_SERVLETS_PACKAGE_NAME, true,
// monitor);
// createNecessaryServlets(createPackageFragment);
Bundle bundle = Activator.getDefault().getBundle();
String entryPath = "/lib";
IStatus copyLibraries = copyLibraries(monitor, bundle, entryPath,
false);
if (!copyLibraries.isOK()) {
return copyLibraries;
}
} catch (CoreException e) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
IStatus.ERROR, e.getMessage(), e);
} catch (FileNotFoundException e) {
WebRootDirProvider.showPropPage(getProject());
}
return Status.OK_STATUS;
}
protected IStatus copyLibraries(IProgressMonitor monitor, Bundle bundle,
String entryPath, boolean addToBuildPath) throws CoreException, FileNotFoundException {
Enumeration<?> entryPaths = bundle.getEntryPaths(entryPath);
IContainer webFolder = GaeBridge.getWebFolder(fCurrJProject
.getProject());
IFolder folder = ((IFolder) webFolder.findMember("WEB-INF"))
.getFolder("lib");
while (entryPaths != null && entryPaths.hasMoreElements()) {
String path = (String) entryPaths.nextElement();
if (path.endsWith("/.svn/")) {
continue;
}
URL entry = bundle.getEntry(path);
String name = new File(path).getName();
IFile file = folder.getFile(name);
if (!file.exists()) {
try {
file.create(entry.openStream(), true, monitor);
IPackageFragmentRoot packageFragmentRoot = fCurrJProject
.getPackageFragmentRoot(file);
if (!packageFragmentRoot.exists()) {
IClasspathEntry newEntry = JavaCore.newLibraryEntry(
file.getFullPath(), null, null);
IClasspathEntry[] rawClasspath = fCurrJProject
.getRawClasspath();
ArrayList<IClasspathEntry> arrrayList = new ArrayList<IClasspathEntry>(
Arrays.asList(rawClasspath));
arrrayList.add(newEntry);
IPath fOutputLocation = fCurrJProject
.getOutputLocation();
fCurrJProject.setRawClasspath(
arrrayList
.toArray(new IClasspathEntry[arrrayList
.size()]), fOutputLocation,
new SubProgressMonitor(monitor, 2));
}
} catch (IOException e) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID,
IStatus.ERROR, e.getMessage(), e);
}
}
}
return Status.OK_STATUS;
}
protected boolean checkLibraries(Bundle bundle, String entryPath)
throws CoreException, FileNotFoundException {
Enumeration<?> entryPaths = bundle.getEntryPaths(entryPath);
IContainer webFolder = GaeBridge.getWebFolder(fCurrJProject
.getProject());
IFolder folder = ((IFolder) webFolder.findMember("WEB-INF"))
.getFolder("lib");
while (entryPaths != null && entryPaths.hasMoreElements()) {
String path = (String) entryPaths.nextElement();
String name = new File(path).getName();
IFile file = folder.getFile(name);
if (file.getName().equals(".svn")) {
continue;
}
if (!file.exists()) {
return false;
}
}
return true;
}
protected void createNecessaryServlets(
IPackageFragment createPackageFragment) {
}
// public IPackageFragmentRoot createPackageFragmentRoot(
// IProgressMonitor monitor) throws CoreException,
// InterruptedException {
// // if (monitor == null) {
// // monitor = new NullProgressMonitor();
// // }
// // try {
// //
// // IPath projPath = fCurrJProject.getProject().getFullPath();
// // IPath fOutputLocation = fCurrJProject.getOutputLocation();
// // IPath fNewOutputLocation = fOutputLocation;
// // if (fOutputLocation.equals(projPath)
// // && !fNewOutputLocation.equals(projPath)) {
// // // if
// // // (BuildPathsBlock.hasClassfiles(fCurrJProject.getProject())) {
// // // // if (BuildPathsBlock.getRemoveOldBinariesQuery(getShell())
// // // // .doQuery(false, projPath)) {
// // // // BuildPathsBlock.removeOldClassfiles(fCurrJProject
// // // // .getProject());
// // // // }
// // // }
// // }
// // String relPath = Constants.SOURCE_FOLDER_NAME;
// // IFolder folder = fCurrJProject.getProject().getFolder(relPath);
// // if (!folder.exists()) {
// // CoreUtility.createFolder(folder, true, true,
// // new SubProgressMonitor(monitor, 1));
// // }
// // IPackageFragmentRoot packageFragmentRoot = fCurrJProject
// // .getPackageFragmentRoot(folder);
// // if (packageFragmentRoot.exists()) {
// // return packageFragmentRoot;
// // }
// // if (monitor.isCanceled()) {
// // throw new InterruptedException();
// // }
// // IClasspathEntry newEntry = JavaCore.newSourceEntry(projPath
// // .append(relPath));
// //
// // IClasspathEntry[] rawClasspath = fCurrJProject.getRawClasspath();
// // ArrayList<IClasspathEntry> arrrayList = new
// ArrayList<IClasspathEntry>(
// // Arrays.asList(rawClasspath));
// // arrrayList.add(newEntry);
// // fCurrJProject.setRawClasspath(arrrayList
// // .toArray(new IClasspathEntry[arrrayList.size()]),
// // fNewOutputLocation, new SubProgressMonitor(monitor, 2));
// //
// // return packageFragmentRoot;
// // } finally {
// // monitor.done();
// // }
// }
public IProject getProject() {
return fCurrJProject.getProject();
}
}