View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander;
23  
24  import java.io.IOException;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  
28  import org.apache.log4j.Logger;
29  import org.apache.log4j.PropertyConfigurator;
30  import org.eclipse.core.resources.IResource;
31  import org.eclipse.core.resources.ResourcesPlugin;
32  import org.eclipse.core.runtime.FileLocator;
33  import org.eclipse.jface.preference.IPreferenceStore;
34  import org.eclipse.jface.resource.ImageDescriptor;
35  import org.eclipse.ui.plugin.AbstractUIPlugin;
36  import org.fosstrak.llrp.commander.check.CheckEclipseProject;
37  import org.fosstrak.llrp.commander.check.CheckRepository;
38  import org.fosstrak.llrp.commander.check.HealthCheck;
39  import org.fosstrak.llrp.commander.preferences.PreferenceConstants;
40  import org.fosstrak.llrp.commander.util.Utility;
41  import org.osgi.framework.BundleContext;
42  
43  /**
44   * The activator class controls the plug-in life cycle
45   *
46   * @author Haoning Zhang
47   * @version 1.0
48   */
49  
50  public class LLRPPlugin extends AbstractUIPlugin {
51  
52  	/**
53  	 * Log4j instance.
54  	 */
55  	private static Logger log = Logger.getLogger(LLRPPlugin.class);
56  	
57  	/**
58  	 * The Eclipse Plug-in ID
59  	 */
60  	public static final String PLUGIN_ID = "llrp_commander";
61  
62  	// The shared instance
63  	private static LLRPPlugin plugin;
64  	
65  	/**
66  	 * The constructor
67  	 */
68  	public LLRPPlugin() {
69  		
70  		String prop = Utility.findWithFullPath("/log4j.properties");
71      	// initialize the log4j facilities.
72      	PropertyConfigurator.configure(prop);
73  	}
74  
75  	/*
76  	 * (non-Javadoc)
77  	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
78  	 */
79  	public void start(BundleContext context) throws Exception {
80  		super.start(context);
81  		plugin = this;
82  		
83  		IPreferenceStore store = LLRPPlugin.getDefault().getPreferenceStore();
84  		String storedProjectName = store.getString(PreferenceConstants.P_PROJECT);
85  		
86  		if ((storedProjectName != null) || (!storedProjectName.equals(""))) {
87  			ResourceCenter.getInstance().setEclipseProjectName(storedProjectName);
88  		}
89  		
90  		/*
91  		if ((readerDefFilename != null) || (!readerDefFilename.equals(""))) {
92  			ResourceCenter.getInstance().setReaderDefinitionFilename(readerDefFilename);
93  			AdaptorManagement.loadFromFile(readerDefFilename);
94  		}*/
95  		
96  		/* swieland deactivate the window and auto fix...
97  		HealthCheckDialog dlg = new HealthCheckDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
98  		if (!dlg.getHealthCheck().validate()) {
99  			dlg.open();
100 		*/
101 				
102 		HealthCheck healthCheck = new HealthCheck();
103 		healthCheck.registerCheckItem(new CheckEclipseProject());
104 		healthCheck.registerCheckItem(new CheckRepository());
105 		ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
106 		if (!healthCheck.validate()) {
107 			for (String report : healthCheck.getReport()) {
108 				log.debug(report);
109 			}
110 			healthCheck.fix();
111 			ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
112 			for (String report : healthCheck.getReport()) {
113 				log.debug(report);
114 			}
115 		}
116 		
117 		// now all the path should be OK and it is safe to start the management.
118 		ResourceCenter.getInstance().initializeAdaptorMgmt();
119 		
120 		// and now initialize the logging facility
121 		ResourceCenter.getInstance().initializeROAccessReportsLogging();
122 	}
123 
124 	/*
125 	 * (non-Javadoc)
126 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
127 	 */
128 	public void stop(BundleContext context) throws Exception {
129 		log.info("Stopping the Plug-In, and disposing resources...");
130 		
131 		ResourceCenter.getInstance().getMessageBoxRefresh().stop();
132 		plugin = null;
133 		super.stop(context);
134 		
135 		//String defFilename = ResourceCenter.getInstance().getReaderDefinitionFilename();
136 		//log.info("Store reader configuration into " + defFilename + "...");
137 		
138 		//AdaptorManagement.storeToFile(defFilename);
139 		ResourceCenter.getInstance().close();
140 	}
141 
142 	/**
143 	 * Returns the shared instance
144 	 *
145 	 * @return the shared instance
146 	 */
147 	public static LLRPPlugin getDefault() {
148 		return plugin;
149 	}
150 
151 	/**
152 	 * Returns an image descriptor for the image file at the given
153 	 * plug-in relative path
154 	 *
155 	 * @param path the path
156 	 * @return the image descriptor
157 	 */
158 	public static ImageDescriptor getImageDescriptor(String path) {
159 		return imageDescriptorFromPlugin(PLUGIN_ID, path);
160 	}
161 	
162 	public void getResourceFile(String aRelativePath) {
163 		URL bundleRoot = getBundle().getEntry("/");
164 		
165 		try {
166 			URL fileURL = FileLocator.toFileURL(bundleRoot);
167 			java.io.File file = new java.io.File(fileURL.toURI());
168 			
169 		} catch (IOException ioe) {
170 			ioe.printStackTrace();
171 		} catch (URISyntaxException urie) {
172 			urie.printStackTrace();
173 		} 
174 
175 		//System.out.println("Bundle location:" + file.getAbsolutePath());
176 	}
177 }