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.PrintWriter;
007: import java.util.Enumeration;
008: import java.util.MissingResourceException;
009: import java.util.Properties;
010: import java.util.ResourceBundle;
011: import java.util.StringTokenizer;
012: import java.util.Vector;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IFolder;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.IWorkspace;
019: import org.eclipse.core.resources.IWorkspaceRoot;
020: import org.eclipse.core.resources.ResourcesPlugin;
021: import org.eclipse.core.runtime.CoreException;
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.core.runtime.IPluginDescriptor;
024: import org.eclipse.debug.core.Launch;
025: import org.eclipse.jdt.core.ICodeFormatter;
026: import org.eclipse.jdt.core.ICompilationUnit;
027: import org.eclipse.jdt.core.IJavaProject;
028: import org.eclipse.jdt.core.IType;
029: import org.eclipse.jdt.core.JavaCore;
030: import org.eclipse.jdt.core.ToolFactory;
031: import org.eclipse.jface.preference.IPreferenceStore;
032: import org.eclipse.jface.viewers.ISelection;
033: import org.eclipse.jface.viewers.IStructuredSelection;
034: import org.eclipse.swt.SWT;
035: import org.eclipse.swt.widgets.MessageBox;
036: import org.eclipse.swt.widgets.Shell;
037: import org.eclipse.ui.plugin.AbstractUIPlugin;
038:
039: import com.salmonllc.ideTools.IDETool;
040: import com.salmonllc.properties.Props;
041:
042: /**
043: * The main plugin class to be used in the desktop.
044: */
045: public class SalmonPlugin extends AbstractUIPlugin {
046:
047: public static final String PREF_SALMON_PROPS_PATH = "com.salmonllc.eclipse.SalmonPropsPath";
048: public static final String PREF_SAVE_ON_BROWSER_RUN = "com.salmonllc.eclipse.SaveOnBrowserRun";
049: public static final String PREF_ASK_SAVE_ON_BROWSER_RUN = "com.salmonllc.eclipse.AskSaveOnBrowserRun";
050: public static final String PREF_RESTART_SERVER_EACH_RUN = "com.salmonllc.eclipse.RestartServerEachRun";
051: public static final String PREF_DEFAULT_PROJECT = "com.salmonllc.eclipse.DefaultSalmonProject";
052: public static final String PREF_CHANGE_DEFAULT_ON_SELECTION = "com.salmonllc.eclipse.ChangeDefaultSalmonProjectonSelectionChange";
053: public static final String PREF_DEBUG_LAUNCH = "com.salmonllc.eclipse.DebugLaunch";
054: public static final String PREF_AUTOFORMAT_GEN_CLASSES = "com.salmonllc.eclipse.AutoFormatGenClasses";
055:
056: public static final String NATURE_SOFIA_PROJECT = "com.salmonllc.IDETools.eclipse.SalmonProjectNature";
057: public static final String NATURE_SOFIA_FRAMEWORK = "com.salmonllc.IDETools.eclipse.SalmonFrameworkNature";
058:
059: //The shared instance.
060: private static SalmonPlugin plugin;
061: //Resource bundle.
062: private ResourceBundle resourceBundle;
063:
064: /**
065: * The constructor.
066: */
067: public SalmonPlugin(IPluginDescriptor descriptor) {
068: super (descriptor);
069: plugin = this ;
070: try {
071: resourceBundle = ResourceBundle
072: .getBundle("com.salmonllc.ideTools.eclipse.SalmonPluginResources");
073: } catch (MissingResourceException x) {
074: resourceBundle = null;
075: }
076: }
077:
078: /**
079: * Returns the shared instance.
080: */
081: public static SalmonPlugin getDefault() {
082: return plugin;
083: }
084:
085: /**
086: * Returns the workspace instance.
087: */
088: public static IWorkspace getWorkspace() {
089: return ResourcesPlugin.getWorkspace();
090: }
091:
092: /**
093: * Returns the string from the plugin's resource bundle,
094: * or 'key' if not found.
095: */
096: public static String getResourceString(String key) {
097: ResourceBundle bundle = SalmonPlugin.getDefault()
098: .getResourceBundle();
099: try {
100: return bundle.getString(key);
101: } catch (MissingResourceException e) {
102: return key;
103: }
104: }
105:
106: /**
107: * Returns the plugin's resource bundle,
108: */
109: public ResourceBundle getResourceBundle() {
110: return resourceBundle;
111: }
112:
113: public static String[] getClassPath(boolean includePlugInJar) {
114: try {
115: String path = getSystemProps().getProperty(
116: Props.IDE_CLASSPATH);
117: if (path == null)
118: path = "";
119: Vector v = new Vector();
120: if (includePlugInJar) {
121: String plugInJar = getSystemProps().getProperty(
122: Props.IDE_ECLIPSE_PLUGIN_JAR);
123: if (plugInJar != null)
124: v.addElement(plugInJar);
125: }
126: StringTokenizer t = new StringTokenizer(path,
127: java.io.File.pathSeparator);
128: while (t.hasMoreTokens())
129: v.addElement(t.nextToken());
130:
131: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
132: .getRoot();
133: IProject[] p = root.getProjects();
134: for (int i = 0; i < p.length; i++) {
135: if (p[i].isOpen()) {
136: if (p[i].hasNature(NATURE_SOFIA_FRAMEWORK)
137: || p[i].hasNature(NATURE_SOFIA_PROJECT)) {
138: IJavaProject proj = JavaCore.create(p[i]);
139: IPath loc = p[i].getLocation();
140: if (loc == null)
141: loc = root.getLocation();
142:
143: IPath outLoc = proj.getOutputLocation();
144: outLoc = outLoc.removeFirstSegments(1);
145: String entry = loc.toOSString()
146: + (loc.toOSString().endsWith(
147: File.separator) ? ""
148: : File.separator)
149: + outLoc.toOSString();
150:
151: if (path != null && entry != null
152: && path.indexOf(entry) == -1)
153: v.addElement(entry);
154: }
155: }
156: }
157:
158: String ret[] = new String[v.size()];
159: v.toArray(ret);
160:
161: return ret;
162: } catch (Exception e) {
163: e.printStackTrace();
164: return new String[0];
165: }
166: }
167:
168: public static String[] getVMArgs(Vector additionalArgs) {
169: String vaArgs = getSystemProps().getProperty(Props.IDE_VMARGS);
170: if (vaArgs == null)
171: vaArgs = "";
172: StringBuffer sb = new StringBuffer();
173: boolean quoteMode = false;
174: Vector v = new Vector();
175: for (int i = 0; i < vaArgs.length(); i++) {
176: char c = vaArgs.charAt(i);
177: if (c == '"')
178: quoteMode = !quoteMode;
179: else if (c == ' ' && !quoteMode) {
180: if (sb.length() > 0) {
181: v.add(removeQuotes(sb));
182: sb.setLength(0);
183: continue;
184: }
185: }
186: sb.append(c);
187: }
188:
189: if (sb.length() > 0)
190: v.addElement(removeQuotes(sb));
191:
192: if (additionalArgs != null) {
193: for (int i = 0; i < additionalArgs.size(); i++)
194: v.addElement(additionalArgs.elementAt(i));
195: }
196:
197: String ret[] = new String[v.size()];
198: v.toArray(ret);
199: return ret;
200: }
201:
202: private static String removeQuotes(StringBuffer sb) {
203: StringBuffer sb2 = new StringBuffer(sb.length());
204: for (int i = 0; i < sb.length(); i++) {
205: char c = sb.charAt(i);
206: if (c != '"')
207: sb2.append(c);
208: }
209: return sb2.toString().trim();
210: }
211:
212: public static String[] getBrowserTypes() {
213: IPreferenceStore pref = getDefault().getPreferenceStore();
214: String salmonPropPath = pref
215: .getString(SalmonPlugin.PREF_SALMON_PROPS_PATH);
216: if (salmonPropPath == null)
217: return new String[0];
218: Properties p = new Properties();
219: Vector v = new Vector();
220: try {
221: FileInputStream f = new FileInputStream(salmonPropPath
222: + File.separator + "System.properties");
223: p.load(f);
224: f.close();
225: } catch (Exception e) {
226: return new String[0];
227: }
228: Enumeration e = p.keys();
229: while (e.hasMoreElements()) {
230: String prop = (String) e.nextElement();
231: if (prop.startsWith("IDEBrowserPath."))
232: v.addElement(prop.substring(15));
233: }
234:
235: String ret[] = new String[v.size()];
236: v.toArray(ret);
237: return ret;
238:
239: }
240:
241: public static void runIDETool(String[] progArgs, boolean refresh) {
242: try {
243: //check for a framework project before running
244: if (!isFrameworkFound()) {
245: displayError(
246: "Error Launching SOFIA IDE Tool",
247: "SOFIA framework project not found in workspace. You can install the framework project by going to File->New->Project. If you have already installed the SOFIA project, make sure it's open.");
248: return;
249: }
250:
251: String project = getDefaultSalmonProject();
252: String progArgsPlus[] = null;
253: if (project != null) {
254: progArgsPlus = new String[progArgs.length + 3];
255: for (int i = 0; i < progArgs.length; i++)
256: progArgsPlus[i] = progArgs[i];
257: progArgsPlus[progArgs.length] = "-OSLOOKANDFEEL";
258: progArgsPlus[progArgs.length + 1] = "-PROJECT";
259: progArgsPlus[progArgs.length + 2] = project;
260: } else {
261: progArgsPlus = new String[progArgs.length + 1];
262: for (int i = 0; i < progArgs.length; i++)
263: progArgsPlus[i] = progArgs[i];
264: progArgsPlus[progArgs.length] = "-OSLOOKANDFEEL";
265: }
266:
267: String classPath[] = getClassPath(true);
268: Props p = getSystemProps();
269:
270: String workingDir = p
271: .getProperty(Props.IDE_WORKING_DIRECTORY);
272: String vmName = p.getProperty(Props.IDE_ECLIPSE_VM);
273:
274: Vector extraVmArgs = new Vector();
275: String salmonPropsPath = getSalmonPropsPath();
276: String classToLaunch = "";
277:
278: if (salmonPropsPath != null)
279: extraVmArgs.addElement("-Dsalmon.props.path="
280: + salmonPropsPath);
281: String libPath = p.getProperty(Props.IDE_LIBPATH);
282: if (libPath != null && !libPath.equals(""))
283: extraVmArgs
284: .addElement("-Djava.library.path=" + libPath);
285:
286: classToLaunch = "com.salmonllc.ideTools.IDETool";
287:
288: String vmArgs[] = getVMArgs(extraVmArgs);
289: LaunchStreamListener l = new LaunchStreamListener();
290: Launch launch = ProgramLauncher.runProgram(classToLaunch,
291: classPath, vmArgs, progArgsPlus, workingDir, false,
292: l, vmName);
293: if (refresh) {
294: while (!launch.isTerminated()) {
295: Thread.sleep(100);
296: Thread.yield();
297: }
298:
299: //reformat any class that was generated
300: String lastGeneratedClass = l.getLastGeneratedClass();
301: if (lastGeneratedClass != null) {
302: boolean formatCode = getDefault()
303: .getPluginPreferences()
304: .getBoolean(
305: SalmonPlugin.PREF_AUTOFORMAT_GEN_CLASSES);
306: if (formatCode) {
307: String unfmtSrc = IDETool.readFile(new File(
308: lastGeneratedClass));
309: ICodeFormatter formatter = ToolFactory
310: .createCodeFormatter();
311: String fmtSrc = formatter.format(unfmtSrc, 0,
312: null, "\n");
313: FileOutputStream out = new FileOutputStream(
314: lastGeneratedClass);
315: PrintWriter w = new PrintWriter(out);
316: w.print(fmtSrc);
317: w.close();
318: out.close();
319: }
320: }
321:
322: //refresh the workspace
323: IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
324: .getRoot();
325: IProject proj[] = root.getProjects();
326:
327: for (int i = 0; i < proj.length; i++)
328: if (proj[i].isOpen())
329: proj[i].refreshLocal(IResource.DEPTH_INFINITE,
330: null);
331: }
332: } catch (Exception e) {
333: e.printStackTrace();
334: }
335: }
336:
337: public static String getSelectedFile(ISelection selection,
338: String ext) {
339: String ret = null;
340: if (selection instanceof IStructuredSelection) {
341: IStructuredSelection sel = (IStructuredSelection) selection;
342: if (sel.size() == 1) {
343: Object o = sel.getFirstElement();
344: if (o instanceof IFile) {
345: String path = ((IFile) o).getFullPath()
346: .lastSegment();
347: if (path.endsWith(ext))
348: ret = ((IFile) o).getFullPath().toOSString();
349: }
350: }
351: }
352: return ret;
353: }
354:
355: public static String getSelectedFileRaw(ISelection selection,
356: String ext) {
357: String ret = null;
358: if (selection instanceof IStructuredSelection) {
359: IStructuredSelection sel = (IStructuredSelection) selection;
360: if (sel.size() == 1) {
361: Object o = sel.getFirstElement();
362: if (o instanceof IFile) {
363: String path = ((IFile) o).getFullPath()
364: .lastSegment();
365: if (path.endsWith(ext))
366: ret = ((IFile) o).getRawLocation().toOSString();
367: }
368: }
369: }
370: return ret;
371: }
372:
373: public static String getSelectedClass(ISelection selection) {
374: ICompilationUnit unit = null;
375: if (selection instanceof IStructuredSelection) {
376: IStructuredSelection sel = (IStructuredSelection) selection;
377: if (sel.size() == 1) {
378: Object o = sel.getFirstElement();
379: if (o instanceof ICompilationUnit)
380: unit = (ICompilationUnit) o;
381: else if (o instanceof IFile) {
382: String path = ((IFile) o).getFullPath()
383: .lastSegment();
384: if (path.endsWith("java"))
385: unit = JavaCore
386: .createCompilationUnitFrom((IFile) o);
387: }
388:
389: }
390: }
391: if (unit == null)
392: return null;
393: else {
394: try {
395: IType[] t = unit.getAllTypes();
396: String ret = t[0].getFullyQualifiedName();
397: return ret;
398: } catch (Exception e) {
399: return null;
400: }
401: }
402: }
403:
404: public static void setSelectedProject(ISelection selection) {
405: IPreferenceStore pref = getDefault().getPreferenceStore();
406: if (!pref.getBoolean(PREF_CHANGE_DEFAULT_ON_SELECTION))
407: return;
408:
409: if (selection instanceof IStructuredSelection) {
410: IStructuredSelection sel = (IStructuredSelection) selection;
411: IProject proj = null;
412: if (sel.size() > 0) {
413: Object o = sel.getFirstElement();
414: if (o instanceof IJavaProject)
415: proj = ((IJavaProject) o).getProject();
416: else if (o instanceof IProject)
417: proj = (IProject) o;
418: else if (o instanceof IFile)
419: proj = ((IFile) o).getProject();
420: else if (o instanceof IFolder)
421: proj = ((IFolder) o).getProject();
422: else if (o instanceof ICompilationUnit)
423: proj = ((ICompilationUnit) o).getJavaProject()
424: .getProject();
425: else if (o instanceof IResource)
426: proj = ((IResource) o).getProject();
427: }
428: if (proj != null) {
429: try {
430: if (proj.isOpen()
431: && proj.hasNature(NATURE_SOFIA_PROJECT))
432: pref.setValue(PREF_DEFAULT_PROJECT, proj
433: .getDescription().getName());
434: } catch (Exception e) {
435: }
436: }
437: }
438: }
439:
440: public static boolean arePropsInApplicationPaths() {
441: Props p = getSystemProps();
442: return p.getBooleanProperty(Props.IDE_USE_APP_PROPERTIES);
443: }
444:
445: public static boolean arePropsInSourceApplicationPaths(
446: String tomcatHome) {
447: //used for framework and sofiaExamples creation, test if the sofiaExamples application in Tomcat has a /WEB-INF/propertes folder
448: File inFile = new File(getAppSystemProperties(tomcatHome));
449: return inFile.exists();
450: }
451:
452: public static boolean arePropsInSourceFrameworkPath(
453: String tomcatHome) {
454: //used for framework and sofiaExamples creation, test if the sofiaExamples application in Tomcat has a /WEB-INF/propertes folder
455: File inFile = new File(getFWSystemProperties(tomcatHome));
456: return inFile.exists();
457: }
458:
459: private static String getAppSystemProperties(String tomcatHome) {
460: return tomcatHome + File.separator + "webapps" + File.separator
461: + "sofiaExamples" + File.separator + "WEB-INF"
462: + File.separator + "properties";
463: }
464:
465: private static String getFWSystemProperties(String tomcatHome) {
466: return tomcatHome + File.separator + "salmonprops";
467: }
468:
469: public static String getSourceSystemProperties(String tomcatHome,
470: boolean pathOnly, boolean appPriority) {
471: boolean app = arePropsInSourceApplicationPaths(tomcatHome);
472: boolean fw = arePropsInSourceFrameworkPath(tomcatHome);
473: if (app && fw)
474: app = appPriority;
475:
476: String fileName = pathOnly ? "" : File.separator
477: + "System.properties";
478: if (app)
479: return getAppSystemProperties(tomcatHome) + fileName;
480: else
481: return getFWSystemProperties(tomcatHome) + fileName;
482: }
483:
484: public static Props getSystemProps() {
485: String path = getSalmonPropsPath();
486: Props.setPropsPath(path);
487: return Props.getSystemProps();
488: }
489:
490: public static String getServerType() {
491: String serverType = getSystemProps().getProperty(
492: Props.IDE_SERVER_TYPE);
493: if (serverType == null)
494: serverType = IDETool.SERVER_TOMCAT40;
495: else if (serverType.equals(IDETool.SERVER_WEBLOGIC6))
496: serverType = IDETool.SERVER_WEBLOGIC6;
497: else if (serverType.equals(IDETool.SERVER_TOMCAT41))
498: serverType = IDETool.SERVER_TOMCAT41;
499: else
500: serverType = IDETool.SERVER_TOMCAT40;
501: return serverType;
502: }
503:
504: public static String getSalmonPropsPath() {
505: return getDefault().getPreferenceStore().getString(
506: PREF_SALMON_PROPS_PATH);
507:
508: }
509:
510: public static String getDefaultSalmonProject() {
511: return getDefault().getPreferenceStore().getString(
512: PREF_DEFAULT_PROJECT);
513: }
514:
515: public static Properties getPlugInInstallProps() {
516: String path = getDefault().getStateLocation().toOSString()
517: + File.separatorChar + "install.properties";
518: Properties ret = new Properties();
519: try {
520: FileInputStream f = new FileInputStream(path);
521: ret.load(f);
522: f.close();
523: } catch (Exception e) {
524: }
525: return ret;
526: }
527:
528: public static IPreferenceStore getEclipsePreferences() {
529: return getDefault().getPreferenceStore();
530: }
531:
532: public static void displayError(String title, String msg) {
533: MessageBox box = new MessageBox(new Shell(), SWT.ICON_ERROR
534: | SWT.OK | SWT.SYSTEM_MODAL);
535: box.setText(title);
536: box.setMessage(msg);
537: box.open();
538: }
539:
540: public static boolean isFrameworkFound() throws CoreException {
541: boolean frameworkFound = false;
542: IProject[] projs = ResourcesPlugin.getWorkspace().getRoot()
543: .getProjects();
544: for (int i = 0; i < projs.length; i++) {
545: if (projs[i].isOpen()) {
546: if (projs[i].hasNature(NATURE_SOFIA_FRAMEWORK)) {
547: frameworkFound = true;
548: break;
549: }
550: }
551: }
552: return frameworkFound;
553: }
554:
555: public static IJavaProject[] getFrameworkProjects()
556: throws CoreException {
557: IProject[] projs = ResourcesPlugin.getWorkspace().getRoot()
558: .getProjects();
559: Vector list = new Vector();
560: for (int i = 0; i < projs.length; i++) {
561: if (projs[i].isOpen()) {
562: if (projs[i].hasNature(NATURE_SOFIA_PROJECT))
563: list.add(JavaCore.create(projs[i]));
564: }
565: }
566: IJavaProject[] ret = new IJavaProject[list.size()];
567: list.copyInto(ret);
568: return ret;
569: }
570:
571: public static String getDefaultJSPDirectory() throws CoreException {
572: boolean frameworkFound = false;
573: IProject proj = ResourcesPlugin.getWorkspace().getRoot()
574: .getProject(getDefaultSalmonProject());
575: if (!proj.isOpen())
576: return null;
577: IFolder fol = proj.getFolder("Jsp");
578: if (fol != null)
579: return fol.getLocation().toOSString();
580: else
581: return null;
582: }
583:
584: /**Return true if we are running from MS Windows**/
585: public static boolean isWindows() {
586: boolean ret = true;
587: String os = System.getProperty("os.name");
588: if (os != null && os.toUpperCase().indexOf("WINDOWS") == -1)
589: ret = false;
590: return ret;
591: }
592:
593: /** Return true if we are running MacOS;*/
594: public static boolean isMacOS() {
595: return System.getProperty("mrj.version") != null;
596: }
597: }
|