001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso.wizards;
005:
006: import org.apache.commons.io.IOUtils;
007: import org.apache.xmlbeans.XmlOptions;
008: import org.eclipse.core.resources.IContainer;
009: import org.eclipse.core.resources.IFile;
010: import org.eclipse.core.resources.IFolder;
011: import org.eclipse.core.resources.IProject;
012: import org.eclipse.core.resources.IProjectDescription;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.jdt.core.IClasspathEntry;
020: import org.eclipse.jdt.core.ICompilationUnit;
021: import org.eclipse.jdt.core.IJavaProject;
022: import org.eclipse.jdt.core.IPackageFragment;
023: import org.eclipse.jdt.core.IPackageFragmentRoot;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.core.JavaModelException;
026: import org.eclipse.jdt.internal.ui.JavaUIStatus;
027: import org.eclipse.jface.operation.IRunnableWithProgress;
028: import org.eclipse.jface.window.ApplicationWindow;
029: import org.eclipse.jface.wizard.Wizard;
030: import org.eclipse.ui.IWorkbench;
031: import org.eclipse.ui.IWorkbenchWindow;
032: import org.eclipse.ui.PlatformUI;
033: import org.terracotta.dso.ProjectNature;
034: import org.terracotta.dso.TcPlugin;
035:
036: import java.io.InputStream;
037: import java.lang.reflect.InvocationTargetException;
038: import java.util.ArrayList;
039: import java.util.Arrays;
040: import java.util.List;
041:
042: public class ProjectWizard extends Wizard {
043: private SetupWizardPage m_page;
044: private IJavaProject m_javaProject;
045: private boolean m_cancelled;
046:
047: public ProjectWizard(IJavaProject javaProject) {
048: super ();
049:
050: m_javaProject = javaProject;
051: m_cancelled = false;
052:
053: setNeedsProgressMonitor(true);
054: }
055:
056: public boolean performCancel() {
057: m_cancelled = true;
058: return super .performCancel();
059: }
060:
061: public void addPages() {
062: addPage(m_page = new SetupWizardPage(m_javaProject));
063: }
064:
065: public IRunnableWithProgress getWorker() {
066: IRunnableWithProgress op = new IRunnableWithProgress() {
067: public void run(IProgressMonitor monitor)
068: throws InvocationTargetException {
069: try {
070: doFinish(monitor);
071: } catch (CoreException e) {
072: throw new InvocationTargetException(e);
073: } finally {
074: monitor.done();
075: }
076: }
077: };
078:
079: return op;
080: }
081:
082: public boolean performFinish() {
083: if (m_cancelled) {
084: return true;
085: }
086:
087: try {
088: getContainer().run(false, true, getWorker());
089: } catch (InterruptedException e) {
090: return false;
091: } catch (InvocationTargetException e) {
092: Throwable cause = e.getTargetException();
093: TcPlugin.getDefault().openError(
094: "Problem setting up project", cause);
095: return false;
096: }
097:
098: return true;
099: }
100:
101: private void handleProblem(String msg, Throwable t,
102: IProgressMonitor monitor) throws CoreException {
103: t.printStackTrace();
104: monitor.setCanceled(true);
105: if (!(t instanceof CoreException)) {
106: t = new CoreException(JavaUIStatus.createError(-1, msg, t));
107: }
108: throw (CoreException) t;
109: }
110:
111: public void doFinish(IProgressMonitor monitor) throws CoreException {
112: String step = "Adding Terracotta nature";
113:
114: monitor.beginTask(step, IProgressMonitor.UNKNOWN);
115: try {
116: addTerracottaNature(monitor);
117: } catch (Throwable t) {
118: handleProblem(step, t, monitor);
119: }
120: monitor.worked(1);
121:
122: monitor.subTask(step = "Creating Terracotta folder");
123: try {
124: createTerracottaFolder(monitor);
125: } catch (Throwable t) {
126: handleProblem(step, t, monitor);
127: }
128: monitor.worked(2);
129:
130: monitor.subTask(step = "Inspecting classes");
131: try {
132: inspectProject(monitor);
133: } catch (Throwable t) {
134: handleProblem(step, t, monitor);
135: }
136: monitor.worked(3);
137:
138: TcPlugin.getDefault().updateDecorators();
139: TcPlugin.getDefault().notifyProjectActions(
140: m_javaProject.getProject());
141:
142: final IWorkbench workbench = PlatformUI.getWorkbench();
143: final IWorkbenchWindow window = workbench
144: .getActiveWorkbenchWindow();
145:
146: if (window instanceof ApplicationWindow) {
147: ApplicationWindow appWin = (ApplicationWindow) window;
148: String msg = "Finished adding Terracotta Nature.";
149:
150: appWin.setStatus(msg);
151: }
152: }
153:
154: private void addTerracottaNature(IProgressMonitor monitor)
155: throws CoreException {
156: IProject proj = m_javaProject.getProject();
157: IProjectDescription description = proj.getDescription();
158: String[] natures = description.getNatureIds();
159: String[] newNatures = new String[natures.length + 1];
160:
161: java.lang.System.arraycopy(natures, 0, newNatures, 0,
162: natures.length);
163:
164: newNatures[natures.length] = ProjectNature.NATURE_ID;
165: description.setNatureIds(newNatures);
166: proj.refreshLocal(IResource.DEPTH_ZERO, monitor);
167: proj.setDescription(description, monitor);
168: }
169:
170: private String getDomainConfigurationPath() {
171: if (m_page == null) {
172: return TcPlugin.DEFAULT_CONFIG_FILENAME;
173: } else {
174: return m_page.getDomainConfigurationPath();
175: }
176: }
177:
178: private String getServerOptions() {
179: if (m_page == null) {
180: return TcPlugin.DEFAULT_SERVER_OPTIONS;
181: } else {
182: return m_page.getServerOptions();
183: }
184: }
185:
186: private void createTerracottaFolder(IProgressMonitor monitor)
187: throws CoreException {
188: TcPlugin plugin = TcPlugin.getDefault();
189: IProject project = m_javaProject.getProject();
190: String configPath = getDomainConfigurationPath();
191: String serverOpts = getServerOptions();
192: IFolder folder = plugin
193: .ensureRuntimeDirectory(project, monitor);
194:
195: /**
196: * Make sure the terracotta artifact directory isn't considered
197: * a package fragment.
198: */
199: IPath relPath = folder.getProjectRelativePath();
200: IPath exclusion = relPath.addTrailingSeparator();
201: ArrayList<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
202: IClasspathEntry[] entries = m_javaProject.getRawClasspath();
203: IClasspathEntry entry;
204:
205: for (int i = 0; i < entries.length; i++) {
206: entry = entries[i];
207:
208: if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
209: && entry.getPath().equals(m_javaProject.getPath())) {
210: List<IPath> exclusions = new ArrayList<IPath>(Arrays
211: .asList(entry.getExclusionPatterns()));
212:
213: exclusions.add(exclusion);
214: entry = JavaCore.newSourceEntry(entry.getPath(), entry
215: .getInclusionPatterns(), exclusions
216: .toArray(new IPath[0]), entry
217: .getOutputLocation(), entry
218: .getExtraAttributes());
219: }
220: list.add(entry);
221: }
222:
223: entries = list.toArray(new IClasspathEntry[0]);
224: m_javaProject.setRawClasspath(entries, monitor);
225:
226: /**
227: * Ensure a config file exists.
228: */
229: if (configPath == null || configPath.length() == 0) {
230: configPath = TcPlugin.DEFAULT_CONFIG_FILENAME;
231: }
232: if (!configPath.endsWith(".xml")) {
233: configPath = configPath.concat(".xml");
234: }
235: final IFile configFile = project.getFile(new Path(configPath));
236: if (!configFile.exists()) {
237: InputStream is = null;
238:
239: ensureParent(configFile);
240: try {
241: XmlOptions xmlOpts = plugin.getXmlOptions();
242:
243: is = TcPlugin.createTemplateConfigDoc().newInputStream(
244: xmlOpts);
245: configFile.create(is, true, monitor);
246: } catch (CoreException ce) {
247: String step = "Creating default Terracotta config file";
248: IStatus status = JavaUIStatus.createError(-1, step, ce);
249:
250: IOUtils.closeQuietly(is);
251: throw new CoreException(status);
252: } finally {
253: IOUtils.closeQuietly(is);
254: }
255: }
256:
257: m_javaProject.getProject().refreshLocal(
258: IResource.DEPTH_INFINITE, monitor);
259: plugin.setup(project, configPath, serverOpts);
260: }
261:
262: private static void ensureParent(IFile file) throws CoreException {
263: if (!file.exists()) {
264: IContainer parent = file.getParent();
265:
266: if (!parent.exists()) {
267: ensureParent(parent);
268: }
269: }
270: }
271:
272: private static void ensureParent(IContainer container)
273: throws CoreException {
274: if (!container.exists()) {
275: IContainer parent = container.getParent();
276:
277: if (!parent.exists()) {
278: ensureParent(parent);
279: }
280:
281: if (container instanceof IFolder) {
282: ((IFolder) container).create(true, true, null);
283: }
284: }
285: }
286:
287: private void inspectProject(IProgressMonitor monitor)
288: throws JavaModelException, CoreException {
289: TcPlugin plugin = TcPlugin.getDefault();
290: IPackageFragment[] fragments = m_javaProject
291: .getPackageFragments();
292: IPackageFragment fragment;
293: ICompilationUnit[] cus;
294:
295: for (int i = 0; i < fragments.length; i++) {
296: fragment = fragments[i];
297:
298: if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
299: cus = fragment.getCompilationUnits();
300:
301: for (int j = 0; j < cus.length; j++) {
302: monitor.subTask(cus[j].getResource().getLocation()
303: .toString());
304: plugin.inspect(cus[j]);
305: }
306: }
307: }
308: }
309:
310: public boolean canFinish() {
311: return m_page.isPageComplete();
312: }
313: }
|