![JAR search and dependency download from the Maven repository](/logo.png)
com.liferay.portal.template.velocity.internal.VelocityServletResourceParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.liferay.portal.template.velocity
Show all versions of com.liferay.portal.template.velocity
Liferay Portal Template Velocity
The newest version!
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.portal.template.velocity.internal;
import com.liferay.osgi.service.tracker.collections.map.ServiceReferenceMapper;
import com.liferay.osgi.service.tracker.collections.map.ServiceTrackerMap;
import com.liferay.osgi.service.tracker.collections.map.ServiceTrackerMapFactory;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.PortalWebResourceConstants;
import com.liferay.portal.kernel.servlet.PortalWebResourcesUtil;
import com.liferay.portal.kernel.template.TemplateConstants;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.template.TemplateResourceParser;
import com.liferay.portal.template.URLResourceParser;
import java.io.IOException;
import java.net.URL;
import javax.servlet.ServletContext;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
/**
* @author Alexander Chow
* @author Raymond Augé
*/
@Component(
property = "lang.type=" + TemplateConstants.LANG_TYPE_VM,
service = TemplateResourceParser.class
)
public class VelocityServletResourceParser extends URLResourceParser {
@Override
public URL getURL(String source) throws IOException {
int pos = source.indexOf(TemplateConstants.SERVLET_SEPARATOR);
if (pos == -1) {
return null;
}
String servletContextName = source.substring(0, pos);
if (servletContextName.equals(_portal.getPathContext())) {
servletContextName = _portal.getServletContextName();
}
ServletContext servletContext = _serviceTrackerMap.getService(
servletContextName);
if (servletContext == null) {
_log.error(
StringBundler.concat(
source, " is not valid because ", servletContextName,
" does not map to a servlet context"));
return null;
}
String name = source.substring(
pos + TemplateConstants.SERVLET_SEPARATOR.length());
if (_log.isDebugEnabled()) {
_log.debug(
StringBundler.concat(
name, " is associated with the servlet context ",
servletContextName, " ", servletContext));
}
URL url = servletContext.getResource(name);
if (url == null) {
url = PortalWebResourcesUtil.getResource(name);
}
if ((url == null) && name.endsWith("/init_custom.vm")) {
if (_log.isWarnEnabled()) {
_log.warn("The template " + name + " should be created");
}
ServletContext themeClassicServletContext =
PortalWebResourcesUtil.getServletContext(
PortalWebResourceConstants.RESOURCE_TYPE_THEME_CLASSIC);
url = themeClassicServletContext.getResource(
"/classic/templates/init_custom.vm");
}
return url;
}
@Activate
protected void activate(final BundleContext bundleContext) {
_serviceTrackerMap = ServiceTrackerMapFactory.openSingleValueMap(
bundleContext, ServletContext.class, null,
new ServiceReferenceMapper() {
@Override
public void map(
ServiceReference serviceReference,
ServiceReferenceMapper.Emitter emitter) {
try {
ServletContext servletContext =
bundleContext.getService(serviceReference);
String servletContextName = GetterUtil.getString(
servletContext.getServletContextName());
emitter.emit(servletContextName);
}
finally {
bundleContext.ungetService(serviceReference);
}
}
});
}
@Deactivate
protected void deactivate() {
_serviceTrackerMap.close();
}
private static final Log _log = LogFactoryUtil.getLog(
VelocityServletResourceParser.class);
@Reference
private Portal _portal;
private ServiceTrackerMap _serviceTrackerMap;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy