1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.message.internal;
15 import java.util.Hashtable;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Plugin;
19 import org.eclipse.osgi.framework.log.FrameworkLog;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.simantics.message.ILogListener;
23 import org.simantics.message.ILogger;
24 import org.simantics.message.IMessageSchemeManager;
27 * The activator class controls the plug-in life cycle
29 @SuppressWarnings("restriction")
30 public class Activator extends Plugin {
32 private static final String DEFAULT_LOG_FILE_NAME = "messages.log";
35 public static final String PLUGIN_ID = "org.simantics.message";
37 private static final String LOGGER_NAME = "org.simantics.message.logger"; //$NON-NLS-1$
39 // The shared instance
40 private static Activator plugin;
41 private BundleContext bundleContext;
43 // The one and only message service instance
44 private Messages messagesService;
45 private MessageSchemeManager messageSchemeManagerService;
47 @SuppressWarnings("rawtypes")
48 private ServiceRegistration messages = null;
49 @SuppressWarnings("rawtypes")
50 private ServiceRegistration messageSchemeManager = null;
52 private LogWriter platformLog = null;
53 private FrameworkLog frameworkLog = null;
65 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
68 @SuppressWarnings({ "rawtypes", "unchecked" })
70 public void start(BundleContext context) throws Exception {
72 this.bundleContext = context;
74 messagesService = new Messages();
75 messageSchemeManagerService = new MessageSchemeManager();
77 messages = bundleContext.registerService(ILogger.class.getName(), messagesService, new Hashtable());
78 messageSchemeManager = bundleContext.registerService(IMessageSchemeManager.class.getName(), messageSchemeManagerService, new Hashtable<String, Object>());
80 // Attach a writer listener to the Messages ILog to actually get the logs stored on disk.
81 // NOTE: This must be the first listener for this ILog, to guarantee that the log on disk
82 // is always up-to-date if other ILogListeners want to read it. LogView does exactly this.
83 FrameworkLog log = getFrameworkLog();
85 platformLog = new LogWriter(getFrameworkLog());
86 addLogListener(platformLog);
97 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
101 public void stop(BundleContext context) throws Exception {
102 if (platformLog != null)
103 removeLogListener(platformLog);
104 if (frameworkLog != null)
105 frameworkLog.close();
107 if (messageSchemeManager != null) {
108 messageSchemeManager.unregister();
109 messageSchemeManager = null;
110 messageSchemeManagerService = null;
113 if (messages != null) {
114 messages.unregister();
116 messagesService = null;
119 bundleContext = null;
125 * Returns the shared instance
127 * @return the shared instance
129 public static Activator getDefault() {
134 * Returns the shared Simantics ILogger service instance.
136 * @return the shared message service
138 public ILogger getMessages() {
139 return messagesService;
143 * Add a listener to the Simantics platform ILogger service.
145 public void addLogListener(ILogListener listener) {
146 getMessages().addLogListener(listener);
150 * Remove a listener from the Simantics platform ILogger service.
152 public void removeLogListener(ILogListener listener) {
153 getMessages().removeLogListener(listener);
159 private FrameworkLog getFrameworkLog() {
160 if (frameworkLog == null) {
161 // TODO: make this a configurable preference, just like the osgi.logfile preference
162 IPath logLocation = getStateLocation().append(DEFAULT_LOG_FILE_NAME);
163 // TODO: need to clone EquinoxLogWriter and EquinoxLogFactory to this plugin
164 // EquinoxLogWriter logWriter = new EclipseLogWriter(logLocation.toFile(), LOGGER_NAME, true);
165 // LogServiceManager logServiceManager = new LogServiceManager(logWriter);
166 // EquinoxLogFactory logFactory = new EquinoxLogFactory(logWriter, logServiceManager);
167 // frameworkLog = logFactory.getService(getBundle(), null);
175 public static File getLogFile() {
176 return getDefault().getFrameworkLog().getFile();
180 * Returns the shared Simantics ILogger service instance.
182 * @return the shared message service
184 public IMessageSchemeManager getMessageSchemeManager() {
185 return messageSchemeManagerService;