Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7922db8ec906e8dab48f31a2327e741d8bc8fa57 (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
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * 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:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20060131 121071   rsinha@ca.ibm.com - Rupam Kuehner (creation)
 * 20060427   126780 rsinha@ca.ibm.com - Rupam Kuehner
 *******************************************************************************/
package org.eclipse.jst.ws.internal.consumption.common;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.common.project.facet.core.IFacetedProjectTemplate;
import org.eclipse.wst.common.project.facet.core.IProjectFacet;
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.VersionFormatException;


/**
 * FacetSetsByTemplateCache caches the sets of facet version combinations for templates.
 */
public class FacetSetsByTemplateCache
{
  // single instance per workbench
  private static FacetSetsByTemplateCache instance_;
  
  //facetSetsByTemplateId_: 
  //key: Object of type String. String id of an IFacetedProjectTemplate.
  //value: Object of Set[]. An array of Sets where each set contains elements of type IProjectFacetVersion.
  private Hashtable facetSetsByTemplateId_;
  /**
   * Returns a singleton instance of this class.
   * 
   * @return A singleton FacetSetsByTemplateCache object.
   */
  public static synchronized FacetSetsByTemplateCache getInstance()
  {
    if (instance_ == null)
    {
      instance_ = new FacetSetsByTemplateCache();
      instance_.load();
    }
    return instance_;
  }
  
  private void load()
  {
    facetSetsByTemplateId_ = new Hashtable();
  }  
  
  /**
   * Returns all the valid facet version combinations that can be derived from the fixed facets
   * of the given template. For example, if the template has two fixed facets, A and B,
   * and A has versions 1.0 and 2.0, B has versions 1.1, 2.1,
   * the following array of Sets will be returned (assuming for the sake of the example
   * that all the combinations are valid):<br/>
   * {Set1, Set2, Set3, Set4}, where<br/>
   * Set1 = [A1.0, B1.1]<br/>
   * Set2 = [A2.0, B1.1]<br/>
   * Set3 = [A1.0, B2.1]<br/>
   * Set4 = [A2.0, B2.1]<br/>
   * <br/>
   * @param templateId id of an {@link IFacetedProjectTemplate}
   * @return Set[] An array of Sets where each set contains elements of type {@link IProjectFacetVersion}.
   */
  public synchronized Set[] getFacetVersionCombinationsFromTemplate(String templateId)
  {
	//Return the cached combinations if present.
    Set[] cachedCombinations = (Set[])facetSetsByTemplateId_.get(templateId);
    if (cachedCombinations != null)
    {
      return cachedCombinations;
    }
    else
    {
      //Combinations have not yet been cached for the given template. 
      //Determine the combinations and cache them.
      IFacetedProjectTemplate template = ProjectFacetsManager.getTemplate(templateId);
      Set fixedFacets = template.getFixedProjectFacets();
      
      //Create an array of arrays. Each element of the array will contain the array
      //of IProjectFacetVersions for each IProjectFacet in the set of fixed facets.
      Iterator itr = fixedFacets.iterator();
      ArrayList projectFacetVersionArrays = new ArrayList();      

      while (itr.hasNext())
      {
        IProjectFacet facet = (IProjectFacet) itr.next();
        List versions = null;
        try
        {
          // Get the facet versions in ascending order.
          versions = facet.getSortedVersions(true);
        } catch (VersionFormatException e) {
            Set versionSet = facet.getVersions();
            Iterator versionSetItr = versionSet.iterator();
            versions = new ArrayList();
            while (versionSetItr.hasNext())
            {
                versions.add(itr.next());
            }            
        } catch (CoreException e) {
          Set versionSet = facet.getVersions();
          Iterator versionSetItr = versionSet.iterator();
          versions = new ArrayList();
          while (versionSetItr.hasNext())
          {
              versions.add(itr.next());
          }            
        }         
        if (versions.size() > 0)
        {
          //Create an array of IProjectFacetVersions from versions.
          Iterator versionItr = versions.iterator();
          ArrayList arrayOfVersionsList = new ArrayList();
          while (versionItr.hasNext())
          {
            IProjectFacetVersion pfv = (IProjectFacetVersion)versionItr.next();
            arrayOfVersionsList.add(pfv);
          }          
          
          //Add the array of versions to the list of arrays.
          projectFacetVersionArrays.add((IProjectFacetVersion[])arrayOfVersionsList.toArray(new IProjectFacetVersion[0]));
        }
      }
      
      IProjectFacetVersion[][] arrayOfProjectFacetVersionArrays = (IProjectFacetVersion[][])projectFacetVersionArrays.toArray(new IProjectFacetVersion[0][0]);
      Set[] allValidCombinationsArray = FacetUtils.getFacetCombinations(arrayOfProjectFacetVersionArrays, true);
      facetSetsByTemplateId_.put(templateId, allValidCombinationsArray);
      return allValidCombinationsArray;
    }    
  }  
}

Back to the top