| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | package org.eclipse.pde.api.tools.internal.model; |
| 12 | |
| 13 | import java.io.File; |
| 14 | import java.io.IOException; |
| 15 | import java.net.URL; |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.Arrays; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.Iterator; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.eclipse.core.runtime.CoreException; |
| 24 | import org.eclipse.core.runtime.FileLocator; |
| 25 | import org.eclipse.core.runtime.IPath; |
| 26 | import org.eclipse.core.runtime.Path; |
| 27 | import org.eclipse.core.runtime.Platform; |
| 28 | import org.eclipse.jdt.launching.LibraryLocation; |
| 29 | import org.eclipse.pde.api.tools.internal.ApiBaselineManager; |
| 30 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
| 31 | import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; |
| 32 | import org.eclipse.pde.api.tools.internal.provisional.IApiFilterStore; |
| 33 | import org.eclipse.pde.api.tools.internal.provisional.ProfileModifiers; |
| 34 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
| 35 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 36 | |
| 37 | /** |
| 38 | * An API component for a system library. |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | */ |
| 42 | public class StubApiComponent extends SystemLibraryApiComponent { |
| 43 | private static final String STUB_PATH = "/org/eclipse/pde/api/tools/internal/api_stubs/"; //$NON-NLS-1$ |
| 44 | private static Map AllSystemLibraryApiComponents; |
| 45 | |
| 46 | public static IApiComponent getStubApiComponent(int eeValue) { |
| 47 | if (AllSystemLibraryApiComponents == null) { |
| 48 | AllSystemLibraryApiComponents = new HashMap(); |
| 49 | } |
| 50 | String name = ProfileModifiers.getName(eeValue); |
| 51 | IApiComponent component = (IApiComponent) AllSystemLibraryApiComponents.get(name); |
| 52 | if (component == null) { |
| 53 | // search if the corresponding stub file exists |
| 54 | File stubFile = getFileFor(eeValue, name); |
| 55 | if (stubFile == null) { |
| 56 | return null; |
| 57 | } |
| 58 | component = new StubApiComponent(ApiBaselineManager.getManager().getWorkspaceBaseline(), stubFile.getAbsolutePath(), name); |
| 59 | AllSystemLibraryApiComponents.put(name, component); |
| 60 | } |
| 61 | return component; |
| 62 | } |
| 63 | |
| 64 | private static File getFileFor(int eeValue, String name) { |
| 65 | try { |
| 66 | String lname = name; |
| 67 | switch(eeValue) { |
| 68 | case ProfileModifiers.CDC_1_0_FOUNDATION_1_0 : |
| 69 | case ProfileModifiers.CDC_1_1_FOUNDATION_1_1 : |
| 70 | case ProfileModifiers.OSGI_MINIMUM_1_0 : |
| 71 | case ProfileModifiers.OSGI_MINIMUM_1_1 : |
| 72 | case ProfileModifiers.OSGI_MINIMUM_1_2 : |
| 73 | lname = lname.replace('/', '_'); |
| 74 | } |
| 75 | String stubName = lname + ".zip"; //$NON-NLS-1$ |
| 76 | URL stub = null; |
| 77 | if (Platform.isRunning()) { |
| 78 | stub = ApiPlugin.getDefault().getBundle().getResource(STUB_PATH + stubName); |
| 79 | if (stub == null) { |
| 80 | return null; |
| 81 | } |
| 82 | stub = FileLocator.toFileURL(stub); |
| 83 | } else { |
| 84 | stub = ApiPlugin.class.getResource(STUB_PATH + stubName); |
| 85 | if (stub == null) { |
| 86 | return null; |
| 87 | } |
| 88 | } |
| 89 | File stubFile = new File(stub.getFile()); |
| 90 | if (!stubFile.exists()) { |
| 91 | return null; |
| 92 | } |
| 93 | return stubFile; |
| 94 | } catch (IOException e) { |
| 95 | return null; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns a listing of all of the installed meta-data or an empty array, never <code>null</code> |
| 101 | * @return list of installed meta-data or an empty list, never <code>null</code> |
| 102 | */ |
| 103 | public static String[] getInstalledMetadata() { |
| 104 | List allEEs = new ArrayList(); |
| 105 | int[] allEEsValues = ProfileModifiers.getAllIds(); |
| 106 | String name = null; |
| 107 | File stubFile = null; |
| 108 | int eeValue = -1; |
| 109 | for (int i = 0; i < allEEsValues.length; i++) { |
| 110 | eeValue = allEEsValues[i]; |
| 111 | name = ProfileModifiers.getName(eeValue); |
| 112 | switch(eeValue) { |
| 113 | case ProfileModifiers.CDC_1_0_FOUNDATION_1_0 : |
| 114 | case ProfileModifiers.CDC_1_1_FOUNDATION_1_1 : |
| 115 | case ProfileModifiers.OSGI_MINIMUM_1_0 : |
| 116 | case ProfileModifiers.OSGI_MINIMUM_1_1 : |
| 117 | case ProfileModifiers.OSGI_MINIMUM_1_2 : |
| 118 | name = name.replace('/', '_'); |
| 119 | } |
| 120 | stubFile = getFileFor(eeValue, name); |
| 121 | if (stubFile == null) { |
| 122 | continue; |
| 123 | } |
| 124 | allEEs.add(ProfileModifiers.getName(eeValue)); |
| 125 | } |
| 126 | String[] result = new String[allEEs.size()]; |
| 127 | allEEs.toArray(result); |
| 128 | Arrays.sort(result); |
| 129 | return result; |
| 130 | } |
| 131 | /** |
| 132 | * Constructs a system library from the given execution environment description file. |
| 133 | * |
| 134 | * @param profile owning profile |
| 135 | * @param fileName the file name that corresponds to the stub file for the corresponding profile |
| 136 | * @param profileName the given profile name |
| 137 | * @exception CoreException if unable to read the execution environment description file |
| 138 | */ |
| 139 | private StubApiComponent(IApiBaseline profile, String fileName, String profileName) { |
| 140 | super(profile); |
| 141 | IPath path = new Path(fileName); |
| 142 | fLibraries = new LibraryLocation[] { new LibraryLocation(path, null, null) }; |
| 143 | fExecEnv = new String[]{ profileName }; |
| 144 | fVersion = fExecEnv[0]; |
| 145 | setName(fExecEnv[0]); |
| 146 | fLocation = path.toOSString(); |
| 147 | } |
| 148 | |
| 149 | /* (non-Javadoc) |
| 150 | * @see org.eclipse.pde.api.tools.internal.descriptors.AbstractApiComponent#createApiDescription() |
| 151 | */ |
| 152 | protected IApiDescription createApiDescription() throws CoreException { |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | /* (non-Javadoc) |
| 157 | * @see org.eclipse.pde.api.tools.internal.AbstractApiComponent#createApiFilterStore() |
| 158 | */ |
| 159 | protected IApiFilterStore createApiFilterStore() { |
| 160 | //TODO |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | /* (non-Javadoc) |
| 165 | * @see org.eclipse.pde.api.tools.internal.descriptors.AbstractApiComponent#createClassFileContainers() |
| 166 | */ |
| 167 | protected List createApiTypeContainers() throws CoreException { |
| 168 | List libs = new ArrayList(fLibraries.length); |
| 169 | for (int i = 0; i < fLibraries.length; i++) { |
| 170 | LibraryLocation lib = fLibraries[i]; |
| 171 | libs.add(new StubArchiveApiTypeContainer(this, lib.getSystemLibraryPath().toOSString())); |
| 172 | } |
| 173 | return libs; |
| 174 | } |
| 175 | /* (non-Javadoc) |
| 176 | * @see org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent#isSystemComponent() |
| 177 | */ |
| 178 | public boolean isSystemComponent() { |
| 179 | return false; |
| 180 | } |
| 181 | public static void disposeAllCaches() { |
| 182 | if (AllSystemLibraryApiComponents != null) { |
| 183 | for (Iterator iterator = AllSystemLibraryApiComponents.values().iterator(); iterator.hasNext(); ) { |
| 184 | IApiComponent apiComponent = (IApiComponent) iterator.next(); |
| 185 | apiComponent.dispose(); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public static boolean isInstalled(int eeValue) { |
| 191 | String name = ProfileModifiers.getName(eeValue); |
| 192 | switch(eeValue) { |
| 193 | case ProfileModifiers.CDC_1_0_FOUNDATION_1_0 : |
| 194 | case ProfileModifiers.CDC_1_1_FOUNDATION_1_1 : |
| 195 | case ProfileModifiers.OSGI_MINIMUM_1_0 : |
| 196 | case ProfileModifiers.OSGI_MINIMUM_1_1 : |
| 197 | case ProfileModifiers.OSGI_MINIMUM_1_2 : |
| 198 | name = name.replace('/', '_'); |
| 199 | } |
| 200 | File stubFile = getFileFor(eeValue, name); |
| 201 | return stubFile != null; |
| 202 | } |
| 203 | } |