001: package com.salmonllc.ideTools.eclipse;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.PrintWriter;
006: import java.util.Hashtable;
007: import java.util.Vector;
008:
009: import org.eclipse.core.resources.IFolder;
010: import org.eclipse.core.resources.IProject;
011: import org.eclipse.core.resources.IProjectDescription;
012: import org.eclipse.core.resources.IWorkspaceRoot;
013: import org.eclipse.core.resources.ResourcesPlugin;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.core.runtime.Path;
018: import org.eclipse.jdt.core.IClasspathEntry;
019: import org.eclipse.jdt.core.IJavaProject;
020: import org.eclipse.jdt.core.JavaCore;
021: import org.eclipse.jdt.internal.ui.wizards.NewElementWizard;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.widgets.MessageBox;
025: import org.eclipse.swt.widgets.Shell;
026: import org.eclipse.ui.IWorkbench;
027: import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
028:
029: import com.salmonllc.ideTools.IDETool;
030: import com.salmonllc.properties.Props;
031:
032: /**
033: * @author Administrator
034: *
035: * To change this generated comment edit the template variable "typecomment":
036: * Window>Preferences>Java>Templates.
037: * To enable and disable the creation of type comments go to
038: * Window>Preferences>Java>Code Generation.
039: */
040: public abstract class SalmonProjectCreationWizardBase extends
041: NewElementWizard {
042:
043: private IConfigurationElement _configElement;
044: private boolean _inError = true;
045:
046: /**
047: * Constructor for SalmonProjectCreationWizardBase.
048: */
049: public SalmonProjectCreationWizardBase() {
050: super ();
051: }
052:
053: public void init(IWorkbench workbench,
054: IStructuredSelection selection) {
055: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
056: IProject[] p = root.getProjects();
057: _inError = true;
058: try {
059: for (int i = 0; i < p.length; i++) {
060: if (p[i].isOpen()) {
061: if (p[i]
062: .hasNature(SalmonPlugin.NATURE_SOFIA_FRAMEWORK))
063: _inError = false;
064: }
065: }
066:
067: if (_inError) {
068: MessageBox box = new MessageBox(new Shell(),
069: SWT.ICON_ERROR | SWT.OK | SWT.SYSTEM_MODAL);
070: box.setText("Salmon Framework Project Doesn't Exist.");
071: box
072: .setMessage("A Salmon Framework Project does not exist in your workspace. Please create one before creating a SOFIA project.");
073: box.open();
074: }
075: } catch (Exception e) {
076: }
077: }
078:
079: public void setInitializationData(IConfigurationElement cfig,
080: String propertyName, Object data) {
081: _configElement = cfig;
082: }
083:
084: protected boolean isInError() {
085: return _inError;
086: }
087:
088: protected IFolder createFolder(IFolder parent, String newFolder)
089: throws CoreException {
090: IFolder ret = parent.getFolder(newFolder);
091: ret.create(false, true, null);
092: return ret;
093: }
094:
095: protected IFolder createFolder(IProject parent, String newFolder)
096: throws CoreException {
097: IFolder ret = parent.getFolder(newFolder);
098: ret.create(false, true, null);
099: return ret;
100: }
101:
102: protected void clearSourceEntries(IJavaProject project)
103: throws CoreException {
104: IClasspathEntry[] entries = project.getRawClasspath();
105: Vector v = new Vector(entries.length);
106: for (int i = 0; i < entries.length; i++) {
107: if (entries[i].getEntryKind() != IClasspathEntry.CPE_SOURCE)
108: v.add(entries[i]);
109: }
110: IClasspathEntry[] newEntries = new IClasspathEntry[v.size()];
111: v.copyInto(newEntries);
112: project.setRawClasspath(newEntries, null);
113: }
114:
115: protected void addSourceEntry(IJavaProject project, IFolder f)
116: throws CoreException {
117: IClasspathEntry[] entries = project.getRawClasspath();
118: IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
119: System.arraycopy(entries, 0, newEntries, 0, entries.length);
120: newEntries[entries.length] = JavaCore.newSourceEntry(f
121: .getFullPath());
122: project.setRawClasspath(newEntries, null);
123: }
124:
125: protected void addNatureEntry(IProject project) {
126: try {
127: //Add a nature to this project
128: IProjectDescription description = project.getDescription();
129: String[] natures = description.getNatureIds();
130: String[] newNatures = new String[natures.length + 1];
131: System.arraycopy(natures, 0, newNatures, 0, natures.length);
132: newNatures[natures.length] = SalmonPlugin.NATURE_SOFIA_PROJECT;
133: description.setNatureIds(newNatures);
134:
135: //Add the framework project to the list of referenced projects
136: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
137: .getRoot();
138: Vector v = new Vector();
139: IProject[] projs = root.getProjects();
140: for (int i = 0; i < projs.length; i++) {
141: boolean open = projs[i].isOpen();
142: if (!open)
143: projs[i].open(null);
144: if (projs[i]
145: .hasNature(SalmonPlugin.NATURE_SOFIA_FRAMEWORK))
146: v.addElement(projs[i]);
147: else if (!open)
148: projs[i].close(null);
149:
150: }
151: if (v.size() > 0) {
152: projs = new IProject[v.size()];
153: v.toArray(projs);
154: description.setReferencedProjects(projs);
155: }
156: project.setDescription(description, null);
157: } catch (CoreException e) {
158: e.printStackTrace();
159: }
160: }
161:
162: protected void addReferencedProjectToClasspath(IProject proj,
163: IJavaProject jProj) throws CoreException {
164: IProject p[] = proj.getDescription().getReferencedProjects();
165:
166: if (p.length > 0) {
167: IClasspathEntry[] entries = jProj.getRawClasspath();
168: IClasspathEntry[] newEntries = new IClasspathEntry[entries.length
169: + p.length];
170: System.arraycopy(entries, 0, newEntries, 0, entries.length);
171: for (int i = entries.length; i < entries.length + p.length; i++) {
172: IPath path = p[i - entries.length].getFullPath();
173: newEntries[i] = JavaCore.newProjectEntry(path);
174: }
175: jProj.setRawClasspath(newEntries, null);
176: }
177: }
178:
179: protected void addServerJarsToClassPath(IJavaProject project,
180: Props p) {
181: try {
182: String serverType = p.getProperty(Props.IDE_SERVER_TYPE);
183: String dir = p.getProperty(Props.IDE_WORKING_DIRECTORY);
184: if (serverType == null || dir == null)
185: return;
186:
187: IPath path = null;
188:
189: int extraEntries = 2;
190: if (serverType.equals(IDETool.SERVER_WEBLOGIC6))
191: path = new Path(dir + File.separator + "lib"
192: + File.separator + "weblogic.jar");
193: else if (serverType.equals(IDETool.SERVER_TOMCAT50)) {
194: path = new Path(dir + File.separator + "common"
195: + File.separator + "lib" + File.separator
196: + "servlet-api.jar");
197: extraEntries = 3;
198: } else
199: path = new Path(dir + File.separator + "common"
200: + File.separator + "lib" + File.separator
201: + "servlet.jar");
202:
203: IClasspathEntry[] entries = project.getRawClasspath();
204: IClasspathEntry[] newEntries = new IClasspathEntry[entries.length
205: + extraEntries];
206:
207: System.arraycopy(entries, 0, newEntries, 0, entries.length);
208: newEntries[entries.length] = JavaCore.newLibraryEntry(path,
209: null, null);
210: newEntries[entries.length + 1] = JavaCore.newLibraryEntry(
211: new Path(dir + File.separator + "common"
212: + File.separator + "lib" + File.separator
213: + "portlet-api-1.0.jar"), null, null);
214: if (serverType.equals(IDETool.SERVER_TOMCAT50))
215: newEntries[entries.length + 2] = JavaCore
216: .newLibraryEntry(new Path(dir + File.separator
217: + "common" + File.separator + "lib"
218: + File.separator + "jsp-api.jar"),
219: null, null);
220:
221: project.setRawClasspath(newEntries, null);
222: } catch (Exception e) {
223: e.printStackTrace();
224: }
225: }
226:
227: protected void copyWEBINFFiles(IFolder webInfFolder, Props p) {
228: try {
229: File inFile = new File(p
230: .getProperty(Props.IDE_FRAMEWORK_RESOURCES_PATH)
231: + File.separator + "Tomcat");
232: File outFile = new File(webInfFolder.getLocation()
233: .toOSString());
234: IDETool.copyDirectory(inFile, outFile);
235: } catch (Exception e) {
236: e.printStackTrace();
237: }
238: }
239:
240: protected void copyHelloJSP(IFolder jspFolder, Props p) {
241: try {
242: File inFile = new File(p
243: .getProperty(Props.IDE_FRAMEWORK_RESOURCES_PATH)
244: + File.separator + "jsp");
245: File outFile = new File(jspFolder.getLocation()
246: .toOSString());
247: IDETool.copyDirectory(inFile, outFile);
248: } catch (Exception e) {
249: e.printStackTrace();
250: }
251: }
252:
253: protected void updateSystemProps(IProject proj, IFolder binFolder,
254: IFolder sourceFolder, IFolder jspFolder,
255: IFolder propsFolder, String jdbcJar, Props p,
256: String homePage) {
257: try {
258:
259: Hashtable tab = new Hashtable();
260:
261: //Add this project to the classpath
262: String classPath = p.getProperty(Props.IDE_CLASSPATH);
263: if (classPath == null)
264: classPath = "";
265: else if (!classPath.endsWith(File.pathSeparator))
266: classPath += File.pathSeparatorChar;
267: // classPath += binFolder.getLocation().toOSString() + File.pathSeparator;
268:
269: //Add the jdbc jar file to the class path if specified
270: if (jdbcJar != null && jdbcJar.trim().length() > 0) {
271: if (classPath.indexOf(jdbcJar) == -1) {
272: classPath += jdbcJar;
273: classPath += File.pathSeparator;
274: }
275: }
276: tab.put(Props.IDE_CLASSPATH, classPath);
277:
278: //Add the fields for this project
279: String projName = proj.getDescription().getName();
280: tab.put(projName + "." + Props.IDE_WEB_APP, projName);
281: tab.put(projName + "." + Props.IDE_DEFAULT_FRAMEWORK_APP,
282: projName);
283: tab.put(projName + "." + Props.IDE_DEFAULT_RUN_URL,
284: jspFolder.getLocation().toOSString()
285: + File.separator + homePage);
286: tab.put(projName + "." + Props.IDE_DEFAULT_SOURCE_PATH,
287: sourceFolder.getLocation().toOSString());
288: if (propsFolder != null)
289: tab.put(projName + "." + Props.IDE_APP_PROPS_PATH,
290: propsFolder.getLocation().toOSString());
291:
292: String propsPath = SalmonPlugin.getSalmonPropsPath();
293: Props.updateSystemProperties(propsPath, tab);
294: } catch (Exception e) {
295: e.printStackTrace();
296: }
297:
298: }
299:
300: protected void createDreamweaverProject(IProject proj, Props pr) {
301: try {
302: //this only works in Windows. On Other platforms you have to manually create the Dreameweaver project
303: if (!SalmonPlugin.isWindows())
304: return;
305:
306: String dreamweaverDir = pr
307: .getProperty(Props.IDE_DREAMWEAVER_PATH);
308: dreamweaverDir.replace('/', File.separatorChar);
309: int iSlashIndex = dreamweaverDir
310: .lastIndexOf(File.separator);
311: dreamweaverDir = dreamweaverDir.substring(0, iSlashIndex);
312: IDETool.createDreamweaverProject(pr
313: .getProperty(Props.IDE_FRAMEWORK_RESOURCES_PATH),
314: proj.getDescription().getName(), dreamweaverDir, pr
315: .getProperty(Props.IDE_DEFAULT_HOST), proj
316: .getLocation().toOSString());
317: } catch (Exception e) {
318: e.printStackTrace();
319: }
320: }
321:
322: protected void setupWeblogicConfigFile(String fileName,
323: IProject proj) {
324: try {
325: String name = proj.getDescription().getName();
326: IPath p = proj.getLocation();
327: p = p.removeLastSegments(1);
328: String path = p.toOSString();
329: File f = new File(fileName);
330: String s = IDETool.readFile(f);
331: f.renameTo(new File(fileName + ".bak"
332: + System.currentTimeMillis()));
333: int pos = s.indexOf("</Domain>");
334: if (pos == -1)
335: return;
336: String left = s.substring(0, pos);
337: String right = s.substring(pos);
338:
339: String insert = "\r\n <Application Deployed=\"true\" Name=\""
340: + name
341: + "\" Path=\""
342: + path
343: + "\">"
344: + "\r\n <WebAppComponent IndexDirectoryEnabled=\"true\" Name=\""
345: + name
346: + "\" Targets=\"myserver\" URI=\""
347: + name
348: + "\"/>\r\n </Application>\r\n";
349: s = left + insert + right;
350: PrintWriter pw = new PrintWriter(new FileOutputStream(
351: fileName));
352: pw.print(s);
353: pw.close();
354: } catch (Exception e) {
355: e.printStackTrace();
356: }
357: }
358:
359: protected void setupTomcatConfigFile(String fileName, IProject proj) {
360: try {
361: String name = proj.getDescription().getName();
362: IPath p = proj.getLocation();
363: String path = p.toOSString();
364: File f = new File(fileName);
365: String s = IDETool.readFile(f);
366: f.renameTo(new File(fileName + ".bak"
367: + System.currentTimeMillis()));
368: int pos = s.indexOf("</Host>");
369: if (pos == -1)
370: return;
371: String left = s.substring(0, pos);
372: String right = s.substring(pos);
373:
374: String insert = "\r\n <Context path=\"/" + name
375: + "\" docBase=\"" + path + "\" debug=\"0\"/>\r\n";
376: s = left + insert + right;
377: PrintWriter pw = new PrintWriter(new FileOutputStream(
378: fileName));
379: pw.print(s);
380: pw.close();
381: } catch (Exception e) {
382: e.printStackTrace();
383: }
384: }
385:
386: protected String replace(String sString, String sOldString,
387: String sNewString) {
388: StringBuffer sbString = new StringBuffer(sString);
389: while (true) {
390: int iIndex = sbString.toString().indexOf(sOldString);
391: if (iIndex < 0)
392: break;
393: sbString.replace(iIndex, iIndex + sOldString.length(),
394: sNewString);
395: }
396: return sbString.toString();
397: }
398:
399: protected String doubleSlash(String in) {
400: StringBuffer sb = new StringBuffer(in.length());
401: for (int i = 0; i < in.length(); i++) {
402: char c = in.charAt(i);
403: if (c == '\\')
404: sb.append("\\\\");
405: else
406: sb.append(c);
407: }
408: return sb.toString();
409: }
410:
411: protected void updatePerspective() {
412: BasicNewProjectResourceWizard.updatePerspective(_configElement);
413: }
414:
415: protected boolean projectExists(String projectName) {
416: IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
417: IProject[] p = root.getProjects();
418: try {
419: for (int i = 0; i < p.length; i++) {
420: if (p[i].getName().equals(projectName))
421: return true;
422: }
423: } catch (Exception e) {
424: }
425: return false;
426: }
427:
428: protected void deleteAllItemsInFolder(IFolder fol) {
429: File folder = new File(fol.getLocation().toOSString());
430: File list[] = folder.listFiles();
431: for (int i = 0; i < list.length; i++) {
432: deleteFile(list[i]);
433: }
434: }
435:
436: protected void deleteFile(File f) {
437: if (f.isDirectory()) {
438: File list[] = f.listFiles();
439: for (int i = 0; i < list.length; i++) {
440: deleteFile(list[i]);
441: }
442: }
443: f.delete();
444: }
445:
446: }
|