001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.macbundler;
011:
012: import java.io.*;
013: import java.io.File;
014: import java.util.*;
015: import java.util.Properties;
016:
017: import org.eclipse.core.runtime.CoreException;
018:
019: import org.eclipse.jface.util.*;
020: import org.eclipse.core.runtime.ListenerList;
021:
022: import org.eclipse.debug.core.*;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
025:
026: class BundleDescription implements BundleAttributes {
027:
028: private static final String STUB = "/System/Library/Frameworks/JavaVM.framework/Versions/A/Resources/MacOS/JavaApplicationStub"; //$NON-NLS-1$
029: private static final String ICON = "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Resources/GenericApp.icns"; //$NON-NLS-1$
030:
031: private ListenerList fListeners = new ListenerList();
032: private Properties fProperties = new Properties();
033: private List fClassPath = new ArrayList();
034: private List fResources = new ArrayList();
035: Properties fProperties2 = new Properties();
036:
037: BundleDescription() {
038: clear();
039: }
040:
041: void clear() {
042: fProperties.clear();
043: fClassPath.clear();
044: fResources.clear();
045: fProperties2.clear();
046: fProperties.put(SIGNATURE, "????"); //$NON-NLS-1$
047: fProperties.put(ICONFILE, ICON);
048: }
049:
050: void addResource(ResourceInfo ri, boolean onClasspath) {
051: if (onClasspath)
052: fClassPath.add(ri);
053: else
054: fResources.add(ri);
055: }
056:
057: boolean removeResource(ResourceInfo ri, boolean onClasspath) {
058: if (onClasspath)
059: return fClassPath.remove(ri);
060: return fResources.remove(ri);
061: }
062:
063: ResourceInfo[] getResources(boolean onClasspath) {
064: if (onClasspath)
065: return (ResourceInfo[]) fClassPath
066: .toArray(new ResourceInfo[fClassPath.size()]);
067: return (ResourceInfo[]) fResources
068: .toArray(new ResourceInfo[fResources.size()]);
069: }
070:
071: void addListener(IPropertyChangeListener listener) {
072: fListeners.add(listener);
073: }
074:
075: void removeListener(IPropertyChangeListener listener) {
076: fListeners.remove(listener);
077: }
078:
079: String get(String key) {
080: return fProperties.getProperty(key);
081: }
082:
083: public String get(String key, String dflt) {
084: return fProperties.getProperty(key, dflt);
085: }
086:
087: public boolean get(String key, boolean dflt) {
088: Boolean v = (Boolean) fProperties.get(key);
089: if (v == null)
090: return dflt;
091: return v.booleanValue();
092: }
093:
094: void setValue(String key, Object value) {
095: fProperties.put(key, value);
096: }
097:
098: void inititialize(ILaunchConfiguration lc) {
099: AbstractJavaLaunchConfigurationDelegate lcd;
100: try {
101: lcd = (AbstractJavaLaunchConfigurationDelegate) lc
102: .getType().getDelegate(ILaunchManager.RUN_MODE);
103: } catch (CoreException e) {
104: return;
105: }
106:
107: String appName = lc.getName();
108: fProperties.put(APPNAME, appName);
109: fProperties.put(GETINFO, appName
110: + Util.getString("BundleDescription.copyright.format")); //$NON-NLS-1$
111:
112: try {
113: fProperties.put(MAINCLASS, lcd.getMainTypeName(lc));
114: } catch (CoreException e) {
115: fProperties.put(MAINCLASS, ""); //$NON-NLS-1$
116: }
117: try {
118: fProperties.put(ARGUMENTS, lcd.getProgramArguments(lc));
119: } catch (CoreException e) {
120: fProperties.put(ARGUMENTS, ""); //$NON-NLS-1$
121: }
122: String wd = null;
123: try {
124: wd = lcd.getWorkingDirectory(lc).getAbsolutePath();
125: // fProperties.put(WORKINGDIR, wd); //$NON-NLS-1$
126: } catch (CoreException e) {
127: // fProperties.put(WORKINGDIR, ""); //$NON-NLS-1$
128: }
129: try {
130: fProperties.put(MAINCLASS, lcd.getMainTypeName(lc));
131: } catch (CoreException e) {
132: fProperties.put(MAINCLASS, ""); //$NON-NLS-1$
133: }
134:
135: try {
136: String[] classpath = lcd.getClasspath(lc);
137: for (int i = 0; i < classpath.length; i++)
138: addResource(new ResourceInfo(classpath[i]), true);
139: } catch (CoreException e) {
140: //
141: }
142:
143: String vmOptions2 = ""; //$NON-NLS-1$
144: String vmOptions = null;
145: try {
146: vmOptions = lcd.getVMArguments(lc);
147: } catch (CoreException e) {
148: //
149: }
150: if (vmOptions != null) {
151: StringTokenizer st = new StringTokenizer(vmOptions);
152: while (st.hasMoreTokens()) {
153: String token = st.nextToken();
154: int pos = token.indexOf('=');
155: if (pos > 2 && token.startsWith("-D")) { //$NON-NLS-1$
156: String key = token.substring(2, pos).trim();
157: String value = token.substring(pos + 1).trim();
158: int l = value.length();
159: if (l >= 2 && value.charAt(0) == '"'
160: && value.charAt(l - 1) == '"')
161: value = value.substring(1, l - 1);
162: if ("java.library.path".equals(key)) { //$NON-NLS-1$
163: addDllDir(wd, value);
164: } else {
165: fProperties2.put(key, value);
166: }
167: } else {
168: vmOptions2 = vmOptions2 + token + ' ';
169: }
170: }
171: }
172:
173: fProperties.put(VMOPTIONS, vmOptions2);
174:
175: boolean isSWT = false;
176: Iterator iter = fResources.iterator();
177: while (iter.hasNext()) {
178: ResourceInfo ri = (ResourceInfo) iter.next();
179: if (ri.fPath.indexOf("libswt-carbon") >= 0) { //$NON-NLS-1$
180: isSWT = true;
181: break;
182: }
183: }
184: fProperties.put(USES_SWT, Boolean.valueOf(isSWT));
185:
186: String launcher = null;
187: if (isSWT)
188: launcher = System.getProperty("org.eclipse.swtlauncher"); //$NON-NLS-1$
189:
190: if (launcher == null) {
191: setValue(JVMVERSION, "1.4*"); //$NON-NLS-1$
192: launcher = STUB;
193: }
194: setValue(LAUNCHER, launcher);
195:
196: IJavaProject p = null;
197: try {
198: p = lcd.getJavaProject(lc);
199: } catch (CoreException e) {
200: // ignore
201: }
202: if (p != null)
203: fProperties.put(IDENTIFIER, p.getElementName());
204: else
205: fProperties.put(IDENTIFIER, ""); //$NON-NLS-1$
206:
207: fireChange();
208: }
209:
210: void fireChange() {
211: PropertyChangeEvent e = new PropertyChangeEvent(this , ALL,
212: null, null);
213: Object[] listeners = fListeners.getListeners();
214: for (int i = 0; i < listeners.length; i++)
215: ((IPropertyChangeListener) listeners[i]).propertyChange(e);
216: }
217:
218: private void addDllDir(String wd, String path) {
219: File lib_dir;
220: if (path.startsWith("../")) { //$NON-NLS-1$
221: lib_dir = new File(wd, path);
222: } else {
223: lib_dir = new File(path);
224: }
225: if (lib_dir.isDirectory()) {
226: File[] dlls = lib_dir.listFiles();
227: for (int j = 0; j < dlls.length; j++) {
228: try {
229: String name = dlls[j].getCanonicalPath();
230: if (name.endsWith(".jnilib")) //$NON-NLS-1$
231: addResource(new ResourceInfo(name), false);
232: } catch (IOException e) {
233: // NeedWork Auto-generated catch block
234: e.printStackTrace();
235: }
236: }
237: }
238: }
239:
240: static boolean verify(ILaunchConfiguration lc) {
241: String name = lc.getName();
242: if (name.indexOf("jpage") >= 0) //$NON-NLS-1$
243: return false;
244: AbstractJavaLaunchConfigurationDelegate lcd;
245: try {
246: lcd = (AbstractJavaLaunchConfigurationDelegate) lc
247: .getType().getDelegate(ILaunchManager.RUN_MODE);
248: if (lcd.getMainTypeName(lc) == null)
249: return false;
250: return true;
251: } catch (CoreException e) {
252: return false;
253: }
254: }
255:
256: static boolean matches(ILaunchConfiguration lc, IJavaProject project) {
257: AbstractJavaLaunchConfigurationDelegate lcd;
258: try {
259: lcd = (AbstractJavaLaunchConfigurationDelegate) lc
260: .getType().getDelegate(ILaunchManager.RUN_MODE);
261: } catch (CoreException e) {
262: return false;
263: }
264: IJavaProject p = null;
265: try {
266: p = lcd.getJavaProject(lc);
267: } catch (CoreException e) {
268: return false;
269: }
270: return project != null && project.equals(p);
271: }
272: }
|