1 package twcsckernel.serverKernel.plugins; 2 3 import java.util.HashMap; 4 import java.util.HashSet; 5 import java.util.Map; 6 import java.util.Set; 7 8 import twcsckernel.projectbase.plugins.PluginDescriptor; 9 10 /*** 11 * PluginContainer jest kontenerem klas pluginów dostępnych dla danego typu 12 * użytkownika. 13 * 14 * @author VMD Group 15 * 16 */ 17 public class PluginHandleContainer { 18 Map<String, Set<PluginHandle>> pluginTypeMap; 19 20 Map<String, PluginHandle> clientPluginPkgMap; 21 22 public PluginHandleContainer() { 23 pluginTypeMap = new HashMap<String, Set<PluginHandle>>(); 24 clientPluginPkgMap = new HashMap<String, PluginHandle>(); 25 } 26 27 /*** 28 * Usuwa typ użytkownika. 29 * 30 * @param userType 31 * typ użytkownika 32 * @return true gdy usunięto, false gdy nie znaleziono takiej nazwy typu 33 */ 34 public boolean removeUserType(String userType) { 35 if (pluginTypeMap.get(userType) == null) 36 return false; 37 pluginTypeMap.remove(userType); 38 return true; 39 } 40 41 /*** 42 * Dodaje klasę pluginu do listy dostępnych dla danego typu użytkownika. 43 * 44 * @param userType - 45 * typ użytkoniwka 46 * @param plugin - 47 * klasa pluginu 48 * @return - <i>true</i> jeśli udało się dodać plugin lub 49 * <code>false</code> w przeciwnym wypadku 50 */ 51 public boolean addPluginToUserType(String userType, PluginHandle plugin) { 52 Set<PluginHandle> userSet = pluginTypeMap.get(userType); 53 if (userSet == null) { 54 userSet = new HashSet<PluginHandle>(); 55 pluginTypeMap.put(userType, userSet); 56 } 57 58 if (clientPluginPkgMap.get(plugin.descriptor.clientPackagePath) == null) 59 clientPluginPkgMap.put(plugin.descriptor.clientPackagePath, plugin); 60 61 return userSet.add(plugin); 62 } 63 64 /*** 65 * Zwraca tablicę uchwytów pluginów dostępnych dla danego typu użytkownika. 66 * 67 * @param userType - 68 * nazwa typu użytkownika 69 * @return talica klas pluginów (lub <code>null</code> jeśli nie ma 70 * takiego typu użytkownika) 71 */ 72 public PluginHandle[] getUserTypePlugins(String userType) { 73 Set<PluginHandle> userPluginSet = pluginTypeMap.get(userType); 74 if (userPluginSet == null) 75 return null; 76 return userPluginSet.toArray(new PluginHandle[0]); 77 } 78 79 /*** 80 * Metoda zwraca tablicę deskryptorów pluginów dla danego typu użytkownika 81 * 82 * @param userType - 83 * typ użytkownika 84 * @return - talibca deskryptorów pluginów lub <code>null</code> jeśli 85 * podany typ nie istnieje 86 */ 87 public PluginDescriptor[] getUserPluginsDescriptors(String userType) { 88 Set<PluginHandle> userPluginSet = pluginTypeMap.get(userType); 89 if (userPluginSet == null) 90 return new PluginDescriptor[0]; 91 PluginDescriptor[] descriptors = new PluginDescriptor[userPluginSet 92 .size()]; 93 int index = 0; 94 for (PluginHandle pluginHandle : userPluginSet) { 95 descriptors[index] = pluginHandle.descriptor; 96 index++; 97 } 98 return descriptors; 99 } 100 101 /*** 102 * Metoda zwraca uchwyt plugina danego typu użytkownika o wyspecyfikowanej 103 * ścieżce pakietowej części klienckiej. 104 * 105 * @param userType - 106 * typ użytkownika 107 * @param clientPackagePath - 108 * ścieżka pakietowa klienckiej części plugina 109 * @return - uchwyt szukanego plugina lub <code>null</code> jeśli nie 110 * istnieje typ użytkownika lub plugin o podanej ścieżce 111 */ 112 public PluginHandle getUserPluginByClientPackagePath(String userType, 113 String clientPackagePath) { 114 Set<PluginHandle> userPluginSet = pluginTypeMap.get(userType); 115 PluginHandle selectedPlugin = clientPluginPkgMap.get(clientPackagePath); 116 if (userPluginSet.contains(selectedPlugin)) 117 return selectedPlugin; 118 return null; 119 120 } 121 }