001: package com.salmonllc.ideTools.eclipse;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.io.PrintWriter;
008: import java.lang.reflect.InvocationTargetException;
009: import java.util.Hashtable;
010: import java.util.Properties;
011:
012: import org.eclipse.core.resources.IFolder;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.resources.IWorkspaceRoot;
016: import org.eclipse.core.resources.ResourcesPlugin;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IExecutableExtension;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.core.runtime.NullProgressMonitor;
021: import org.eclipse.jdt.core.IJavaProject;
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.jdt.internal.ui.JavaPlugin;
024: import org.eclipse.jdt.ui.wizards.NewJavaProjectWizardPage;
025: import org.eclipse.jface.operation.IRunnableWithProgress;
026: import org.eclipse.swt.SWT;
027: import org.eclipse.swt.widgets.MessageBox;
028: import org.eclipse.swt.widgets.Shell;
029: import org.eclipse.ui.actions.WorkspaceModifyDelegatingOperation;
030:
031: import com.salmonllc.ideTools.IDETool;
032: import com.salmonllc.properties.Props;
033:
034: /**
035: * @author Administrator
036: *
037: * To change this generated comment edit the template variable "typecomment":
038: * Window>Preferences>Java>Templates.
039: * To enable and disable the creation of type comments go to
040: * Window>Preferences>Java>Code Generation.
041: */
042: public class SalmonExamplesCreationWizard extends
043: SalmonProjectCreationWizardBase implements IExecutableExtension {
044:
045: private SalmonWizardNewProjectCreationPage _mainPage;
046: private SalmonFrameworkCreationWizardPage _sofiaPage;
047: private SalmonFrameworkUpgradeWizardPage _upgradePage;
048: private NewJavaProjectWizardPage _javaPage;
049: private boolean _doUpgrade = false;
050: private IProject _currentProject;
051:
052: /**
053: * Constructor for SalmonProjectCreationWizard.
054: */
055: public SalmonExamplesCreationWizard() {
056: super ();
057: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
058: IProject[] p = root.getProjects();
059: _doUpgrade = false;
060: try {
061: for (int i = 0; i < p.length; i++) {
062: if (p[i].isOpen()) {
063: if (p[i]
064: .hasNature(SalmonPlugin.NATURE_SOFIA_PROJECT)) {
065: IFolder fol = p[i].getFolder("Jsp");
066: if (fol.exists()) {
067: fol = fol.getFolder("example1");
068: if (fol.exists()) {
069: if (fol.getFile("NewsList.jsp")
070: .exists()) {
071: _currentProject = p[i];
072: _doUpgrade = true;
073: break;
074: }
075: }
076: }
077: }
078: }
079: }
080: } catch (Exception e) {
081: }
082: }
083:
084: /**
085: * @see org.eclipse.jface.wizard.IWizard#performFinish()
086: */
087: public boolean performFinish() {
088: IRunnableWithProgress op1 = new WorkspaceModifyDelegatingOperation(
089: _javaPage.getRunnable());
090: IRunnableWithProgress op2 = new WorkspaceModifyDelegatingOperation(
091: getRunnable());
092: try {
093: if (!_doUpgrade)
094: getContainer().run(false, true, op1);
095: getContainer().run(false, true, op2);
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099:
100: return true;
101: }
102:
103: private void copyJavaCode(IFolder outFolder) {
104: try {
105: File inFile = new File(_sofiaPage.getSOFIAHome()
106: + File.separator + "SourceCode" + File.separator
107: + "SofiaExamples");
108: if (!inFile.exists())
109: inFile = new File(_sofiaPage.getSOFIAHome()
110: + File.separator + "SourceCode"
111: + File.separator + "Examples" + File.separator
112: + "Java");
113: File outFile = new File(outFolder.getLocation()
114: .toOSString());
115: IDETool.copyDirectory(inFile, outFile);
116: } catch (Exception e) {
117: e.printStackTrace();
118: }
119: }
120:
121: private void copyJSPCode(IFolder outFolder) {
122: try {
123: File inFile = new File(_sofiaPage.getTomcatHome()
124: + File.separator + "webapps" + File.separator
125: + "sofiaExamples" + File.separator + "Jsp");
126: File outFile = new File(outFolder.getLocation()
127: .toOSString());
128: IDETool.copyDirectory(inFile, outFile);
129: } catch (Exception e) {
130: e.printStackTrace();
131: }
132: }
133:
134: private void copyDocumentation(IFolder outFolder) {
135: try {
136: File inFile = new File(_sofiaPage.getTomcatHome()
137: + File.separator + "webapps" + File.separator
138: + "sofiaExamples" + File.separator
139: + "documentation");
140: File outFile = new File(outFolder.getLocation()
141: .toOSString());
142: IDETool.copyDirectory(inFile, outFile);
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146: }
147:
148: private void copyWEBINFFiles(IFolder outFolder) {
149: try {
150: File deleteFolder = new File(outFolder.getLocation()
151: .toOSString()
152: + File.separator + "properties");
153: boolean deleteFolderExists = deleteFolder.exists();
154: File inFile = new File(_sofiaPage.getTomcatHome()
155: + File.separator + "webapps" + File.separator
156: + "sofiaExamples" + File.separator + "WEB-INF");
157: File outFile = new File(outFolder.getLocation()
158: .toOSString());
159: IDETool.copyDirectory(inFile, outFile);
160:
161: if (deleteFolder.exists()) {
162: deleteFile(deleteFolder);
163: if (deleteFolderExists)
164: deleteFolder.mkdir();
165: }
166:
167: } catch (Exception e) {
168: e.printStackTrace();
169: }
170: }
171:
172: private void copyLanguageFiles(String dest) {
173: String source = SalmonPlugin.getSourceSystemProperties(
174: _sofiaPage.getTomcatHome(), true, true)
175: + File.separator;
176: dest += File.separator;
177:
178: try {
179: File copyTo = new File(dest + "en.properties");
180: if (!copyTo.exists())
181: IDETool.copyFile(new File(source + "en.properties"),
182: copyTo);
183:
184: copyTo = new File(dest + "en-us.properties");
185: if (!copyTo.exists())
186: IDETool.copyFile(new File(source + "en-us.properties"),
187: copyTo);
188:
189: copyTo = new File(dest + "es.properties");
190: if (!copyTo.exists())
191: IDETool.copyFile(new File(source + "es.properties"),
192: copyTo);
193:
194: copyTo = new File(dest + "tr.properties");
195: if (!copyTo.exists())
196: IDETool.copyFile(new File(source + "tr.properties"),
197: copyTo);
198: } catch (Exception e) {
199: e.printStackTrace();
200: }
201: }
202:
203: private void copySkinFiles(String dest) {
204: String source = SalmonPlugin.getSourceSystemProperties(
205: _sofiaPage.getTomcatHome(), true, true)
206: + File.separator;
207: dest += File.separator;
208: try {
209: File f[] = new File(source).listFiles();
210: for (int i = 0; i < f.length; i++) {
211: if (f[i].toString().endsWith(".skin")) {
212: File out = new File(dest + f[i].getName());
213: if (!out.exists())
214: IDETool.copyFile(f[i], out);
215: }
216: }
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: }
221:
222: private void copySiteMap(String dest, IProject project) {
223: try {
224: String path = SalmonPlugin.getSourceSystemProperties(
225: _sofiaPage.getTomcatHome(), true, true)
226: + File.separator;
227: File sourceFile = new File(path + "sofiaExamples.map");
228: String name = project.getDescription().getName();
229: if (!sourceFile.exists()) {
230: sourceFile = new File(path + "System.map");
231: name = "System";
232: }
233: File destFile = new File(dest += File.separator + name
234: + ".map");
235: IDETool.copyFile(sourceFile, destFile);
236: } catch (Exception e) {
237: e.printStackTrace();
238: }
239: }
240:
241: private void createProjectPropertyFile(IProject project,
242: IFolder webFolder, IFolder jspFolder, IFolder javaFolder,
243: String propsPath, String propsName, boolean full, Props pr) {
244: try {
245: String path = SalmonPlugin.getSourceSystemProperties(
246: _sofiaPage.getTomcatHome(), true, true)
247: + File.separator;
248: File f = new File(path + "sofiaExamples.properties");
249: if (!f.exists())
250: f = new File(path + "System.properties");
251:
252: FileInputStream fin = new FileInputStream(f);
253: Properties examplesPr = new Properties();
254: examplesPr.load(fin);
255: fin.close();
256: String defProfile = examplesPr
257: .getProperty(Props.DB_DEFAULT)
258: + ".";
259:
260: String resourcePath = pr
261: .getProperty(Props.IDE_FRAMEWORK_RESOURCES_PATH);
262: f = new File(resourcePath
263: + File.separator
264: + "salmonprops"
265: + File.separator
266: + (full ? "eclipseFull.properties"
267: : "eclipse.properties"));
268: String text = IDETool.readFile(f);
269:
270: text = replace(text, "$PROJECTNAME$", project
271: .getDescription().getName());
272: text = replace(text, "$DRIVER$", examplesPr
273: .getProperty(defProfile + Props.DB_DRIVER));
274: text = replace(text, "$DBUSER$", examplesPr
275: .getProperty(defProfile + Props.DB_USER));
276: text = replace(text, "$DBPASSWORD$", examplesPr
277: .getProperty(defProfile + Props.DB_PASSWORD));
278: text = replace(text, "$CONNECTURL$", examplesPr
279: .getProperty(defProfile + Props.DB_URL));
280: text = replace(text, "$WEBINF$", doubleSlash(webFolder
281: .getLocation().toOSString()));
282:
283: text += "\r\n\r\n#--------------------------------------------------------------\r\n";
284: text += "#Directories for the source code\r\n";
285: text += "#--------------------------------------------------------------\r\n";
286: text += "JSPSource="
287: + doubleSlash(jspFolder.getLocation().toOSString())
288: + "\r\n";
289: text += "JavaSource="
290: + doubleSlash(javaFolder.getLocation().toOSString())
291: + "\r\n";
292:
293: f = new File(propsPath + File.separatorChar + propsName
294: + ".properties");
295: PrintWriter pw = new PrintWriter(new FileOutputStream(f));
296: pw.print(text);
297: pw.close();
298: } catch (Exception e) {
299: e.printStackTrace();
300: }
301:
302: }
303:
304: public void addPages() {
305: if (isInError())
306: return;
307:
308: super .addPages();
309:
310: _mainPage = new SalmonWizardNewProjectCreationPage("Page 1",
311: "sofiaExamplesEclipse",
312: "Install a new examples project for the Salmon Open Framework");
313: _mainPage.setTitle("Install SOFIA Examples Project");
314: _upgradePage = new SalmonFrameworkUpgradeWizardPage(
315: "Page1",
316: "A SOFIA examples Project already exists in your workspace. Would you like to do an upgrade?");
317: _upgradePage.setTitle("Upgrade SOFIA Examples Project");
318:
319: if (!_doUpgrade)
320: addPage(_mainPage);
321: else
322: addPage(_upgradePage);
323:
324: _sofiaPage = new SalmonFrameworkCreationWizardPage("Page 2",
325: false);
326: _sofiaPage.setTitle("SOFIA Example Project Source Files");
327: _sofiaPage
328: .setDescription("Please enter the location of SOFIA Examples Source Files");
329: addPage(_sofiaPage);
330:
331: IWorkspaceRoot root = JavaPlugin.getWorkspace().getRoot();
332: _javaPage = new NewJavaProjectWizardPage(root, _mainPage);
333: }
334:
335: public IRunnableWithProgress getRunnable() {
336: return new IRunnableWithProgress() {
337: public void run(IProgressMonitor monitor)
338: throws InvocationTargetException,
339: InterruptedException {
340: if (monitor == null)
341: monitor = new NullProgressMonitor();
342: monitor.beginTask("Building Example Project", 10);
343: try {
344: if (!_doUpgrade) {
345: IJavaProject javaProj = _javaPage
346: .getNewJavaProject();
347: IProject proj = javaProj.getProject();
348: Props pr = SalmonPlugin.getSystemProps();
349: boolean propsInWebApp = pr.getBooleanProperty(
350: Props.IDE_USE_APP_PROPERTIES, false);
351:
352: //First clear out the old source entries. There is a blank one that causes a lot of trouble
353: clearSourceEntries(javaProj);
354:
355: //Create a JSP folder for the JSP
356: IFolder jspFolder = createFolder(proj, "Jsp");
357:
358: //Create a folder for the documentation
359: IFolder docsFolder = createFolder(proj,
360: "documentation");
361:
362: //Create a WEB-INF folder
363: IFolder webInf = createFolder(proj, "WEB-INF");
364: createFolder(webInf, "XML");
365: IFolder propsFolder = null;
366: if (propsInWebApp)
367: propsFolder = createFolder(webInf,
368: "properties");
369:
370: //Create a bin folder for the output
371: IFolder binFolder = createFolder(proj, "bin");
372: javaProj.setOutputLocation(binFolder
373: .getFullPath(), null);
374:
375: //Create a Java folder for the Java source
376: IFolder javaFolder = createFolder(proj, "Java");
377: addSourceEntry(javaProj, javaFolder);
378:
379: //Add the Salmon Project Nature to the project and add the Salmon project to the classpath
380: addNatureEntry(proj);
381: addReferencedProjectToClasspath(proj, javaProj);
382:
383: //Add the proper jars for the server type to the classpath
384: addServerJarsToClassPath(javaProj, pr);
385:
386: //Copy the WEB-INF files from the resources directory
387: copyWEBINFFiles(webInf);
388:
389: //Add stuff to the salmon System.properties file for this project
390: updateSystemProps(proj, binFolder, javaFolder,
391: jspFolder, propsFolder, null, pr,
392: "HomePage.jsp");
393:
394: //Import the source code
395: copyJavaCode(javaFolder);
396:
397: //Refresh the project to get all the copied files
398: proj.refreshLocal(IResource.DEPTH_INFINITE,
399: monitor);
400:
401: //Import the Jsp files
402: copyJSPCode(jspFolder);
403:
404: //Import the documentation
405: copyDocumentation(docsFolder);
406:
407: //Import and modify the project properties file
408: String projectPropertyPath = SalmonPlugin
409: .getSalmonPropsPath();
410: String projectPropsName = proj.getDescription()
411: .getName();
412: boolean full = false;
413: if (propsInWebApp) {
414: projectPropertyPath = propsFolder
415: .getLocation().toOSString();
416: projectPropsName = "System";
417: full = true;
418: }
419: createProjectPropertyFile(proj, webInf,
420: jspFolder, javaFolder,
421: projectPropertyPath, projectPropsName,
422: full, pr);
423:
424: //Import the language properties files
425: copyLanguageFiles(projectPropertyPath);
426:
427: //Import the skin files files
428: copySkinFiles(projectPropertyPath);
429:
430: //Import the site map
431: copySiteMap(projectPropertyPath, proj);
432:
433: //Create a dreamweaver project
434: createDreamweaverProject(proj, pr);
435:
436: //Make this the default project
437: SalmonPlugin
438: .getDefault()
439: .getPreferenceStore()
440: .setValue(
441: SalmonPlugin.PREF_DEFAULT_PROJECT,
442: proj.getDescription().getName());
443:
444: //Refresh the project to get all the copied files
445: proj.refreshLocal(IResource.DEPTH_INFINITE,
446: monitor);
447:
448: //Set up the configuration file for Weblogic or tomcat
449: String type = SalmonPlugin.getServerType();
450: if (type != null) {
451: if (type == IDETool.SERVER_TOMCAT40
452: || type == IDETool.SERVER_TOMCAT41)
453: setupTomcatConfigFile(
454: pr
455: .getProperty(Props.IDE_SERVER_CONFIG_FILE),
456: proj);
457: else if (type
458: .equals(IDETool.SERVER_WEBLOGIC6))
459: setupWeblogicConfigFile(
460: pr
461: .getProperty(Props.IDE_SERVER_CONFIG_FILE),
462: proj);
463: }
464:
465: //update the perspective
466: updatePerspective();
467: } else {
468: IJavaProject javaProj = JavaCore
469: .create(_currentProject);
470: IProject proj = _currentProject;
471: Props pr = SalmonPlugin.getSystemProps();
472: boolean propsInWebApp = pr.getBooleanProperty(
473: Props.IDE_USE_APP_PROPERTIES, false);
474:
475: //Get and clear the JSP folder
476: IFolder jspFolder = proj.getFolder("Jsp");
477: deleteAllItemsInFolder(jspFolder);
478:
479: //Get and clear the documentation folder
480: IFolder docsFolder = proj
481: .getFolder("documentation");
482: deleteAllItemsInFolder(docsFolder);
483:
484: //Get and clear the WEB-INF folder
485: IFolder webInf = proj.getFolder("WEB-INF");
486: deleteAllItemsInFolder(webInf);
487:
488: //Create a properties folder if necessary
489: proj.refreshLocal(IResource.DEPTH_INFINITE,
490: monitor);
491: IFolder propsFolder = null;
492: if (propsInWebApp)
493: propsFolder = createFolder(webInf,
494: "properties");
495:
496: //Get and clear the Java Folder
497: IFolder javaFolder = proj.getFolder("Java");
498: deleteAllItemsInFolder(javaFolder);
499:
500: //Copy the WEB-INF files from the resources directory
501: copyWEBINFFiles(webInf);
502:
503: //Import the source code
504: copyJavaCode(javaFolder);
505:
506: //Refresh the project to get all the copied files
507: proj.refreshLocal(IResource.DEPTH_INFINITE,
508: monitor);
509:
510: //Import the Jsp files
511: copyJSPCode(jspFolder);
512:
513: //Import the documentation
514: copyDocumentation(docsFolder);
515:
516: String propsPath = SalmonPlugin
517: .getSalmonPropsPath();
518: if (propsInWebApp)
519: propsPath = propsFolder.getLocation()
520: .toOSString();
521:
522: //Import the language properties files
523: copyLanguageFiles(propsPath);
524:
525: //Import the skin files files
526: copySkinFiles(propsPath);
527:
528: //Import the site map
529: copySiteMap(propsPath, proj);
530:
531: //System.properties file for the application
532: if (propsInWebApp) {
533: String projectPropertyPath = propsFolder
534: .getLocation().toOSString();
535: String projectPropsName = "System";
536: createProjectPropertyFile(proj, webInf,
537: jspFolder, javaFolder,
538: projectPropertyPath,
539: projectPropsName, true, pr);
540: updateSystemProperties(propsFolder, proj);
541: }
542:
543: //Make this the default project
544: SalmonPlugin
545: .getDefault()
546: .getPreferenceStore()
547: .setValue(
548: SalmonPlugin.PREF_DEFAULT_PROJECT,
549: proj.getDescription().getName());
550:
551: //Refresh the project to get all the copied files
552: proj.refreshLocal(IResource.DEPTH_INFINITE,
553: monitor);
554:
555: }
556: } catch (Exception e) {
557: MessageBox box = new MessageBox(new Shell(),
558: SWT.ICON_ERROR | SWT.OK | SWT.SYSTEM_MODAL);
559: box.setText("Error Creating Project.");
560: box.setMessage(e.getMessage());
561: box.open();
562: throw new InvocationTargetException(e);
563: } finally {
564: monitor.done();
565: }
566: }
567:
568: };
569: }
570:
571: private void updateSystemProperties(IFolder propsFolder,
572: IProject proj) throws IOException, CoreException {
573: String projName = proj.getDescription().getName();
574: Hashtable tab = new Hashtable();
575: tab.put(projName + "." + Props.IDE_APP_PROPS_PATH, propsFolder
576: .getLocation().toOSString());
577: String propsPath = SalmonPlugin.getSalmonPropsPath();
578: Props.updateSystemProperties(propsPath, tab);
579: }
580:
581: /* (non-Javadoc)
582: * @see org.eclipse.jdt.internal.ui.wizards.NewElementWizard#finishPage(org.eclipse.core.runtime.IProgressMonitor)
583: */
584: protected void finishPage(IProgressMonitor monitor)
585: throws InterruptedException, CoreException {
586:
587: }
588:
589: }
|