Skip to main content
summaryrefslogtreecommitdiffstats
blob: aa5c5b1a3779517435eee28fcb358817c8321cec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*******************************************************************************
 * Copyright (c) 2005 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Gerry Kessler - initial API and implementation
 *    Ian Trimble - JSFLibraryRegistry work
 *******************************************************************************/ 
package org.eclipse.jst.jsf.core.internal;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.InvalidRegistryObjectException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.ArchiveFile;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.JSFLibrary;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.JSFLibraryRegistry;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.JSFLibraryRegistryFactory;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.PluginProvidedJSFLibrary;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.adapter.MaintainDefaultImplementationAdapter;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.impl.JSFLibraryRegistryPackageImpl;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.util.JSFLibraryRegistryResourceFactoryImpl;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.util.JSFLibraryRegistryResourceImpl;
import org.eclipse.jst.jsf.core.internal.jsflibraryregistry.util.JSFLibraryRegistryUpgradeUtil;
import org.eclipse.jst.jsf.core.jsflibraryregistry.PluginProvidedJSFLibraryCreationHelper;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.eclipse.wst.common.frameworks.internal.WTPPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * JSF Core plugin.
 * 
 * @author Gerry Kessler - Oracle, Ian Trimble - Oracle
 */
public class JSFCorePlugin extends WTPPlugin {
	/**
	 * The plugin id
	 */
	public static final String PLUGIN_ID = "org.eclipse.jst.jsf.core";//org.eclipse.jst.jsf.core.internal.JSFCorePlugin"; //$NON-NLS-1$

	/**
	 * The JSF facet identifier
	 */
	public static final String FACET_ID = "jst.jsf"; //$NON-NLS-1$
	
	// The shared instance.
	private static JSFCorePlugin plugin;
	
	// The NS URI of the JSF Library Registry's Ecore package. (Must match
	// setting on package in Ecore model.)
	private static final String JSF_LIBRARY_REGISTRY_NSURI = "http://www.eclipse.org/webtools/jsf/schema/jsflibraryregistry.xsd"; //$NON-NLS-1$

	/**
	 * The id of the library extension point
	 */
	public static final String LIB_EXT_PT = "jsfLibraries"; //$NON-NLS-1$

	// The JSF Library Registry EMF resource instance.
	private JSFLibraryRegistryResourceImpl jsfLibraryRegistryResource = null;

	// The JSF Library Registry instance.
	private JSFLibraryRegistry jsfLibraryRegistry = null;

    private IPreferenceStore  preferenceStore;

	/**
	 * The constructor.
	 */
	public JSFCorePlugin() {
		plugin = this;
	}

	/**
	 * This method is called upon plug-in activation
	 * @param context 
	 * @throws Exception 
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		loadJSFLibraryRegistry();
	}

	/**
	 * This method is called when the plug-in is stopped
	 * @param context 
	 * @throws Exception 
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 * @return the shared instance
	 */
	public static JSFCorePlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns the JSFLibraryRegistry EMF object.
	 * 
	 * @return the JSFLibraryRegistry EMF object.
	 */
	public synchronized JSFLibraryRegistry getJSFLibraryRegistry() {
		return jsfLibraryRegistry;
	}

	/**
	 * Loads the JSFLibraryRegistry EMF object from plugin-specfic workspace
	 * settings location. (Called from start(BundleContext).)
	 */
	public void loadJSFLibraryRegistry() {
		try {
			
			EPackage.Registry.INSTANCE.put(JSF_LIBRARY_REGISTRY_NSURI, JSFLibraryRegistryPackageImpl.init());
			URI jsfLibRegURI = JSFLibraryRegistryUpgradeUtil.getRegistryURI(JSFLibraryRegistryUpgradeUtil.JSF_LIBRARY_REGISTRY_LATESTVERSION_URL);			
			JSFLibraryRegistryUpgradeUtil.getInstance().upgradeRegistryIfNecessary(JSFLibraryRegistryUpgradeUtil.LATESTVERSION);

			JSFLibraryRegistryResourceFactoryImpl resourceFactory = new JSFLibraryRegistryResourceFactoryImpl();
			jsfLibraryRegistryResource = (JSFLibraryRegistryResourceImpl)resourceFactory.createResource(jsfLibRegURI);
			try {
				Map options = new HashMap();
				//disable notifications during load to avoid changing stored default implementation
				options.put(XMLResource.OPTION_DISABLE_NOTIFY, Boolean.TRUE);
				jsfLibraryRegistryResource.load(options);
				jsfLibraryRegistry = (JSFLibraryRegistry)jsfLibraryRegistryResource.getContents().get(0);
			 	
				loadJSFLibraryExtensions();
				
			} catch(IOException ioe) {
				//Create a new Registry instance
				jsfLibraryRegistry = JSFLibraryRegistryFactory.eINSTANCE.createJSFLibraryRegistry();
				jsfLibraryRegistryResource = (JSFLibraryRegistryResourceImpl)resourceFactory.createResource(jsfLibRegURI);
				jsfLibraryRegistryResource.getContents().add(jsfLibraryRegistry);
				loadJSFLibraryExtensions();
				saveJSFLibraryRegistry();
			}
			//add adapter to maintain default implementation
			if (jsfLibraryRegistry != null) {
				//check that a default impl is set.   if not pick first one if available.
				JSFLibrary defLib = jsfLibraryRegistry.getDefaultImplementation();
				if (defLib == null && jsfLibraryRegistry.getImplJSFLibraries().size() > 0){
					jsfLibraryRegistry.setDefaultImplementation((JSFLibrary)jsfLibraryRegistry.getImplJSFLibraries().get(0));
					saveJSFLibraryRegistry();
				}
				jsfLibraryRegistry.eAdapters().add(MaintainDefaultImplementationAdapter.getInstance());

			}
		} catch(MalformedURLException mue) {
			log(IStatus.ERROR, Messages.JSFLibraryRegistry_ErrorCreatingURL, mue);
		}
	}




	/**
	 * Creates library registry items from extension points.
	 */
	private void loadJSFLibraryExtensions() {
		try {
			IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(JSFCorePlugin.PLUGIN_ID, JSFCorePlugin.LIB_EXT_PT);
			IExtension[] extensions = point.getExtensions();
			for (int i=0;i < extensions.length;i++){
				IExtension ext = extensions[i];
				for (int j=0;j < ext.getConfigurationElements().length;j++){
					PluginProvidedJSFLibraryCreationHelper newLibCreator = new PluginProvidedJSFLibraryCreationHelper(ext.getConfigurationElements()[j]);						
					JSFLibrary newLib = newLibCreator.create();
					
					/**
					 * Additional check on if a plug-in contributes jsflibraries is an expanded folder.
					 * Fix related to bug 144954.  
					 * 
					 * It would be ideal to check if a plug-in is distributed as a JAR 
					 * before a JSFLibrary is created.
					 * 
					 * This is a temporary solution since JARs in a JAR case is not 
					 * supported in this release.  Bug 14496.
					 */
					if (newLib != null && isJSFLibinExpandedFolder(newLib))
						jsfLibraryRegistry.addJSFLibrary(newLib);
				}
			}
		} catch (InvalidRegistryObjectException e) {
			log(IStatus.ERROR, Messages.JSFLibraryRegistry_ErrorLoadingFromExtPt, e);
		}
	}
	
	/**
	 * Saves the JSFLibraryRegistry EMF object from plugin-specfic workspace
	 * settings location. (Called from stop(BundleContext).)
	 * @return true if save is successful
	 */
	public boolean saveJSFLibraryRegistry() {
		boolean saved = false;
		if (jsfLibraryRegistryResource != null) {
			try {
				jsfLibraryRegistryResource.save(Collections.EMPTY_MAP);
				saved = true;
			} catch(IOException ioe) {
				log(IStatus.ERROR, Messages.JSFLibraryRegistry_ErrorSaving, ioe);
			}
		} else {
			log(IStatus.ERROR, Messages.JSFLibraryRegistry_ErrorSaving);
		}
		return saved;
	}

	/**
	 * @param e
	 * @param msg
	 */
	public static void log(final Exception e, final String msg) {
		final ILog log = getDefault().getLog();

		log.log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, msg, e));
	}

	/**
	 * Logs using the default ILog implementation provided by getLog().
	 * 
	 * @param severity Severity (IStatus constant) of log entry
	 * @param message Human-readable message describing log entry
	 * @param ex Throwable instance (can be null)
	 */
	public static void log(int severity, String message, Throwable ex) {
		getDefault().getLog().log(new Status(severity, PLUGIN_ID, IStatus.OK, message, ex));
	}

	/**
	 * Logs using the default ILog implementation provided by getLog().
	 * 
	 * @param severity Severity (IStatus constant) of log entry
	 * @param message Human-readable message describing log entry
	 */
	public static void log(int severity, String message) {
		log(severity, message, null);
	}

    /**
     * Logs a message for this plugin
     * 
     * @param message
     * @param t
     */
    public static void log(String message, Throwable t)
    {
        ILog log = plugin.getLog();
        log.log(
           new Status(
             IStatus.ERROR, plugin.getBundle().getSymbolicName(), 0, message, t));
    }
    
	public String getPluginID() {
		return PLUGIN_ID;
	}
	
	/**
	 * To check if a jsflibraries contribution plug-in is 
	 * distributed in a JAR.
	 * 
	 * This is temporary for bug 144954.  Need to be removed when 
	 * fixing 144996.
	 *    
	 * @param jsflib
	 * @return true if jsflib is in the expanded folder
	 */
	private boolean isJSFLibinExpandedFolder(JSFLibrary jsflib) {
		boolean exists = false;
		if (jsflib instanceof PluginProvidedJSFLibrary) { 		// No need to check probably  
			if (jsflib.getArchiveFiles().size() > 0) {
				ArchiveFile ar = (ArchiveFile) jsflib.getArchiveFiles().get(0);
				String resolvedSourceLocation = ar.getResolvedSourceLocation();
				if (resolvedSourceLocation != null) {
					exists = new File(resolvedSourceLocation).exists();
				}		
			}			
		}		
		return exists;
	}

    /**
     * @return all registered symbol source providers
     */
    public synchronized static Map getVariableResolvers()
    {
        if (_registeredVariableResolvers == null)
        {
            registerVariableResolverProviders();
            if (_registeredVariableResolvers == null)
            {
                throw new AssertionError("registerProviders failed");
            }
        }
        return Collections.unmodifiableMap(_registeredVariableResolvers);
    }
    
    private static Map    _registeredVariableResolvers;
    private final static String VARIABLE_RESOLVER_EXT_POINT_NAME = "variableresolver";
    
    private static void registerVariableResolverProviders()
    {
        _registeredVariableResolvers = new HashMap();
        loadRegisteredExtensions(VARIABLE_RESOLVER_EXT_POINT_NAME,
                                _registeredVariableResolvers,
                                 "variableresolver");
    }
    
    /**
     * @return a map of all registered property resolvers by id
     */
    public synchronized static Map getPropertyResolvers()
    {
        if (_registeredPropertyResolvers == null)
        {
            registerPropertyResolverProviders();
            if (_registeredPropertyResolvers == null)
            {
                throw new AssertionError("registerProviders failed");
            }
        }
        return Collections.unmodifiableMap(_registeredPropertyResolvers);
    }
    
    private static Map    _registeredPropertyResolvers;
    private final static String PROPERTY_RESOLVER_EXT_POINT_NAME = 
                                                             "propertyresolver";
    
    private static void registerPropertyResolverProviders()
    {
        _registeredPropertyResolvers = new HashMap();
        loadRegisteredExtensions(PROPERTY_RESOLVER_EXT_POINT_NAME,
                                _registeredPropertyResolvers,
                                 "propertyresolver");
    }
    
    
    /**
     * @return a map of all registered method resolvers by id
     */
    public synchronized static Map getMethodResolvers()
    {
        if (_registeredMethodResolvers == null)
        {
            registerMethodResolverProviders();
            if (_registeredMethodResolvers == null)
            {
                throw new AssertionError("registerProviders failed");
            }
        }
        return Collections.unmodifiableMap(_registeredMethodResolvers);
    }

    private static Map     _registeredMethodResolvers;
    private final static String METHOD_RESOLVER_EXT_POINT_NAME = 
                                                               "methodresolver";
    
    private static void registerMethodResolverProviders()
    {
        _registeredMethodResolvers = new HashMap();
        loadRegisteredExtensions(METHOD_RESOLVER_EXT_POINT_NAME,
                _registeredMethodResolvers,
                 "methodresolver");

    }

    /**
     * @return a map of all registered external context providers by id
     */
    public synchronized static Map getExternalContextProviders()
    {
        if (_registeredExternalContextProviders == null)
        {
            registerExternalContextProviders();
            if (_registeredExternalContextProviders == null)
            {
                throw new AssertionError("registerProviders failed");
            }
        }
        return Collections.unmodifiableMap(_registeredExternalContextProviders);
    }
    
    private static Map     _registeredExternalContextProviders;
    private final static String EXTERNAL_CONTEXT_EXT_POINT_NAME = 
                                                               "externalcontext";

    private static void registerExternalContextProviders()
    {
        _registeredExternalContextProviders = new HashMap();
        loadRegisteredExtensions(EXTERNAL_CONTEXT_EXT_POINT_NAME,
                                 _registeredExternalContextProviders,
                                 "externalcontext");
    }
    
    private static void loadRegisteredExtensions(final String extName,
                                                 final Map    registry,
                                                 final String elementName)
    {
        final IExtensionPoint point = Platform.getExtensionRegistry().
        getExtensionPoint(plugin.getBundle().getSymbolicName(), 
                extName);
        final IExtension[] extensions = point.getExtensions();

        for (int i = 0; i < extensions.length; i++)
        {
            final IExtension extension = extensions[i];
            final IConfigurationElement[] elements = 
                extension.getConfigurationElements();
            final String bundleId = extension.getContributor().getName();
            
            for (int j = 0; j < elements.length; j++)
            {
                final IConfigurationElement element = elements[j];
                if (elementName.equals(element.getName())
                        && element.getAttribute("class") != null
                        && element.getAttribute("id") != null)
                {
                    final String factoryClassName = element.getAttribute("class");
                    final String id = element.getAttribute("id");
                    final Bundle bundle = Platform.getBundle(bundleId);
                    
                    if (bundle != null)
                    {
                        try
                        {
                            final Class factoryClass = 
                                bundle.loadClass(factoryClassName);
                            
                            final Object variableResolver= 
                                factoryClass.newInstance();
    
                            registry.put(id, variableResolver);
                        }
                        catch (Exception e)
                        {
                            final ILog        logger_ = getDefault().getLog();
                            logger_.log(new Status(IStatus.ERROR, plugin.getBundle()
                                    .getSymbolicName(), 0, 
                                    "Error loading property resolver provider extension point",e));
                        }
                    }
                }
            }
        }
    }
    
    /**
     * @return the preference store for this bundle
     * TODO: this is copied from AbstractUIPlugin; need to upgrade to new IPreferencesService
     */
    public IPreferenceStore getPreferenceStore() {
        // Create the preference store lazily.
        if ( this.preferenceStore == null) {
            this.preferenceStore = new ScopedPreferenceStore(new InstanceScope(),getBundle().getSymbolicName());

        }
        return this.preferenceStore;
    }
}

Back to the top