View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: Activator.java
13   * Created: 2007-03-08
14   * Author: apohllo
15   * $Id: Activator.java 3296 2009-09-02 13:13:14Z tmilos $
16   */
17  
18  package pl.edu.agh.cast;
19  
20  import java.util.Arrays;
21  import java.util.LinkedList;
22  import java.util.List;
23  
24  import org.apache.log4j.ConsoleAppender;
25  import org.apache.log4j.Logger;
26  import org.apache.log4j.PatternLayout;
27  import org.eclipse.core.runtime.CoreException;
28  import org.eclipse.core.runtime.IConfigurationElement;
29  import org.eclipse.core.runtime.IPath;
30  import org.eclipse.core.runtime.Platform;
31  import org.eclipse.jface.resource.ImageDescriptor;
32  import org.eclipse.ui.plugin.AbstractUIPlugin;
33  import org.osgi.framework.BundleContext;
34  
35  import pl.edu.agh.cast.data.converter.ConverterReference;
36  import pl.edu.agh.cast.data.converter.ConverterSpecification;
37  import pl.edu.agh.cast.resource.IVisualResource;
38  import pl.edu.agh.cast.resource.IVisualResourcesProvider;
39  import pl.edu.agh.cast.util.Configuration;
40  import pl.edu.agh.cast.util.logging.LoggingUtil;
41  
42  /**
43   * The activator class controls the plug-in life cycle.
44   *
45   * @author AGH CAST Team
46   */
47  public class Activator extends AbstractUIPlugin {
48  
49  	/**
50  	 * The plugin ID.
51  	 */
52  	public static final String PLUGIN_ID = "pl.edu.agh.cast"; //$NON-NLS-1$
53  
54  	/**
55  	 * The name of the standard output and error stream redirection file.
56  	 */
57  	public static final String STD_OUT_ERR_FILE = ".stdouterr"; //$NON-NLS-1$
58  
59  	/**
60  	 * The application argument flag for switching off standard output and error stream redirection.
61  	 */
62  	public static final String NO_STD_OUT_ERR_REDIRECT = "-nostdouterr"; //$NON-NLS-1$
63  
64  	private static final String STATISTIC_EXTENSION_ID = "pl.edu.agh.cast.statistics"; //$NON-NLS-1$
65  
66  	/**
67  	 * Name of the <em>name</em> attribute of <code>pl.edu.agh.cast.statistics</code> extension point.
68  	 */
69  	public static final String STATISTIC_NAME = "name"; //$NON-NLS-1$
70  
71  	/**
72  	 * Name of the <em>point</em> attribute of <code>pl.edu.agh.cast.statistics</code> extension point.
73  	 */
74  	public static final String STATISTIC_CLASS = "point"; //$NON-NLS-1$
75  
76  	/**
77  	 * Name of the <em>target_label</em> attribute of <code>pl.edu.agh.cast.statistics</code> extension point.
78  	 */
79  	public static final String STATISTIC_TARGET_LABEL = "target_label"; //$NON-NLS-1$
80  
81  	/**
82  	 * Name of the <em>source_label</em> attribute of <code>pl.edu.agh.cast.statistics</code> extension point.
83  	 */
84  	public static final String STATISTIC_SOURCE_LABEL = "source_label"; //$NON-NLS-1$
85  
86  	/**
87  	 * Name of the <em>description</em> attribute of <code>pl.edu.agh.cast.statistics</code> extension point.
88  	 */
89  	public static final String STATISTIC_DESCRIPTION = "description"; //$NON-NLS-1$
90  
91  	private static final String NODE_ICON_EXTENSION_ID = "pl.edu.agh.cast.nodeIcons"; //$NON-NLS-1$
92  
93  	/**
94  	 * Name of the <em>factory</em> attribute of <code>pl.edu.agh.cast.nodeIcons</code> extension point.
95  	 */
96  	public static final String NODE_ICON_FACTORY = "factory"; //$NON-NLS-1$
97  
98  	/**
99  	 * Name of resource provider extension point id.
100 	 */
101 	private static final String RESOURCES_PROVIDER_EXTENSION_ID = "pl.edu.agh.cast.resource.provider"; //$NON-NLS-1$
102 
103 	/**
104 	 * Name of attribute holding resource provider class.
105 	 */
106 	private static final String RESOURCES_PROVIDER_CLASS = "point"; //$NON-NLS-1$
107 
108 	/**
109 	 * Tag for visual resources used in editors.
110 	 *
111 	 * @see IVisualResource
112 	 */
113 	public static final String RESOURCE_TAG_EDITOR = "EditorIcon"; //$NON-NLS-1$
114 
115 	/**
116 	 * Tag for visual resources used in editors.
117 	 *
118 	 * @see IVisualResource
119 	 */
120 	public static final String RESOURCE_TAG_DATA_MODEL = "DataModelIcon"; //$NON-NLS-1$
121 
122 	/**
123 	 * Name of data set converter extension point id.
124 	 */
125 	private static final String DS_CONVERTER_EXTENSION_ID = "pl.edu.agh.cast.data.converter"; //$NON-NLS-1$
126 
127 	/**
128 	 * Name of attribute holding converter specification class.
129 	 */
130 	private static final String DS_CONVERTER_SPECIFICATION_CLASS = "specification"; //$NON-NLS-1$
131 
132 	/**
133 	 * Name of attribute holding converter name.
134 	 */
135 	private static final String DS_CONVERTER_SPECIFICATION_NAME = "name"; //$NON-NLS-1$
136 
137 	/**
138 	 * Name of attribute holding converter description.
139 	 */
140 	private static final String DS_CONVERTER_SPECIFICATION_DESCRIPTION = "description"; //$NON-NLS-1$
141 
142 	// The shared instance
143 	private static Activator plugin;
144 
145 	// Plugin logger instance
146 	private static Logger log;
147 
148 	/**
149 	 * The constructor.
150 	 */
151 	public Activator() {
152 		plugin = this;
153 
154 		if (!Arrays.asList(Platform.getApplicationArgs()).contains(NO_STD_OUT_ERR_REDIRECT)) {
155 			try {
156 				IPath stdPath = Platform.getLocation().append(".metadata").append(STD_OUT_ERR_FILE); //$NON-NLS-1$
157 				LoggingUtil.rebindStdOutErr(stdPath.toFile());
158 			} catch (Exception e) {
159 				getLogger().error("Failed to rebind standard output and error streams", e); //$NON-NLS-1$
160 			}
161 		}
162 	}
163 
164 	/**
165 	 * Returns {@link Logger} instance for this plugin.
166 	 *
167 	 * @return {@link Logger} instance
168 	 */
169 	public static Logger getLogger() {
170 		if (log == null) {
171 			Configuration.getInstance(); // configures logger
172 			log = Logger.getLogger(Activator.class);
173 
174 			if (!log.getAllAppenders().hasMoreElements()) {
175 				// add default console appender
176 				log.addAppender(new ConsoleAppender(new PatternLayout("%5p [%t] (%F:%L) - %m%n"))); //$NON-NLS-1$
177 			}
178 		}
179 		return log;
180 	}
181 
182 	/**
183 	 * {@inheritDoc}
184 	 *
185 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
186 	 */
187 	@Override
188 	public void start(BundleContext context) throws Exception {
189 		super.start(context);
190 	}
191 
192 	/**
193 	 * {@inheritDoc}
194 	 *
195 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
196 	 */
197 	@Override
198 	public void stop(BundleContext context) throws Exception {
199 		plugin = null;
200 		super.stop(context);
201 	}
202 
203 	/**
204 	 * Returns the shared instance.
205 	 *
206 	 * @return the shared instance
207 	 */
208 	public static Activator getDefault() {
209 		return plugin;
210 	}
211 
212 	/**
213 	 * Returns an image descriptor for the image file at the given plug-in relative path.
214 	 *
215 	 * @param path
216 	 *            the path
217 	 * @return the image descriptor
218 	 */
219 	public static ImageDescriptor getImageDescriptor(String path) {
220 		return imageDescriptorFromPlugin(PLUGIN_ID, path);
221 	}
222 
223 	/**
224 	 * Returns configurations of extensions for <code>pl.edu.agh.cast.statistics</code> extension point.
225 	 *
226 	 * @return array of configurations
227 	 */
228 	public static IConfigurationElement[] getStatisticsConfigurations() {
229 		return Platform.getExtensionRegistry().getConfigurationElementsFor(STATISTIC_EXTENSION_ID);
230 	}
231 
232 	/**
233 	 * Returns configurations of extensions for <code>pl.edu.agh.cast.nodeIcons</code> extension point.
234 	 *
235 	 * @return array of configurations
236 	 */
237 	public static IConfigurationElement[] getNodeIconsConfigurations() {
238 		return Platform.getExtensionRegistry().getConfigurationElementsFor(NODE_ICON_EXTENSION_ID);
239 	}
240 
241 	/**
242 	 * Returns list of {@link IVisualResourcesProvider} providers read from extension point
243 	 * <code>pl.edu.agh.cast.resource.provider</code>. All errors during providers initialization are logged and
244 	 * ignored.
245 	 *
246 	 * @return list of visual resources providers; if none, the empty list is returned
247 	 */
248 	public static List<IVisualResourcesProvider> getResourcesProviders() {
249 
250 		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
251 		        RESOURCES_PROVIDER_EXTENSION_ID);
252 
253 		List<IVisualResourcesProvider> providers = new LinkedList<IVisualResourcesProvider>();
254 
255 		for (IConfigurationElement element : elements) {
256 			try {
257 				providers.add((IVisualResourcesProvider)element.createExecutableExtension(RESOURCES_PROVIDER_CLASS));
258 			} catch (CoreException e) {
259 				log.warn(String.format("Failed to instantiate IVisualResourceProvider provided by extension: %1$s ", //$NON-NLS-1$
260 				        element.getDeclaringExtension().getUniqueIdentifier()), e);
261 				continue;
262 			}
263 		}
264 		return providers;
265 	}
266 
267 	/**
268 	 * Returns list of {@link ConverterSpecification} read from extension point
269 	 * <code>pl.edu.agh.cast.data.converter</code>. All errors during converter initialization are logged and ignored.
270 	 *
271 	 * @return list of converter specifications; if none, the empty list is returned
272 	 */
273 	public static List<ConverterReference> getDataSetConverterReferences() {
274 
275 		IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
276 		        DS_CONVERTER_EXTENSION_ID);
277 
278 		List<ConverterReference> references = new LinkedList<ConverterReference>();
279 
280 		for (IConfigurationElement element : elements) {
281 			ConverterReference reference = new ConverterReference();
282 			reference.setName(element.getAttribute(DS_CONVERTER_SPECIFICATION_NAME));
283 			reference.setDescription(element.getAttribute(DS_CONVERTER_SPECIFICATION_DESCRIPTION));
284 			try {
285 				reference.setSpecification((ConverterSpecification)element
286 				        .createExecutableExtension(DS_CONVERTER_SPECIFICATION_CLASS));
287 			} catch (CoreException e) {
288 				log.warn(String.format("Failed to instantiate ConverterSpecification provided by extension: %1$s ", //$NON-NLS-1$
289 				        element.getDeclaringExtension().getUniqueIdentifier()), e);
290 				continue;
291 			}
292 			references.add(reference);
293 		}
294 		return references;
295 	}
296 
297 }