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, 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.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 m_port = port; 067 } 068 069 /** 070 * Initializes the remote shell server.<p> 071 * 072 * @param systemConfiguration the OpenCms system configuration read from opencms-system.xml 073 * 074 * @return the initialized remote shell server, or <code>null</code> if the server is disabled 075 */ 076 public static CmsRemoteShellServer initialize(CmsSystemConfiguration systemConfiguration) { 077 078 CmsRemoteShellServer result = null; 079 080 if ((systemConfiguration.getShellServerOptions() != null) 081 && systemConfiguration.getShellServerOptions().isEnabled()) { 082 083 result = new CmsRemoteShellServer(systemConfiguration.getShellServerOptions().getPort()); 084 085 if (CmsLog.INIT.isInfoEnabled()) { 086 CmsLog.INIT.info( 087 Messages.get().getBundle().key(Messages.INIT_REMOTESHELL_ENABLED_1, Integer.valueOf(result.m_port))); 088 } 089 090 result.initServer(); 091 } else { 092 if (CmsLog.INIT.isInfoEnabled()) { 093 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_REMOTESHELL_DISABLED_0)); 094 } 095 } 096 097 return result; 098 } 099 100 /** 101 * Initializes the RMI registry and exports the remote shell provider to it.<p> 102 */ 103 public void initServer() { 104 105 if (m_initialized) { 106 return; 107 } 108 try { 109 m_registry = LocateRegistry.createRegistry(m_port); 110 m_provider = new CmsRemoteShellProvider(m_port); 111 I_CmsRemoteShellProvider providerStub = (I_CmsRemoteShellProvider)(UnicastRemoteObject.exportObject( 112 m_provider, 113 m_port)); 114 m_registry.bind(CmsRemoteShellConstants.PROVIDER, providerStub); 115 m_initialized = true; 116 } catch (Exception e) { 117 LOG.error(e.getLocalizedMessage(), e); 118 } 119 } 120 121 /** 122 * Unregisters remote objects.<p> 123 * 124 * @throws Exception in case shutting down the RMI threads failed 125 */ 126 public void shutDown() throws Exception { 127 128 if (CmsLog.INIT.isInfoEnabled()) { 129 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_1, this.getClass().getName())); 130 } 131 for (String boundName : m_registry.list()) { 132 m_registry.unbind(boundName); 133 } 134 UnicastRemoteObject.unexportObject(m_registry, true); 135 UnicastRemoteObject.unexportObject(m_provider, true); 136 CmsRemoteShell.unregisterAll(); 137 } 138}