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.launching;
011:
012: import java.io.File;
013: import java.net.URL;
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.IExecutableExtension;
019: import org.eclipse.jdt.internal.launching.LaunchingMessages;
020:
021: import com.ibm.icu.text.MessageFormat;
022:
023: /**
024: * Abstract implementation of a VM install type.
025: * Subclasses should implement
026: * <ul>
027: * <li><code>IVMInstall doCreateVMInstall(String id)</code></li>
028: * <li><code>String getName()</code></li>
029: * <li><code>IStatus validateInstallLocation(File installLocation)</code></li>
030: * </ul>
031: * <p>
032: * Clients implementing VM install types should subclass this class.
033: * </p>
034: */
035:
036: public abstract class AbstractVMInstallType implements IVMInstallType,
037: IExecutableExtension {
038: private List fVMs;
039: private String fId;
040:
041: /**
042: * Constructs a new VM install type.
043: */
044: protected AbstractVMInstallType() {
045: fVMs = new ArrayList(10);
046: }
047:
048: /* (non-Javadoc)
049: * Subclasses should not override this method.
050: * @see IVMType#getVMs()
051: */
052: public IVMInstall[] getVMInstalls() {
053: IVMInstall[] vms = new IVMInstall[fVMs.size()];
054: return (IVMInstall[]) fVMs.toArray(vms);
055: }
056:
057: /* (non-Javadoc)
058: * Subclasses should not override this method.
059: * @see IVMType#disposeVM(String)
060: */
061: public void disposeVMInstall(String id) {
062: for (int i = 0; i < fVMs.size(); i++) {
063: IVMInstall vm = (IVMInstall) fVMs.get(i);
064: if (vm.getId().equals(id)) {
065: fVMs.remove(i);
066: JavaRuntime.fireVMRemoved(vm);
067: return;
068: }
069: }
070: }
071:
072: /* (non-Javadoc)
073: * Subclasses should not override this method.
074: * @see IVMType#getVM(String)
075: */
076: public IVMInstall findVMInstall(String id) {
077: for (int i = 0; i < fVMs.size(); i++) {
078: IVMInstall vm = (IVMInstall) fVMs.get(i);
079: if (vm.getId().equals(id)) {
080: return vm;
081: }
082: }
083: return null;
084: }
085:
086: /* (non-Javadoc)
087: * Subclasses should not override this method.
088: * @see IVMType#createVM(String)
089: */
090: public IVMInstall createVMInstall(String id)
091: throws IllegalArgumentException {
092: if (findVMInstall(id) != null) {
093: String format = LaunchingMessages.vmInstallType_duplicateVM;
094: throw new IllegalArgumentException(MessageFormat.format(
095: format, new String[] { id }));
096: }
097: IVMInstall vm = doCreateVMInstall(id);
098: fVMs.add(vm);
099: return vm;
100: }
101:
102: /**
103: * Subclasses should return a new instance of the appropriate
104: * <code>IVMInstall</code> subclass from this method.
105: * @param id The vm's id. The <code>IVMInstall</code> instance that is created must
106: * return <code>id</code> from its <code>getId()</code> method.
107: * Must not be <code>null</code>.
108: * @return the newly created IVMInstall instance. Must not return <code>null</code>.
109: */
110: protected abstract IVMInstall doCreateVMInstall(String id);
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
114: */
115: /**
116: * Initializes the id parameter from the "id" attribute
117: * in the configuration markup.
118: * Subclasses should not override this method.
119: * @param config the configuration element used to trigger this execution.
120: * It can be queried by the executable extension for specific
121: * configuration properties
122: * @param propertyName the name of an attribute of the configuration element
123: * used on the <code>createExecutableExtension(String)</code> call. This
124: * argument can be used in the cases where a single configuration element
125: * is used to define multiple executable extensions.
126: * @param data adapter data in the form of a <code>String</code>,
127: * a <code>Hashtable</code>, or <code>null</code>.
128: * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
129: */
130: public void setInitializationData(IConfigurationElement config,
131: String propertyName, Object data) {
132: fId = config.getAttribute("id"); //$NON-NLS-1$
133: }
134:
135: /* (non-Javadoc)
136: * Subclasses should not override this method.
137: * @see IVMType#getId()
138: */
139: public String getId() {
140: return fId;
141: }
142:
143: /* (non-Javadoc)
144: * @see org.eclipse.jdt.launching.IVMInstallType#findVMInstallByName(java.lang.String)
145: */
146: public IVMInstall findVMInstallByName(String name) {
147: for (int i = 0; i < fVMs.size(); i++) {
148: IVMInstall vm = (IVMInstall) fVMs.get(i);
149: if (vm.getName().equals(name)) {
150: return vm;
151: }
152: }
153: return null;
154: }
155:
156: /**
157: * Returns a URL for the default javadoc location of a VM installed at the
158: * given home location, or <code>null</code> if none. The default
159: * implementation returns <code>null</code>, subclasses must override as
160: * appropriate.
161: * <p>
162: * Since 3.4, the argument may also be a file describing a VM install.
163: * For example, the standard VM installs support execution
164: * environment description files, as described by
165: * <code>http://wiki.eclipse.org/index.php/Execution_Environment_Descriptions</code>.
166: * </p>
167: * <p>
168: * Note, this method would ideally be added to <code>IVMInstallType</code>,
169: * but it would have been a breaking API change between 2.0 and 2.1. Thus,
170: * it has been added to the abstract base class that VM install types should
171: * subclass.
172: * </p>
173: *
174: * @param installLocationOrDefinitionFile home location or since 3.4, a definition file
175: * @return default javadoc location or <code>null</code>
176: * @since 2.1
177: */
178: public URL getDefaultJavadocLocation(
179: File installLocationOrDefinitionFile) {
180: return null;
181: }
182:
183: /**
184: * Returns a string of default VM arguments for a VM installed at the
185: * given home location or definition file, or <code>null</code> if none.
186: * The default implementation returns <code>null</code>, subclasses must override
187: * as appropriate.
188: * <p>
189: * Note, this method would ideally be added to <code>IVMInstallType</code>,
190: * but it would have been a breaking API change between 2.0 and 3.4. Thus,
191: * it has been added to the abstract base class that VM install types should
192: * subclass.
193: * </p>
194: * @param installLocationOrDefinitionFile home location or definition file
195: * @return default VM arguments or <code>null</code> if none
196: * @since 3.4
197: */
198: public String getDefaultVMArguments(
199: File installLocationOrDefinitionFile) {
200: return null;
201: }
202: }
|