Révision 297
| tmp/org.txm.rcp/src/main/java/org/txm/rcpapplication/utils/GSERunner.java (revision 297) | ||
|---|---|---|
| 2 | 2 |
|
| 3 | 3 |
import java.io.IOException; |
| 4 | 4 |
import java.util.HashMap; |
| 5 |
import java.util.HashSet; |
|
| 5 | 6 |
import java.util.Map; |
| 6 | 7 |
|
| 8 |
import org.eclipse.core.internal.runtime.InternalPlatform; |
|
| 9 |
import org.eclipse.core.runtime.FileLocator; |
|
| 10 |
import org.osgi.framework.Bundle; |
|
| 11 |
import org.osgi.framework.BundleContext; |
|
| 12 |
import org.osgi.framework.wiring.BundleWiring; |
|
| 13 |
import org.txm.rcpapplication.TXMClassLoader; |
|
| 14 |
import org.txm.utils.logger.Log; |
|
| 15 |
|
|
| 7 | 16 |
import groovy.lang.Binding; |
| 8 | 17 |
import groovy.util.GroovyScriptEngine; |
| 9 | 18 |
import groovy.util.ResourceException; |
| ... | ... | |
| 69 | 78 |
arrangedMap.put("args", new HashMap<String, Object>());
|
| 70 | 79 |
return run(clazz.getCanonicalName().replace(".", "/")+".groovy", new Binding(arrangedMap));
|
| 71 | 80 |
} |
| 81 |
|
|
| 82 |
/** |
|
| 83 |
* Creates a new GSERunner and adds to it the class loaders of all installed bundles of the platform. |
|
| 84 |
* @return a new GSERunner |
|
| 85 |
*/ |
|
| 86 |
public static GSERunner createGSERunner(String[] urls) {
|
|
| 87 |
|
|
| 88 |
GSERunner gse = null; |
|
| 89 |
|
|
| 90 |
// ArrayList<String> roots2 = new ArrayList<String>(200); |
|
| 91 |
// roots2.add(roots[0]); |
|
| 92 |
// roots2.add(roots[1]); |
|
| 93 |
// roots2.add(roots[2]); |
|
| 94 |
// |
|
| 95 |
// |
|
| 96 |
// HashMap<String, Bundle> bundleWithActivators = new HashMap<String, Bundle>(); |
|
| 97 |
HashSet<ClassLoader> loaders = new HashSet<ClassLoader>(); |
|
| 98 |
BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext(); |
|
| 99 |
// String[] bundlesRoots = new String[bundleContext.getBundles().length]; |
|
| 100 |
|
|
| 101 |
for (Bundle b : bundleContext.getBundles()) {
|
|
| 102 |
|
|
| 103 |
//FIXME: debug |
|
| 104 |
// System.out.println("ExecuteGroovyScript.executeScript(): bundle symbolic name: " + b.getSymbolicName());
|
|
| 105 |
// System.out.println("ExecuteGroovyScript.executeScript(): bundle id: " + b.getBundleId());
|
|
| 106 |
// System.out.println("ExecuteGroovyScript.executeScript(): bundle location: " + b.getLocation());
|
|
| 107 |
// try {
|
|
| 108 |
// System.out.println("ExecuteGroovyScript.executeScript(): bundle file location: " + FileLocator.getBundleFile(b).getAbsolutePath());
|
|
| 109 |
// } |
|
| 110 |
// catch(IOException e) {
|
|
| 111 |
// // TODO Auto-generated catch block |
|
| 112 |
// e.printStackTrace(); |
|
| 113 |
// } |
|
| 114 |
|
|
| 115 |
BundleWiring bundleWiring = b.adapt(BundleWiring.class); |
|
| 116 |
loaders.add(bundleWiring.getClassLoader()); |
|
| 117 |
|
|
| 118 |
//FIXME: debug |
|
| 119 |
System.out.println("GSERunner.createGSERunner(): class loader added: " + bundleWiring.getClassLoader());
|
|
| 120 |
// try {
|
|
| 121 |
// roots2.add(FileLocator.getBundleFile(b).getAbsolutePath()); |
|
| 122 |
// } |
|
| 123 |
// catch(IOException e) {
|
|
| 124 |
// // TODO Auto-generated catch block |
|
| 125 |
// e.printStackTrace(); |
|
| 126 |
// } |
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
// String acName = b.getHeaders().get("Bundle-Activator");
|
|
| 131 |
// if (acName != null) {
|
|
| 132 |
// if (!bundleWithActivators.containsKey(b.getSymbolicName())) {
|
|
| 133 |
// bundleWithActivators.put(b.getSymbolicName(),b); |
|
| 134 |
// } else {
|
|
| 135 |
// Bundle b2 = bundleWithActivators.get(b.getSymbolicName()); |
|
| 136 |
// if (b2.getVersion().compareTo(b.getVersion()) < 0) {
|
|
| 137 |
// bundleWithActivators.put(b.getSymbolicName(),b); |
|
| 138 |
// } |
|
| 139 |
// } |
|
| 140 |
// } |
|
| 141 |
} |
|
| 142 |
|
|
| 143 |
// for (Bundle b : bundleWithActivators.values()) {
|
|
| 144 |
// try {
|
|
| 145 |
// String acName = b.getHeaders().get("Bundle-Activator");
|
|
| 146 |
// Class<?> ac = b.loadClass(acName); |
|
| 147 |
// loaders.add(ac.getClassLoader()); |
|
| 148 |
// //FIXME: debug |
|
| 149 |
// System.out.println("ExecuteGroovyScript.executeScript(): loaders: " + ac.getClassLoader());
|
|
| 150 |
// } catch (ClassNotFoundException e) {
|
|
| 151 |
// e.printStackTrace(); |
|
| 152 |
// } |
|
| 153 |
// } |
|
| 154 |
Log.info("Initializing TXMClassLoader with " + loaders.size() + " bundles.");
|
|
| 155 |
TXMClassLoader scl = new TXMClassLoader(loaders); |
|
| 156 |
|
|
| 157 |
try {
|
|
| 158 |
gse = new GSERunner(urls, scl); |
|
| 159 |
// gse = new GSERunner(roots, ExecuteGroovyScript.class.getClassLoader()); |
|
| 160 |
//String[] roots3 = roots2.toArray(new String[roots2.size()]); |
|
| 161 |
//gse = new GSERunner(roots3, ExecuteGroovyScript.class.getClassLoader()); |
|
| 162 |
} catch (IOException e) {
|
|
| 163 |
e.printStackTrace(); |
|
| 164 |
} |
|
| 165 |
|
|
| 166 |
return gse; |
|
| 167 |
} |
|
| 168 |
|
|
| 72 | 169 |
} |
| tmp/org.txm.rcp/src/main/java/org/txm/rcpapplication/commands/ExecuteImportScript.java (revision 297) | ||
|---|---|---|
| 33 | 33 |
|
| 34 | 34 |
import java.io.File; |
| 35 | 35 |
import java.util.ArrayList; |
| 36 |
import java.util.HashMap; |
|
| 37 |
import java.util.HashSet; |
|
| 38 | 36 |
import java.util.logging.Level; |
| 39 | 37 |
|
| 40 | 38 |
import org.eclipse.core.commands.AbstractHandler; |
| 41 | 39 |
import org.eclipse.core.commands.ExecutionEvent; |
| 42 | 40 |
import org.eclipse.core.commands.ExecutionException; |
| 43 |
import org.eclipse.core.internal.runtime.InternalPlatform; |
|
| 44 | 41 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 45 | 42 |
import org.eclipse.core.runtime.IStatus; |
| 46 | 43 |
import org.eclipse.core.runtime.Platform; |
| ... | ... | |
| 53 | 50 |
import org.eclipse.swt.widgets.FileDialog; |
| 54 | 51 |
import org.eclipse.swt.widgets.Shell; |
| 55 | 52 |
import org.eclipse.ui.handlers.HandlerUtil; |
| 56 |
import org.osgi.framework.Bundle; |
|
| 57 |
import org.osgi.framework.BundleContext; |
|
| 58 | 53 |
import org.txm.Toolbox; |
| 59 | 54 |
import org.txm.importer.xtz.ImportKeys; |
| 60 | 55 |
import org.txm.objects.Base; |
| ... | ... | |
| 62 | 57 |
import org.txm.rcpapplication.Application; |
| 63 | 58 |
import org.txm.rcpapplication.Messages; |
| 64 | 59 |
import org.txm.rcpapplication.StatusLine; |
| 65 |
import org.txm.rcpapplication.TXMClassLoader; |
|
| 66 | 60 |
import org.txm.rcpapplication.commands.workspace.LoadBinaryCorpus; |
| 67 | 61 |
import org.txm.rcpapplication.preferences.ScriptPreferenceInitializer; |
| 68 | 62 |
import org.txm.rcpapplication.preferences.ScriptPreferencePage; |
| 63 |
import org.txm.rcpapplication.utils.GSERunner; |
|
| 69 | 64 |
import org.txm.rcpapplication.utils.JobHandler; |
| 70 | 65 |
import org.txm.rcpapplication.views.corpora.CorporaView; |
| 71 | 66 |
import org.txm.searchengine.cqp.corpus.MainCorpus; |
| ... | ... | |
| 165 | 160 |
ScriptPreferencePage.SCRIPT_ROOT_DIR, |
| 166 | 161 |
ScriptPreferenceInitializer.SCRIPT_ROOT_DIR_DEFAULT, null); |
| 167 | 162 |
|
| 168 |
//FIXME: old version |
|
| 169 | 163 |
// ClassLoader parent = ExecuteImportScript.class.getClassLoader(); |
| 170 | 164 |
// final GroovyClassLoader loader = new GroovyClassLoader(parent); |
| 171 |
// final String[] roots = new String[] {
|
|
| 172 |
// script.getParent(), |
|
| 173 |
// scriptRootDir }; |
|
| 174 | 165 |
|
| 175 |
params.load(); // reload params |
|
| 176 |
|
|
| 177 |
//FIXME: new version |
|
| 166 |
|
|
| 178 | 167 |
final String[] roots = new String[] {
|
| 179 |
scriptRootDir+"/user/", //$NON-NLS-1$ |
|
| 180 |
scriptRootDir+"/macro/", //$NON-NLS-1$ |
|
| 181 |
scriptRootDir+"/import/", //$NON-NLS-1$ |
|
| 182 |
}; |
|
| 183 |
System.err.println("ExecuteImportScript.executeScript(); retrieving bundles class loaders.");
|
|
| 184 |
HashMap<String, Bundle> bundleWithActivators = new HashMap<String, Bundle>(); |
|
| 185 |
HashSet<ClassLoader> loaders = new HashSet<ClassLoader>(); |
|
| 186 |
BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext(); |
|
| 187 |
for (Bundle b : bundleContext.getBundles()) {
|
|
| 188 |
String acName = b.getHeaders().get("Bundle-Activator");
|
|
| 189 |
if (acName != null) {
|
|
| 190 |
if (!bundleWithActivators.containsKey(b.getSymbolicName())) {
|
|
| 191 |
bundleWithActivators.put(b.getSymbolicName(),b); |
|
| 192 |
} else {
|
|
| 193 |
Bundle b2 = bundleWithActivators.get(b.getSymbolicName()); |
|
| 194 |
if (b2.getVersion().compareTo(b.getVersion()) < 0) {
|
|
| 195 |
bundleWithActivators.put(b.getSymbolicName(),b); |
|
| 196 |
} |
|
| 197 |
} |
|
| 198 |
} |
|
| 199 |
} |
|
| 168 |
script.getParent(), |
|
| 169 |
scriptRootDir }; |
|
| 200 | 170 |
|
| 201 |
for (Bundle b : bundleWithActivators.values()) {
|
|
| 202 |
try {
|
|
| 203 |
String acName = b.getHeaders().get("Bundle-Activator");
|
|
| 204 |
Class<?> ac = b.loadClass(acName); |
|
| 205 |
loaders.add(ac.getClassLoader()); |
|
| 206 |
//FIXME: debug |
|
| 207 |
System.out.println("ExecuteImportScript.executeScript(): " + ac.getClassLoader());
|
|
| 208 |
} catch (ClassNotFoundException e) {
|
|
| 209 |
e.printStackTrace(); |
|
| 210 |
} |
|
| 211 |
} |
|
| 212 |
Log.info("Initialized TXMClassLoader with "+loaders.size()+ " bundles.");
|
|
| 213 |
final TXMClassLoader loader = new TXMClassLoader(loaders); |
|
| 214 | 171 |
|
| 215 |
// FIXME: end of new version |
|
| 216 |
|
|
| 217 |
|
|
| 172 |
params.load(); // reload params |
|
| 173 |
|
|
| 218 | 174 |
// the binary directory which is going to be created |
| 219 | 175 |
|
| 220 | 176 |
final File basedir = new File(corporadir, params.name); |
| ... | ... | |
| 258 | 214 |
//org.txm.Toolbox.shutdownSearchEngine(); |
| 259 | 215 |
monitor.worked(5); |
| 260 | 216 |
|
| 261 |
//if (ExecuteImportScript.gse == null) |
|
| 262 |
ExecuteImportScript.gse = new GroovyScriptEngine(roots, loader); |
|
| 217 |
if (ExecuteImportScript.gse == null) {
|
|
| 218 |
//ExecuteImportScript.gse = new GroovyScriptEngine(roots, loader); |
|
| 219 |
ExecuteImportScript.gse = GSERunner.createGSERunner(roots); |
|
| 220 |
|
|
| 221 |
} |
|
| 263 | 222 |
|
| 264 | 223 |
Binding binding = new Binding(); |
| 265 | 224 |
binding.setProperty("paramsBinding", p); //$NON-NLS-1$
|
| tmp/org.txm.rcp/src/main/java/org/txm/rcpapplication/commands/ExecuteGroovyScript.java (revision 297) | ||
|---|---|---|
| 220 | 220 |
scriptRootDir+"/import/", //$NON-NLS-1$ |
| 221 | 221 |
}; |
| 222 | 222 |
|
| 223 |
// ArrayList<String> roots2 = new ArrayList<String>(200); |
|
| 224 |
// roots2.add(roots[0]); |
|
| 225 |
// roots2.add(roots[1]); |
|
| 226 |
// roots2.add(roots[2]); |
|
| 227 |
// |
|
| 228 |
// |
|
| 229 |
// HashMap<String, Bundle> bundleWithActivators = new HashMap<String, Bundle>(); |
|
| 230 |
HashSet<ClassLoader> loaders = new HashSet<ClassLoader>(); |
|
| 231 |
BundleContext bundleContext = InternalPlatform.getDefault().getBundleContext(); |
|
| 232 |
// String[] bundlesRoots = new String[bundleContext.getBundles().length]; |
|
| 233 |
|
|
| 234 |
for (Bundle b : bundleContext.getBundles()) {
|
|
| 235 |
|
|
| 236 |
//FIXME: debug |
|
| 237 |
System.out.println("ExecuteGroovyScript.executeScript(): bundle symbolic name: " + b.getSymbolicName());
|
|
| 238 |
System.out.println("ExecuteGroovyScript.executeScript(): bundle id: " + b.getBundleId());
|
|
| 239 |
System.out.println("ExecuteGroovyScript.executeScript(): bundle location: " + b.getLocation());
|
|
| 240 |
try {
|
|
| 241 |
System.out.println("ExecuteGroovyScript.executeScript(): bundle file location: " + FileLocator.getBundleFile(b).getAbsolutePath());
|
|
| 242 |
} |
|
| 243 |
catch(IOException e) {
|
|
| 244 |
// TODO Auto-generated catch block |
|
| 245 |
e.printStackTrace(); |
|
| 246 |
} |
|
| 247 | 223 |
|
| 248 |
BundleWiring bundleWiring = b.adapt(BundleWiring.class); |
|
| 249 |
loaders.add(bundleWiring.getClassLoader()); |
|
| 250 |
|
|
| 251 |
//FIXME: debug |
|
| 252 |
System.out.println("ExecuteGroovyScript.executeScript(): loaders: " + bundleWiring.getClassLoader());
|
|
| 253 |
// try {
|
|
| 254 |
// roots2.add(FileLocator.getBundleFile(b).getAbsolutePath()); |
|
| 255 |
// } |
|
| 256 |
// catch(IOException e) {
|
|
| 257 |
// // TODO Auto-generated catch block |
|
| 258 |
// e.printStackTrace(); |
|
| 259 |
// } |
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
// String acName = b.getHeaders().get("Bundle-Activator");
|
|
| 264 |
// if (acName != null) {
|
|
| 265 |
// if (!bundleWithActivators.containsKey(b.getSymbolicName())) {
|
|
| 266 |
// bundleWithActivators.put(b.getSymbolicName(),b); |
|
| 267 |
// } else {
|
|
| 268 |
// Bundle b2 = bundleWithActivators.get(b.getSymbolicName()); |
|
| 269 |
// if (b2.getVersion().compareTo(b.getVersion()) < 0) {
|
|
| 270 |
// bundleWithActivators.put(b.getSymbolicName(),b); |
|
| 271 |
// } |
|
| 272 |
// } |
|
| 273 |
// } |
|
| 274 |
} |
|
| 275 |
|
|
| 276 |
// for (Bundle b : bundleWithActivators.values()) {
|
|
| 277 |
// try {
|
|
| 278 |
// String acName = b.getHeaders().get("Bundle-Activator");
|
|
| 279 |
// Class<?> ac = b.loadClass(acName); |
|
| 280 |
// loaders.add(ac.getClassLoader()); |
|
| 281 |
// //FIXME: debug |
|
| 282 |
// System.out.println("ExecuteGroovyScript.executeScript(): loaders: " + ac.getClassLoader());
|
|
| 283 |
// } catch (ClassNotFoundException e) {
|
|
| 284 |
// e.printStackTrace(); |
|
| 285 |
// } |
|
| 286 |
// } |
|
| 287 |
Log.info("Initialized TXMClassLoader with "+loaders.size()+ " bundles.");
|
|
| 288 |
TXMClassLoader scl = new TXMClassLoader(loaders); |
|
| 224 |
gse = GSERunner.createGSERunner(roots); |
|
| 289 | 225 |
|
| 290 |
try {
|
|
| 291 |
gse = new GSERunner(roots, scl); |
|
| 292 |
// gse = new GSERunner(roots, ExecuteGroovyScript.class.getClassLoader()); |
|
| 293 |
//String[] roots3 = roots2.toArray(new String[roots2.size()]); |
|
| 294 |
//gse = new GSERunner(roots3, ExecuteGroovyScript.class.getClassLoader()); |
|
| 295 |
} catch (IOException e) {
|
|
| 296 |
e.printStackTrace(); |
|
| 297 |
} |
|
| 298 |
|
|
| 299 | 226 |
File jardir = new File(scriptRootDir,"lib"); //$NON-NLS-1$ |
| 300 | 227 |
if (jardir.exists() && jardir.isDirectory()) {
|
| 301 | 228 |
for (File f: jardir.listFiles(new FilenameFilter() {
|
Formats disponibles : Unified diff