KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > BuildApplication


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.equinox.app.IApplication;
19 import org.eclipse.equinox.app.IApplicationContext;
20 import org.osgi.framework.Bundle;
21
22 public class BuildApplication implements IApplication {
23
24     class ApplicationContext implements IApplicationContext {
25
26         IApplicationContext parent;
27         Map JavaDoc arguments;
28
29         ApplicationContext(IApplicationContext parent, Map JavaDoc arguments) {
30             this.parent = parent;
31             this.arguments = arguments;
32         }
33
34         /* (non-Javadoc)
35          * @see org.eclipse.equinox.app.IApplicationContext#applicationRunning()
36          */

37         public void applicationRunning() {
38             parent.applicationRunning();
39         }
40
41         /* (non-Javadoc)
42          * @see org.eclipse.equinox.app.IApplicationContext#getArguments()
43          */

44         public Map JavaDoc getArguments() {
45             return arguments;
46         }
47
48         /* (non-Javadoc)
49          * @see org.eclipse.equinox.app.IApplicationContext#getBrandingApplication()
50          */

51         public String JavaDoc getBrandingApplication() {
52             return parent.getBrandingApplication();
53         }
54
55         /* (non-Javadoc)
56          * @see org.eclipse.equinox.app.IApplicationContext#getBrandingBundle()
57          */

58         public Bundle getBrandingBundle() {
59             return parent.getBrandingBundle();
60         }
61
62         /* (non-Javadoc)
63          * @see org.eclipse.equinox.app.IApplicationContext#getBrandingDescription()
64          */

65         public String JavaDoc getBrandingDescription() {
66             return parent.getBrandingDescription();
67         }
68
69         /* (non-Javadoc)
70          * @see org.eclipse.equinox.app.IApplicationContext#getBrandingId()
71          */

72         public String JavaDoc getBrandingId() {
73             return parent.getBrandingId();
74         }
75
76         /* (non-Javadoc)
77          * @see org.eclipse.equinox.app.IApplicationContext#getBrandingName()
78          */

79         public String JavaDoc getBrandingName() {
80             return parent.getBrandingName();
81         }
82
83         /* (non-Javadoc)
84          * @see org.eclipse.equinox.app.IApplicationContext#getBrandingProperty(java.lang.String)
85          */

86         public String JavaDoc getBrandingProperty(String JavaDoc key) {
87             return parent.getBrandingProperty(key);
88         }
89     }
90
91     /* (non-Javadoc)
92      * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
93      */

94     public Object JavaDoc start(IApplicationContext context) throws Exception JavaDoc {
95         Platform.endSplash();
96         IExtension extension = Platform.getExtensionRegistry().getExtension("org.eclipse.ant.core.antRunner"); //$NON-NLS-1$
97
if (extension == null)
98             return null;
99         IConfigurationElement element = extension.getConfigurationElements()[0];
100         Object JavaDoc ee = element.createExecutableExtension("run"); //$NON-NLS-1$
101
Object JavaDoc args = context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
102         args = updateArgs((String JavaDoc[]) args);
103
104         if (ee instanceof IApplication) {
105             // create a copy of this context arguments
106
Map JavaDoc arguments = new HashMap JavaDoc(context.getArguments());
107             // add the updated args as a key for launching antRunner
108
arguments.put(IApplicationContext.APPLICATION_ARGS, args);
109             IApplicationContext appContext = new ApplicationContext(context, arguments);
110             return ((IApplication) ee).start(appContext);
111         }
112         // else it is probably an old IPlatformRunnable
113
return doPlatformRunnable(ee, args);
114     }
115
116     /**
117      * If the Executable Extension is an old IPlatformRunnable, use this method to run it to
118      * avoid the warnings about deprecation.
119      * @deprecated
120      * @param ee
121      * @param args
122      * @return
123      * @throws Exception
124      */

125     private Object JavaDoc doPlatformRunnable(Object JavaDoc ee, Object JavaDoc args) throws Exception JavaDoc {
126         if (ee instanceof IPlatformRunnable)
127             return ((IPlatformRunnable) ee).run(args);
128         return null;
129     }
130
131     private Object JavaDoc updateArgs(String JavaDoc[] args) throws IOException JavaDoc {
132         for (int i = 0; i < args.length; i++) {
133             String JavaDoc string = args[i];
134             if (string.equals("-f") || string.equals("-buildfile")) //$NON-NLS-1$ //$NON-NLS-2$
135
return args;
136         }
137         int length = args.length;
138         String JavaDoc[] result = new String JavaDoc[length + 2];
139         System.arraycopy(args, 0, result, 0, length);
140         result[length] = "-f"; //$NON-NLS-1$
141
URL JavaDoc buildURL = BundleHelper.getDefault().find(new Path("/scripts/build.xml")); //$NON-NLS-1$
142
result[length + 1] = FileLocator.toFileURL(buildURL).getFile();
143         return result;
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.equinox.app.IApplication#stop()
148      */

149     public void stop() {
150         // do nothing for now
151
}
152 }
153
Popular Tags