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.File;
024    
025    import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
026    import org.apache.directory.studio.apacheds.ApacheDsPluginUtils;
027    import org.apache.directory.studio.apacheds.dialogs.DeleteServerDialog;
028    import org.apache.directory.studio.apacheds.jobs.LaunchServerJob;
029    import org.apache.directory.studio.apacheds.model.Server;
030    import org.apache.directory.studio.apacheds.model.ServerStateEnum;
031    import org.apache.directory.studio.apacheds.model.ServersHandler;
032    import org.apache.directory.studio.apacheds.views.ServersView;
033    import org.eclipse.debug.core.DebugException;
034    import org.eclipse.debug.core.ILaunch;
035    import org.eclipse.jface.action.Action;
036    import org.eclipse.jface.action.IAction;
037    import org.eclipse.jface.viewers.ISelection;
038    import org.eclipse.jface.viewers.StructuredSelection;
039    import org.eclipse.ui.ISharedImages;
040    import org.eclipse.ui.IWorkbenchWindow;
041    import org.eclipse.ui.IWorkbenchWindowActionDelegate;
042    import org.eclipse.ui.PlatformUI;
043    
044    
045    /**
046     * This class implements the delete 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 DeleteAction extends Action implements IWorkbenchWindowActionDelegate
052    {
053        /** The associated view */
054        private ServersView view;
055    
056    
057        /**
058         * Creates a new instance of DeleteAction.
059         */
060        public DeleteAction()
061        {
062            super( Messages.getString( "DeleteAction.Delete" ) ); //$NON-NLS-1$
063            init();
064        }
065    
066    
067        /**
068         * Creates a new instance of DeleteAction.
069         * 
070         * @param view
071         *      the associated view
072         */
073        public DeleteAction( ServersView view )
074        {
075            super( Messages.getString( "DeleteAction.Delete" ) ); //$NON-NLS-1$
076            this.view = view;
077            init();
078        }
079    
080    
081        /**
082         * Initializes the action.
083         */
084        private void init()
085        {
086            setId( ApacheDsPluginConstants.CMD_DELETE );
087            setActionDefinitionId( ApacheDsPluginConstants.CMD_DELETE );
088            setToolTipText( Messages.getString( "DeleteAction.DeleteToolTip" ) ); //$NON-NLS-1$
089            setImageDescriptor( PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
090                ISharedImages.IMG_TOOL_DELETE ) );
091        }
092    
093    
094        /* (non-Javadoc)
095         * @see org.eclipse.jface.action.Action#run()
096         */
097        public void run()
098        {
099            if ( view != null )
100            {
101                // What we get from the TableViewer is a StructuredSelection
102                StructuredSelection selection = ( StructuredSelection ) view.getViewer().getSelection();
103    
104                // Here's the real object
105                Server server = ( Server ) selection.getFirstElement();
106    
107                // Asking for confirmation
108                DeleteServerDialog dsd = new DeleteServerDialog( view.getSite().getShell(), server );
109                if ( dsd.open() == DeleteServerDialog.OK )
110                {
111                    // Checking if the server is running
112                    // If yes, we need to shut it down before removing its data
113                    if ( server.getState() == ServerStateEnum.STARTED )
114                    {
115                        // Setting the server of the server to 'stopping'
116                        server.setState( ServerStateEnum.STOPPING );
117    
118                        // Getting the launch job
119                        LaunchServerJob launchJob = server.getLaunchJob();
120                        if ( launchJob != null )
121                        {
122                            // Getting the launch
123                            ILaunch launch = launchJob.getLaunch();
124                            if ( ( launch != null ) && ( !launch.isTerminated() ) )
125                            {
126                                // Terminating the launch
127                                try
128                                {
129                                    launch.terminate();
130                                }
131                                catch ( DebugException e )
132                                {
133                                    ApacheDsPluginUtils.reportError( Messages.getString( "DeleteAction.ErrorWhileStopping" ) //$NON-NLS-1$
134                                        + e.getMessage() );
135                                }
136                            }
137                        }
138                    }
139    
140                    // Removing the server
141                    ServersHandler.getDefault().removeServer( server );
142    
143                    // Deleting the associated directory on disk
144                    deleteDirectory( new File( ApacheDsPluginUtils.getApacheDsServersFolder().append( server.getId() )
145                        .toOSString() ) );
146                }
147            }
148        }
149    
150    
151        /**
152         * Deletes the given directory
153         *
154         * @param path
155         *      the directory
156         * @return
157         *      <code>true</code> if and only if the directory is 
158         *      successfully deleted; <code>false</code> otherwise
159         */
160        private boolean deleteDirectory( File path )
161        {
162            if ( path.exists() )
163            {
164                File[] files = path.listFiles();
165                for ( int i = 0; i < files.length; i++ )
166                {
167                    if ( files[i].isDirectory() )
168                    {
169                        deleteDirectory( files[i] );
170                    }
171                    else
172                    {
173                        files[i].delete();
174                    }
175                }
176            }
177            return ( path.delete() );
178        }
179    
180    
181        /* (non-Javadoc)
182         * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
183         */
184        public void run( IAction action )
185        {
186            run();
187        }
188    
189    
190        /* (non-Javadoc)
191         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
192         */
193        public void dispose()
194        {
195            // Nothing to do
196        }
197    
198    
199        /* (non-Javadoc)
200         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
201         */
202        public void init( IWorkbenchWindow window )
203        {
204            // Nothing to do
205        }
206    
207    
208        /* (non-Javadoc)
209         * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
210         */
211        public void selectionChanged( IAction action, ISelection selection )
212        {
213            // Nothing to do
214        }
215    }