Révision 3670

TXM/trunk/bundles/org.txm.groovy.core/plugin.xml (revision 3670)
23 23
            name="Groovy">
24 24
      </ScriptEngine>
25 25
   </extension>
26
   <extension
27
         point="org.eclipse.core.runtime.preferences">
28
      <initializer
29
            class="org.txm.groovy.core.GroovyPreferences">
30
      </initializer>
31
   </extension>
26 32

  
27 33
</plugin>
TXM/trunk/bundles/org.txm.groovy.core/META-INF/MANIFEST.MF (revision 3670)
1 1
Manifest-Version: 1.0
2
Export-Package: org.txm.groovy.core
2
Export-Package: org.txm.groovy.core,
3
 org.txm.groovy.core.messages
3 4
Bundle-SymbolicName: org.txm.groovy.core;singleton:=true
4 5
Bundle-Version: 1.0.0.qualifier
5 6
Bundle-Name: Macro
TXM/trunk/bundles/org.txm.groovy.core/src/java/org/txm/groovy/core/TXMClassLoader.java (revision 3670)
1 1
package org.txm.groovy.core;
2 2

  
3
import java.util.Dictionary;
3 4
import java.util.HashMap;
4 5
import java.util.HashSet;
5 6

  
6 7
import org.eclipse.osgi.internal.loader.EquinoxClassLoader;
7 8

  
9
import cern.colt.Arrays;
10

  
8 11
/**
9 12
 * Class loader that looking for a class in all installed RCP Bundle then load it.
10 13
 * 
......
12 15
 *
13 16
 */
14 17
public class TXMClassLoader extends ClassLoader {
15
	
18

  
16 19
	private HashMap<String, EquinoxClassLoader> loaders;
17
	
20

  
21
	private HashMap<String, HashSet<EquinoxClassLoader>> packageToLoaders = new HashMap<String, HashSet<EquinoxClassLoader>>();
22

  
18 23
	ClassLoader defaultLoader;
19
	
24

  
20 25
	public TXMClassLoader(ClassLoader defaultLoader, HashSet<EquinoxClassLoader> loaders2) {
21 26
		this.loaders = new HashMap<>();
27
		
28
		boolean useFastIndex = GroovyPreferences.getInstance().getBoolean(GroovyPreferences.FAST_PACKAGE_INDEX);
29
		
22 30
		for (EquinoxClassLoader loader : loaders2) {
31

  
23 32
			String k = loader.getBundle().getSymbolicName();
33

  
24 34
			loaders.put(k, loader);
35

  
36
			if (useFastIndex) {
37
				Dictionary<String, String> d = loader.getBundle().getHeaders();
38
				String packagesString = d.get("Export-Package");
39
				if (packagesString != null) {
40
					packagesString = packagesString.replace("\n", "");
41
					for (String p : packagesString.split(",")) {
42
						p = p.trim();
43
						if (p.contains(";")) p = p.substring(p.indexOf(";"));
44
						if (!packageToLoaders.containsKey(p)) {
45
							HashSet<EquinoxClassLoader> tmp = new HashSet<EquinoxClassLoader>();
46
							packageToLoaders.put(p, tmp);
47
						}
48
						packageToLoaders.get(p).add(loader);
49
					}
50
				}
51
			}
25 52
		}
53
		//		for(String p : packageToLoaders.keySet()) System.out.println(p);
26 54
		this.defaultLoader = defaultLoader;
27 55
	}
28
	
56

  
29 57
	@Override
30 58
	public Class<?> findClass(String name) throws ClassNotFoundException {
31 59

  
32 60
		// try with the System class loader
33 61
		try {
34
			return defaultLoader.loadClass(name);
62
			Class<?> c =  defaultLoader.loadClass(name);
63
			return c;
35 64
		}
36 65
		catch (Exception e) {
37 66
		}
38 67

  
68
		String p = null;
69
		int idx = name.lastIndexOf(".");
70
		if (idx > 0) {
71
			p = name.substring(0, idx);
72
		} else {
73
			p = "";
74
		}
75

  
76
		if (packageToLoaders.containsKey(p)) {
77
			for (EquinoxClassLoader loader : packageToLoaders.get(p)) {
78
				if (loader != null) {
79
					Class<?> c = loader.loadClass(name);
80
					//System.out.println("load from "+loaderName+": "+name);
81
					return c;
82
				}
83
			}
84
		}
85

  
39 86
		//TODO first tests did not boost the class loading
40
//		// try using the class package name
41
//		if (name.startsWith("org.txm.")) {
42
//			int idx = name.indexOf(".rcp.");
43
//			if (idx > 0) {
44
//				String loaderName = name.substring(0, idx+4);
45
//				EquinoxClassLoader loader = loaders.get(loaderName);
46
//				try {
47
//					if (loader != null) {
48
//						Class<?> c = loader.loadClass(name);
49
//						//System.out.println("load from "+loaderName+": "+name);
50
//						return c;
51
//					}
52
//				}
53
//				catch (Exception e) {
54
//					//System.out.println("can't load from "+loaderName+": "+name+" "+e);
55
//					if (e instanceof NullPointerException) {
56
//						e.printStackTrace();
57
//					}
58
//				}
59
//			}
60
//			idx = name.indexOf(".core.");
61
//			if (idx > 0) {
62
//				String loaderName =name.substring(0, idx)+".rcp";
63
//				EquinoxClassLoader loader = loaders.get(name.substring(0, idx)+".rcp");
64
//				try {
65
//					if (loader != null) {
66
//						Class<?> c = loader.loadClass(name);
67
//						//System.out.println("load from "+loaderName+": "+name);
68
//						return c;
69
//					}
70
//				}
71
//				catch (Exception e) {
72
//					//System.out.println("can't load from "+loaderName+": "+name+" "+e);
73
//					if (e instanceof NullPointerException) {
74
//						e.printStackTrace();
75
//					}
76
//				}
77
//				
78
//				loaderName = name.substring(0, idx+5);
79
//				loader = loaders.get(loaderName);
80
//				try {
81
//					if (loader != null) {
82
//						Class<?> c = loader.loadClass(name);
83
//						//System.out.println("load from "+loaderName+": "+name);
84
//						return c;
85
//					}
86
//				}
87
//				catch (Exception e) {
88
//					//System.out.println("can't load from "+loaderName+": "+name+" "+e);
89
//					if (e instanceof NullPointerException) {
90
//						e.printStackTrace();
91
//					}
92
//				}
93
//			}
94
//		}
95
		
87
		//		// try using the class package name
88
		//		if (name.startsWith("org.txm.")) {
89
		//			int idx = name.indexOf(".rcp.");
90
		//			if (idx > 0) {
91
		//				String loaderName = name.substring(0, idx+4);
92
		//				EquinoxClassLoader loader = loaders.get(loaderName);
93
		//				try {
94
		//					if (loader != null) {
95
		//						Class<?> c = loader.loadClass(name);
96
		//						//System.out.println("load from "+loaderName+": "+name);
97
		//						return c;
98
		//					}
99
		//				}
100
		//				catch (Exception e) {
101
		//					//System.out.println("can't load from "+loaderName+": "+name+" "+e);
102
		//					if (e instanceof NullPointerException) {
103
		//						e.printStackTrace();
104
		//					}
105
		//				}
106
		//			}
107
		//			idx = name.indexOf(".core.");
108
		//			if (idx > 0) {
109
		//				String loaderName =name.substring(0, idx)+".rcp";
110
		//				EquinoxClassLoader loader = loaders.get(name.substring(0, idx)+".rcp");
111
		//				try {
112
		//					if (loader != null) {
113
		//						Class<?> c = loader.loadClass(name);
114
		//						//System.out.println("load from "+loaderName+": "+name);
115
		//						return c;
116
		//					}
117
		//				}
118
		//				catch (Exception e) {
119
		//					//System.out.println("can't load from "+loaderName+": "+name+" "+e);
120
		//					if (e instanceof NullPointerException) {
121
		//						e.printStackTrace();
122
		//					}
123
		//				}
124
		//				
125
		//				loaderName = name.substring(0, idx+5);
126
		//				loader = loaders.get(loaderName);
127
		//				try {
128
		//					if (loader != null) {
129
		//						Class<?> c = loader.loadClass(name);
130
		//						//System.out.println("load from "+loaderName+": "+name);
131
		//						return c;
132
		//					}
133
		//				}
134
		//				catch (Exception e) {
135
		//					//System.out.println("can't load from "+loaderName+": "+name+" "+e);
136
		//					if (e instanceof NullPointerException) {
137
		//						e.printStackTrace();
138
		//					}
139
		//				}
140
		//			}
141
		//		}
142

  
96 143
		// last resort test all loaders
97 144
		for (EquinoxClassLoader cl : loaders.values()) {
98
			
145

  
99 146
			// FIXME: SJ: tests to speed up this very long process
100 147
			// if(cl instanceof EquinoxClassLoader) {
101 148
			// EquinoxClassLoader ecl = (EquinoxClassLoader) cl;
......
111 158
			// }
112 159
			// }
113 160
			//
114
			
161

  
115 162
			try {
116 163
				return cl.loadClass(name);
117 164
			}
TXM/trunk/bundles/org.txm.groovy.core/src/java/org/txm/groovy/core/GroovyPreferences.java (revision 3670)
7 7
	public static final String PREFERENCES_NODE = GroovyPreferences.class.getName();// FrameworkUtil.getBundle(RPreferences.class).getSymbolicName();
8 8
	
9 9
	public static final String VERSION = "version"; //$NON-NLS-1$
10

  
11
	public static String FAST_PACKAGE_INDEX = "fast_package_index";
10 12
	
11 13
	/**
12 14
	 * Gets the instance.
......
23 25
	@Override
24 26
	public void initializeDefaultPreferences() {
25 27
		super.initializeDefaultPreferences();
28
		
29
		this.put(FAST_PACKAGE_INDEX, true);
26 30
	}
27 31
}
TXM/trunk/bundles/org.txm.groovy.core/src/java/org/txm/groovy/core/GSERunner.java (revision 3670)
3 3
import java.io.File;
4 4
import java.io.FilenameFilter;
5 5
import java.io.IOException;
6
import java.io.Reader;
7
import java.lang.reflect.Field;
6 8
import java.net.MalformedURLException;
9
import java.net.URL;
10
import java.net.URLClassLoader;
7 11
import java.util.HashMap;
8 12
import java.util.HashSet;
9 13
import java.util.Map;
......
17 21
import org.osgi.framework.wiring.BundleWiring;
18 22
import org.txm.Toolbox;
19 23
import org.txm.utils.BundleUtils;
24
import org.txm.utils.DeleteDir;
20 25
import org.txm.utils.logger.Log;
21 26

  
22 27
import cern.colt.Arrays;
23 28
import groovy.lang.Binding;
29
import groovy.lang.GroovyClassLoader;
24 30
import groovy.lang.GroovyShell;
25 31
import groovy.util.GroovyScriptEngine;
26 32
import groovy.util.ResourceException;
......
93 99
		configuration.addCompilationCustomizers(imports);
94 100
		
95 101
		defaultGSE.setConfig(configuration);
102

  
103
		// FIXME MD classes file are created but the GSE don't read from it and rewrite the class files
104
//		File classesDir = new File(Toolbox.getTxmHomePath()+"/scripts/groovy/classes");
105
//		classesDir.mkdirs();
106
//		configuration.setTargetDirectory(classesDir);
107
//		configuration.setRecompileGroovySource(false);
108
//		defaultGSE.reloadCaches();	
96 109
		
97 110
		File jardir = new File(rootDir, "lib"); //$NON-NLS-1$
98 111
		try {
......
128 141
		return defaultGSE;
129 142
	}
130 143
	
144
	/**
145
	 * reload classes from the target directory in the sourceCache
146
	 */
147
	public void reloadCaches() {
148
		GroovyClassLoader gcl = this.getGroovyClassLoader();
149
		// get field sourceCache
150
		Field field;
151
		try {
152
			field = gcl.getClass().getSuperclass().getDeclaredField("sourceCache");
153
			field.setAccessible(true);
154
			Map<String, Class> sourceCache = (Map<String, Class>) field.get(gcl);
155
			
156
			File target = this.getConfig().getTargetDirectory();
157
			URLClassLoader classLoader = new URLClassLoader(new URL[]{target.toURI().toURL()}, Thread.currentThread().getContextClassLoader());
158
					
159
			for (File clazzFile : DeleteDir.scanDirectory(target, true, true)) {
160
				if (!clazzFile.getName().endsWith(".class")) continue;
161
				String fullclassname = clazzFile.getAbsolutePath().substring(target.getAbsolutePath().length()+1);
162
				fullclassname = fullclassname.replace("/", ".");
163
				fullclassname = fullclassname.replace("\\", ".");
164
				fullclassname = fullclassname.substring(0, fullclassname.length()-6);
165
				Class<?> clazz = Class.forName(fullclassname, true, classLoader);
166
				sourceCache.put(clazzFile.getAbsolutePath(), clazz);
167
			}
168
			
169
			//this.getConfig().setTargetDirectory((File)null);
170
		} catch (Exception e) {
171
			// TODO Auto-generated catch block
172
			e.printStackTrace();
173
		}
174
		
175
	}
176

  
131 177
	public Object evaluate(String code) {
132 178
		return new GroovyShell(this.getGroovyClassLoader(), new Binding()).evaluate(code);
133 179
	}
......
171 217
		for (Bundle b : bundles) {
172 218
			if (b == null) continue;
173 219
			String name = b.getSymbolicName();
174
			if (!name.startsWith("org.txm")) continue; // only keep txm plugins (they contains the necessary plugins deps)
175
			if (name.endsWith(".core")) continue; // usually TXM *.rcp plugins depends on the *.core plugins
220
			//if (!name.startsWith("org.txm")) continue; // only keep txm plugins (they contains the necessary plugins deps)
221
			//if (name.endsWith(".core")) continue; // usually TXM *.rcp plugins depends on the *.core plugins
176 222
			if (defaultPlugins.contains(name)) continue;
177 223
			
178 224
			BundleWiring bundleWiring = b.adapt(BundleWiring.class);
179 225
			if (bundleWiring == null) {
180 226
				// System.out.println("GSERunner.createGSERunner(): NO wiring: " + b.getSymbolicName());
181 227
			}
182
			else if (bundleWiring.getClassLoader() != null) {
228
			else if (bundleWiring.getClassLoader() != null  && bundleWiring.getClassLoader() instanceof EquinoxClassLoader) {
183 229
				EquinoxClassLoader loader = (EquinoxClassLoader) bundleWiring.getClassLoader();
184 230
				
185 231
				loaders.add(loader);
TXM/trunk/bundles/org.txm.groovy.rcp/src/org/txm/groovy/rcp/GroovyPreferencePage.java (revision 3670)
1 1
package org.txm.groovy.rcp;
2 2

  
3
import org.eclipse.jface.preference.BooleanFieldEditor;
3 4
import org.eclipse.jface.preference.StringFieldEditor;
4 5
import org.eclipse.ui.IWorkbench;
5 6
import org.txm.groovy.core.GroovyPreferences;
......
15 16
	@Override
16 17
	protected void createFieldEditors() {
17 18
		//this.addField(new StringFieldEditor(GroovyPreferences.ADDITIONAL_ROOT_DIRECTORY, "Additional root directory", this.getFieldEditorParent()));
19
		
20
		this.addField(new BooleanFieldEditor(GroovyPreferences.FAST_PACKAGE_INDEX, "Use fast package index (speed-up scripts compilation)", this.getFieldEditorParent()));
18 21
	}
19 22
	
20 23
	@Override
TXM/trunk/bundles/org.txm.groovy.rcp/plugin.xml (revision 3670)
4 4
   <extension
5 5
         point="org.eclipse.ui.preferencePages">
6 6
      <page
7
            class="org.txm.groovy.rcp.WorkbenchPreferencePage1"
8
            id="org.txm.groovy.rcp.page1"
7
            category="org.txm.rcp.preferences.ScriptPreferencePage"
8
            class="org.txm.groovy.rcp.GroovyPreferencePage"
9
            id="org.txm.groovy.rcp.GroovyPreferencePage"
9 10
            name="Groovy">
10 11
      </page>
11 12
   </extension>
TXM/trunk/bundles/org.txm.groovy.rcp/META-INF/MANIFEST.MF (revision 3670)
3 3
Bundle-Name: org.txm.groovy.rcp
4 4
Bundle-SymbolicName: org.txm.groovy.rcp;singleton:=true
5 5
Bundle-Version: 1.0.0.qualifier
6
Export-Package: org.txm.groovy.rcp
6 7
Bundle-Vendor: textometrie.org
7 8
Automatic-Module-Name: org.txm.groovy.rcp
8 9
Bundle-RequiredExecutionEnvironment: JavaSE-1.8

Formats disponibles : Unified diff