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.publish;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.db.CmsSecurityManager;
032import org.opencms.db.generic.CmsPublishHistoryCleanupFilter;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsResourceFilter;
036import org.opencms.file.CmsUser;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.main.CmsRuntimeException;
040import org.opencms.main.OpenCms;
041import org.opencms.relations.CmsRelation;
042import org.opencms.relations.CmsRelationFilter;
043import org.opencms.report.CmsShellReport;
044import org.opencms.report.I_CmsReport;
045import org.opencms.security.CmsRole;
046import org.opencms.security.CmsSecurityException;
047import org.opencms.util.CmsUUID;
048
049import java.util.ArrayList;
050import java.util.Collection;
051import java.util.Iterator;
052import java.util.List;
053import java.util.Map;
054import java.util.concurrent.CopyOnWriteArrayList;
055import java.util.concurrent.TimeUnit;
056import java.util.function.Supplier;
057
058import org.apache.commons.logging.Log;
059
060/**
061 * This manager provide access to the publish engine runtime information.<p>
062 *
063 * @since 6.5.5
064 */
065public class CmsPublishManager {
066
067    /**
068     * Enum for the different publish  list remove modes.<p>
069     */
070    public static enum PublishListRemoveMode {
071        /** Remove publish list entries for all users if a resource is published. */
072        allUsers,
073        /** Remove publish list entry for the current user if a resource is published. */
074        currentUser
075    }
076
077    /** Log for this class. */
078    private static final Log LOG = CmsLog.getLog(CmsPublishManager.class);
079
080    /** The default history size. */
081    public static final int DEFAULT_HISTORY_SIZE = 100;
082
083    /** The default persistence setting for the publish queue. */
084    public static final boolean DEFAULT_QUEUE_PERSISTANCE = false;
085
086    /** The default shutdown time for the running publish job. */
087    public static final int DEFAULT_QUEUE_SHUTDOWNTIME = 1;
088
089    /** Milliseconds in a second. */
090    private static final int MS_ONE_SECOND = 1000;
091
092    /** List of providers for special publish history ids that shouldn't be cleaned up. */
093    private static CopyOnWriteArrayList<Supplier<List<CmsUUID>>> m_specialHistoryIdProviders = new CopyOnWriteArrayList<>();
094
095    /** Flag which controls whether CMS_PUBLISH_HISTORY entries should be automatically removed. */
096    private boolean m_autoCleanupHistoryEntries;
097
098    /** Indicates if the configuration can be modified. */
099    private boolean m_frozen;
100
101    /** The underlying publish engine. */
102    private CmsPublishEngine m_publishEngine;
103
104    /** The maximum size of the publish history. */
105    private int m_publishHistorySize;
106
107    /** The publish list remove mode. */
108    private CmsPublishManager.PublishListRemoveMode m_publishListRemoveMode;
109
110    /** Publish job verifier. */
111    private CmsPublishListVerifier m_publishListVerifier = new CmsPublishListVerifier();
112
113    /** Indicates if the publish queue is re-initialized on startup. */
114    private boolean m_publishQueuePersistance;
115
116    /** The amount of time to wait for a publish job during shutdown. */
117    private int m_publishQueueShutdowntime;
118
119    /** The security manager. */
120    private CmsSecurityManager m_securityManager;
121
122    /**
123     * Default constructor used in digester initialization.<p>
124     */
125    public CmsPublishManager() {
126
127        m_publishEngine = null;
128        m_frozen = false;
129    }
130
131    /**
132     * Constructor used to create a pre-initialized instance.<p>
133     *
134     * @param historySize the size of the publish history
135     * @param queuePersistance indicates if the queue is re-initialized on startup
136     * @param queueShutdowntime the amount of time to wait for a publish job during shutdown
137     */
138    public CmsPublishManager(int historySize, boolean queuePersistance, int queueShutdowntime) {
139
140        m_publishEngine = null;
141        m_publishHistorySize = historySize;
142        m_publishQueuePersistance = queuePersistance;
143        m_publishQueueShutdowntime = queueShutdowntime;
144        m_frozen = false;
145    }
146
147    /**
148     * Adds provider for history ids that shouldn't be removed by bulk history cleanup operations.
149     *
150     * @param provider the provider for special history ids
151     */
152    public static void addSpecialHistoryIdProvider(Supplier<List<CmsUUID>> provider) {
153
154        m_specialHistoryIdProviders.add(provider);
155
156    }
157
158    /**
159     * Abandons the current publish thread.<p>
160     */
161    public void abandonThread() {
162
163        m_publishEngine.abandonThread();
164    }
165
166    /**
167     * Aborts the given publish job.<p>
168     *
169     * @param cms the cms context
170     * @param publishJob the publish job to abort
171     * @param removeJob indicates if the job will be removed or added to history
172     *
173     * @throws CmsException if there is some problem during unlocking the resources
174     * @throws CmsSecurityException if the current user has not enough permissions
175     * @throws CmsPublishException if the publish job can not been aborted
176     */
177    public void abortPublishJob(CmsObject cms, CmsPublishJobEnqueued publishJob, boolean removeJob)
178    throws CmsException, CmsSecurityException, CmsPublishException {
179
180        if (!OpenCms.getRoleManager().hasRole(cms, CmsRole.PROJECT_MANAGER)
181            && !cms.getRequestContext().getCurrentUser().getId().equals(publishJob.getUserId())) {
182            // Can only be executed by somebody with the role CmsRole#PROJECT_MANAGER or the owner of the job
183            throw new CmsSecurityException(
184                Messages.get().container(
185                    Messages.ERR_PUBLISH_ENGINE_ABORT_DENIED_1,
186                    cms.getRequestContext().getCurrentUser().getName()));
187        }
188        m_publishEngine.abortPublishJob(cms.getRequestContext().getCurrentUser().getId(), publishJob, removeJob);
189    }
190
191    /**
192     * Adds a publish listener to listen on publish events.<p>
193     *
194     * @param listener the publish listener to add
195     */
196    public void addPublishListener(I_CmsPublishEventListener listener) {
197
198        m_publishEngine.addPublishListener(listener);
199    }
200
201    /**
202     * Check if the thread for the current publish job is still active or was interrupted
203     * and so the next job in the queue can be started.<p>
204     */
205    public void checkCurrentPublishJobThread() {
206
207        m_publishEngine.checkCurrentPublishJobThread();
208    }
209
210    /**
211     * Cleans up all unreferenced publish history entries except for the ones with a history id provided by the special history id providers.
212     *
213     * @param cms the CMS context
214     * @return the number of cleaned up rows
215     * @throws CmsException if something goes wrong
216     */
217    public int cleanupPublishHistory(CmsObject cms) throws CmsException {
218
219        List<CmsUUID> exceptions = new ArrayList<>();
220        for (Supplier<List<CmsUUID>> provider : m_specialHistoryIdProviders) {
221            exceptions.addAll(provider.get());
222        }
223        return m_securityManager.cleanupPublishHistory(
224            cms.getRequestContext(),
225            CmsPublishHistoryCleanupFilter.allUnreferencedExcept(exceptions));
226    }
227
228    /**
229     * Removes publish history entries for the given history id.
230     *
231     * @param cms the CMS context
232     * @param historyId a publish history id
233     * @return the number of cleaned up rows
234     * @throws CmsException if something goes wrong
235     */
236    public int cleanupPublishHistory(CmsObject cms, CmsUUID historyId) throws CmsException {
237
238        return m_securityManager.cleanupPublishHistory(
239            cms.getRequestContext(),
240            CmsPublishHistoryCleanupFilter.forHistoryId(historyId));
241    }
242
243    /**
244     * Cleans up all unreferenced publish history entries except for the ones with a history id from the given list of
245     * exceptions.
246     *
247     * @param cms the CMS context
248     * @param exceptions the list of exceptional history ids which shouldn't be removed
249     * @return the number of cleaned up rows
250     * @throws CmsException if something goes wrong
251     */
252    public int cleanupPublishHistory(CmsObject cms, List<CmsUUID> exceptions) throws CmsException {
253
254        return m_securityManager.cleanupPublishHistory(
255            cms.getRequestContext(),
256            CmsPublishHistoryCleanupFilter.allUnreferencedExcept(exceptions));
257    }
258
259    /**
260     * Disables the publishing of resources.<p>
261     */
262    public void disablePublishing() {
263
264        m_publishEngine.disableEngine();
265    }
266
267    /**
268     * Enables the enqeueing of resources for publishing.<p>
269     */
270    public void enablePublishing() {
271
272        m_publishEngine.enableEngine();
273    }
274
275    /**
276     * Returns the current running publish job.<p>
277     *
278     * @return the current running publish job
279     */
280    public CmsPublishJobRunning getCurrentPublishJob() {
281
282        if (m_publishEngine.getCurrentPublishJob() == null) {
283            return null;
284        }
285        return new CmsPublishJobRunning(m_publishEngine.getCurrentPublishJob().getPublishJob());
286    }
287
288    /**
289     * Returns a publish job based on its publish history id.<p>
290     *
291     * The returned publish job may be an enqueued, running or finished publish job.<p>
292     *
293     * @param publishHistoryId the publish history id to search for
294     *
295     * @return the publish job with the given publish history id, or <code>null</code>
296     */
297    public CmsPublishJobBase getJobByPublishHistoryId(CmsUUID publishHistoryId) {
298
299        return m_publishEngine.getJobByPublishHistoryId(publishHistoryId);
300    }
301
302    /**
303     * Returns the publish history list with already publish jobs.<p>
304     *
305     * @return a list of {@link CmsPublishJobFinished} objects
306     */
307    public List<CmsPublishJobFinished> getPublishHistory() {
308
309        return m_publishEngine.getPublishHistory().asList();
310    }
311
312    /**
313     * Returns the publish history list with already publish jobs, filtered by the given user.<p>
314     *
315     * @param user the user to filter the jobs with
316     *
317     * @return a list of {@link CmsPublishJobFinished} objects
318     */
319    public List<CmsPublishJobFinished> getPublishHistory(CmsUser user) {
320
321        List<CmsPublishJobFinished> result = new ArrayList<CmsPublishJobFinished>();
322        Iterator<CmsPublishJobFinished> it = getPublishHistory().iterator();
323        while (it.hasNext()) {
324            CmsPublishJobFinished publishJob = it.next();
325            if (publishJob.getUserId().equals(user.getId())) {
326                result.add(publishJob);
327            }
328        }
329        return result;
330    }
331
332    /**
333     * Returns the publish History Size.<p>
334     *
335     * @return the publish History Size
336     */
337    public int getPublishHistorySize() {
338
339        return m_publishHistorySize;
340    }
341
342    /**
343     * Returns a publish list with all new/changed/deleted resources of the current (offline)
344     * project that actually get published.<p>
345     *
346     * @param cms the cms request context
347     *
348     * @return a publish list
349     *
350     * @throws CmsException if something goes wrong
351     */
352    public CmsPublishList getPublishList(CmsObject cms) throws CmsException {
353
354        return m_securityManager.fillPublishList(
355            cms.getRequestContext(),
356            new CmsPublishList(cms.getRequestContext().getCurrentProject()));
357    }
358
359    /**
360     * Returns a publish list with all new/changed/deleted resources of the current (offline)
361     * project that actually get published for a direct publish of a single resource.<p>
362     *
363     * @param cms the cms request context
364     * @param directPublishResource the resource which will be directly published
365     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct
366     *                      published resource should also get published.
367     *
368     * @return a publish list
369     *
370     * @throws CmsException if something goes wrong
371     */
372    public CmsPublishList getPublishList(
373        CmsObject cms,
374        CmsResource directPublishResource,
375        boolean directPublishSiblings)
376    throws CmsException {
377
378        return m_securityManager.fillPublishList(
379            cms.getRequestContext(),
380            new CmsPublishList(directPublishResource, directPublishSiblings));
381    }
382
383    /**
384     * Returns a publish list with all new/changed/deleted resources of the current (offline)
385     * project that actually get published for a direct publish of a List of resources.<p>
386     *
387     * @param cms the cms request context
388     * @param directPublishResources the resources which will be directly published
389     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct
390     *                      published resources should also get published.
391     *
392     * @return a publish list
393     *
394     * @throws CmsException if something goes wrong
395     */
396    public CmsPublishList getPublishList(
397        CmsObject cms,
398        List<CmsResource> directPublishResources,
399        boolean directPublishSiblings)
400    throws CmsException {
401
402        return getPublishList(cms, directPublishResources, directPublishSiblings, true);
403    }
404
405    /**
406     * Returns a publish list with all new/changed/deleted resources of the current (offline)
407     * project that actually get published for a direct publish of a List of resources.<p>
408     *
409     * @param cms the cms request context
410     * @param directPublishResources the {@link CmsResource} objects which will be directly published
411     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct
412     *                      published resources should also get published.
413     * @param publishSubResources indicates if sub-resources in folders should be published (for direct publish only)
414     *
415     * @return a publish list
416     *
417     * @throws CmsException if something goes wrong
418     */
419    public CmsPublishList getPublishList(
420        CmsObject cms,
421        List<CmsResource> directPublishResources,
422        boolean directPublishSiblings,
423        boolean publishSubResources)
424    throws CmsException {
425
426        return m_securityManager.fillPublishList(
427            cms.getRequestContext(),
428            new CmsPublishList(directPublishResources, directPublishSiblings, publishSubResources));
429    }
430
431    /**
432     * Returns a publish list with all the given resources, filtered only by state.<p>
433     *
434     * @param cms the cms request context
435     * @param directPublishResources the {@link CmsResource} objects which will be directly published
436     * @param directPublishSiblings <code>true</code>, if all eventual siblings of the direct
437     *                      published resources should also get published
438     * @param isUserPublishList if true, the publish list consists of resources directly selected by the user to publish
439     *
440     * @return a publish list
441     *
442     * @throws CmsException if something goes wrong
443     */
444    public CmsPublishList getPublishListAll(
445        CmsObject cms,
446        List<CmsResource> directPublishResources,
447        boolean directPublishSiblings,
448        boolean isUserPublishList)
449    throws CmsException {
450
451        CmsPublishList pubList = new CmsPublishList(true, directPublishResources, directPublishSiblings);
452        pubList.setUserPublishList(isUserPublishList);
453
454        return m_securityManager.fillPublishList(cms.getRequestContext(), pubList);
455    }
456
457    /**
458     * Gets the publish list remove mode.<p>
459     *
460     * @return the publish list remove mode
461     */
462    public CmsPublishManager.PublishListRemoveMode getPublishListRemoveMode() {
463
464        return m_publishListRemoveMode;
465    }
466
467    /**
468     * Gets the publish job verifier.<p>
469     *
470     * @return the publish job verifier
471     */
472    public CmsPublishListVerifier getPublishListVerifier() {
473
474        return m_publishListVerifier;
475    }
476
477    /**
478     * Returns the queue with still waiting publish jobs.<p>
479     *
480     * @return a list of {@link CmsPublishJobEnqueued} objects
481     */
482    public List<CmsPublishJobEnqueued> getPublishQueue() {
483
484        return m_publishEngine.getPublishQueue().asList();
485    }
486
487    /**
488     * Returns the amount of time in seconds the system will wait during shutdown for a running publish job.<p>
489     *
490     * @return the shutdown time for a running publish job
491     */
492    public int getPublishQueueShutdowntime() {
493
494        return m_publishQueueShutdowntime;
495    }
496
497    /**
498     * Returns a new publish list that contains the unpublished resources related
499     * to all resources in the given publish list, the related resources exclude
500     * all resources in the given publish list and also locked (by other users) resources.<p>
501     *
502     * @param cms the cms request context
503     * @param publishList the publish list to exclude from result
504     *
505     * @return a new publish list that contains the related resources
506     *
507     * @throws CmsException if something goes wrong
508     */
509    public CmsPublishList getRelatedResourcesToPublish(CmsObject cms, CmsPublishList publishList) throws CmsException {
510
511        return m_securityManager.getRelatedResourcesToPublish(
512            cms.getRequestContext(),
513            publishList,
514            CmsRelationFilter.TARGETS.filterStrong());
515    }
516
517    /**
518     * Returns the content of the publish report assigned to the given publish job.<p>
519     *
520     * @param publishJob the published job
521     * @return the content of the assigned publish report
522     *
523     * @throws CmsException if something goes wrong
524     */
525    public byte[] getReportContents(CmsPublishJobFinished publishJob) throws CmsException {
526
527        return m_publishEngine.getReportContents(publishJob);
528    }
529
530    /**
531     * Returns the current user's publish list.<p>
532     *
533     * @param cms the current cms context
534     *
535     * @return the current user's publish list
536     *
537     * @throws CmsException if something goes wrong
538     */
539    public List<CmsResource> getUsersPubList(CmsObject cms) throws CmsException {
540
541        return m_securityManager.getUsersPubList(cms.getRequestContext());
542    }
543
544    /**
545     * Initializes the publish manager and the publish engine finally.<p>
546     *
547     * @param cms an admin cms object
548     *
549     * @throws CmsException if something goes wrong
550     */
551    public void initialize(CmsObject cms) throws CmsException {
552
553        m_publishEngine.initialize(cms, m_publishQueuePersistance, m_publishQueueShutdowntime);
554        // Ensure publish history gets written to DB regularly,
555        OpenCms.getExecutor().scheduleWithFixedDelay(new Runnable() {
556
557            @SuppressWarnings("synthetic-access")
558            public void run() {
559
560                try {
561                    m_securityManager.updateLog();
562                } catch (Exception e) {
563                    LOG.error(e.getLocalizedMessage(), e);
564                }
565            }
566        }, 5, 5, TimeUnit.SECONDS);
567        m_frozen = true;
568    }
569
570    /**
571     * Returns true if publish history entries should be automatically removed when the corresponding publish job is removed.
572     *
573     * @return true if publish history entries should autoamtically be removed
574     */
575    public boolean isAutoCleanupHistoryEntries() {
576
577        return m_autoCleanupHistoryEntries;
578    }
579
580    /**
581     * Returns if the publish queue is persisted an will be re-initialized on startup.<p>
582     *
583     * @return <code>true</code> if the publish queue is persisted
584     */
585    public boolean isPublishQueuePersistanceEnabled() {
586
587        return m_publishQueuePersistance;
588    }
589
590    /**
591     * Returns the working state, that is if no publish job
592     * is waiting to be processed and there is no current running
593     * publish job.<p>
594     *
595     * @return the working state
596     */
597    public boolean isRunning() {
598
599        return m_publishEngine.isRunning();
600    }
601
602    /**
603     * Returns a new publish list that contains all resources of both given publish lists.<p>
604     *
605     * @param cms the cms request context
606     * @param pubList1 the first publish list
607     * @param pubList2 the second publish list
608     *
609     * @return a new publish list that contains all resources of both given publish lists
610     *
611     * @throws CmsException if something goes wrong
612     */
613    public CmsPublishList mergePublishLists(CmsObject cms, CmsPublishList pubList1, CmsPublishList pubList2)
614    throws CmsException {
615
616        return m_securityManager.mergePublishLists(cms.getRequestContext(), pubList1, pubList2);
617    }
618
619    /**
620     * Publishes the current project, printing messages to a shell report.<p>
621     *
622     * @param cms the cms request context
623     * @return the publish history id of the published project
624     *
625     * @throws Exception if something goes wrong
626     *
627     * @see CmsShellReport
628     */
629    public CmsUUID publishProject(CmsObject cms) throws Exception {
630
631        return publishProject(cms, new CmsShellReport(cms.getRequestContext().getLocale()));
632    }
633
634    /**
635     * Publishes the current project.<p>
636     *
637     * @param cms the cms request context
638     * @param report an instance of <code>{@link I_CmsReport}</code> to print messages
639     *
640     * @return the publish history id of the published project
641     *
642     * @throws CmsException if something goes wrong
643     */
644    public CmsUUID publishProject(CmsObject cms, I_CmsReport report) throws CmsException {
645
646        return publishProject(cms, report, getPublishList(cms));
647    }
648
649    /**
650     * Publishes the resources of a specified publish list.<p>
651     *
652     * @param cms the cms request context
653     * @param report an instance of <code>{@link I_CmsReport}</code> to print messages
654     * @param publishList a publish list
655     *
656     * @return the publish history id of the published project
657     *
658     * @throws CmsException if something goes wrong
659     *
660     * @see #getPublishList(CmsObject)
661     * @see #getPublishList(CmsObject, CmsResource, boolean)
662     * @see #getPublishList(CmsObject, List, boolean)
663     */
664    public CmsUUID publishProject(CmsObject cms, I_CmsReport report, CmsPublishList publishList) throws CmsException {
665
666        return m_securityManager.publishProject(cms, publishList, report);
667    }
668
669    /**
670     * Direct publishes a specified resource.<p>
671     *
672     * @param cms the cms request context
673     * @param report an instance of <code>{@link I_CmsReport}</code> to print messages
674     * @param directPublishResource a <code>{@link CmsResource}</code> that gets directly published;
675     *                          or <code>null</code> if an entire project gets published.
676     * @param directPublishSiblings if a <code>{@link CmsResource}</code> that should get published directly is
677     *                          provided as an argument, all eventual siblings of this resource
678     *                          get publish too, if this flag is <code>true</code>.
679     *
680     * @return the publish history id of the published project
681     *
682     * @throws CmsException if something goes wrong
683     */
684    public CmsUUID publishProject(
685        CmsObject cms,
686        I_CmsReport report,
687        CmsResource directPublishResource,
688        boolean directPublishSiblings)
689    throws CmsException {
690
691        return publishProject(cms, report, getPublishList(cms, directPublishResource, directPublishSiblings));
692    }
693
694    /**
695     * Publishes a single resource, printing messages to a shell report.<p>
696     *
697     * The siblings of the resource will not be published.<p>
698     *
699     * @param cms the cms request context
700     * @param resourcename the name of the resource to be published
701     *
702     * @return the publish history id of the published project
703     *
704     * @throws Exception if something goes wrong
705     *
706     * @see CmsShellReport
707     */
708    public CmsUUID publishResource(CmsObject cms, String resourcename) throws Exception {
709
710        return publishResource(cms, resourcename, false, new CmsShellReport(cms.getRequestContext().getLocale()));
711    }
712
713    /**
714     * Publishes a single resource.<p>
715     *
716     * @param cms the cms request context
717     * @param resourcename the name of the resource to be published
718     * @param publishSiblings if <code>true</code>, all siblings of the resource are also published
719     * @param report the report to write the progress information to
720     *
721     * @return the publish history id of the published project
722     *
723     * @throws Exception if something goes wrong
724     */
725    public CmsUUID publishResource(CmsObject cms, String resourcename, boolean publishSiblings, I_CmsReport report)
726    throws Exception {
727
728        CmsResource resource = cms.readResource(resourcename, CmsResourceFilter.ALL);
729        return publishProject(cms, report, resource, publishSiblings);
730    }
731
732    /**
733     * Removes the given publish listener.<p>
734     *
735     * @param listener the publish listener to remove
736     */
737    public void removePublishListener(I_CmsPublishEventListener listener) {
738
739        m_publishEngine.removePublishListener(listener);
740    }
741
742    /**
743     * Removes the given resource to the given user's publish list.<p>
744     *
745     * @param cms the current cms context
746     * @param structureIds the collection of structure IDs to remove
747     *
748     * @throws CmsException if something goes wrong
749     */
750    public void removeResourceFromUsersPubList(CmsObject cms, Collection<CmsUUID> structureIds) throws CmsException {
751
752        m_securityManager.removeResourceFromUsersPubList(cms.getRequestContext(), structureIds);
753    }
754
755    /**
756     * Configures automatic removal of publish history entries.
757     *
758     * @param val the string value from the configuration
759     */
760    public void setAutoCleanupHistoryEntries(String val) {
761
762        if (m_frozen) {
763            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
764        }
765        m_autoCleanupHistoryEntries = Boolean.parseBoolean(val);
766
767    }
768
769    /**
770     * Sets the publish engine during initialization.<p>
771     *
772     * @param publishEngine the publish engine instance
773     */
774    public void setPublishEngine(CmsPublishEngine publishEngine) {
775
776        if (m_frozen) {
777            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
778        }
779        m_publishEngine = publishEngine;
780    }
781
782    /**
783     * Sets the publish History Size.<p>
784     *
785     * @param publishHistorySize the publish History Size to set
786     */
787    public void setPublishHistorySize(String publishHistorySize) {
788
789        if (m_frozen) {
790            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
791        }
792        m_publishHistorySize = Integer.parseInt(publishHistorySize);
793    }
794
795    /**
796     * Sets the publish list remove mode.<p>
797     *
798     * @param publishListRemoveMode the publish list remove mode
799     */
800    public void setPublishListRemoveMode(CmsPublishManager.PublishListRemoveMode publishListRemoveMode) {
801
802        if (m_frozen) {
803            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
804        }
805        m_publishListRemoveMode = publishListRemoveMode;
806    }
807
808    /**
809     * Sets if the publish queue is re-initialized on startup.<p>
810     *
811     * @param publishQueuePersistance the persistence flag, parsed as <code>boolean</code>
812     */
813    public void setPublishQueuePersistance(String publishQueuePersistance) {
814
815        if (m_frozen) {
816            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
817        }
818        m_publishQueuePersistance = Boolean.valueOf(publishQueuePersistance).booleanValue();
819    }
820
821    /**
822     * Sets the publish queue shutdown time.
823     *
824     * @param publishQueueShutdowntime the shutdown time to set, parsed as <code>int</code>
825     */
826    public void setPublishQueueShutdowntime(String publishQueueShutdowntime) {
827
828        if (m_frozen) {
829            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
830        }
831        m_publishQueueShutdowntime = Integer.parseInt(publishQueueShutdowntime);
832    }
833
834    /**
835     * Sets the security manager during initialization.<p>
836     *
837     * @param securityManager the security manager
838     */
839    public void setSecurityManager(CmsSecurityManager securityManager) {
840
841        if (m_frozen) {
842            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_CONFIG_FROZEN_0));
843        }
844        m_securityManager = securityManager;
845    }
846
847    /**
848     * Starts publishing of enqueued publish jobs.<p>
849     */
850    public void startPublishing() {
851
852        m_publishEngine.startEngine();
853    }
854
855    /**
856     * Stops the publishing of enqueued publish jobs.<p>
857     */
858    public void stopPublishing() {
859
860        m_publishEngine.stopEngine();
861    }
862
863    /**
864     * Validates the relations for the given resources.<p>
865     *
866     * @param cms the cms request context
867     * @param publishList the publish list to validate against the online project
868     * @param report a report to write the messages to
869     *
870     * @return a map with lists of invalid links
871     *          (<code>{@link org.opencms.relations.CmsRelation}}</code> objects)
872     *          keyed by root paths
873     *
874     * TODO: change return value to List of CmsRelation
875     *
876     * @throws Exception if something goes wrong
877     */
878    public Map<String, List<CmsRelation>> validateRelations(
879        CmsObject cms,
880        CmsPublishList publishList,
881        I_CmsReport report)
882    throws Exception {
883
884        return m_securityManager.validateRelations(cms.getRequestContext(), publishList, report);
885    }
886
887    /**
888     * Waits until no publish jobs remain.<p>
889     */
890    public void waitWhileRunning() {
891
892        waitWhileRunning(Long.MAX_VALUE);
893    }
894
895    /**
896     * Waits until no publish jobs remain or the given max milliseconds.<p>
897     *
898     * @param ms the max milliseconds to wait
899     */
900    public void waitWhileRunning(long ms) {
901
902        int i = 0;
903        // wait until it is done or time is over
904        synchronized (this) {
905            try {
906                Thread.sleep(100); // wait a bit to give the publish engine the chance to actualize the state
907            } catch (InterruptedException e) {
908                // ignore
909                e.printStackTrace();
910            }
911            while (isRunning() && ((MS_ONE_SECOND * i) <= ms)) {
912                try {
913                    Thread.sleep(MS_ONE_SECOND); // wait a second
914                } catch (InterruptedException e) {
915                    // ignore
916                    e.printStackTrace();
917                }
918                i++;
919            }
920        }
921    }
922
923    /**
924     * Returns the currently used publish engine.<p>
925     *
926     * @return the publish engine
927     */
928    protected CmsPublishEngine getEngine() {
929
930        return m_publishEngine;
931    }
932}