Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 05f63a1ab7b113cc3ae1fb3a12434ab51c04e8d5 (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
/******************************************************************************
 * Copyright (c) 2005, 2007 BEA Systems, Inc.
 * 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:
 *    Konstantin Komissarchik
 ******************************************************************************/

package org.eclipse.jst.j2ee.internal.archive;

import static org.eclipse.jst.j2ee.internal.archive.ArchiveExportParticipantsExtensionPoint.PluginUtil.*;
import static org.eclipse.jst.j2ee.internal.plugin.J2EEPlugin.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionConverter;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jst.j2ee.archive.IArchiveExportParticipant;
import org.eclipse.jst.j2ee.internal.archive.ArchiveExportParticipantsExtensionPoint.PluginUtil.InvalidExtensionException;
import org.eclipse.osgi.util.NLS;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntimeComponent;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntimeComponentType;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntimeComponentVersion;
import org.eclipse.wst.common.project.facet.core.runtime.RuntimeManager;
import org.osgi.framework.Bundle;

/**
 * Contains the logic for processing the <code>org.eclipse.jst.j2ee.archiveExportParticipants</code>
 * extension point. 
 * 
 * @author <a href="mailto:kosta@bea.com">Konstantin Komissarchik</a>
 */

public final class ArchiveExportParticipantsExtensionPoint
{
    public static final String EXTENSION_POINT_ID = "archiveExportParticipants"; //$NON-NLS-1$

    private static final String EL_PARTICIPANT = "participant"; //$NON-NLS-1$
    private static final String EL_RUNTIME_COMPONENT = "runtime-component"; //$NON-NLS-1$
    private static final String EL_FACTORY = "factory"; //$NON-NLS-1$
    private static final String EL_ENABLEMENT = "enablement"; //$NON-NLS-1$
    private static final String ATTR_ID = "id"; //$NON-NLS-1$
    private static final String ATTR_TYPE = "type"; //$NON-NLS-1$
    private static final String ATTR_VERSION = "version"; //$NON-NLS-1$
    private static final String ATTR_CLASS = "class"; //$NON-NLS-1$
    
    private static List<ParticipantInfo> extensions = null;
    
    public static class ParticipantInfo
    {
        private String id;
        private String pluginId = null;
        private String className = null;
        private IRuntimeComponentType runtimeComponentType = null;
        private String runtimeComponentVersionExpr = null;
        private Expression enablementCondition = null;
        
        public String getId()
        {
            return this.id;
        }
        
        public IArchiveExportParticipant loadParticipant()
        {
            try
            {
                return instantiate( this.pluginId, this.className, IArchiveExportParticipant.class );
            }
            catch( CoreException e )
            {
                log( e.getStatus() );
                return null;
            }
        }
    }
    
    public static List<ParticipantInfo> getExtensions( final IProject project,
                                                       final IRuntime runtime )
    {
        readExtensions();
        
        final List<ParticipantInfo> result = new ArrayList<ParticipantInfo>();
        
        for( ParticipantInfo partInfo : extensions )
        {
            boolean match = false;
            
            for( IRuntimeComponent rc : runtime.getRuntimeComponents() )
            {
                final IRuntimeComponentType rct = rc.getRuntimeComponentType();
                
                if( rct == partInfo.runtimeComponentType )
                {
                    final IRuntimeComponentVersion rcv = rc.getRuntimeComponentVersion();
                    
                    try
                    {
                        if( rct.getVersions( partInfo.runtimeComponentVersionExpr ).contains( rcv ) )
                        {
                            match = true;
                            break;
                        }
                    }
                    catch( CoreException e )
                    {
                        logError( -1, e.getMessage(), e );
                    }
                }
            }
            
            if( match && partInfo.enablementCondition != null )
            {
                final EvaluationContext evalContext = new EvaluationContext( null, project );
                evalContext.setAllowPluginActivation( true );
            
                try
                {
                    final EvaluationResult evalResult 
                        = partInfo.enablementCondition.evaluate( evalContext );
                    
                    if( evalResult != EvaluationResult.TRUE )
                    {
                        match = false;
                    }
                }
                catch( CoreException e )
                {
                    logError( -1, e.getMessage(), e );
                }
            }
            
            if( match )
            {
                result.add( partInfo );
            }
        }
        
        return Collections.unmodifiableList( result );
    }
                                                                             
    private static synchronized void readExtensions()
    {
        if( extensions != null )
        {
            return;
        }
        
        extensions = new ArrayList<ParticipantInfo>();
        
        for( IConfigurationElement element 
             : getTopLevelElements( findExtensions( PLUGIN_ID, EXTENSION_POINT_ID ) ) )
        {
            if( element.getName().equals( EL_PARTICIPANT ) )
            {
                try
                {
                    readExtension( element );
                }
                catch( InvalidExtensionException e )
                {
                    // Continue. The problem has been reported to the user via the log.
                }
            }
        }
    }

    private static void readExtension( final IConfigurationElement config )
    
        throws InvalidExtensionException
        
    {
        final ParticipantInfo info = new ParticipantInfo();

        info.pluginId = config.getContributor().getName();
        info.id = info.pluginId + "." + findRequiredAttribute( config, ATTR_ID ); //$NON-NLS-1$
        
        final IConfigurationElement elFactory = findRequiredElement( config, EL_FACTORY );
        info.className = findRequiredAttribute( elFactory, ATTR_CLASS );
        
        final IConfigurationElement elRuntimeComponent 
            = findRequiredElement( config, EL_RUNTIME_COMPONENT );
        
        final String rcTypeString = findRequiredAttribute( elRuntimeComponent, ATTR_TYPE );
        
        if( ! RuntimeManager.isRuntimeComponentTypeDefined( rcTypeString ) )
        {
            // TODO: Log the problem
            throw new InvalidExtensionException();
        }
        
        info.runtimeComponentType = RuntimeManager.getRuntimeComponentType( rcTypeString );
        info.runtimeComponentVersionExpr = elRuntimeComponent.getAttribute( ATTR_VERSION );
        
        final IConfigurationElement elEnablement = findOptionalElement( config, EL_ENABLEMENT );
        
        try
        {
            info.enablementCondition = ExpressionConverter.getDefault().perform( elEnablement );
        }
        catch( CoreException e )
        {
            logError( -1, e.getMessage(), e );
            throw new InvalidExtensionException();
        }
        
        extensions.add( info );
    }
    
    /**
     * Utility methods that are helpful for implementing extension points.
     *
     * @author <a href="mailto:kosta@bea.com">Konstantin Komissarchik</a>
     */

    public static final class PluginUtil
    {
        public static final class InvalidExtensionException

            extends Exception
        
        {
            private static final long serialVersionUID = 1L;
        }
        
        private PluginUtil() {}
        
        public static Collection<IExtension> findExtensions( final String pluginId,
                                                             final String extensionPointId )
        {
            final IExtensionRegistry registry = Platform.getExtensionRegistry();
            final IExtensionPoint point = registry.getExtensionPoint( pluginId, extensionPointId );
            
            if( point == null )
            {
                throw new RuntimeException();
            }
            
            final List<IExtension> extensions = new ArrayList<IExtension>();
            
            for( IExtension extension : point.getExtensions() )
            {
                extensions.add( extension );
            }
            
            return extensions;
        }
        
        public static Collection<IConfigurationElement> getTopLevelElements( final Collection<IExtension> extensions )
        {
            final List<IConfigurationElement> elements = new ArrayList<IConfigurationElement>();
            
            for( IExtension extension : extensions )
            {
                for( IConfigurationElement element : extension.getConfigurationElements() )
                {
                    elements.add( element );
                }
            }
            
            return elements;
        }

        public static void reportMissingAttribute( final IConfigurationElement el,
                                                   final String attribute )
        {
            final String pluginId = el.getContributor().getName();
            final String extPointId = el.getDeclaringExtension().getExtensionPointUniqueIdentifier();
            
            final String msg
                = Resources.bind( Resources.missingAttribute, pluginId, extPointId, el.getName(), 
                                  attribute );

            log( createErrorStatus( -1, msg, null ) );
        }

        public static void reportMissingElement( final IConfigurationElement el,
                                                 final String element )
        {
            final String pluginId = el.getContributor().getName();
            final String extPointId = el.getDeclaringExtension().getExtensionPointUniqueIdentifier();
            
            final String msg
                = Resources.bind( Resources.missingElement, pluginId, extPointId, el.getName(), 
                                  element );

            log( createErrorStatus( -1, msg, null ) );
        }

        public static String findRequiredAttribute( final IConfigurationElement el,
                                                    final String attribute )

            throws InvalidExtensionException

        {
            final String val = el.getAttribute( attribute );

            if( val == null )
            {
                reportMissingAttribute( el, attribute );
                throw new InvalidExtensionException();
            }

            return val;
        }

        public static IConfigurationElement findRequiredElement( final IConfigurationElement el,
                                                                 final String childElement )

            throws InvalidExtensionException

        {
            final IConfigurationElement[] children = el.getChildren( childElement );

            if( children.length == 0 )
            {
                reportMissingElement( el, childElement );
                throw new InvalidExtensionException();
            }

            return children[ 0 ];
        }

        public static IConfigurationElement findOptionalElement( final IConfigurationElement el,
                                                                 final String childElement )
        {
            final IConfigurationElement[] children = el.getChildren( childElement );

            if( children.length == 0 )
            {
                return null;
            }
            return children[ 0 ];
        }
        
        public static String getElementValue( final IConfigurationElement el,
                                              final String defaultValue )
        {
            if( el != null )
            {
                String text = el.getValue();
                
                if( text != null )
                {
                    text = text.trim();
                    
                    if( text.length() > 0 )
                    {
                        return text;
                    }
                }
            }
            
            return defaultValue;
        }
        
        @SuppressWarnings( "unchecked" )
        public static <T> T instantiate( final String pluginId,
                                         final String clname,
                                         final Class<T> interfc )
        
            throws CoreException
            
        {
            final Bundle bundle = Platform.getBundle( pluginId );
            
            final Object obj;
            
            try
            {
                final Class cl = bundle.loadClass( clname );
                obj = cl.newInstance();
            }
            catch( Exception e )
            {
                final String msg = NLS.bind( Resources.failedToCreate, clname );
                throw new CoreException( createErrorStatus( -1, msg, e ) );
            }
            
            if( ! interfc.isAssignableFrom( obj.getClass() ) )
            {
                final String msg
                    = NLS.bind( Resources.doesNotImplement, clname, interfc.getClass().getName() );
                
                throw new CoreException( createErrorStatus( -1, msg, null ) );
            }
            
            return (T) obj;
        }
        
        private static final class Resources

            extends NLS
        
        {
            public static String missingAttribute;
            public static String missingElement;
            public static String failedToCreate;
            public static String doesNotImplement;
        
            static
            {
                initializeMessages( PluginUtil.class.getName(), Resources.class );
            }
            
            public static String bind( final String message,
                                       final String arg1,
                                       final String arg2,
                                       final String arg3,
                                       final String arg4 )
            {
                return bind( message, new Object[] { arg1, arg2, arg3, arg4 } );
            }
        }
        
    }
    
}

Back to the top