1 4 package org.terracotta.dso.wizards; 5 6 import org.apache.commons.io.IOUtils; 7 import org.apache.xmlbeans.XmlOptions; 8 import org.eclipse.core.resources.IContainer; 9 import org.eclipse.core.resources.IFile; 10 import org.eclipse.core.resources.IFolder; 11 import org.eclipse.core.resources.IProject; 12 import org.eclipse.core.resources.IProjectDescription; 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.jdt.core.IClasspathEntry; 20 import org.eclipse.jdt.core.ICompilationUnit; 21 import org.eclipse.jdt.core.IJavaProject; 22 import org.eclipse.jdt.core.IPackageFragment; 23 import org.eclipse.jdt.core.IPackageFragmentRoot; 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.jdt.internal.ui.JavaUIStatus; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.window.ApplicationWindow; 29 import org.eclipse.jface.wizard.Wizard; 30 import org.eclipse.ui.IWorkbench; 31 import org.eclipse.ui.IWorkbenchWindow; 32 import org.eclipse.ui.PlatformUI; 33 import org.terracotta.dso.ProjectNature; 34 import org.terracotta.dso.TcPlugin; 35 36 import com.terracottatech.config.Server; 37 import com.terracottatech.config.Servers; 38 import com.terracottatech.config.TcConfigDocument; 39 import com.terracottatech.config.TcConfigDocument.TcConfig; 40 41 import java.io.InputStream ; 42 import java.lang.reflect.InvocationTargetException ; 43 import java.util.ArrayList ; 44 import java.util.Arrays ; 45 import java.util.List ; 46 47 public class ProjectWizard extends Wizard { 48 private SetupWizardPage m_page; 49 private IJavaProject m_javaProject; 50 private boolean m_cancelled; 51 52 public ProjectWizard(IJavaProject javaProject) { 53 super(); 54 55 m_javaProject = javaProject; 56 m_cancelled = false; 57 58 setNeedsProgressMonitor(true); 59 } 60 61 public boolean performCancel() { 62 m_cancelled = true; 63 return super.performCancel(); 64 } 65 66 public void addPages() { 67 addPage(m_page = new SetupWizardPage(m_javaProject)); 68 } 69 70 public IRunnableWithProgress getWorker() { 71 IRunnableWithProgress op = new IRunnableWithProgress() { 72 public void run(IProgressMonitor monitor) 73 throws InvocationTargetException 74 { 75 try { 76 doFinish(monitor); 77 } catch(CoreException e) { 78 throw new InvocationTargetException (e); 79 } finally { 80 monitor.done(); 81 } 82 } 83 }; 84 85 return op; 86 } 87 88 public boolean performFinish() { 89 if(m_cancelled) { 90 return true; 91 } 92 93 try { 94 getContainer().run(false, true, getWorker()); 95 } catch(InterruptedException e) { 96 return false; 97 } catch(InvocationTargetException e) { 98 Throwable cause = e.getTargetException(); 99 TcPlugin.getDefault().openError("Problem setting up project", cause); 100 return false; 101 } 102 103 return true; 104 } 105 106 private void handleProblem(String msg, Throwable t, IProgressMonitor monitor) 107 throws CoreException 108 { 109 t.printStackTrace(); 110 monitor.setCanceled(true); 111 if(!(t instanceof CoreException)) { 112 t = new CoreException(JavaUIStatus.createError(-1, msg, t)); 113 } 114 throw (CoreException)t; 115 } 116 117 public void doFinish(IProgressMonitor monitor) 118 throws CoreException 119 { 120 String step = "Adding Terracotta nature"; 121 122 monitor.beginTask(step, IProgressMonitor.UNKNOWN); 123 try { 124 addTerracottaNature(monitor); 125 } catch(Throwable t) { 126 handleProblem(step, t, monitor); 127 } 128 monitor.worked(1); 129 130 monitor.subTask(step = "Creating Terracotta folder"); 131 try { 132 createTerracottaFolder(monitor); 133 } catch(Throwable t) { 134 handleProblem(step, t, monitor); 135 } 136 monitor.worked(2); 137 138 monitor.subTask(step = "Inspecting classes"); 139 try { 140 inspectProject(monitor); 141 } catch(Throwable t) { 142 handleProblem(step, t, monitor); 143 } 144 monitor.worked(3); 145 146 TcPlugin.getDefault().updateDecorators(); 147 TcPlugin.getDefault().notifyProjectActions(m_javaProject.getProject()); 148 149 final IWorkbench workbench = PlatformUI.getWorkbench(); 150 final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 151 152 if(window instanceof ApplicationWindow) { 153 ApplicationWindow appWin = (ApplicationWindow)window; 154 String msg = "Finished adding Terracotta Nature."; 155 156 appWin.setStatus(msg); 157 } 158 } 159 160 private void addTerracottaNature(IProgressMonitor monitor) 161 throws CoreException 162 { 163 IProject proj = m_javaProject.getProject(); 164 IProjectDescription description = proj.getDescription(); 165 String [] natures = description.getNatureIds(); 166 String [] newNatures = new String [natures.length + 1]; 167 168 java.lang.System.arraycopy(natures, 0, newNatures, 0, natures.length); 169 170 newNatures[natures.length] = ProjectNature.NATURE_ID; 171 description.setNatureIds(newNatures); 172 proj.refreshLocal(IResource.DEPTH_ZERO, monitor); 173 proj.setDescription(description, monitor); 174 } 175 176 private String getDomainConfigurationPath() { 177 if(m_page == null) { 178 return TcPlugin.DEFAULT_CONFIG_FILENAME; 179 } else { 180 return m_page.getDomainConfigurationPath(); 181 } 182 } 183 184 private String getServerOptions() { 185 if(m_page == null) { 186 return TcPlugin.DEFAULT_SERVER_OPTIONS; 187 } else { 188 return m_page.getServerOptions(); 189 } 190 } 191 192 private void createTerracottaFolder(IProgressMonitor monitor) 193 throws CoreException 194 { 195 TcPlugin plugin = TcPlugin.getDefault(); 196 IProject proj = m_javaProject.getProject(); 197 String configPath = getDomainConfigurationPath(); 198 String serverOpts = getServerOptions(); 199 IFolder folder = proj.getFolder("terracotta"); 200 201 if(!folder.exists()) { 202 folder.create(true, true, monitor); 203 } 204 205 209 IPath relPath = folder.getProjectRelativePath(); 210 IPath exclusion = relPath.addTrailingSeparator(); 211 ArrayList <IClasspathEntry> list = new ArrayList <IClasspathEntry>(); 212 IClasspathEntry[] entries = m_javaProject.getRawClasspath(); 213 IClasspathEntry entry; 214 215 for(int i = 0; i < entries.length; i++) { 216 entry = entries[i]; 217 218 if(entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && 219 entry.getPath().equals(m_javaProject.getPath())) 220 { 221 List <IPath> exclusions = new ArrayList <IPath>(Arrays.asList(entry.getExclusionPatterns())); 222 223 exclusions.add(exclusion); 224 entry = JavaCore.newSourceEntry(entry.getPath(), 225 entry.getInclusionPatterns(), 226 exclusions.toArray(new IPath[0]), 227 entry.getOutputLocation(), 228 entry.getExtraAttributes()); 229 } 230 list.add(entry); 231 } 232 233 entries = list.toArray(new IClasspathEntry[0]); 234 m_javaProject.setRawClasspath(entries, monitor); 235 236 239 if(configPath == null || configPath.length() == 0) { 240 configPath = TcPlugin.DEFAULT_CONFIG_FILENAME; 241 } 242 if(!configPath.endsWith(".xml")) { 243 configPath = configPath.concat(".xml"); 244 } 245 final IFile configFile = proj.getFile(new Path(configPath)); 246 if(!configFile.exists()) { 247 InputStream is = null; 248 249 ensureParent(configFile); 250 try { 251 XmlOptions xmlOpts = plugin.getXmlOptions(); 252 253 is = createTemplateConfigDoc().newInputStream(xmlOpts); 254 configFile.create(is, true, monitor); 255 } catch(CoreException ce) { 256 String step = "Creating default Terracotta config file"; 257 IStatus status = JavaUIStatus.createError(-1, step, ce); 258 259 IOUtils.closeQuietly(is); 260 throw new CoreException(status); 261 } finally { 262 IOUtils.closeQuietly(is); 263 } 264 } 265 266 m_javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor); 267 plugin.setup(proj, configPath, serverOpts); 268 } 269 270 private static void ensureParent(IFile file) throws CoreException { 271 if(!file.exists()) { 272 IContainer parent = file.getParent(); 273 274 if(!parent.exists()) { 275 ensureParent(parent); 276 } 277 } 278 } 279 280 private static void ensureParent(IContainer container) throws CoreException { 281 if(!container.exists()) { 282 IContainer parent = container.getParent(); 283 284 if(!parent.exists()) { 285 ensureParent(parent); 286 } 287 288 if(container instanceof IFolder) { 289 ((IFolder)container).create(true, true, null); 290 } 291 } 292 } 293 294 private void inspectProject(IProgressMonitor monitor) 295 throws JavaModelException, 296 CoreException 297 { 298 TcPlugin plugin = TcPlugin.getDefault(); 299 IPackageFragment[] fragments = m_javaProject.getPackageFragments(); 300 IPackageFragment fragment; 301 ICompilationUnit[] cus; 302 303 for(int i = 0; i < fragments.length; i++) { 304 fragment = fragments[i]; 305 306 if(fragment.getKind() == IPackageFragmentRoot.K_SOURCE) { 307 cus = fragment.getCompilationUnits(); 308 309 for(int j = 0; j < cus.length; j++) { 310 monitor.subTask(cus[j].getResource().getLocation().toString()); 311 plugin.inspect(cus[j]); 312 } 313 } 314 } 315 } 316 317 public static TcConfigDocument createTemplateConfigDoc() { 318 TcConfigDocument doc = TcConfigDocument.Factory.newInstance(); 319 TcConfig config = doc.addNewTcConfig(); 320 Servers servers = config.addNewServers(); 321 Server server = servers.addNewServer(); 322 323 server.setHost("%i"); 324 server.setName("localhost"); 325 server.setDsoPort(9510); 326 server.setJmxPort(9520); 327 server.setData("terracotta/server-data"); 328 server.setLogs("terracotta/server-logs"); 329 330 config.addNewClients().setLogs("terracotta/client-logs"); 331 332 return doc; 333 } 334 335 public boolean canFinish() { 336 return m_page.isPageComplete(); 337 } 338 } 339 | Popular Tags |