📄 cmsresourcemanager.java
字号:
// initialize the resource types
initResourceTypes();
// call initialize method on all resource types
Iterator i = m_configuration.m_resourceTypeList.iterator();
while (i.hasNext()) {
I_CmsResourceType type = (I_CmsResourceType)i.next();
type.initialize(cms);
}
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_LOADER_CONFIG_FINISHED_0));
}
}
/**
* Loads the requested resource and writes the contents to the response stream.<p>
*
* @param req the current HTTP request
* @param res the current HTTP response
* @param cms the current OpenCms user context
* @param resource the requested resource
* @throws ServletException if something goes wrong
* @throws IOException if something goes wrong
* @throws CmsException if something goes wrong
*/
public void loadResource(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException, CmsException {
res.setContentType(getMimeType(resource.getName(), cms.getRequestContext().getEncoding()));
I_CmsResourceLoader loader = getLoader(resource);
loader.load(cms, resource, req, res);
}
/**
* Extension method for handling special, loader depended actions during the include process.<p>
*
* Note: If you have multiple loaders configured that require include extensions,
* all loaders are called in the order they are configured in.<p>
*
* @param target the target for the include, might be <code>null</code>
* @param element the element to select form the target might be <code>null</code>
* @param editable the flag to indicate if the target is is enabled for direct edit
* @param paramMap a map of parameters for the include, can be modified, might be <code>null</code>
* @param req the current request
* @param res the current response
* @throws CmsException in case something goes wrong
* @return the modified target URI
*/
public String resolveIncludeExtensions(
String target,
String element,
boolean editable,
Map paramMap,
ServletRequest req,
ServletResponse res) throws CmsException {
if (m_includeExtensions == null) {
return target;
}
String result = target;
for (int i = 0; i < m_includeExtensions.size(); i++) {
// offer the element to every include extension
I_CmsLoaderIncludeExtension loader = (I_CmsLoaderIncludeExtension)m_includeExtensions.get(i);
result = loader.includeExtension(target, element, editable, paramMap, req, res);
}
return result;
}
/**
* Sets the folder and the file translator.<p>
*
* @param folderTranslator the folder translator to set
* @param fileTranslator the file translator to set
*/
public void setTranslators(CmsResourceTranslator folderTranslator, CmsResourceTranslator fileTranslator) {
m_folderTranslator = folderTranslator;
m_fileTranslator = fileTranslator;
}
/**
* Shuts down this resource manage instance.<p>
*
* @throws Exception in case of errors during shutdown
*/
public synchronized void shutDown() throws Exception {
Iterator it = m_loaderList.iterator();
while (it.hasNext()) {
// destroy all resource loaders
I_CmsResourceLoader loader = (I_CmsResourceLoader)it.next();
loader.destroy();
}
m_loaderList = null;
m_loaders = null;
m_collectorNameMappings = null;
m_includeExtensions = null;
m_mimeTypes = null;
m_configuredMimeTypes = null;
m_configuredRelationTypes = null;
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_1, this.getClass().getName()));
}
}
/**
* Initialize the MIME types.<p>
*
* MIME types are configured in the OpenCms <code>opencms-vfs.xml</code> configuration file.<p>
*
* For legacy reasons, the MIME types are also read from a file <code>"mimetypes.properties"</code>
* that must be located in the default <code>"classes"</code> folder of the web application.<p>
*/
private void initMimeTypes() {
// legacy MIME type initialization: try to read properties file
Properties mimeTypes = new Properties();
try {
// first try: read MIME types from default package
mimeTypes.load(getClass().getClassLoader().getResourceAsStream("mimetypes.properties"));
} catch (Throwable t) {
try {
// second try: read MIME types from loader package (legacy reasons, there are no types by default)
mimeTypes.load(getClass().getClassLoader().getResourceAsStream(
"org/opencms/loader/mimetypes.properties"));
} catch (Throwable t2) {
if (LOG.isInfoEnabled()) {
LOG.info(Messages.get().getBundle().key(
Messages.LOG_READ_MIMETYPES_FAILED_2,
"mimetypes.properties",
"org/opencms/loader/mimetypes.properties"));
}
}
}
// initialize the Map with all available MIME types
List combinedMimeTypes = new ArrayList(mimeTypes.size() + m_configuredMimeTypes.size());
// first add all MIME types from the configuration
combinedMimeTypes.addAll(m_configuredMimeTypes);
// now add the MIME types from the properties
Iterator i = mimeTypes.entrySet().iterator();
while (i.hasNext()) {
Map.Entry entry = (Map.Entry)i.next();
CmsMimeType mimeType = new CmsMimeType((String)entry.getKey(), (String)entry.getValue(), false);
if (!combinedMimeTypes.contains(mimeType)) {
// make sure no MIME types from the XML configuration are overwritten
combinedMimeTypes.add(mimeType);
}
}
// create a lookup Map for the MIME types
m_mimeTypes = new HashMap(mimeTypes.size());
Iterator j = combinedMimeTypes.iterator();
while (j.hasNext()) {
CmsMimeType mimeType = (CmsMimeType)j.next();
m_mimeTypes.put(mimeType.getExtension(), mimeType.getType());
}
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_NUM_MIMETYPES_1,
new Integer(m_mimeTypes.size())));
}
}
/**
* Adds a new resource type to the internal list of loaded resource types and initializes
* options for the resource type.<p>
*
* @param resourceType the resource type to add
* @param configuration the resource configuration
*/
private synchronized void initResourceType(
I_CmsResourceType resourceType,
CmsResourceManagerConfiguration configuration) {
// add the loader to the internal list of loaders
configuration.addResourceType(resourceType);
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_ADD_RESTYPE_3,
resourceType.getTypeName(),
new Integer(resourceType.getTypeId()),
resourceType.getClass().getName()));
}
// add the mappings
List mappings = resourceType.getConfiguredMappings();
Iterator i = mappings.iterator();
while (i.hasNext()) {
String mapping = (String)i.next();
// only add this mapping if a mapping with this file extension does not
// exist already
if (!configuration.m_extensionMappings.containsKey(mapping)) {
configuration.m_extensionMappings.put(mapping, resourceType.getTypeName());
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_MAP_RESTYPE_2,
mapping,
resourceType.getTypeName()));
}
}
}
}
/**
* Initializes member variables required for storing the resource types.<p>
*
* @throws CmsConfigurationException in case of duplicate resource types in the configuration
*/
private synchronized void initResourceTypes() throws CmsConfigurationException {
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_STARTING_LOADER_CONFIG_0));
}
CmsResourceManagerConfiguration newConfiguration = new CmsResourceManagerConfiguration();
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_ADD_RESTYPE_FROM_FILE_2,
new Integer(m_resourceTypesFromXml.size()),
CmsVfsConfiguration.DEFAULT_XML_FILE_NAME));
}
// build a new resource type list from the resource types of the XML configuration
Iterator i;
i = m_resourceTypesFromXml.iterator();
while (i.hasNext()) {
I_CmsResourceType resourceType = (I_CmsResourceType)i.next();
initResourceType(resourceType, newConfiguration);
}
// add all resource types declared in the modules
CmsModuleManager moduleManager = OpenCms.getModuleManager();
if (moduleManager != null) {
i = moduleManager.getModuleNames().iterator();
while (i.hasNext()) {
CmsModule module = moduleManager.getModule((String)i.next());
if ((module != null) && (module.getResourceTypes().size() > 0)) {
// module contains resource types
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(
Messages.INIT_ADD_NUM_RESTYPES_FROM_MOD_2,
new Integer(module.getResourceTypes().size()),
module.getName()));
}
Iterator j = module.getResourceTypes().iterator();
while (j.hasNext()) {
I_CmsResourceType resourceType = (I_CmsResourceType)j.next();
I_CmsResourceType conflictingType = null;
if (resourceType.getTypeId() == CmsResourceTypeUnknownFile.RESOURCE_TYPE_ID) {
// default unknown file resource type
if (m_restypeUnknownFile != null) {
// error: already set
conflictingType = m_restypeUnknownFile;
} else {
m_restypeUnknownFile = resourceType;
continue;
}
} else if (resourceType.getTypeId() == CmsResourceTypeUnknownFolder.RESOURCE_TYPE_ID) {
// default unknown folder resource type
if (m_restypeUnknownFolder != null) {
// error: already set
conflictingType = m_restypeUnknownFolder;
} else {
m_restypeUnknownFile = resourceType;
continue;
}
} else {
// normal resource types
conflictingType = newConfiguration.getResourceTypeById(resourceType.getTypeId());
}
if (conflictingType != null) {
throw new CmsConfigurationException(Messages.get().container(
Messages.ERR_CONFLICTING_MODULE_RESOURCE_TYPES_5,
new Object[] {
resourceType.getTypeName(),
new Integer(resourceType.getTypeId()),
module.getName(),
conflictingType.getTypeName(),
new Integer(conflictingType.getTypeId())}));
}
initResourceType(resourceType, newConfiguration);
}
}
}
}
// freeze the current configuration
newConfiguration.freeze(m_restypeUnknownFile, m_restypeUnknownFile);
m_configuration = newConfiguration;
m_frozen = true;
if (CmsLog.INIT.isInfoEnabled()) {
CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_RESOURCE_TYPE_INITIALIZED_0));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -