001: /*******************************************************************************
002: * Copyright (c) 2007 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 - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build;
011:
012: import java.io.IOException;
013: import java.net.URL;
014: import java.util.HashMap;
015: import java.util.Map;
016: import org.eclipse.core.runtime.*;
017: import org.eclipse.equinox.app.IApplication;
018: import org.eclipse.equinox.app.IApplicationContext;
019: import org.osgi.framework.Bundle;
020:
021: public class BuildApplication implements IApplication {
022:
023: class ApplicationContext implements IApplicationContext {
024:
025: IApplicationContext parent;
026: Map arguments;
027:
028: ApplicationContext(IApplicationContext parent, Map arguments) {
029: this .parent = parent;
030: this .arguments = arguments;
031: }
032:
033: /* (non-Javadoc)
034: * @see org.eclipse.equinox.app.IApplicationContext#applicationRunning()
035: */
036: public void applicationRunning() {
037: parent.applicationRunning();
038: }
039:
040: /* (non-Javadoc)
041: * @see org.eclipse.equinox.app.IApplicationContext#getArguments()
042: */
043: public Map getArguments() {
044: return arguments;
045: }
046:
047: /* (non-Javadoc)
048: * @see org.eclipse.equinox.app.IApplicationContext#getBrandingApplication()
049: */
050: public String getBrandingApplication() {
051: return parent.getBrandingApplication();
052: }
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.equinox.app.IApplicationContext#getBrandingBundle()
056: */
057: public Bundle getBrandingBundle() {
058: return parent.getBrandingBundle();
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.equinox.app.IApplicationContext#getBrandingDescription()
063: */
064: public String getBrandingDescription() {
065: return parent.getBrandingDescription();
066: }
067:
068: /* (non-Javadoc)
069: * @see org.eclipse.equinox.app.IApplicationContext#getBrandingId()
070: */
071: public String getBrandingId() {
072: return parent.getBrandingId();
073: }
074:
075: /* (non-Javadoc)
076: * @see org.eclipse.equinox.app.IApplicationContext#getBrandingName()
077: */
078: public String getBrandingName() {
079: return parent.getBrandingName();
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.equinox.app.IApplicationContext#getBrandingProperty(java.lang.String)
084: */
085: public String getBrandingProperty(String key) {
086: return parent.getBrandingProperty(key);
087: }
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
092: */
093: public Object start(IApplicationContext context) throws Exception {
094: Platform.endSplash();
095: IExtension extension = Platform.getExtensionRegistry()
096: .getExtension("org.eclipse.ant.core.antRunner"); //$NON-NLS-1$
097: if (extension == null)
098: return null;
099: IConfigurationElement element = extension
100: .getConfigurationElements()[0];
101: Object ee = element.createExecutableExtension("run"); //$NON-NLS-1$
102: Object args = context.getArguments().get(
103: IApplicationContext.APPLICATION_ARGS);
104: args = updateArgs((String[]) args);
105:
106: if (ee instanceof IApplication) {
107: // create a copy of this context arguments
108: Map arguments = new HashMap(context.getArguments());
109: // add the updated args as a key for launching antRunner
110: arguments.put(IApplicationContext.APPLICATION_ARGS, args);
111: IApplicationContext appContext = new ApplicationContext(
112: context, arguments);
113: return ((IApplication) ee).start(appContext);
114: }
115: // else it is probably an old IPlatformRunnable
116: return doPlatformRunnable(ee, args);
117: }
118:
119: /**
120: * If the Executable Extension is an old IPlatformRunnable, use this method to run it to
121: * avoid the warnings about deprecation.
122: * @deprecated
123: * @param ee
124: * @param args
125: * @return
126: * @throws Exception
127: */
128: private Object doPlatformRunnable(Object ee, Object args)
129: throws Exception {
130: if (ee instanceof IPlatformRunnable)
131: return ((IPlatformRunnable) ee).run(args);
132: return null;
133: }
134:
135: private Object updateArgs(String[] args) throws IOException {
136: for (int i = 0; i < args.length; i++) {
137: String string = args[i];
138: if (string.equals("-f") || string.equals("-buildfile")) //$NON-NLS-1$ //$NON-NLS-2$
139: return args;
140: }
141: int length = args.length;
142: String[] result = new String[length + 2];
143: System.arraycopy(args, 0, result, 0, length);
144: result[length] = "-f"; //$NON-NLS-1$
145: URL buildURL = BundleHelper.getDefault().find(
146: new Path("/scripts/build.xml")); //$NON-NLS-1$
147: result[length + 1] = FileLocator.toFileURL(buildURL).getFile();
148: return result;
149: }
150:
151: /* (non-Javadoc)
152: * @see org.eclipse.equinox.app.IApplication#stop()
153: */
154: public void stop() {
155: // do nothing for now
156: }
157: }
|