29 import java.util.WeakHashMap; 30 31 32 /** 33 * The CommandMap class provides an interface to a registry of 34 * command objects available in the system. 35 * Developers are expected to either use the CommandMap 36 * implementation included with this package (MailcapCommandMap) or 37 * develop their own. Note that some of the methods in this class are 38 * abstract. 39 * 40 * @since 1.6 41 */ 42 public abstract class CommandMap { 43 private static CommandMap defaultCommandMap = null; 44 private static Map<ClassLoader,CommandMap> map = 45 new WeakHashMap<ClassLoader,CommandMap>(); 46 47 /** 48 * Get the default CommandMap. 49 * <p> 50 * 51 * <ul> 52 * <li> In cases where a CommandMap instance has been previously set 53 * to some value (via <i>setDefaultCommandMap</i>) 54 * return the CommandMap. 55 * <li> 56 * In cases where no CommandMap has been set, the CommandMap 57 * creates an instance of <code>MailcapCommandMap</code> and 58 * set that to the default, returning its value. 59 * 60 * </ul> 61 * 62 * @return the CommandMap 63 */ 64 public static synchronized CommandMap getDefaultCommandMap() { 65 if (defaultCommandMap != null) 66 return defaultCommandMap; 67 68 // fetch per-thread-context-class-loader default 69 ClassLoader tccl = SecuritySupport.getContextClassLoader(); 70 CommandMap def = map.get(tccl); 71 if (def == null) { 72 def = new MailcapCommandMap(); 73 map.put(tccl, def); 74 } 75 return def; 76 } 77 78 /** 79 * Set the default CommandMap. Reset the CommandMap to the default by 80 * calling this method with <code>null</code>. 81 * 82 * @param commandMap The new default CommandMap. 83 * @exception SecurityException if the caller doesn't have permission 84 * to change the default 85 */ 86 public static synchronized void setDefaultCommandMap(CommandMap commandMap) { 87 SecurityManager security = System.getSecurityManager(); 88 if (security != null) { 89 try { 90 // if it's ok with the SecurityManager, it's ok with me... 91 security.checkSetFactory(); 92 } catch (SecurityException ex) { 93 // otherwise, we also allow it if this code and the 94 // factory come from the same (non-system) class loader (e.g., 95 // the JAF classes were loaded with the applet classes). 96 ClassLoader cl = CommandMap.class.getClassLoader(); 97 if (cl == null || cl.getParent() == null || 98 cl != commandMap.getClass().getClassLoader()) { 99 throw ex; 100 } 101 } 102 } 103 // remove any per-thread-context-class-loader CommandMap 104 map.remove(SecuritySupport.getContextClassLoader()); 105 defaultCommandMap = commandMap; 106 } 107 108 /** 109 * Get the preferred command list from a MIME Type. The actual semantics 110 * are determined by the implementation of the CommandMap. 111 * 112 * @param mimeType the MIME type 113 * @return the CommandInfo classes that represent the command Beans. 114 */ 115 abstract public CommandInfo[] getPreferredCommands(String mimeType); 116 117 /** 118 * Get the preferred command list from a MIME Type. The actual semantics 119 * are determined by the implementation of the CommandMap. <p> 120 * 121 * The <code>DataSource</code> provides extra information, such as 122 * the file name, that a CommandMap implementation may use to further 123 * refine the list of commands that are returned. The implementation 124 * in this class simply calls the <code>getPreferredCommands</code> 125 * method that ignores this argument. 126 * 127 * @param mimeType the MIME type 128 * @param ds a DataSource for the data 129 * @return the CommandInfo classes that represent the command Beans. 130 * @since 1.6, JAF 1.1 131 */ 132 public CommandInfo[] getPreferredCommands(String mimeType, DataSource ds) { 133 return getPreferredCommands(mimeType); 134 } 135 136 /** 137 * Get all the available commands for this type. This method 138 * should return all the possible commands for this MIME type. 139 * 140 * @param mimeType the MIME type 141 * @return the CommandInfo objects representing all the commands. 142 */ 143 abstract public CommandInfo[] getAllCommands(String mimeType); 144 145 /** 146 * Get all the available commands for this type. This method 147 * should return all the possible commands for this MIME type. <p> 148 * 149 * The <code>DataSource</code> provides extra information, such as 150 * the file name, that a CommandMap implementation may use to further 151 * refine the list of commands that are returned. The implementation 152 * in this class simply calls the <code>getAllCommands</code> 153 * method that ignores this argument. 154 * 155 * @param mimeType the MIME type 156 * @param ds a DataSource for the data 157 * @return the CommandInfo objects representing all the commands. 158 * @since 1.6, JAF 1.1 159 */ 160 public CommandInfo[] getAllCommands(String mimeType, DataSource ds) { 161 return getAllCommands(mimeType); 162 } 163 164 /** 165 * Get the default command corresponding to the MIME type. 166 * 167 * @param mimeType the MIME type 168 * @param cmdName the command name 169 * @return the CommandInfo corresponding to the command. 170 */ 171 abstract public CommandInfo getCommand(String mimeType, String cmdName); 172 173 /** 174 * Get the default command corresponding to the MIME type. <p> 175 * 176 * The <code>DataSource</code> provides extra information, such as 177 * the file name, that a CommandMap implementation may use to further 178 * refine the command that is chosen. The implementation 179 * in this class simply calls the <code>getCommand</code> 180 * method that ignores this argument. 181 * 182 * @param mimeType the MIME type 183 * @param cmdName the command name 184 * @param ds a DataSource for the data 185 * @return the CommandInfo corresponding to the command. 186 * @since 1.6, JAF 1.1 187 */ 188 public CommandInfo getCommand(String mimeType, String cmdName, 189 DataSource ds) { 190 return getCommand(mimeType, cmdName); 191 } 192 193 /** 194 * Locate a DataContentHandler that corresponds to the MIME type. 195 * The mechanism and semantics for determining this are determined 196 * by the implementation of the particular CommandMap. 197 * 198 * @param mimeType the MIME type 199 * @return the DataContentHandler for the MIME type 200 */ 201 abstract public DataContentHandler createDataContentHandler(String 202 mimeType); 203 204 /** 205 * Locate a DataContentHandler that corresponds to the MIME type. 206 * The mechanism and semantics for determining this are determined 207 * by the implementation of the particular CommandMap. <p> 208 * 209 * The <code>DataSource</code> provides extra information, such as 210 * the file name, that a CommandMap implementation may use to further 211 * refine the choice of DataContentHandler. The implementation 212 * in this class simply calls the <code>createDataContentHandler</code> 213 * method that ignores this argument. 214 * 215 * @param mimeType the MIME type 216 * @param ds a DataSource for the data 217 * @return the DataContentHandler for the MIME type 218 * @since 1.6, JAF 1.1 219 */ 220 public DataContentHandler createDataContentHandler(String mimeType, 221 DataSource ds) { 222 return createDataContentHandler(mimeType); 223 } 224 225 /** 226 * Get all the MIME types known to this command map. 227 * If the command map doesn't support this operation, 228 * null is returned. 229 * 230 * @return array of MIME types as strings, or null if not supported 231 * @since 1.6, JAF 1.1 232 */ | 29 import java.util.WeakHashMap; 30 31 32 /** 33 * The CommandMap class provides an interface to a registry of 34 * command objects available in the system. 35 * Developers are expected to either use the CommandMap 36 * implementation included with this package (MailcapCommandMap) or 37 * develop their own. Note that some of the methods in this class are 38 * abstract. 39 * 40 * @since 1.6 41 */ 42 public abstract class CommandMap { 43 private static CommandMap defaultCommandMap = null; 44 private static Map<ClassLoader,CommandMap> map = 45 new WeakHashMap<ClassLoader,CommandMap>(); 46 47 /** 48 * Get the default CommandMap. 49 * 50 * <ul> 51 * <li> In cases where a CommandMap instance has been previously set 52 * to some value (via <i>setDefaultCommandMap</i>) 53 * return the CommandMap. 54 * <li> 55 * In cases where no CommandMap has been set, the CommandMap 56 * creates an instance of {@code MailcapCommandMap} and 57 * set that to the default, returning its value. 58 * 59 * </ul> 60 * 61 * @return the CommandMap 62 */ 63 public static synchronized CommandMap getDefaultCommandMap() { 64 if (defaultCommandMap != null) 65 return defaultCommandMap; 66 67 // fetch per-thread-context-class-loader default 68 ClassLoader tccl = SecuritySupport.getContextClassLoader(); 69 CommandMap def = map.get(tccl); 70 if (def == null) { 71 def = new MailcapCommandMap(); 72 map.put(tccl, def); 73 } 74 return def; 75 } 76 77 /** 78 * Set the default CommandMap. Reset the CommandMap to the default by 79 * calling this method with {@code null}. 80 * 81 * @param commandMap The new default CommandMap. 82 * @exception SecurityException if the caller doesn't have permission 83 * to change the default 84 */ 85 public static synchronized void setDefaultCommandMap(CommandMap commandMap) { 86 SecurityManager security = System.getSecurityManager(); 87 if (security != null) { 88 try { 89 // if it's ok with the SecurityManager, it's ok with me... 90 security.checkSetFactory(); 91 } catch (SecurityException ex) { 92 // otherwise, we also allow it if this code and the 93 // factory come from the same (non-system) class loader (e.g., 94 // the JAF classes were loaded with the applet classes). 95 ClassLoader cl = CommandMap.class.getClassLoader(); 96 if (cl == null || cl.getParent() == null || 97 cl != commandMap.getClass().getClassLoader()) { 98 throw ex; 99 } 100 } 101 } 102 // remove any per-thread-context-class-loader CommandMap 103 map.remove(SecuritySupport.getContextClassLoader()); 104 defaultCommandMap = commandMap; 105 } 106 107 /** 108 * Get the preferred command list from a MIME Type. The actual semantics 109 * are determined by the implementation of the CommandMap. 110 * 111 * @param mimeType the MIME type 112 * @return the CommandInfo classes that represent the command Beans. 113 */ 114 abstract public CommandInfo[] getPreferredCommands(String mimeType); 115 116 /** 117 * Get the preferred command list from a MIME Type. The actual semantics 118 * are determined by the implementation of the CommandMap. <p> 119 * 120 * The {@code DataSource} provides extra information, such as 121 * the file name, that a CommandMap implementation may use to further 122 * refine the list of commands that are returned. The implementation 123 * in this class simply calls the {@code getPreferredCommands} 124 * method that ignores this argument. 125 * 126 * @param mimeType the MIME type 127 * @param ds a DataSource for the data 128 * @return the CommandInfo classes that represent the command Beans. 129 * @since 1.6, JAF 1.1 130 */ 131 public CommandInfo[] getPreferredCommands(String mimeType, DataSource ds) { 132 return getPreferredCommands(mimeType); 133 } 134 135 /** 136 * Get all the available commands for this type. This method 137 * should return all the possible commands for this MIME type. 138 * 139 * @param mimeType the MIME type 140 * @return the CommandInfo objects representing all the commands. 141 */ 142 abstract public CommandInfo[] getAllCommands(String mimeType); 143 144 /** 145 * Get all the available commands for this type. This method 146 * should return all the possible commands for this MIME type. <p> 147 * 148 * The {@code DataSource} provides extra information, such as 149 * the file name, that a CommandMap implementation may use to further 150 * refine the list of commands that are returned. The implementation 151 * in this class simply calls the {@code getAllCommands} 152 * method that ignores this argument. 153 * 154 * @param mimeType the MIME type 155 * @param ds a DataSource for the data 156 * @return the CommandInfo objects representing all the commands. 157 * @since 1.6, JAF 1.1 158 */ 159 public CommandInfo[] getAllCommands(String mimeType, DataSource ds) { 160 return getAllCommands(mimeType); 161 } 162 163 /** 164 * Get the default command corresponding to the MIME type. 165 * 166 * @param mimeType the MIME type 167 * @param cmdName the command name 168 * @return the CommandInfo corresponding to the command. 169 */ 170 abstract public CommandInfo getCommand(String mimeType, String cmdName); 171 172 /** 173 * Get the default command corresponding to the MIME type. <p> 174 * 175 * The {@code DataSource} provides extra information, such as 176 * the file name, that a CommandMap implementation may use to further 177 * refine the command that is chosen. The implementation 178 * in this class simply calls the {@code getCommand} 179 * method that ignores this argument. 180 * 181 * @param mimeType the MIME type 182 * @param cmdName the command name 183 * @param ds a DataSource for the data 184 * @return the CommandInfo corresponding to the command. 185 * @since 1.6, JAF 1.1 186 */ 187 public CommandInfo getCommand(String mimeType, String cmdName, 188 DataSource ds) { 189 return getCommand(mimeType, cmdName); 190 } 191 192 /** 193 * Locate a DataContentHandler that corresponds to the MIME type. 194 * The mechanism and semantics for determining this are determined 195 * by the implementation of the particular CommandMap. 196 * 197 * @param mimeType the MIME type 198 * @return the DataContentHandler for the MIME type 199 */ 200 abstract public DataContentHandler createDataContentHandler(String 201 mimeType); 202 203 /** 204 * Locate a DataContentHandler that corresponds to the MIME type. 205 * The mechanism and semantics for determining this are determined 206 * by the implementation of the particular CommandMap. <p> 207 * 208 * The {@code DataSource} provides extra information, such as 209 * the file name, that a CommandMap implementation may use to further 210 * refine the choice of DataContentHandler. The implementation 211 * in this class simply calls the {@code createDataContentHandler} 212 * method that ignores this argument. 213 * 214 * @param mimeType the MIME type 215 * @param ds a DataSource for the data 216 * @return the DataContentHandler for the MIME type 217 * @since 1.6, JAF 1.1 218 */ 219 public DataContentHandler createDataContentHandler(String mimeType, 220 DataSource ds) { 221 return createDataContentHandler(mimeType); 222 } 223 224 /** 225 * Get all the MIME types known to this command map. 226 * If the command map doesn't support this operation, 227 * null is returned. 228 * 229 * @return array of MIME types as strings, or null if not supported 230 * @since 1.6, JAF 1.1 231 */ |