001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.studio.apacheds.actions;
021    
022    
023    import java.io.IOException;
024    
025    import org.apache.directory.studio.apacheds.ApacheDsPlugin;
026    import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
027    import org.apache.directory.studio.apacheds.ApacheDsPluginUtils;
028    import org.apache.directory.studio.apacheds.ConsolesHandler;
029    import org.apache.directory.studio.apacheds.LogMessageConsole;
030    import org.apache.directory.studio.apacheds.jobs.LaunchServerJob;
031    import org.apache.directory.studio.apacheds.model.Server;
032    import org.apache.directory.studio.apacheds.model.ServerStateEnum;
033    import org.apache.directory.studio.apacheds.views.ServersView;
034    import org.eclipse.debug.core.DebugException;
035    import org.eclipse.debug.core.ILaunch;
036    import org.eclipse.jface.action.Action;
037    import org.eclipse.jface.action.IAction;
038    import org.eclipse.jface.viewers.ISelection;
039    import org.eclipse.jface.viewers.StructuredSelection;
040    import org.eclipse.swt.widgets.Display;
041    import org.eclipse.ui.IWorkbenchWindow;
042    import org.eclipse.ui.IWorkbenchWindowActionDelegate;
043    
044    
045    /**
046     * This class implements the stop action for a server.
047     *
048     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
049     * @version $Rev$, $Date$
050     */
051    public class StopAction extends Action implements IWorkbenchWindowActionDelegate
052    {
053        /** The associated view */
054        private ServersView view;
055    
056        /** The server */
057        private Server server;
058    
059    
060        /**
061         * Creates a new instance of StopAction.
062         */
063        public StopAction()
064        {
065            super( Messages.getString( "StopAction.Stop" ) ); //$NON-NLS-1$
066            init();
067        }
068    
069    
070        /**
071         * Creates a new instance of StopAction.
072         *
073         * @param view
074         *      the associated view
075         */
076        public StopAction( ServersView view )
077        {
078            super( Messages.getString( "StopAction.Stop" ) ); //$NON-NLS-1$
079            this.view = view;
080            init();
081        }
082    
083    
084        /**
085         * Initializes the action.
086         */
087        private void init()
088        {
089            setId( ApacheDsPluginConstants.CMD_STOP );
090            setActionDefinitionId( ApacheDsPluginConstants.CMD_STOP );
091            setToolTipText( Messages.getString( "StopAction.StopToolTip" ) ); //$NON-NLS-1$
092            setImageDescriptor( ApacheDsPlugin.getDefault().getImageDescriptor( ApacheDsPluginConstants.IMG_STOP ) );
093        }
094    
095    
096        /* (non-Javadoc)
097         * @see org.eclipse.jface.action.Action#run()
098         */
099        public void run()
100        {
101            if ( view != null )
102            {
103                // Getting the selection
104                StructuredSelection selection = ( StructuredSelection ) view.getViewer().getSelection();
105                if ( ( !selection.isEmpty() ) && ( selection.size() == 1 ) )
106                {
107                    // Getting the server
108                    server = ( Server ) selection.getFirstElement();
109    
110                    // Setting the server of the server to 'stopping'
111                    server.setState( ServerStateEnum.STOPPING );
112    
113                    // Getting the launch job
114                    LaunchServerJob launchJob = server.getLaunchJob();
115                    if ( launchJob != null )
116                    {
117                        // Getting the launch
118                        ILaunch launch = launchJob.getLaunch();
119                        if ( ( launch != null ) && ( !launch.isTerminated() ) )
120                        {
121                            // Terminating the launch
122                            try
123                            {
124                                launch.terminate();
125                                writeToInfoConsoleMessageStream( Messages.getString( "StopAction.ServerStopped" ) ); //$NON-NLS-1$
126                            }
127                            catch ( DebugException e )
128                            {
129                                ApacheDsPluginUtils.reportError( Messages.getString( "StopAction.ErrorWhenStopping" ) //$NON-NLS-1$
130                                    + e.getMessage() );
131                            }
132                        }
133                    }
134                }
135            }
136        }
137    
138    
139        /**
140         * Writes the given message to the Info console message stream.
141         *
142         * @param message
143         *      the message
144         */
145        private void writeToInfoConsoleMessageStream( final String message )
146        {
147            Display.getDefault().asyncExec( new Runnable()
148            {
149                public void run()
150                {
151                    LogMessageConsole console = ConsolesHandler.getDefault().getLogMessageConsole( server.getId() );
152                    try
153                    {
154                        console.getInfoConsoleMessageStream().write( message );
155                    }
156                    catch ( IOException e )
157                    {
158                        ApacheDsPluginUtils.reportError( Messages.getString( "StopAction.ErrorWhenWriting" ) //$NON-NLS-1$
159                            + e.getMessage() );
160                    }
161                }
162            } );
163        }
164    
165    
166        /* (non-Javadoc)
167         * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
168         */
169        public void run( IAction action )
170        {
171            run();
172        }
173    
174    
175        /* (non-Javadoc)
176         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
177         */
178        public void dispose()
179        {
180            // Nothing to do
181        }
182    
183    
184        /* (non-Javadoc)
185         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
186         */
187        public void init( IWorkbenchWindow window )
188        {
189            // Nothing to do
190        }
191    
192    
193        /* (non-Javadoc)
194         * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
195         */
196        public void selectionChanged( IAction action, ISelection selection )
197        {
198            // Nothing to do
199        }
200    }