1 31 32 package org.opencms.loader; 33 34 import org.opencms.configuration.CmsConfigurationException; 35 import org.opencms.configuration.CmsVfsConfiguration; 36 import org.opencms.file.CmsObject; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.file.collectors.I_CmsResourceCollector; 40 import org.opencms.file.types.CmsResourceTypePlain; 41 import org.opencms.file.types.I_CmsResourceType; 42 import org.opencms.main.CmsException; 43 import org.opencms.main.CmsLog; 44 import org.opencms.main.OpenCms; 45 import org.opencms.module.CmsModule; 46 import org.opencms.module.CmsModuleManager; 47 import org.opencms.security.CmsRole; 48 import org.opencms.security.CmsRoleViolationException; 49 import org.opencms.util.CmsResourceTranslator; 50 import org.opencms.util.CmsStringUtil; 51 52 import java.io.IOException ; 53 import java.util.ArrayList ; 54 import java.util.Collections ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 import java.util.Locale ; 59 import java.util.Map ; 60 import java.util.Properties ; 61 62 import javax.servlet.ServletException ; 63 import javax.servlet.ServletRequest ; 64 import javax.servlet.ServletResponse ; 65 import javax.servlet.http.HttpServletRequest ; 66 import javax.servlet.http.HttpServletResponse ; 67 68 import org.apache.commons.logging.Log; 69 70 80 public class CmsResourceManager { 81 82 85 final class CmsResourceManagerConfiguration { 86 87 88 protected I_CmsResourceType[] m_resourceTypes; 89 90 91 private Map m_mappings; 92 93 94 private List m_resourceTypeList; 95 96 97 private Map m_resourceTypeMap; 98 99 102 protected CmsResourceManagerConfiguration() { 103 104 m_resourceTypes = new I_CmsResourceType[100]; 105 m_resourceTypeMap = new HashMap (); 106 m_mappings = new HashMap (); 107 m_resourceTypeList = new ArrayList (); 108 } 109 110 113 protected void freeze() { 114 115 m_resourceTypeList = Collections.unmodifiableList(m_resourceTypeList); 117 m_resourceTypeMap = Collections.unmodifiableMap(m_resourceTypeMap); 118 m_mappings = Collections.unmodifiableMap(m_mappings); 119 } 120 121 126 protected Map getMappings() { 127 128 return m_mappings; 129 } 130 131 136 protected List getResourceTypeList() { 137 138 return m_resourceTypeList; 139 } 140 141 146 protected Map getResourceTypeMap() { 147 148 return m_resourceTypeMap; 149 } 150 } 151 152 153 public static final String MIMETYPE_HTML = "text/html"; 154 155 156 public static final String MIMETYPE_TEXT = "text/plain"; 157 158 159 private static final Log LOG = CmsLog.getLog(CmsResourceManager.class); 160 161 162 private Map m_collectorNameMappings; 163 164 165 private List m_collectors; 166 167 168 private CmsResourceManagerConfiguration m_configuration; 169 170 171 private List m_configuredMimeTypes; 172 173 174 private CmsResourceTranslator m_fileTranslator; 175 176 177 private CmsResourceTranslator m_folderTranslator; 178 179 180 private boolean m_frozen; 181 182 183 private List m_includeExtensions; 184 185 186 private List m_loaderList; 187 188 189 private I_CmsResourceLoader[] m_loaders; 190 191 192 private Map m_mimeTypes; 193 194 195 private List m_resourceTypesFromXml; 196 197 201 public CmsResourceManager() { 202 203 if (CmsLog.INIT.isInfoEnabled()) { 204 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STARTING_LOADER_CONFIG_0)); 205 } 206 207 m_resourceTypesFromXml = new ArrayList (); 208 m_loaders = new I_CmsResourceLoader[16]; 209 m_loaderList = new ArrayList (); 210 m_includeExtensions = new ArrayList (); 211 m_configuredMimeTypes = new ArrayList (); 212 } 213 214 224 public synchronized I_CmsResourceCollector addContentCollector(String className, String order) 225 throws CmsConfigurationException { 226 227 Class classClazz; 228 try { 230 classClazz = Class.forName(className); 231 } catch (ClassNotFoundException e) { 232 LOG.error(Messages.get().getBundle().key(Messages.LOG_CONTENT_COLLECTOR_CLASS_NOT_FOUND_1, className), e); 233 return null; 234 } 235 236 I_CmsResourceCollector collector; 237 try { 238 collector = (I_CmsResourceCollector)classClazz.newInstance(); 239 } catch (InstantiationException e) { 240 throw new CmsConfigurationException(Messages.get().container( 241 Messages.ERR_INVALID_COLLECTOR_NAME_1, 242 className)); 243 } catch (IllegalAccessException e) { 244 throw new CmsConfigurationException(Messages.get().container( 245 Messages.ERR_INVALID_COLLECTOR_NAME_1, 246 className)); 247 } catch (ClassCastException e) { 248 throw new CmsConfigurationException(Messages.get().container( 249 Messages.ERR_INVALID_COLLECTOR_NAME_1, 250 className)); 251 } 252 253 int ord = 0; 255 try { 256 ord = Integer.valueOf(order).intValue(); 257 } catch (NumberFormatException e) { 258 LOG.error(Messages.get().getBundle().key(Messages.LOG_COLLECTOR_BAD_ORDER_NUMBER_1, className), e); 259 } 260 collector.setOrder(ord); 261 262 if (CmsLog.INIT.isInfoEnabled()) { 263 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_ADD_COLLECTOR_CLASS_2, className, order)); 264 } 265 266 if (m_collectors != null) { 268 m_collectors = new ArrayList (m_collectors); 269 m_collectorNameMappings = new HashMap (m_collectorNameMappings); 270 } else { 271 m_collectors = new ArrayList (); 272 m_collectorNameMappings = new HashMap (); 273 } 274 275 if (!m_collectors.contains(collector)) { 276 m_collectors.add(collector); 278 279 Iterator i = collector.getCollectorNames().iterator(); 280 while (i.hasNext()) { 281 String name = (String )i.next(); 282 if (m_collectorNameMappings.containsKey(name)) { 283 I_CmsResourceCollector otherCollector = (I_CmsResourceCollector)m_collectorNameMappings.get(name); 285 if (collector.getOrder() > otherCollector.getOrder()) { 286 m_collectorNameMappings.put(name, collector); 288 if (CmsLog.INIT.isInfoEnabled()) { 289 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_COLLECTOR_REPLACED_1, name)); 290 } 291 } else { 292 if (CmsLog.INIT.isInfoEnabled()) { 293 CmsLog.INIT.info(Messages.get().getBundle().key( 294 Messages.INIT_DUPLICATE_COLLECTOR_SKIPPED_1, 295 name)); 296 } 297 } 298 } else { 299 m_collectorNameMappings.put(name, collector); 300 if (CmsLog.INIT.isInfoEnabled()) { 301 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_ADD_COLLECTOR_1, name)); 302 } 303 } 304 } 305 } 306 307 Collections.sort(m_collectors); 309 m_collectors = Collections.unmodifiableList(m_collectors); 310 m_collectorNameMappings = Collections.unmodifiableMap(m_collectorNameMappings); 311 312 return collector; 314 } 315 316 322 public synchronized void addLoader(I_CmsResourceLoader loader) throws CmsConfigurationException { 323 324 if (m_frozen) { 326 throw new CmsConfigurationException(Messages.get().container(Messages.ERR_NO_CONFIG_AFTER_STARTUP_0)); 327 } 328 329 int pos = loader.getLoaderId(); 331 if (pos > m_loaders.length) { 332 I_CmsResourceLoader[] buffer = new I_CmsResourceLoader[pos * 2]; 333 System.arraycopy(m_loaders, 0, buffer, 0, m_loaders.length); 334 m_loaders = buffer; 335 } 336 m_loaders[pos] = loader; 337 if (loader instanceof I_CmsLoaderIncludeExtension) { 338 m_includeExtensions.add(loader); 340 } 341 m_loaderList.add(loader); 342 if (CmsLog.INIT.isInfoEnabled()) { 343 CmsLog.INIT.info(Messages.get().getBundle().key( 344 Messages.INIT_ADD_LOADER_2, 345 loader.getClass().getName(), 346 new Integer (pos))); 347 } 348 } 349 350 360 public CmsMimeType addMimeType(String extension, String type) throws CmsConfigurationException { 361 362 if (m_frozen) { 364 throw new CmsConfigurationException(Messages.get().container(Messages.ERR_NO_CONFIG_AFTER_STARTUP_0)); 365 } 366 367 CmsMimeType mimeType = new CmsMimeType(extension, type); 368 m_configuredMimeTypes.add(mimeType); 369 return mimeType; 370 } 371 372 380 public synchronized void addResourceType(I_CmsResourceType resourceType) throws CmsConfigurationException { 381 382 if (m_frozen) { 384 throw new CmsConfigurationException(Messages.get().container(Messages.ERR_NO_CONFIG_AFTER_STARTUP_0)); 385 } 386 387 m_resourceTypesFromXml.add(resourceType); 388 } 389 390 397 public I_CmsResourceCollector getContentCollector(String collectorName) { 398 399 return (I_CmsResourceCollector)m_collectorNameMappings.get(collectorName); 400 } 401 402 421 public I_CmsResourceType getDefaultTypeForName(String resourcename) throws CmsException { 422 423 String typeName = null; 424 String suffix = null; 425 if (CmsStringUtil.isNotEmpty(resourcename)) { 426 int pos = resourcename.lastIndexOf('.'); 427 if (pos >= 0) { 428 suffix = resourcename.substring(pos); 429 if (CmsStringUtil.isNotEmpty(suffix)) { 430 suffix = suffix.toLowerCase(); 431 typeName = (String )m_configuration.getMappings().get(suffix); 432 433 } 434 } 435 } 436 437 if (typeName == null) { 438 typeName = CmsResourceTypePlain.getStaticTypeName(); 440 } 441 442 if (CmsLog.INIT.isDebugEnabled()) { 443 CmsLog.INIT.debug(Messages.get().getBundle().key(Messages.INIT_GET_RESTYPE_2, typeName, suffix)); 444 } 445 return getResourceType(typeName); 447 } 448 449 454 public Map getExtensionMapping() { 455 456 return m_configuration.getMappings(); 457 } 458 459 464 public CmsResourceTranslator getFileTranslator() { 465 466 return m_fileTranslator; 467 } 468 469 474 public CmsResourceTranslator getFolderTranslator() { 475 476 return m_folderTranslator; 477 } 478 479 486 public I_CmsResourceLoader getLoader(CmsResource resource) throws CmsLoaderException { 487 488 return getLoader(getResourceType(resource.getTypeId()).getLoaderId()); 489 } 490 491 497 public I_CmsResourceLoader getLoader(int id) { 498 499 return m_loaders[id]; 500 } 501 502 507 public List getLoaders() { 508 509 return m_loaderList; 510 } 511 512 526 public String getMimeType(String filename, String encoding) { 527 528 return getMimeType(filename, encoding, MIMETYPE_HTML); 529 } 530 531 546 public String getMimeType(String filename, String encoding, String defaultMimeType) { 547 548 String mimeType = null; 549 int lastDot = filename.lastIndexOf('.'); 550 if ((lastDot > 0) && (lastDot < (filename.length() - 1))) { 552 mimeType = (String )m_mimeTypes.get(filename.substring(lastDot).toLowerCase(Locale.ENGLISH)); 553 } 554 if (mimeType == null) { 555 mimeType = defaultMimeType; 556 if (mimeType == null) { 557 return null; 559 } 560 } 561 StringBuffer result = new StringBuffer (mimeType); 562 if ((encoding != null) && mimeType.startsWith("text") && (mimeType.indexOf("charset") == -1)) { 563 result.append("; charset="); 564 result.append(encoding); 565 } 566 return result.toString(); 567 } 568 569 574 public List getMimeTypes() { 575 576 return m_configuredMimeTypes; 577 } 578 579 584 public List getRegisteredContentCollectors() { 585 586 return m_collectors; 587 } 588 589 596 public I_CmsResourceType getResourceType(int typeId) throws CmsLoaderException { 597 598 I_CmsResourceType result = null; 599 if (typeId < m_configuration.m_resourceTypes.length) { 600 result = m_configuration.m_resourceTypes[typeId]; 601 } 602 if (result == null) { 603 throw new CmsLoaderException(Messages.get().container( 604 Messages.ERR_UNKNOWN_RESTYPE_ID_REQ_1, 605 new Integer (typeId))); 606 } 607 return result; 608 } 609 610 617 public I_CmsResourceType getResourceType(String typeName) throws CmsLoaderException { 618 619 I_CmsResourceType result = (I_CmsResourceType)m_configuration.getResourceTypeMap().get(typeName); 620 if (result != null) { 621 return result; 622 } 623 throw new CmsLoaderException(Messages.get().container(Messages.ERR_UNKNOWN_RESTYPE_NAME_REQ_1, typeName)); 624 } 625 626 631 public List getResourceTypes() { 632 633 return m_configuration.getResourceTypeList(); 635 } 636 637 646 public CmsTemplateLoaderFacade getTemplateLoaderFacade(CmsObject cms, CmsResource resource, String templateProperty) 647 throws CmsException { 648 649 String absolutePath = cms.getSitePath(resource); 650 651 String templateProp = cms.readPropertyObject(absolutePath, templateProperty, true).getValue(); 652 653 if (templateProp == null) { 654 throw new CmsLoaderException(Messages.get().container( 656 Messages.ERR_NONDEF_PROP_2, 657 templateProperty, 658 absolutePath)); 659 } 660 661 CmsResource template = cms.readFile(templateProp, CmsResourceFilter.IGNORE_EXPIRATION); 662 return new CmsTemplateLoaderFacade(getLoader(template), resource, template); 663 } 664 665 668 public void initConfiguration() { 669 670 if (CmsLog.INIT.isInfoEnabled()) { 671 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_LOADER_CONFIG_FINISHED_0)); 672 } 673 674 m_resourceTypesFromXml = Collections.unmodifiableList(m_resourceTypesFromXml); 675 m_loaderList = Collections.unmodifiableList(m_loaderList); 676 Collections.sort(m_configuredMimeTypes); 677 m_configuredMimeTypes = Collections.unmodifiableList(m_configuredMimeTypes); 678 679 initResourceTypes(); 681 initMimeTypes(); 683 } 684 685 692 public synchronized void initialize(CmsObject cms) throws CmsRoleViolationException { 693 694 if (OpenCms.getRunLevel() > OpenCms.RUNLEVEL_1_CORE_OBJECT) { 695 cms.checkRole(CmsRole.RESOURCE_TYPE_MANAGER); 697 } 698 699 initResourceTypes(); 701 702 Iterator i = m_configuration.getResourceTypeList().iterator(); 704 while (i.hasNext()) { 705 I_CmsResourceType type = (I_CmsResourceType)i.next(); 706 type.initialize(cms); 707 } 708 709 if (CmsLog.INIT.isInfoEnabled()) { 710 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_LOADER_CONFIG_FINISHED_0)); 711 } 712 } 713 714 725 public void loadResource(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 726 throws ServletException , IOException , CmsException { 727 728 res.setContentType(getMimeType(resource.getName(), cms.getRequestContext().getEncoding())); 729 I_CmsResourceLoader loader = getLoader(resource); 730 loader.load(cms, resource, req, res); 731 } 732 733 748 public String resolveIncludeExtensions( 749 String target, 750 String element, 751 boolean editable, 752 Map paramMap, 753 ServletRequest req, 754 ServletResponse res) throws CmsException { 755 756 if (m_includeExtensions == null) { 757 return target; 758 } 759 String result = target; 760 for (int i = 0; i < m_includeExtensions.size(); i++) { 761 I_CmsLoaderIncludeExtension loader = (I_CmsLoaderIncludeExtension)m_includeExtensions.get(i); 763 result = loader.includeExtension(target, element, editable, paramMap, req, res); 764 } 765 return result; 766 } 767 768 774 public void setTranslators(CmsResourceTranslator folderTranslator, CmsResourceTranslator fileTranslator) { 775 776 m_folderTranslator = folderTranslator; 777 m_fileTranslator = fileTranslator; 778 } 779 780 785 public synchronized void shutDown() throws Exception { 786 787 Iterator it = m_loaderList.iterator(); 788 while (it.hasNext()) { 789 I_CmsResourceLoader loader = (I_CmsResourceLoader)it.next(); 791 loader.destroy(); 792 } 793 794 m_loaderList = null; 795 m_loaders = null; 796 m_collectorNameMappings = null; 797 m_includeExtensions = null; 798 m_mimeTypes = null; 799 m_configuredMimeTypes = null; 800 801 if (CmsLog.INIT.isInfoEnabled()) { 802 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_1, this.getClass().getName())); 803 } 804 } 805 806 814 private void initMimeTypes() { 815 816 Properties mimeTypes = new Properties (); 818 try { 819 mimeTypes.load(getClass().getClassLoader().getResourceAsStream("mimetypes.properties")); 821 } catch (Throwable t) { 822 try { 823 mimeTypes.load(getClass().getClassLoader().getResourceAsStream( 825 "org/opencms/loader/mimetypes.properties")); 826 } catch (Throwable t2) { 827 if (LOG.isInfoEnabled()) { 828 LOG.info(Messages.get().getBundle().key( 829 Messages.LOG_READ_MIMETYPES_FAILED_2, 830 "mimetypes.properties", 831 "org/opencms/loader/mimetypes.properties")); 832 } 833 } 834 } 835 836 List combinedMimeTypes = new ArrayList (mimeTypes.size() + m_configuredMimeTypes.size()); 838 combinedMimeTypes.addAll(m_configuredMimeTypes); 840 Iterator i = mimeTypes.entrySet().iterator(); 842 while (i.hasNext()) { 843 Map.Entry entry = (Map.Entry )i.next(); 844 CmsMimeType mimeType = new CmsMimeType((String )entry.getKey(), (String )entry.getValue(), false); 845 if (!combinedMimeTypes.contains(mimeType)) { 846 combinedMimeTypes.add(mimeType); 848 } 849 } 850 851 m_mimeTypes = new HashMap (mimeTypes.size()); 853 Iterator j = combinedMimeTypes.iterator(); 854 while (j.hasNext()) { 855 CmsMimeType mimeType = (CmsMimeType)j.next(); 856 m_mimeTypes.put(mimeType.getExtension(), mimeType.getType()); 857 } 858 859 if (CmsLog.INIT.isInfoEnabled()) { 860 CmsLog.INIT.info(Messages.get().getBundle().key( 861 Messages.INIT_NUM_MIMETYPES_1, 862 new Integer (m_mimeTypes.size()))); 863 } 864 } 865 866 873 private synchronized void initResourceType( 874 I_CmsResourceType resourceType, 875 CmsResourceManagerConfiguration configuration) { 876 877 int pos = resourceType.getTypeId(); 879 if (pos > configuration.m_resourceTypes.length) { 880 I_CmsResourceType[] types = new I_CmsResourceType[pos * 2]; 881 System.arraycopy(configuration.m_resourceTypes, 0, types, 0, configuration.m_resourceTypes.length); 882 configuration.m_resourceTypes = types; 883 } 884 configuration.m_resourceTypes[pos] = resourceType; 885 configuration.getResourceTypeList().add(resourceType); 886 configuration.getResourceTypeMap().put(resourceType.getTypeName(), resourceType); 887 if (CmsLog.INIT.isInfoEnabled()) { 888 CmsLog.INIT.info(Messages.get().getBundle().key( 889 Messages.INIT_ADD_RESTYPE_3, 890 resourceType.getTypeName(), 891 new Integer (resourceType.getTypeId()), 892 resourceType.getClass().getName())); 893 } 894 895 List mappings = resourceType.getConfiguredMappings(); 897 Iterator i = mappings.iterator(); 898 while (i.hasNext()) { 899 String mapping = (String )i.next(); 900 if (!configuration.getMappings().containsKey(mapping)) { 903 configuration.getMappings().put(mapping, resourceType.getTypeName()); 904 if (CmsLog.INIT.isInfoEnabled()) { 905 CmsLog.INIT.info(Messages.get().getBundle().key( 906 Messages.INIT_MAP_RESTYPE_2, 907 mapping, 908 resourceType.getTypeName())); 909 } 910 } 911 } 912 } 913 914 917 private synchronized void initResourceTypes() { 918 919 if (CmsLog.INIT.isInfoEnabled()) { 920 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STARTING_LOADER_CONFIG_0)); 921 } 922 923 CmsResourceManagerConfiguration newConfiguration = new CmsResourceManagerConfiguration(); 924 925 if (CmsLog.INIT.isInfoEnabled()) { 926 CmsLog.INIT.info(Messages.get().getBundle().key( 927 Messages.INIT_ADD_RESTYPE_FROM_FILE_2, 928 new Integer (m_resourceTypesFromXml.size()), 929 CmsVfsConfiguration.DEFAULT_XML_FILE_NAME)); 930 } 931 932 Iterator i; 934 i = m_resourceTypesFromXml.iterator(); 935 while (i.hasNext()) { 936 I_CmsResourceType resourceType = (I_CmsResourceType)i.next(); 937 initResourceType(resourceType, newConfiguration); 938 } 939 940 CmsModuleManager moduleManager = OpenCms.getModuleManager(); 942 if (moduleManager != null) { 943 i = moduleManager.getModuleNames().iterator(); 944 while (i.hasNext()) { 945 CmsModule module = moduleManager.getModule((String )i.next()); 946 if ((module != null) && (module.getResourceTypes().size() > 0)) { 947 if (CmsLog.INIT.isInfoEnabled()) { 949 CmsLog.INIT.info(Messages.get().getBundle().key( 950 Messages.INIT_ADD_NUM_RESTYPES_FROM_MOD_2, 951 new Integer (module.getResourceTypes().size()), 952 module.getName())); 953 } 954 955 Iterator j = module.getResourceTypes().iterator(); 956 while (j.hasNext()) { 957 I_CmsResourceType resourceType = (I_CmsResourceType)j.next(); 958 initResourceType(resourceType, newConfiguration); 959 } 960 } 961 } 962 } 963 964 newConfiguration.freeze(); 966 m_configuration = newConfiguration; 967 m_frozen = true; 968 969 if (CmsLog.INIT.isInfoEnabled()) { 970 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_RESOURCE_TYPE_INITIALIZED_0)); 971 } 972 } 973 } | Popular Tags |