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: a27b139b7fa451ce7a78aa71b8b2bca4190796ea (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
/******************************************************************************
 * Copyright (c) 2006 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 - initial API and implementation
 ******************************************************************************/

package org.eclipse.jst.j2ee.project.facet;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.wst.common.componentcore.ComponentCore;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;
import org.eclipse.wst.common.componentcore.resources.IVirtualReference;
import org.eclipse.wst.common.project.facet.core.IDelegate;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntime;

/** 
 * @author <a href="mailto:kosta@bea.com">Konstantin Komissarchik</a>
 */

public final class EarFacetRuntimeHandler 
{
    /**
     * Private constructor. This class is not meant to be instantiated.
     */
    
    private EarFacetRuntimeHandler() {}
    
    public static void updateModuleProjectRuntime( final IProject earProject,
                                                   final IProject moduleProject,
                                                   final IProgressMonitor monitor )
    
        throws CoreException
        
    {
        if( monitor != null )
        {
            monitor.beginTask( "", 1 );
        }
        
        try
        {
            final IFacetedProject earFacetedProject
                = ProjectFacetsManager.create( earProject );
        
            final IRuntime earRuntime = earFacetedProject.getPrimaryRuntime();
    
            final IFacetedProject moduleFacetedProject 
                = ProjectFacetsManager.create( moduleProject );
            
            if( moduleFacetedProject != null && 
                ! equals( earRuntime, moduleFacetedProject.getPrimaryRuntime() ) )
            {
                boolean supports = true;
                
                if( earRuntime != null )
                {
                    for( Iterator itr = moduleFacetedProject.getProjectFacets().iterator(); 
                         itr.hasNext(); )
                    {
                        final IProjectFacetVersion fver 
                            = (IProjectFacetVersion) itr.next();
                        
                        if( ! earRuntime.supports( fver ) )
                        {
                            supports = false;
                            break;
                        }
                    }
                }
                
                if( supports )
                {
                    moduleFacetedProject.setTargetedRuntimes( Collections.singleton( earRuntime ), submon( monitor, 1 ) );
                }
            }
        }
        finally
        {
            if( monitor != null )
            {
                monitor.done();
            }
        }
    }

    public static void updateModuleProjectRuntime( final IProject earProject,
                                                   final Set moduleProjects,
                                                   final IProgressMonitor monitor )
    
        throws CoreException
        
    {
        if( monitor != null )
        {
            monitor.beginTask( "", moduleProjects.size() );
        }
        
        try
        {
            for( Iterator itr = moduleProjects.iterator(); itr.hasNext(); )
            {
                final IProject moduleProject = (IProject) itr.next();
                
                updateModuleProjectRuntime( earProject, moduleProject, 
                                            submon( monitor, 1 ) );
            }
        }
        finally
        {
            if( monitor != null )
            {
                monitor.done();
            }
        }
    }
    
    public static final class RuntimeChangedDelegate
    
        implements IDelegate
        
    {
        public void execute( final IProject project, 
                             final IProjectFacetVersion fv, 
                             final Object cfg, 
                             final IProgressMonitor monitor ) 
        
            throws CoreException 
            
        {
            if( monitor != null ) 
            {
                monitor.beginTask( "", 10 ); //$NON-NLS-1$
            }
    
            try 
            {
                // Compile the list of projects referenced by this ear project.
                
                final Set moduleProjects = new HashSet();
                
                final IVirtualComponent earvc 
                    = ComponentCore.createComponent( project );
                
                final IVirtualReference[] vrefs = earvc.getReferences();
                
                for( int i = 0; i < vrefs.length; i++ )
                {
                    final IVirtualReference vref = vrefs[ i ];
                    final IVirtualComponent vc = vref.getReferencedComponent();
                    
                    moduleProjects.add( vc.getProject() );
                }
                
                if( monitor != null )
                {
                    monitor.worked( 1 );
                }
                
                // Attempt to change the runtime for each of the referenced projects.
                
                updateModuleProjectRuntime( project, moduleProjects, 
                                            submon( monitor, 9 ) );
            }
            finally 
            {
                if( monitor != null ) 
                {
                    monitor.done();
                }
            }
        }
    }

    private static IProgressMonitor submon( final IProgressMonitor parent,
                                            final int ticks )
    {
        return ( parent == null ? null : new SubProgressMonitor( parent, ticks ) );
    }
    
    private static boolean equals( final Object obj1,
                                   final Object obj2 )
    {
        if( obj1 == obj2 )
        {
            return true;
        }
        else if( obj1 == null || obj2 == null )
        {
            return false;
        }
        else
        {
            return obj1.equals( obj2 );
        }
    }

    
    
}

Back to the top