1 21 22 27 28 29 package javax.activation; 30 31 import java.util.*; 32 import java.io.*; 33 import java.net.*; 34 import com.sun.activation.registries.MailcapFile; 35 import com.sun.activation.registries.LogSupport; 36 37 119 120 public class MailcapCommandMap extends CommandMap { 121 127 private static MailcapFile defDB = null; 128 private MailcapFile[] DB; 129 private static final int PROG = 0; 131 134 public MailcapCommandMap() { 135 super(); 136 List dbv = new ArrayList(5); MailcapFile mf = null; 138 dbv.add(null); 140 LogSupport.log("MailcapCommandMap: load HOME"); 141 try { 142 String user_home = System.getProperty("user.home"); 143 144 if (user_home != null) { 145 String path = user_home + File.separator + ".mailcap"; 146 mf = loadFile(path); 147 if (mf != null) 148 dbv.add(mf); 149 } 150 } catch (SecurityException ex) {} 151 152 LogSupport.log("MailcapCommandMap: load SYS"); 153 try { 154 String system_mailcap = System.getProperty("java.home") + 156 File.separator + "lib" + File.separator + "mailcap"; 157 mf = loadFile(system_mailcap); 158 if (mf != null) 159 dbv.add(mf); 160 } catch (SecurityException ex) {} 161 162 LogSupport.log("MailcapCommandMap: load JAR"); 163 loadAllResources(dbv, "META-INF/mailcap"); 165 166 LogSupport.log("MailcapCommandMap: load DEF"); 167 synchronized (MailcapCommandMap .class) { 168 if (defDB == null) 170 defDB = loadResource("/META-INF/mailcap.default"); 171 } 172 173 if (defDB != null) 174 dbv.add(defDB); 175 176 DB = new MailcapFile[dbv.size()]; 177 DB = (MailcapFile[])dbv.toArray(DB); 178 } 179 180 183 private MailcapFile loadResource(String name) { 184 InputStream clis = null; 185 try { 186 clis = SecuritySupport.getResourceAsStream(this.getClass(), name); 187 if (clis != null) { 188 MailcapFile mf = new MailcapFile(clis); 189 if (LogSupport.isLoggable()) 190 LogSupport.log("MailcapCommandMap: successfully loaded " + 191 "mailcap file: " + name); 192 return mf; 193 } else { 194 if (LogSupport.isLoggable()) 195 LogSupport.log("MailcapCommandMap: not loading " + 196 "mailcap file: " + name); 197 } 198 } catch (IOException e) { 199 if (LogSupport.isLoggable()) 200 LogSupport.log("MailcapCommandMap: can't load " + name, e); 201 } catch (SecurityException sex) { 202 if (LogSupport.isLoggable()) 203 LogSupport.log("MailcapCommandMap: can't load " + name, sex); 204 } finally { 205 try { 206 if (clis != null) 207 clis.close(); 208 } catch (IOException ex) { } } 210 return null; 211 } 212 213 216 private void loadAllResources(List v, String name) { 217 boolean anyLoaded = false; 218 try { 219 URL[] urls; 220 ClassLoader cld = null; 221 cld = SecuritySupport.getContextClassLoader(); 223 if (cld == null) 224 cld = this.getClass().getClassLoader(); 225 if (cld != null) 226 urls = SecuritySupport.getResources(cld, name); 227 else 228 urls = SecuritySupport.getSystemResources(name); 229 if (urls != null) { 230 if (LogSupport.isLoggable()) 231 LogSupport.log("MailcapCommandMap: getResources"); 232 for (int i = 0; i < urls.length; i++) { 233 URL url = urls[i]; 234 InputStream clis = null; 235 if (LogSupport.isLoggable()) 236 LogSupport.log("MailcapCommandMap: URL " + url); 237 try { 238 clis = SecuritySupport.openStream(url); 239 if (clis != null) { 240 v.add(new MailcapFile(clis)); 241 anyLoaded = true; 242 if (LogSupport.isLoggable()) 243 LogSupport.log("MailcapCommandMap: " + 244 "successfully loaded " + 245 "mailcap file from URL: " + 246 url); 247 } else { 248 if (LogSupport.isLoggable()) 249 LogSupport.log("MailcapCommandMap: " + 250 "not loading mailcap " + 251 "file from URL: " + url); 252 } 253 } catch (IOException ioex) { 254 if (LogSupport.isLoggable()) 255 LogSupport.log("MailcapCommandMap: can't load " + 256 url, ioex); 257 } catch (SecurityException sex) { 258 if (LogSupport.isLoggable()) 259 LogSupport.log("MailcapCommandMap: can't load " + 260 url, sex); 261 } finally { 262 try { 263 if (clis != null) 264 clis.close(); 265 } catch (IOException cex) { } 266 } 267 } 268 } 269 } catch (Exception ex) { 270 if (LogSupport.isLoggable()) 271 LogSupport.log("MailcapCommandMap: can't load " + name, ex); 272 } 273 274 if (!anyLoaded) { 276 if (LogSupport.isLoggable()) 277 LogSupport.log("MailcapCommandMap: !anyLoaded"); 278 MailcapFile mf = loadResource("/" + name); 279 if (mf != null) 280 v.add(mf); 281 } 282 } 283 284 287 private MailcapFile loadFile(String name) { 288 MailcapFile mtf = null; 289 290 try { 291 mtf = new MailcapFile(name); 292 } catch (IOException e) { 293 } 295 return mtf; 296 } 297 298 305 public MailcapCommandMap(String fileName) throws IOException { 306 this(); 307 308 if (LogSupport.isLoggable()) 309 LogSupport.log("MailcapCommandMap: load PROG from " + fileName); 310 if (DB[PROG] == null) { 311 DB[PROG] = new MailcapFile(fileName); 312 } 313 } 314 315 316 322 public MailcapCommandMap(InputStream is) { 323 this(); 324 325 LogSupport.log("MailcapCommandMap: load PROG"); 326 if (DB[PROG] == null) { 327 try { 328 DB[PROG] = new MailcapFile(is); 329 } catch (IOException ex) { 330 } 332 } 333 } 334 335 348 public synchronized CommandInfo [] getPreferredCommands(String mimeType) { 349 List cmdList = new ArrayList(); 350 if (mimeType != null) 351 mimeType = mimeType.toLowerCase(); 352 353 for (int i = 0; i < DB.length; i++) { 354 if (DB[i] == null) 355 continue; 356 Map cmdMap = DB[i].getMailcapList(mimeType); 357 if (cmdMap != null) 358 appendPrefCmdsToList(cmdMap, cmdList); 359 } 360 361 for (int i = 0; i < DB.length; i++) { 363 if (DB[i] == null) 364 continue; 365 Map cmdMap = DB[i].getMailcapFallbackList(mimeType); 366 if (cmdMap != null) 367 appendPrefCmdsToList(cmdMap, cmdList); 368 } 369 370 CommandInfo [] cmdInfos = new CommandInfo [cmdList.size()]; 371 cmdInfos = (CommandInfo [])cmdList.toArray(cmdInfos); 372 373 return cmdInfos; 374 } 375 376 379 private void appendPrefCmdsToList(Map cmdHash, List cmdList) { 380 Iterator verb_enum = cmdHash.keySet().iterator(); 381 382 while (verb_enum.hasNext()) { 383 String verb = (String )verb_enum.next(); 384 if (!checkForVerb(cmdList, verb)) { 385 List cmdList2 = (List)cmdHash.get(verb); String className = (String )cmdList2.get(0); 387 cmdList.add(new CommandInfo (verb, className)); 388 } 389 } 390 } 391 392 396 private boolean checkForVerb(List cmdList, String verb) { 397 Iterator ee = cmdList.iterator(); 398 while (ee.hasNext()) { 399 String enum_verb = 400 (String )((CommandInfo )ee.next()).getCommandName(); 401 if (enum_verb.equals(verb)) 402 return true; 403 } 404 return false; 405 } 406 407 414 public synchronized CommandInfo [] getAllCommands(String mimeType) { 415 List cmdList = new ArrayList(); 416 if (mimeType != null) 417 mimeType = mimeType.toLowerCase(); 418 419 for (int i = 0; i < DB.length; i++) { 420 if (DB[i] == null) 421 continue; 422 Map cmdMap = DB[i].getMailcapList(mimeType); 423 if (cmdMap != null) 424 appendCmdsToList(cmdMap, cmdList); 425 } 426 427 for (int i = 0; i < DB.length; i++) { 429 if (DB[i] == null) 430 continue; 431 Map cmdMap = DB[i].getMailcapFallbackList(mimeType); 432 if (cmdMap != null) 433 appendCmdsToList(cmdMap, cmdList); 434 } 435 436 CommandInfo [] cmdInfos = new CommandInfo [cmdList.size()]; 437 cmdInfos = (CommandInfo [])cmdList.toArray(cmdInfos); 438 439 return cmdInfos; 440 } 441 442 445 private void appendCmdsToList(Map typeHash, List cmdList) { 446 Iterator verb_enum = typeHash.keySet().iterator(); 447 448 while (verb_enum.hasNext()) { 449 String verb = (String )verb_enum.next(); 450 List cmdList2 = (List)typeHash.get(verb); 451 Iterator cmd_enum = ((List)cmdList2).iterator(); 452 453 while (cmd_enum.hasNext()) { 454 String cmd = (String )cmd_enum.next(); 455 cmdList.add(new CommandInfo (verb, cmd)); 456 } 458 } 459 } 460 461 468 public synchronized CommandInfo getCommand(String mimeType, 469 String cmdName) { 470 if (mimeType != null) 471 mimeType = mimeType.toLowerCase(); 472 473 for (int i = 0; i < DB.length; i++) { 474 if (DB[i] == null) 475 continue; 476 Map cmdMap = DB[i].getMailcapList(mimeType); 477 if (cmdMap != null) { 478 List v = (List)cmdMap.get(cmdName); 480 if (v != null) { 481 String cmdClassName = (String )v.get(0); 482 483 if (cmdClassName != null) 484 return new CommandInfo (cmdName, cmdClassName); 485 } 486 } 487 } 488 489 for (int i = 0; i < DB.length; i++) { 491 if (DB[i] == null) 492 continue; 493 Map cmdMap = DB[i].getMailcapFallbackList(mimeType); 494 if (cmdMap != null) { 495 List v = (List)cmdMap.get(cmdName); 497 if (v != null) { 498 String cmdClassName = (String )v.get(0); 499 500 if (cmdClassName != null) 501 return new CommandInfo (cmdName, cmdClassName); 502 } 503 } 504 } 505 return null; 506 } 507 508 517 public synchronized void addMailcap(String mail_cap) { 518 LogSupport.log("MailcapCommandMap: add to PROG"); 520 if (DB[PROG] == null) 521 DB[PROG] = new MailcapFile(); 522 523 DB[PROG].appendToMailcap(mail_cap); 524 } 525 526 532 public synchronized DataContentHandler createDataContentHandler( 533 String mimeType) { 534 if (LogSupport.isLoggable()) 535 LogSupport.log( 536 "MailcapCommandMap: createDataContentHandler for " + mimeType); 537 if (mimeType != null) 538 mimeType = mimeType.toLowerCase(); 539 540 for (int i = 0; i < DB.length; i++) { 541 if (DB[i] == null) 542 continue; 543 if (LogSupport.isLoggable()) 544 LogSupport.log(" search DB #" + i); 545 Map cmdMap = DB[i].getMailcapList(mimeType); 546 if (cmdMap != null) { 547 List v = (List)cmdMap.get("content-handler"); 548 if (v != null) { 549 String name = (String )v.get(0); 550 DataContentHandler dch = getDataContentHandler(name); 551 if (dch != null) 552 return dch; 553 } 554 } 555 } 556 557 for (int i = 0; i < DB.length; i++) { 559 if (DB[i] == null) 560 continue; 561 if (LogSupport.isLoggable()) 562 LogSupport.log(" search fallback DB #" + i); 563 Map cmdMap = DB[i].getMailcapFallbackList(mimeType); 564 if (cmdMap != null) { 565 List v = (List)cmdMap.get("content-handler"); 566 if (v != null) { 567 String name = (String )v.get(0); 568 DataContentHandler dch = getDataContentHandler(name); 569 if (dch != null) 570 return dch; 571 } 572 } 573 } 574 return null; 575 } 576 577 private DataContentHandler getDataContentHandler(String name) { 578 if (LogSupport.isLoggable()) 579 LogSupport.log(" got content-handler"); 580 if (LogSupport.isLoggable()) 581 LogSupport.log(" class " + name); 582 try { 583 ClassLoader cld = null; 584 cld = SecuritySupport.getContextClassLoader(); 586 if (cld == null) 587 cld = this.getClass().getClassLoader(); 588 Class cl = null; 589 try { 590 cl = cld.loadClass(name); 591 } catch (Exception ex) { 592 cl = Class.forName(name); 594 } 595 if (cl != null) return (DataContentHandler )cl.newInstance(); 597 } catch (IllegalAccessException e) { 598 if (LogSupport.isLoggable()) 599 LogSupport.log("Can't load DCH " + name, e); 600 } catch (ClassNotFoundException e) { 601 if (LogSupport.isLoggable()) 602 LogSupport.log("Can't load DCH " + name, e); 603 } catch (InstantiationException e) { 604 if (LogSupport.isLoggable()) 605 LogSupport.log("Can't load DCH " + name, e); 606 } 607 return null; 608 } 609 610 616 public synchronized String [] getMimeTypes() { 617 List mtList = new ArrayList(); 618 619 for (int i = 0; i < DB.length; i++) { 620 if (DB[i] == null) 621 continue; 622 String [] ts = DB[i].getMimeTypes(); 623 if (ts != null) { 624 for (int j = 0; j < ts.length; j++) { 625 if (!mtList.contains(ts[j])) 627 mtList.add(ts[j]); 628 } 629 } 630 } 631 632 String [] mts = new String [mtList.size()]; 633 mts = (String [])mtList.toArray(mts); 634 635 return mts; 636 } 637 638 652 public synchronized String [] getNativeCommands(String mimeType) { 653 List cmdList = new ArrayList(); 654 if (mimeType != null) 655 mimeType = mimeType.toLowerCase(); 656 657 for (int i = 0; i < DB.length; i++) { 658 if (DB[i] == null) 659 continue; 660 String [] cmds = DB[i].getNativeCommands(mimeType); 661 if (cmds != null) { 662 for (int j = 0; j < cmds.length; j++) { 663 if (!cmdList.contains(cmds[j])) 665 cmdList.add(cmds[j]); 666 } 667 } 668 } 669 670 String [] cmds = new String [cmdList.size()]; 671 cmds = (String [])cmdList.toArray(cmds); 672 673 return cmds; 674 } 675 676 701 } 702 | Popular Tags |