001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.file.types;
029
030import org.opencms.configuration.CmsConfigurationException;
031import org.opencms.db.CmsSecurityManager;
032import org.opencms.file.CmsFile;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsProperty;
035import org.opencms.file.CmsResource;
036import org.opencms.file.CmsResource.CmsResourceDeleteMode;
037import org.opencms.file.CmsResource.CmsResourceUndoMode;
038import org.opencms.loader.CmsDumpLoader;
039import org.opencms.loader.CmsJspLoader;
040import org.opencms.main.CmsException;
041import org.opencms.main.CmsIllegalArgumentException;
042import org.opencms.main.OpenCms;
043
044import java.util.HashSet;
045import java.util.List;
046import java.util.Set;
047
048/**
049 * Resource type descriptor for the type "plain".<p>
050 *
051 * @since 6.0.0
052 */
053public class CmsResourceTypePlain extends A_CmsResourceType {
054
055    /** Static type id. */
056    private static int m_staticTypeId;
057
058    /** The type id of this resource type. */
059    @SuppressWarnings("unused")
060    private static final int RESOURCE_TYPE_ID = 1;
061
062    /** The name of this resource type. */
063    private static final String RESOURCE_TYPE_NAME = "plain";
064
065    /** The serial version id. */
066    private static final long serialVersionUID = -4496210486951893369L;
067
068    /** JSP Loader instance. */
069    protected CmsJspLoader m_jspLoader;
070
071    /**
072     * Default constructor, used to initialize member variables.<p>
073     */
074    public CmsResourceTypePlain() {
075
076        super();
077    }
078
079    /**
080     * Returns the static type id of this (default) resource type.<p>
081     *
082     * @return the static type id of this (default) resource type
083     */
084    public static int getStaticTypeId() {
085
086        return m_staticTypeId;
087    }
088
089    /**
090     * Returns the static type name of this (default) resource type.<p>
091     *
092     * @return the static type name of this (default) resource type
093     */
094    public static String getStaticTypeName() {
095
096        return RESOURCE_TYPE_NAME;
097    }
098
099    /**
100     * @see org.opencms.file.types.A_CmsResourceType#chtype(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, int)
101     */
102    @Override
103    public void chtype(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int type)
104    throws CmsException {
105
106        Set<String> references = getReferencingStrongLinks(cms, resource);
107        super.chtype(cms, securityManager, resource, type);
108        removeReferencingFromCache(references);
109    }
110
111    /**
112     * @see org.opencms.file.types.A_CmsResourceType#deleteResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, org.opencms.file.CmsResource.CmsResourceDeleteMode)
113     */
114    @Override
115    public void deleteResource(
116        CmsObject cms,
117        CmsSecurityManager securityManager,
118        CmsResource resource,
119        CmsResourceDeleteMode siblingMode)
120    throws CmsException {
121
122        Set<String> references = getReferencingStrongLinks(cms, resource);
123        super.deleteResource(cms, securityManager, resource, siblingMode);
124        removeReferencingFromCache(references);
125    }
126
127    /**
128     * A plain resource might appear as a sub-element in a JSP,
129     * therefore it needs cache properties.<p>
130     *
131     * @see org.opencms.file.types.I_CmsResourceType#getCachePropertyDefault()
132     */
133    @Override
134    public String getCachePropertyDefault() {
135
136        return "always;";
137    }
138
139    /**
140     * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
141     */
142    @Override
143    public int getLoaderId() {
144
145        return CmsDumpLoader.RESOURCE_LOADER_ID;
146    }
147
148    /**
149     * @see org.opencms.file.types.A_CmsResourceType#initConfiguration(java.lang.String, java.lang.String, String)
150     */
151    @Override
152    public void initConfiguration(String name, String id, String className) throws CmsConfigurationException {
153
154        super.initConfiguration(name, id, className);
155        if (name.equals(RESOURCE_TYPE_NAME)) {
156            m_staticTypeId = m_typeId;
157        }
158    }
159
160    /**
161     * @see org.opencms.file.types.A_CmsResourceType#initialize(org.opencms.file.CmsObject)
162     */
163    @Override
164    public void initialize(CmsObject cms) {
165
166        super.initialize(cms);
167        try {
168            m_jspLoader = (CmsJspLoader)OpenCms.getResourceManager().getLoader(CmsJspLoader.RESOURCE_LOADER_ID);
169        } catch (ArrayIndexOutOfBoundsException e) {
170            // ignore, loader not configured
171        }
172    }
173
174    /**
175     * @see org.opencms.file.types.A_CmsResourceType#moveResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, java.lang.String)
176     */
177    @Override
178    public void moveResource(
179        CmsObject cms,
180        CmsSecurityManager securityManager,
181        CmsResource resource,
182        String destination)
183    throws CmsException, CmsIllegalArgumentException {
184
185        Set<String> references = getReferencingStrongLinks(cms, resource);
186        super.moveResource(cms, securityManager, resource, destination);
187        removeReferencingFromCache(references);
188    }
189
190    /**
191     * @see org.opencms.file.types.A_CmsResourceType#replaceResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, int, byte[], java.util.List)
192     */
193    @Override
194    public void replaceResource(
195        CmsObject cms,
196        CmsSecurityManager securityManager,
197        CmsResource resource,
198        int type,
199        byte[] content,
200        List<CmsProperty> properties)
201    throws CmsException {
202
203        Set<String> references = getReferencingStrongLinks(cms, resource);
204        super.replaceResource(cms, securityManager, resource, type, content, properties);
205        removeReferencingFromCache(references);
206    }
207
208    /**
209     * @see org.opencms.file.types.A_CmsResourceType#restoreResource(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, int)
210     */
211    @Override
212    public void restoreResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int version)
213    throws CmsException {
214
215        Set<String> references = getReferencingStrongLinks(cms, resource);
216        super.restoreResource(cms, securityManager, resource, version);
217        removeReferencingFromCache(references);
218    }
219
220    /**
221     * @see org.opencms.file.types.A_CmsResourceType#setDateExpired(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, long, boolean)
222     */
223    @Override
224    public void setDateExpired(
225        CmsObject cms,
226        CmsSecurityManager securityManager,
227        CmsResource resource,
228        long dateExpired,
229        boolean recursive)
230    throws CmsException {
231
232        Set<String> references = getReferencingStrongLinks(cms, resource);
233        super.setDateExpired(cms, securityManager, resource, dateExpired, recursive);
234        removeReferencingFromCache(references);
235    }
236
237    /**
238     * @see org.opencms.file.types.A_CmsResourceType#setDateLastModified(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, long, boolean)
239     */
240    @Override
241    public void setDateLastModified(
242        CmsObject cms,
243        CmsSecurityManager securityManager,
244        CmsResource resource,
245        long dateLastModified,
246        boolean recursive)
247    throws CmsException {
248
249        Set<String> references = getReferencingStrongLinks(cms, resource);
250        super.setDateLastModified(cms, securityManager, resource, dateLastModified, recursive);
251        removeReferencingFromCache(references);
252    }
253
254    /**
255     * @see org.opencms.file.types.A_CmsResourceType#setDateReleased(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, long, boolean)
256     */
257    @Override
258    public void setDateReleased(
259        CmsObject cms,
260        CmsSecurityManager securityManager,
261        CmsResource resource,
262        long dateReleased,
263        boolean recursive)
264    throws CmsException {
265
266        Set<String> references = getReferencingStrongLinks(cms, resource);
267        super.setDateReleased(cms, securityManager, resource, dateReleased, recursive);
268        removeReferencingFromCache(references);
269    }
270
271    /**
272     * @see org.opencms.file.types.A_CmsResourceType#undoChanges(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsResource, org.opencms.file.CmsResource.CmsResourceUndoMode)
273     */
274    @Override
275    public void undoChanges(
276        CmsObject cms,
277        CmsSecurityManager securityManager,
278        CmsResource resource,
279        CmsResourceUndoMode mode)
280    throws CmsException {
281
282        Set<String> references = getReferencingStrongLinks(cms, resource);
283        super.undoChanges(cms, securityManager, resource, mode);
284        removeReferencingFromCache(references);
285    }
286
287    /**
288     * @see org.opencms.file.types.A_CmsResourceType#writeFile(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, org.opencms.file.CmsFile)
289     */
290    @Override
291    public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource) throws CmsException {
292
293        Set<String> references = getReferencingStrongLinks(cms, resource);
294        CmsFile file = super.writeFile(cms, securityManager, resource);
295        removeReferencingFromCache(references);
296        return file;
297    }
298
299    /**
300     * Returns a set of root paths of files that are including the given resource using the 'link.strong' macro.<p>
301     *
302     * @param cms the current cms context
303     * @param resource the resource to check
304     *
305     * @return the set of referencing paths
306     *
307     * @throws CmsException if something goes wrong
308     */
309    protected Set<String> getReferencingStrongLinks(CmsObject cms, CmsResource resource) throws CmsException {
310
311        Set<String> references = new HashSet<String>();
312        if (m_jspLoader == null) {
313            return references;
314        }
315        m_jspLoader.getReferencingStrongLinks(cms, resource, references);
316        return references;
317    }
318
319    /**
320     * Removes the referencing resources from the cache.<p>
321     *
322     * @param references the references to remove
323     */
324    protected void removeReferencingFromCache(Set<String> references) {
325
326        if (m_jspLoader != null) {
327            m_jspLoader.removeFromCache(references, false);
328        }
329    }
330}