]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message/src/org/simantics/message/internal/Activator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.message / src / org / simantics / message / internal / Activator.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.internal;
13
14 import java.io.File;
15 import java.util.Hashtable;
16
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;
25
26 /**
27  * The activator class controls the plug-in life cycle
28  */
29 @SuppressWarnings("restriction")
30 public class Activator extends Plugin {
31
32     private static final String  DEFAULT_LOG_FILE_NAME = "messages.log";
33
34     // The plug-in ID
35     public static final String   PLUGIN_ID             = "org.simantics.message";
36
37     private static final String  LOGGER_NAME           = "org.simantics.message.logger"; //$NON-NLS-1$
38
39     // The shared instance
40     private static Activator     plugin;
41     private BundleContext        bundleContext;
42
43     // The one and only message service instance
44     private Messages             messagesService;
45     private MessageSchemeManager messageSchemeManagerService;
46
47     @SuppressWarnings("rawtypes")
48     private ServiceRegistration  messages              = null;
49     @SuppressWarnings("rawtypes")
50     private ServiceRegistration  messageSchemeManager  = null;
51
52     private LogWriter            platformLog           = null;
53     private FrameworkLog         frameworkLog          = null;
54
55     /**
56      * The constructor
57      */
58     public Activator() {
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see
65      * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
66      * )
67      */
68     @SuppressWarnings({ "rawtypes", "unchecked" })
69     @Override
70     public void start(BundleContext context) throws Exception {
71         super.start(context);
72         this.bundleContext = context;
73
74         messagesService = new Messages();
75         messageSchemeManagerService = new MessageSchemeManager();
76
77         messages = bundleContext.registerService(ILogger.class.getName(), messagesService, new Hashtable());
78         messageSchemeManager = bundleContext.registerService(IMessageSchemeManager.class.getName(), messageSchemeManagerService, new Hashtable<String, Object>());
79
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();
84         if (log != null) {
85             platformLog = new LogWriter(getFrameworkLog());
86             addLogListener(platformLog);
87         } else
88             platformLog = null;
89
90         plugin = this;
91     }
92
93     /*
94      * (non-Javadoc)
95      * 
96      * @see
97      * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
98      * )
99      */
100     @Override
101     public void stop(BundleContext context) throws Exception {
102         if (platformLog != null)
103             removeLogListener(platformLog);
104         if (frameworkLog != null)
105             frameworkLog.close();
106
107         if (messageSchemeManager != null) {
108             messageSchemeManager.unregister();
109             messageSchemeManager = null;
110             messageSchemeManagerService = null;
111         }
112
113         if (messages != null) {
114             messages.unregister();
115             messages = null;
116             messagesService = null;
117         }
118
119         bundleContext = null;
120         plugin = null;
121         super.stop(context);
122     }
123
124     /**
125      * Returns the shared instance
126      * 
127      * @return the shared instance
128      */
129     public static Activator getDefault() {
130         return plugin;
131     }
132
133     /**
134      * Returns the shared Simantics ILogger service instance.
135      * 
136      * @return the shared message service
137      */
138     public ILogger getMessages() {
139         return messagesService;
140     }
141
142     /**
143      * Add a listener to the Simantics platform ILogger service.
144      */
145     public void addLogListener(ILogListener listener) {
146         getMessages().addLogListener(listener);
147     }
148
149     /**
150      * Remove a listener from the Simantics platform ILogger service.
151      */
152     public void removeLogListener(ILogListener listener) {
153         getMessages().removeLogListener(listener);
154     }
155
156     /**
157      * @return
158      */
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);
168         }
169         return frameworkLog;
170     }
171
172     /**
173      * @return
174      */
175     public static File getLogFile() {
176         return getDefault().getFrameworkLog().getFile();
177     }
178
179     /**
180      * Returns the shared Simantics ILogger service instance.
181      * 
182      * @return the shared message service
183      */
184     public IMessageSchemeManager getMessageSchemeManager() {
185         return messageSchemeManagerService;
186     }
187
188 }