Skip to main content
summaryrefslogtreecommitdiffstats
blob: 73052a5515cd6ef7bada3b6f1a2bdc4f65527827 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**********************************************************************
 * This file is part of "Object Teams Development Tooling"-Software
 * 
 * Copyright 2013, 2015 GK Software AG
 *  
 * 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
 * 
 * Please visit http://www.objectteams.org for updates and contact.
 * 
 * Contributors:
 * 	Stephan Herrmann - Initial API and implementation
 **********************************************************************/
package org.eclipse.objectteams.otequinox;

import static org.eclipse.objectteams.otequinox.Constants.TRANSFORMER_PLUGIN_ID;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.log.ExtendedLogReaderService;
import org.eclipse.equinox.log.ExtendedLogService;
import org.eclipse.equinox.log.LogFilter;
import org.eclipse.equinox.log.Logger;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.objectteams.internal.osgi.weaving.AspectBinding;
import org.eclipse.objectteams.internal.osgi.weaving.AspectBindingRegistry;
import org.eclipse.objectteams.internal.osgi.weaving.AspectPermissionManager;
import org.eclipse.objectteams.internal.osgi.weaving.OTWeavingHook;
import org.eclipse.objectteams.otre.ClassLoaderAccess;
import org.objectteams.Team;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.hooks.weaving.WeavingHook;
import org.osgi.framework.hooks.weaving.WovenClassListener;
import org.osgi.service.log.LogEntry;
import org.osgi.service.log.LogListener;
import org.osgi.util.tracker.ServiceTracker;

@NonNullByDefault
public class TransformerPlugin implements BundleActivator, IAspectRegistry {

    private static final String OTEQUINOX_AGENT_JAR_FILENAME = "otequinoxAgent.jar"; //$NON-NLS-1$
	private static final String OTEQUINOX_LOGGER_NAME = "org.eclipse.objectteams.otequinox.logger"; //$NON-NLS-1$

	/**
	 * State class representing the initialized state, i.e., after {@link start()}
	 * and {@link #initialize()} have been called.
	 */
	static class InitializedPlugin extends TransformerPlugin {

		AspectBindingRegistry aspectBindingRegistry;
		@Nullable AspectPermissionManager aspectPermissionManager;
		ILog log;
		List<Team> teamInstances = new ArrayList<>();
		
		public InitializedPlugin(AspectBindingRegistry aspectBindingRegistry, @Nullable AspectPermissionManager permissionManager, ILog log) {
			this.aspectBindingRegistry = aspectBindingRegistry;
			this.aspectPermissionManager = permissionManager;
			this.log = log;
		}

		@Override
		public boolean isDeniedAspectPlugin(String symbolicName) {
			final AspectPermissionManager manager = this.aspectPermissionManager;
			if (manager != null)
				return manager.isDeniedAspectPlugin(symbolicName);
			return false;
		}

		@Override
		public boolean isOTDT() {
			return this.aspectBindingRegistry.isOTDT();
		}

		@Override
		public boolean isAdaptedBasePlugin(@Nullable String baseBundleName) {
			return this.aspectBindingRegistry.isAdaptedBasePlugin(baseBundleName);
		}

		@Override
		public @Nullable String[] getAdaptedBasePlugins(Bundle aspectBundle) {
			return this.aspectBindingRegistry.getAdaptedBasePlugins(aspectBundle);
		}

		@Override
		public String[] getAdaptingAspectPlugins(@Nullable String id) {
			List<AspectBinding> aspectBindings = this.aspectBindingRegistry.getAdaptingAspectBindings(id);
			if (aspectBindings == null)
				return new String[0];
			String[] result = new String[aspectBindings.size()];
			for (int i = 0; i < result.length; i++)
				result[i] = aspectBindings.get(i).aspectPlugin;
			return result;
		}
	}
	private static @Nullable InitializedPlugin plugin;
	/**
	 * Single point of access: either we get a fully initialized instance, or ISE is thrown.
	 * @throws IllegalStateException if the plugin has not been initialized yet.
	 */
	private static InitializedPlugin plugin() {
		InitializedPlugin plugin = TransformerPlugin.plugin;
		if (plugin == null)
			throw notInitialized();
		return plugin;
	}
	
	static @Nullable BundleContext context;
	public static Bundle getBundle() {
		BundleContext context = TransformerPlugin.context;
		if (context != null)
			return context.getBundle();
		throw new IllegalStateException("TransformerPlugin has not been started");
	}

	private static List<IStatus> pendingLogEntries = new ArrayList<>();
	private static @Nullable URL agentURL; // null signals an error
	

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(final BundleContext bundleContext) throws Exception {
		TransformerPlugin.context = bundleContext;
	
		if (!"false".equals(System.getProperty("ot.equinox"))) {
			OTREInit();
			
			// register our weaving service:
			final OTWeavingHook otWeavingHook = new OTWeavingHook();
			bundleContext.registerService(new String[] { WeavingHook.class.getName(), WovenClassListener.class.getName() },
					otWeavingHook, null);
			
			// but wait until the extension registry is available for reading aspectBindings:
			try {
				ServiceReference<IExtensionRegistry> reference = bundleContext.getServiceReference(IExtensionRegistry.class);
				if (reference != null) {
					otWeavingHook.activate(bundleContext, reference);
				} else {
					bundleContext.addServiceListener(
						new ServiceListener() { 
							public void serviceChanged(ServiceEvent event) {
								if(event.getType() == ServiceEvent.REGISTERED)
									otWeavingHook.activate(bundleContext, bundleContext.getServiceReference(IExtensionRegistry.class));
							}
						},
						"(objectclass="+IExtensionRegistry.class.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			catch (InvalidSyntaxException ex) {
				log(ex, "Failed to register service listener");
			}
		}
		agentURL = bundleContext.getBundle().getEntry("/"+OTEQUINOX_AGENT_JAR_FILENAME);
		log(IStatus.INFO, "agentURL="+agentURL);
	}

	@SuppressWarnings("restriction")
	private static ILog acquireLog(BundleContext bundleContext) {
		ServiceTracker<ExtendedLogService,ExtendedLogService> tracker
				= new ServiceTracker<ExtendedLogService,ExtendedLogService>(bundleContext, ExtendedLogService.class, null);
		tracker.open();
		ExtendedLogService logService = tracker.getService();
		Bundle bundle = bundleContext.getBundle();
		Logger logger = logService.getLogger(bundle, OTEQUINOX_LOGGER_NAME);
		org.eclipse.core.internal.runtime.Log result = new org.eclipse.core.internal.runtime.Log(bundle, logger);

		ServiceTracker<ExtendedLogReaderService, ExtendedLogReaderService> logReaderTracker 
				= new ServiceTracker<ExtendedLogReaderService,ExtendedLogReaderService>(bundleContext, ExtendedLogReaderService.class.getName(), null);
		logReaderTracker.open();
		ExtendedLogReaderService logReader = logReaderTracker.getService();

		final Logger equinoxLogger = logService.getLogger(bundle, org.eclipse.core.internal.runtime.PlatformLogWriter.EQUINOX_LOGGER_NAME);

		// listen to log events from our logger and asynchronously dispatch them to the equinox logger
		logReader.addLogListener(
			new LogListener() {
				@Override @NonNullByDefault(false)
				public void logged(LogEntry entry) {
					equinoxLogger.log(entry.getLevel(), entry.getMessage(), entry.getException());
				}
			},
			new LogFilter() {
				@Override @NonNullByDefault(false)
				public boolean isLoggable(Bundle bundle, String loggerName, int logLevel) {
					return OTEQUINOX_LOGGER_NAME.equals(loggerName);
				}
			}
		);
		return result;
	}

	private void OTREInit() {
		// this influences the OTRE behavior (see e.g., JPLISEnhancer):
		System.setProperty("ot.equinox", "true");
		try {
			ClassLoaderAccess.setLoadClass(Bundle.class.getMethod("loadClass", String.class));
			ClassLoaderAccess.setGetResource(Bundle.class.getMethod("getResource", String.class));
		} catch (NoSuchMethodException | SecurityException e) {
			log(e, "Failed to wire an OSGi class into the OTRE");
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(@Nullable BundleContext bundleContext) throws Exception {
		plugin = null;
	}

	// configure OT/Equinox debugging:
	public static int WARN_LEVEL = IStatus.ERROR;
	static {
		String level = System.getProperty("otequinox.debug");
		if (level != null) {
			level = level.toUpperCase();
			if (level.equals("OK"))
				WARN_LEVEL = IStatus.OK;
			else if (level.equals("INFO"))
				WARN_LEVEL = IStatus.INFO;
			else if (level.startsWith("WARN"))
				WARN_LEVEL = IStatus.WARNING;
			else if (level.startsWith("ERR"))
				WARN_LEVEL = IStatus.ERROR;
			else
				WARN_LEVEL = IStatus.OK;
		}
	}

	public static void log (Throwable ex, String msg) {
		msg = "OT/Equinox: "+msg;
		Status status = new Status(IStatus.ERROR, TRANSFORMER_PLUGIN_ID, msg, ex);
		final InitializedPlugin plugin = TransformerPlugin.plugin;
		if (plugin != null) {
			plugin.log.log(status);
		} else {
			System.err.println(msg);
			ex.printStackTrace();
			synchronized (TransformerPlugin.class) {
				pendingLogEntries.add(status);
			}
		}
	}
	
	public static void log(int status, String msg) {
		if (status >= WARN_LEVEL)
			doLog(status, msg);
	}

	public static void doLog(int level, String msg) {
		try {
			Status status = new Status(level, TRANSFORMER_PLUGIN_ID, "OT/Equinox: "+msg);
			final InitializedPlugin plugin = TransformerPlugin.plugin;
			if (plugin != null) {
				plugin.log.log(status);
			} else {
				synchronized(TransformerPlugin.class) {
					pendingLogEntries.add(status);
				}
			}
		} catch (NoClassDefFoundError err) {
			if (level >= WARN_LEVEL)
				System.out.println(">> OT/Equinox: "+msg);
		}
	}
	
	public static void flushLog() {
		List<IStatus> copy;
		synchronized(TransformerPlugin.class) {
			copy = pendingLogEntries;
			pendingLogEntries = new ArrayList<>();
		}
		for (IStatus status : copy) {
			final InitializedPlugin plugin = TransformerPlugin.plugin;
			if (plugin != null) {
				plugin.log.log(status);
			} else {
				if (status.getCode() == IStatus.ERROR)
					System.err.println(status.getMessage());
				else
					System.out.println(status.getMessage());
			}
		}
	}
	
	public static void initialize(BundleContext bundleContext, AspectBindingRegistry aspectBindingRegistry, @Nullable AspectPermissionManager permissionManager) {
		plugin = new InitializedPlugin(aspectBindingRegistry, permissionManager, acquireLog(bundleContext));
	}

	/**
	 * Get the singleton instance of this class.
	 * <p>
	 * This method must not be called before the plugin is fully initialized, which depends
	 * on two triggers:
	 * </p>
	 * <ul>
	 * <li>This current plugin must be started by Equinox (should be guaranteed on access by Equinox).</li>
	 * <li>The extension registry has been started, which in turn triggers reading extensions against our extension points.</li>
	 * </ul>
	 */
	public static TransformerPlugin getDefault() {
		return plugin();
	}

	public static synchronized void registerTeamInstance(Team instance) {
		plugin().teamInstances.add(instance);
	}
	/**
	 * Copy all registered team instances into the given list,
     */
	public static synchronized void getTeamInstances(List<Team> list) {
		list.addAll(plugin().teamInstances);
	}

	/**
	 * public API:
	 * {@link IAspectRegistry#getAdaptingAspectPlugins(Bundle)} 
	 */
	public String[] getAdaptingAspectPlugins(Bundle basePlugin) {
		return getAdaptingAspectPlugins(basePlugin.getSymbolicName());
	}

	public String[] getAdaptingAspectPlugins(@Nullable String id) {
		throw notInitialized();
	}

	@Override
	public boolean isOTDT() {
		throw notInitialized();
	}

	@Override
	public boolean isAdaptedBasePlugin(@Nullable String baseBundleName) {
		throw notInitialized();
	}

	@Override
	public @Nullable String[] getAdaptedBasePlugins(Bundle aspectBundle) {
		throw notInitialized();
	}

	@Override
	public boolean hasInternalTeams(Bundle bundle) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isDeniedAspectPlugin(String symbolicName) {
		throw notInitialized();
	}

	static IllegalStateException notInitialized() {
		return new IllegalStateException("TransformerPlugin has not been initialized");
	}

	public static @Nullable String getOtequinoxAgentPath() {
		if (agentURL != null) {
			try {
				return new File(FileLocator.toFileURL(agentURL).getFile())
							.getAbsolutePath();
			} catch (IOException e) {
				log(new IllegalStateException(e), "Failed to intialize location of "+OTEQUINOX_AGENT_JAR_FILENAME);
			}
		} else {
			log(IStatus.ERROR, "Failed to intialize location of "+OTEQUINOX_AGENT_JAR_FILENAME);
		}
		return null;
	}
}

Back to the top