001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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, 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.jlan;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032
033import java.io.File;
034
035import org.apache.commons.logging.Log;
036
037import org.alfresco.jlan.app.JLANServer;
038
039/**
040 * A simple class used to start and stop JLAN.<p>
041 *
042 * Since the JLAN server requires its own thread and cannot be run in the same thread as the startup,
043 * this creates a new thread solely for starting JLAN.<p>
044 */
045public class CmsJlanThreadManager {
046
047    /**
048     * The thread for starting the JLAN server.<p>
049     */
050    protected class JlanThread extends Thread {
051
052        /** Path of the jlan config file. */
053        private String m_configPath;
054
055        /**
056         * Constructor.<p>
057         *
058         * @param configPath the path of the JLAN config file
059         */
060        public JlanThread(String configPath) {
061
062            m_configPath = configPath;
063        }
064
065        /**
066         * @see java.lang.Thread#run()
067         */
068        @Override
069        public void run() {
070
071            CmsJlanServer server = new CmsJlanServer();
072
073            // we don't want to interactively shut down the server!
074            JLANServer.setAllowConsoleShutdown(false);
075            server.start(new String[] {m_configPath});
076
077        }
078    }
079
080    /** The logger for this class. */
081    private static final Log LOG = CmsLog.getLog(CmsJlanThreadManager.class);
082
083    /** The maximum amount of time OpenCms should wait during shutdown after trying to stop the JLAN server. */
084    private static final int MAX_SHUTDOWN_WAIT_MILLIS = 30000;
085
086    /** The JLAN thread instance. */
087    private Thread m_thread;
088
089    /**
090     * Starts the JLAN server in a new thread.<p>
091     */
092    public synchronized void start() {
093
094        String path = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("config/jlanConfig.xml");
095        File configFile = new File(path);
096        if (configFile.exists()) {
097
098            if (m_thread == null) {
099                m_thread = new JlanThread(path);
100                m_thread.start();
101            }
102        } else {
103            String message = "Not starting JLAN server because no config file was found at " + path;
104            System.out.println(message);
105            LOG.warn(message);
106        }
107    }
108
109    /**
110     * Tries to stop the JLAN server and return after it is stopped, but will also return if the thread hasn't stopped after MAX_SHUTDOWN_WAIT_MILLIS.
111     */
112    public synchronized void stop() {
113
114        if (m_thread != null) {
115            long timeBeforeShutdownWasCalled = System.currentTimeMillis();
116            JLANServer.shutdownServer(new String[] {});
117            while (m_thread.isAlive()
118                && ((System.currentTimeMillis() - timeBeforeShutdownWasCalled) < MAX_SHUTDOWN_WAIT_MILLIS)) {
119                try {
120                    Thread.sleep(500);
121                } catch (InterruptedException e) {
122                    // ignore
123                }
124            }
125        }
126
127    }
128
129}