1 /******************************************************************************* 2 * Copyright (c) 2007, 2017 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Genuitec, LLC - added license support 14 *******************************************************************************/ 15 package org.eclipse.equinox.internal.p2.ui.sdk; 16 17 import java.util.HashSet; 18 import java.util.StringTokenizer; 19 import org.eclipse.equinox.internal.p2.ui.sdk.prefs.PreferenceConstants; 20 import org.eclipse.equinox.p2.core.IAgentLocation; 21 import org.eclipse.equinox.p2.engine.IProfileRegistry; 22 import org.eclipse.equinox.p2.engine.ProfileScope; 23 import org.eclipse.equinox.p2.metadata.ILicense; 24 import org.eclipse.equinox.p2.ui.LicenseManager; 25 import org.osgi.service.prefs.Preferences; 26 27 /** 28 * SimpleLicenseManager is a license manager that keeps track of 29 * IInstallableUnit licenses using their UUID. The licenses ids 30 * are stored in the profile's preferences. 31 * 32 * @since 3.6 33 */ 34 public class SimpleLicenseManager extends LicenseManager { 35 java.util.Set<String> accepted = new HashSet<>(); 36 String profileId; 37 SimpleLicenseManager(String profileId)38 public SimpleLicenseManager(String profileId) { 39 super(); 40 this.profileId = profileId; 41 initializeFromPreferences(); 42 } 43 SimpleLicenseManager()44 public SimpleLicenseManager() { 45 this(IProfileRegistry.SELF); 46 } 47 48 @Override accept(ILicense license)49 public boolean accept(ILicense license) { 50 accepted.add(license.getUUID()); 51 updatePreferences(); 52 return true; 53 } 54 55 @Override reject(ILicense license)56 public boolean reject(ILicense license) { 57 accepted.remove(license.getUUID()); 58 updatePreferences(); 59 return true; 60 } 61 62 @Override isAccepted(ILicense license)63 public boolean isAccepted(ILicense license) { 64 return accepted.contains(license.getUUID()); 65 } 66 67 @Override hasAcceptedLicenses()68 public boolean hasAcceptedLicenses() { 69 return !accepted.isEmpty(); 70 } 71 getPreferences()72 private Preferences getPreferences() { 73 IAgentLocation location = ProvSDKUIActivator.getDefault().getProvisioningAgent() 74 .getService(IAgentLocation.class); 75 return new ProfileScope(location, profileId).getNode(ProvSDKUIActivator.PLUGIN_ID); 76 } 77 initializeFromPreferences()78 private void initializeFromPreferences() { 79 Preferences pref = getPreferences(); 80 if (pref != null) { 81 String digestList = pref.get(PreferenceConstants.PREF_LICENSE_DIGESTS, ""); //$NON-NLS-1$ 82 StringTokenizer tokenizer = new StringTokenizer(digestList, ","); //$NON-NLS-1$ 83 while (tokenizer.hasMoreTokens()) { 84 accepted.add(tokenizer.nextToken().trim()); 85 } 86 } 87 } 88 updatePreferences()89 private void updatePreferences() { 90 Preferences pref = getPreferences(); 91 StringBuilder result = new StringBuilder(); 92 Object[] indexedList = accepted.toArray(); 93 for (int i = 0; i < indexedList.length; i++) { 94 if (i != 0) 95 result.append(","); //$NON-NLS-1$ 96 result.append((String) indexedList[i]); 97 } 98 pref.put(PreferenceConstants.PREF_LICENSE_DIGESTS, result.toString()); 99 } 100 } 101