Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2759823bb1058ba90e4caed0c1acdcb1bc110184 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
 * Copyright (c) 2007 Symbian Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Andrew Ferguson (Symbian) - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.export;

import java.io.File;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMIndexerTask;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.index.export.ExternalExportProjectProvider;
import org.eclipse.cdt.core.index.export.IExportProjectProvider;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.ProgressProvider;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

/**
 * An eclipse application for generating PDOM's without starting the Workbench
 */
public class GeneratePDOMApplication implements IApplication {
	private static final String EXPORT_PROJECT_PROVIDER = "ExportProjectProvider"; //$NON-NLS-1$
	private static final String DEFAULT_PROJECT_PROVIDER = ExternalExportProjectProvider.class.getName();
	public static final String OPT_PROJECTPROVIDER= "-pprovider"; //$NON-NLS-1$
	public static final String OPT_TARGET= "-target"; //$NON-NLS-1$
	public static final String OPT_QUIET= "-quiet"; //$NON-NLS-1$
	public static final String OPT_INDEXER_ID= "-indexer"; //$NON-NLS-1$

	/**
	 * Applications needing to fail in an expected way (without stack dump), should throw
	 * CoreExceptions with this error code.
	 */
	public static final int ECODE_EXPECTED_FAILURE= 1;
	
	private static Map/*<String,IProjectForExportManager>*/ projectInitializers;

	/**
	 * Starts this application
	 * @throws CoreException on an unexpected failure
	 */
	public Object start(IApplicationContext context) throws CoreException {
		Object result= IApplication.EXIT_OK;
		try {
			result= startImpl(context);
		} catch(CoreException ce) {
			IStatus s= ce.getStatus();
			if(s.getCode()==ECODE_EXPECTED_FAILURE) {
				output(s.getMessage());
			} else {
				throw ce;
			}
		}
		return result;
	}
	
	private Object startImpl(IApplicationContext context) throws CoreException {
		String[] appArgs= (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
		Map arguments= CLIUtil.parseToMap(appArgs);
		output(Messages.GeneratePDOMApplication_Initializing);

		setupCLIProgressProvider();

		String pproviderFQN;
		if(!arguments.containsKey(OPT_PROJECTPROVIDER)) {
			output(MessageFormat.format(Messages.GeneratePDOMApplication_UsingDefaultProjectProvider, new Object[] {DEFAULT_PROJECT_PROVIDER}));
			pproviderFQN= DEFAULT_PROJECT_PROVIDER;
		} else {
			pproviderFQN= (String) CLIUtil.getArg(arguments, OPT_PROJECTPROVIDER, 1).get(0);
		}
		String target= (String) CLIUtil.getArg(arguments, OPT_TARGET, 1).get(0); 
		boolean quiet= arguments.get(OPT_QUIET)!=null;

		String indexerID= IPDOMManager.ID_FAST_INDEXER;
		String[] indexerIDs= (String[]) arguments.get(OPT_INDEXER_ID);
		if(indexerIDs!=null) {
			if(indexerIDs.length==1) {
				indexerID= indexerIDs[0];
			} else if(indexerIDs.length>1) {
				fail(MessageFormat.format(Messages.GeneratePDOMApplication_InvalidIndexerID, new Object[] {OPT_INDEXER_ID}));
			}
		}
		
		if(!quiet) {
			System.setProperty(IPDOMIndexerTask.TRACE_ACTIVITY, Boolean.TRUE.toString());
			System.setProperty(IPDOMIndexerTask.TRACE_PROBLEMS, Boolean.TRUE.toString());
			System.setProperty(IPDOMIndexerTask.TRACE_STATISTICS, Boolean.TRUE.toString());
		}

		IExportProjectProvider pprovider = getExportProjectProvider(pproviderFQN);
		if(pprovider==null) {
			fail(MessageFormat.format(Messages.GeneratePDOMApplication_CouldNotFindInitializer, new Object[]{pproviderFQN}));
		}
		File targetLocation = new File(target);

		GeneratePDOM generate = new GeneratePDOM(pprovider,	appArgs, targetLocation, indexerID);
		output(Messages.GeneratePDOMApplication_GenerationStarts);
		generate.run();
		output(Messages.GeneratePDOMApplication_GenerationEnds);
		return null;
	}

	protected void output(String s) {
		System.out.println(s);
	}

	public void stop() {
		// do nothing
	}

	/**
	 * Causes the appliation to fail in a way that has been anticipated (e.g. a command-line or interface
	 * contract violation by a extension implementation)
	 * @param message
	 * @throws CoreException
	 */
	public static final void fail(String message) throws CoreException {
		IStatus status= new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, ECODE_EXPECTED_FAILURE, message, null);
		CCorePlugin.log(status);
		throw new CoreException(status);
	}
	
	/**
	 * Returns the IExportProjectProvider registed in the plug-in registry under the
	 * specified fully qualified class name
	 * May return null
	 * @param fqn
	 * @return
	 */
	private static synchronized IExportProjectProvider getExportProjectProvider(String fqn) {
		if(projectInitializers==null) {
			projectInitializers = new HashMap();
			IExtensionRegistry registry = Platform.getExtensionRegistry();
			IExtensionPoint indexExtensions = registry.getExtensionPoint(CCorePlugin.INDEX_UNIQ_ID);
			IExtension[] extensions = indexExtensions.getExtensions();
			for(int i=0; i<extensions.length; i++) {
				IExtension extension = extensions[i];
				IConfigurationElement[] ce = extension.getConfigurationElements();

				for(int j=0; j<ce.length; j++) {
					if(ce[j].getName().equals(EXPORT_PROJECT_PROVIDER)) {
						try {
							IExportProjectProvider epp = (IExportProjectProvider) ce[j].createExecutableExtension("class"); //$NON-NLS-1$
							projectInitializers.put(epp.getClass().getName(), epp);
						} catch(CoreException cee) {
							CCorePlugin.log(cee);
						}
					}
				}
			}
		}

		IExportProjectProvider initer = (IExportProjectProvider) projectInitializers.get(fqn);
		return initer;
	}

	/**
	 * In this application, the usual progress reports are redirected to stdoutt
	 */
	private void setupCLIProgressProvider() {
		ProgressProvider pp = new ProgressProvider() {
			class IndexingStreamProgressMonitor extends StreamProgressMonitor {
				public IndexingStreamProgressMonitor(PrintStream writer) {
					super(writer);
				}
				protected boolean shouldOutput() {
					return taskName!=null && taskName.equals(CCorePlugin.getResourceString("pdom.indexer.task")); //$NON-NLS-1$
				}
			}
			public IProgressMonitor createMonitor(Job job) {
				return new IndexingStreamProgressMonitor(System.out);
			}
			public IProgressMonitor createMonitor(Job job,
					IProgressMonitor group, int ticks) {
				return new NullProgressMonitor();
			}
			public IProgressMonitor createProgressGroup() {
				return new NullProgressMonitor();
			}
		};
		Job.getJobManager().setProgressProvider(pp);
	}

	static class StreamProgressMonitor implements IProgressMonitor {
		volatile boolean canceled;
		volatile int totalWork;
		volatile double worked;
		final PrintStream writer;
		volatile String taskName, subTask;
		Object mutex = new Object();

		StreamProgressMonitor(PrintStream writer) {
			this.writer = writer;
			this.totalWork = -1;
		}

		protected boolean shouldOutput() {
			return true;
		}

		public void done() {
		}
		public void worked(int work) {
			internalWorked(work);
		}
		public void beginTask(String name, int totalWork) {
			this.taskName = name;
			this.totalWork = totalWork;
		}
		public void internalWorked(double work) {
			synchronized(mutex) {
				worked += work;
				int pc = totalWork<1 ? 0 : (int) ((worked*100D)/totalWork);
				if(shouldOutput()) {
					writer.println(pc+"% "+subTask);  //$NON-NLS-1$
				}
			}
		}

		public boolean isCanceled() {
			return canceled;
		}

		public void setCanceled(boolean value) {
			canceled = value;
		}
		public void setTaskName(String name) {
			taskName = name;
		}
		public void subTask(String name) {
			subTask = name;
		}
	}	
}

Back to the top