001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.rmi;
029
030import org.opencms.configuration.CmsSystemConfiguration;
031import org.opencms.main.CmsLog;
032import org.opencms.main.Messages;
033
034import java.rmi.registry.LocateRegistry;
035import java.rmi.registry.Registry;
036import java.rmi.server.UnicastRemoteObject;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * This class is used to initialize the RMI mechanism and export the object used to access the remote shell.<p>
042 */
043public class CmsRemoteShellServer {
044
045    /** The log instance for this class. */
046    private static final Log LOG = CmsLog.getLog(CmsRemoteShellServer.class);
047
048    /** Indicates whether the remote shell server has been initialized. */
049    private static boolean m_initialized;
050
051    /** The port for the RMI registry. */
052    private int m_port = CmsRemoteShellConstants.DEFAULT_PORT;
053
054    /** The real instance of the shell provider. */
055    private I_CmsRemoteShellProvider m_provider;
056
057    /** The RMI registry. */
058    private Registry m_registry;
059
060    /**
061     * Creates a new instance.<p>
062     *
063     * @param port the port for the RMI registry
064     */
065    public CmsRemoteShellServer(int port) {
066
067        m_port = port;
068    }
069
070    /**
071     * Initializes the remote shell server.<p>
072     *
073     * @param systemConfiguration the OpenCms system configuration read from opencms-system.xml
074     *
075     * @return the initialized remote shell server, or <code>null</code> if the server is disabled
076     */
077    public static CmsRemoteShellServer initialize(CmsSystemConfiguration systemConfiguration) {
078
079        CmsRemoteShellServer result = null;
080
081        if ((systemConfiguration.getShellServerOptions() != null)
082            && systemConfiguration.getShellServerOptions().isEnabled()) {
083
084            result = new CmsRemoteShellServer(systemConfiguration.getShellServerOptions().getPort());
085
086            if (CmsLog.INIT.isInfoEnabled()) {
087                CmsLog.INIT.info(
088                    Messages.get().getBundle().key(
089                        Messages.INIT_REMOTESHELL_ENABLED_1,
090                        Integer.valueOf(result.m_port)));
091            }
092
093            result.initServer();
094        } else {
095            if (CmsLog.INIT.isInfoEnabled()) {
096                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_REMOTESHELL_DISABLED_0));
097            }
098        }
099
100        return result;
101    }
102
103    /**
104     * Initializes the RMI registry and exports the remote shell provider to it.<p>
105     */
106    public void initServer() {
107
108        if (m_initialized) {
109            return;
110        }
111        try {
112            m_registry = LocateRegistry.createRegistry(m_port);
113            m_provider = new CmsRemoteShellProvider(m_port);
114            I_CmsRemoteShellProvider providerStub = (I_CmsRemoteShellProvider)(UnicastRemoteObject.exportObject(
115                m_provider,
116                m_port));
117            m_registry.bind(CmsRemoteShellConstants.PROVIDER, providerStub);
118            m_initialized = true;
119        } catch (Exception e) {
120            LOG.error(e.getLocalizedMessage(), e);
121        }
122    }
123
124    /**
125     * Unregisters remote objects.<p>
126     *
127     * @throws Exception in case shutting down the RMI threads failed
128     */
129    public void shutDown() throws Exception {
130
131        if (CmsLog.INIT.isInfoEnabled()) {
132            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_1, this.getClass().getName()));
133        }
134        for (String boundName : m_registry.list()) {
135            m_registry.unbind(boundName);
136        }
137        UnicastRemoteObject.unexportObject(m_registry, true);
138        UnicastRemoteObject.unexportObject(m_provider, true);
139        CmsRemoteShell.unregisterAll();
140    }
141}