001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceReference;
020    import com.liferay.registry.ServiceTracker;
021    import com.liferay.registry.ServiceTrackerCustomizer;
022    
023    import java.net.MalformedURLException;
024    import java.net.URL;
025    
026    import java.util.Collection;
027    import java.util.Map;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    import javax.servlet.ServletContext;
031    
032    /**
033     * @author Peter Fellwock
034     */
035    public class PortalWebResourcesUtil {
036    
037            public static String getContextPath(String resourceType) {
038                    return getPortalWebResources(resourceType).getContextPath();
039            }
040    
041            public static long getLastModified(String resourceType) {
042                    return getPortalWebResources(resourceType).getLastModified();
043            }
044    
045            public static PortalWebResources getPortalWebResources(
046                    String resourceType) {
047    
048                    for (PortalWebResources portalWebResources :
049                                    _instance._getPortalWebResourcesList()) {
050    
051                            if (resourceType.equals(portalWebResources.getResourceType())) {
052                                    return portalWebResources;
053                            }
054                    }
055    
056                    return null;
057            }
058    
059            public static ServletContext getServletContext(String resourceType) {
060                    return getPortalWebResources(resourceType).getServletContext();
061            }
062    
063            public static URL getServletContextResource(String resourceName) {
064                    for (PortalWebResources portalWebResources :
065                                    _instance._getPortalWebResourcesList()) {
066    
067                            String contextPath = portalWebResources.getContextPath();
068    
069                            if (resourceName.startsWith(contextPath)) {
070                                    resourceName = resourceName.substring(contextPath.length());
071                            }
072    
073                            ServletContext servletContext =
074                                    portalWebResources.getServletContext();
075    
076                            try {
077                                    URL url = servletContext.getResource(resourceName);
078    
079                                    if (url != null) {
080                                            return url;
081                                    }
082                            }
083                            catch (MalformedURLException murle) {
084                            }
085                    }
086    
087                    return null;
088            }
089    
090            public static boolean isResourceAvailable(String path) {
091                    URL url = getServletContextResource(path);
092    
093                    if (url != null) {
094                            return true;
095                    }
096    
097                    return false;
098            }
099    
100            public static boolean isResourceContextPath(String requestURI) {
101                    for (PortalWebResources portalWebResources :
102                                    _instance._getPortalWebResourcesList()) {
103    
104                            if (requestURI.startsWith(portalWebResources.getContextPath())) {
105                                    return true;
106                            }
107                    }
108    
109                    return false;
110            }
111    
112            private PortalWebResourcesUtil() {
113                    Registry registry = RegistryUtil.getRegistry();
114    
115                    _serviceTracker = registry.trackServices(
116                            PortalWebResources.class,
117                            new PortalWebResourcesServiceTrackerCustomizer());
118    
119                    _serviceTracker.open();
120            }
121    
122            private Collection<PortalWebResources> _getPortalWebResourcesList() {
123                    return _portalWebResourcesMap.values();
124            }
125    
126            private static final PortalWebResourcesUtil _instance =
127                    new PortalWebResourcesUtil();
128    
129            private final Map<ServiceReference<PortalWebResources>, PortalWebResources>
130                    _portalWebResourcesMap = new ConcurrentHashMap<>();
131            private final ServiceTracker<PortalWebResources, PortalWebResources>
132                    _serviceTracker;
133    
134            private class PortalWebResourcesServiceTrackerCustomizer
135                    implements ServiceTrackerCustomizer
136                            <PortalWebResources, PortalWebResources> {
137    
138                    @Override
139                    public PortalWebResources addingService(
140                            ServiceReference<PortalWebResources> serviceReference) {
141    
142                            Registry registry = RegistryUtil.getRegistry();
143    
144                            PortalWebResources portalWebResources = registry.getService(
145                                    serviceReference);
146    
147                            _portalWebResourcesMap.put(serviceReference, portalWebResources);
148    
149                            return portalWebResources;
150                    }
151    
152                    @Override
153                    public void modifiedService(
154                            ServiceReference<PortalWebResources> serviceReference,
155                            PortalWebResources portalWebResources) {
156                    }
157    
158                    @Override
159                    public void removedService(
160                            ServiceReference<PortalWebResources> serviceReference,
161                            PortalWebResources portalWebResources) {
162    
163                            Registry registry = RegistryUtil.getRegistry();
164    
165                            registry.ungetService(serviceReference);
166    
167                            _portalWebResourcesMap.remove(serviceReference);
168                    }
169    
170            }
171    
172    }