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    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.apache.directory.studio.apacheds.ApacheDsPlugin;
028    import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
029    import org.apache.directory.studio.apacheds.ApacheDsPluginUtils;
030    import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
031    import org.apache.directory.studio.apacheds.configuration.model.ServerXmlIOException;
032    import org.apache.directory.studio.apacheds.configuration.model.v153.ServerConfigurationV153;
033    import org.apache.directory.studio.apacheds.configuration.model.v154.ServerConfigurationV154;
034    import org.apache.directory.studio.apacheds.configuration.model.v155.ServerConfigurationV155;
035    import org.apache.directory.studio.apacheds.jobs.LaunchServerJob;
036    import org.apache.directory.studio.apacheds.model.Server;
037    import org.apache.directory.studio.apacheds.views.ServersView;
038    import org.apache.mina.util.AvailablePortFinder;
039    import org.eclipse.jface.action.Action;
040    import org.eclipse.jface.action.IAction;
041    import org.eclipse.jface.dialogs.IDialogConstants;
042    import org.eclipse.jface.dialogs.MessageDialog;
043    import org.eclipse.jface.viewers.ISelection;
044    import org.eclipse.jface.viewers.StructuredSelection;
045    import org.eclipse.osgi.util.NLS;
046    import org.eclipse.ui.IWorkbenchWindow;
047    import org.eclipse.ui.IWorkbenchWindowActionDelegate;
048    
049    
050    /**
051     * This class implements the run action for a server.
052     *
053     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
054     * @version $Rev$, $Date$
055     */
056    public class RunAction extends Action implements IWorkbenchWindowActionDelegate
057    {
058        /** The associated view */
059        private ServersView view;
060    
061    
062        /**
063         * Creates a new instance of RunAction.
064         */
065        public RunAction()
066        {
067            super( Messages.getString( "RunAction.Run" ) ); //$NON-NLS-1$
068            init();
069        }
070    
071    
072        /**
073         * Creates a new instance of RunAction.
074         *
075         * @param view
076         *      the associated view
077         */
078        public RunAction( ServersView view )
079        {
080            super( Messages.getString( "RunAction.Run" ) ); //$NON-NLS-1$
081            this.view = view;
082            init();
083        }
084    
085    
086        /**
087         * Initializes the action.
088         */
089        private void init()
090        {
091            setId( ApacheDsPluginConstants.CMD_RUN );
092            setActionDefinitionId( ApacheDsPluginConstants.CMD_RUN );
093            setToolTipText( Messages.getString( "RunAction.RunToolTip" ) ); //$NON-NLS-1$
094            setImageDescriptor( ApacheDsPlugin.getDefault().getImageDescriptor( ApacheDsPluginConstants.IMG_RUN ) );
095        }
096    
097    
098        /* (non-Javadoc)
099         * @see org.eclipse.jface.action.Action#run()
100         */
101        public void run()
102        {
103            if ( view != null )
104            {
105                // Getting the selection
106                StructuredSelection selection = ( StructuredSelection ) view.getViewer().getSelection();
107                if ( ( !selection.isEmpty() ) && ( selection.size() == 1 ) )
108                {
109                    // Getting the server
110                    final Server server = ( Server ) selection.getFirstElement();
111    
112                    // Parsing the 'server.xml' file
113                    ServerConfiguration serverConfiguration = null;
114                    try
115                    {
116                        serverConfiguration = ApacheDsPluginUtils.getServerConfiguration( server );
117                    }
118                    catch ( IOException e )
119                    {
120                        reportErrorReadingServerConfiguration( e.getMessage() );
121                        return;
122                    }
123                    catch ( ServerXmlIOException e )
124                    {
125                        reportErrorReadingServerConfiguration( e.getMessage() );
126                        return;
127                    }
128    
129                    // Checking if we could read the 'server.xml' file
130                    if ( serverConfiguration == null )
131                    {
132                        reportErrorReadingServerConfiguration( null );
133                        return;
134                    }
135    
136                    // Verifying if the protocol ports are currently available
137                    String[] alreadyInUseProtocolPortsList = getAlreadyInUseProtocolPorts( serverConfiguration );
138                    if ( ( alreadyInUseProtocolPortsList != null ) && ( alreadyInUseProtocolPortsList.length > 0 ) )
139                    {
140                        String title = null;
141                        String message = null;
142    
143                        if ( alreadyInUseProtocolPortsList.length == 1 )
144                        {
145                            title = Messages.getString( "RunAction.PortInUse" ); //$NON-NLS-1$
146                            message = NLS
147                                .bind(
148                                    Messages.getString( "RunAction.PortOfProtocolInUse" ), new String[] { alreadyInUseProtocolPortsList[0] } ); //$NON-NLS-1$
149                        }
150                        else
151                        {
152                            title = Messages.getString( "RunAction.PortsInUse" ); //$NON-NLS-1$
153                            message = Messages.getString( "RunAction.PortsOfProtocolsInUse" ); //$NON-NLS-1$
154                            for ( String alreadyInUseProtocolPort : alreadyInUseProtocolPortsList )
155                            {
156                                message += ApacheDsPluginUtils.LINE_SEPARATOR + "    - " + alreadyInUseProtocolPort; //$NON-NLS-1$
157                            }
158                        }
159    
160                        message += ApacheDsPluginUtils.LINE_SEPARATOR + ApacheDsPluginUtils.LINE_SEPARATOR
161                            + Messages.getString( "RunAction.Continue" ); //$NON-NLS-1$
162    
163                        MessageDialog dialog = new MessageDialog( view.getSite().getShell(), title, null, message,
164                            MessageDialog.WARNING, new String[]
165                                { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }, MessageDialog.OK );
166                        if ( dialog.open() == MessageDialog.CANCEL )
167                        {
168                            return;
169                        }
170                    }
171    
172                    // Verifying the libraries in the plugin's folder
173                    ApacheDsPluginUtils.verifyLibrariesFolder( server );
174    
175                    // Creating, setting and launching the launch job
176                    LaunchServerJob job = new LaunchServerJob( server, serverConfiguration );
177                    job.setLogsLevel( ApacheDsPluginUtils.getServerLogsLevel() );
178                    job.setLogsPattern( ApacheDsPluginUtils.getServerLogsPattern() );
179                    server.setLaunchJob( job );
180                    job.schedule();
181                }
182            }
183        }
184    
185    
186        /**
187         * Reports to the user an error message indicating the server 
188         * configuration could not be read correctly.
189         *
190         * @param errorMessage
191         *      an error message which can be <code>null</code>
192         */
193        private void reportErrorReadingServerConfiguration( String errorMessage )
194        {
195            String message = null;
196    
197            if ( errorMessage == null )
198            {
199                message = Messages.getString( "RunAction.UnableReadServerConfiguration" ); //$NON-NLS-1$
200            }
201            else
202            {
203                message = Messages.getString( "RunAction.UnableReadServerConfiguration" ) + ApacheDsPluginUtils.LINE_SEPARATOR //$NON-NLS-1$
204                    + ApacheDsPluginUtils.LINE_SEPARATOR
205                    + Messages.getString( "RunAction.FollowingErrorOccurred" ) + errorMessage; //$NON-NLS-1$
206            }
207    
208            MessageDialog dialog = new MessageDialog( view.getSite().getShell(), Messages
209                .getString( "RunAction.UnableReadServerConfiguration" ), //$NON-NLS-1$
210                null, message, MessageDialog.ERROR, new String[]
211                    { IDialogConstants.OK_LABEL }, MessageDialog.OK );
212            dialog.open();
213        }
214    
215    
216        /**
217         * Gets an array of String containing the ports and their associated 
218         * protocols which are already in use.
219         *
220         * @param serverConfiguration
221         *      the server configuration
222         * @return
223         *      an array of String containing the ports and their associated 
224         * protocols which are already in use.
225         */
226        private String[] getAlreadyInUseProtocolPorts( ServerConfiguration serverConfiguration )
227        {
228            // Version 1.5.5
229            if ( serverConfiguration instanceof ServerConfigurationV155 )
230            {
231                return getAlreadyInUseProtocolPortsVersion155( ( ServerConfigurationV155 ) serverConfiguration );
232            }
233            // Version 1.5.4
234            else if ( serverConfiguration instanceof ServerConfigurationV154 )
235            {
236                return getAlreadyInUseProtocolPortsVersion154( ( ServerConfigurationV154 ) serverConfiguration );
237            }
238            // Version 1.5.3
239            else if ( serverConfiguration instanceof ServerConfigurationV153 )
240            {
241                return getAlreadyInUseProtocolPortsVersion153( ( ServerConfigurationV153 ) serverConfiguration );
242            }
243            else
244            {
245                return new String[0];
246            }
247        }
248    
249    
250        /**
251         * Gets an array of String containing the ports and their associated 
252         * protocols which are already in use.
253         *
254         * @param serverConfiguration
255         *      the 1.5.3 server configuration
256         * @return
257         *      an array of String containing the ports and their associated 
258         * protocols which are already in use.
259         */
260        private String[] getAlreadyInUseProtocolPortsVersion153( ServerConfigurationV153 serverConfiguration )
261        {
262            List<String> alreadyInUseProtocolPortsList = new ArrayList<String>();
263    
264            // LDAP
265            if ( serverConfiguration.isEnableLdap() )
266            {
267                if ( !AvailablePortFinder.available( serverConfiguration.getLdapPort() ) )
268                {
269                    alreadyInUseProtocolPortsList.add( NLS.bind(
270                        Messages.getString( "RunAction.LDAPPort" ), new Object[] { serverConfiguration.getLdapPort() } ) ); //$NON-NLS-1$
271                }
272            }
273    
274            // LDAPS
275            if ( serverConfiguration.isEnableLdaps() )
276            {
277                if ( !AvailablePortFinder.available( serverConfiguration.getLdapsPort() ) )
278                {
279                    alreadyInUseProtocolPortsList.add( NLS.bind(
280                        Messages.getString( "RunAction.LDAPSPort" ), new Object[] { serverConfiguration.getLdapsPort() } ) ); //$NON-NLS-1$
281                }
282            }
283    
284            // Kerberos
285            if ( serverConfiguration.isEnableKerberos() )
286            {
287                if ( !AvailablePortFinder.available( serverConfiguration.getKerberosPort() ) )
288                {
289                    alreadyInUseProtocolPortsList
290                        .add( NLS
291                            .bind(
292                                Messages.getString( "RunAction.KerberosPort" ), new Object[] { serverConfiguration.getKerberosPort() } ) ); //$NON-NLS-1$
293                }
294            }
295    
296            // DNS
297            if ( serverConfiguration.isEnableDns() )
298            {
299                if ( !AvailablePortFinder.available( serverConfiguration.getDnsPort() ) )
300                {
301                    alreadyInUseProtocolPortsList.add( NLS.bind(
302                        Messages.getString( "RunAction.DNSPort" ), new Object[] { serverConfiguration.getDnsPort() } ) ); //$NON-NLS-1$
303                }
304            }
305    
306            // NTP
307            if ( serverConfiguration.isEnableNtp() )
308            {
309                if ( !AvailablePortFinder.available( serverConfiguration.getNtpPort() ) )
310                {
311                    alreadyInUseProtocolPortsList.add( NLS.bind(
312                        Messages.getString( "RunAction.NTPPort" ), new Object[] { serverConfiguration.getNtpPort() } ) ); //$NON-NLS-1$
313                }
314            }
315    
316            // Change Password
317            if ( serverConfiguration.isEnableChangePassword() )
318            {
319                if ( !AvailablePortFinder.available( serverConfiguration.getChangePasswordPort() ) )
320                {
321                    alreadyInUseProtocolPortsList
322                        .add( NLS
323                            .bind(
324                                Messages.getString( "RunAction.ChangePasswordPort" ), new Object[] { serverConfiguration.getChangePasswordPort() } ) ); //$NON-NLS-1$
325                }
326            }
327    
328            return alreadyInUseProtocolPortsList.toArray( new String[0] );
329        }
330    
331    
332        /**
333         * Gets an array of String containing the ports and their associated 
334         * protocols which are already in use.
335         *
336         * @param serverConfiguration
337         *      the 1.5.4 server configuration
338         * @return
339         *      an array of String containing the ports and their associated 
340         * protocols which are already in use.
341         */
342        private String[] getAlreadyInUseProtocolPortsVersion154( ServerConfigurationV154 serverConfiguration )
343        {
344            List<String> alreadyInUseProtocolPortsList = new ArrayList<String>();
345    
346            // LDAP
347            if ( serverConfiguration.isEnableLdap() )
348            {
349                if ( !AvailablePortFinder.available( serverConfiguration.getLdapPort() ) )
350                {
351                    alreadyInUseProtocolPortsList.add( NLS.bind(
352                        Messages.getString( "RunAction.LDAPPort" ), new Object[] { serverConfiguration.getLdapPort() } ) ); //$NON-NLS-1$
353                }
354            }
355    
356            // LDAPS
357            if ( serverConfiguration.isEnableLdaps() )
358            {
359                if ( !AvailablePortFinder.available( serverConfiguration.getLdapsPort() ) )
360                {
361                    alreadyInUseProtocolPortsList.add( NLS.bind(
362                        Messages.getString( "RunAction.LDAPSPort" ), new Object[] { serverConfiguration.getLdapsPort() } ) ); //$NON-NLS-1$
363                }
364            }
365    
366            // Kerberos
367            if ( serverConfiguration.isEnableKerberos() )
368            {
369                if ( !AvailablePortFinder.available( serverConfiguration.getKerberosPort() ) )
370                {
371                    alreadyInUseProtocolPortsList
372                        .add( NLS
373                            .bind(
374                                Messages.getString( "RunAction.KerberosPort" ), new Object[] { serverConfiguration.getKerberosPort() } ) ); //$NON-NLS-1$
375                }
376            }
377    
378            // DNS
379            if ( serverConfiguration.isEnableDns() )
380            {
381                if ( !AvailablePortFinder.available( serverConfiguration.getDnsPort() ) )
382                {
383                    alreadyInUseProtocolPortsList.add( NLS.bind(
384                        Messages.getString( "RunAction.DNSPort" ), new Object[] { serverConfiguration.getDnsPort() } ) ); //$NON-NLS-1$
385                }
386            }
387    
388            // NTP
389            if ( serverConfiguration.isEnableNtp() )
390            {
391                if ( !AvailablePortFinder.available( serverConfiguration.getNtpPort() ) )
392                {
393                    alreadyInUseProtocolPortsList.add( NLS.bind( Messages.getString( "RunAction.NTPPort" ), new Object[] //$NON-NLS-1$
394                        { serverConfiguration.getNtpPort() } ) );
395                }
396            }
397    
398            // Change Password
399            if ( serverConfiguration.isEnableChangePassword() )
400            {
401                if ( !AvailablePortFinder.available( serverConfiguration.getChangePasswordPort() ) )
402                {
403                    alreadyInUseProtocolPortsList
404                        .add( NLS
405                            .bind(
406                                Messages.getString( "RunAction.ChangePasswordPort" ), new Object[] { serverConfiguration.getChangePasswordPort() } ) ); //$NON-NLS-1$
407                }
408            }
409    
410            return alreadyInUseProtocolPortsList.toArray( new String[0] );
411        }
412    
413    
414        /**
415         * Gets an array of String containing the ports and their associated 
416         * protocols which are already in use.
417         *
418         * @param serverConfiguration
419         *      the 1.5.5 server configuration
420         * @return
421         *      an array of String containing the ports and their associated 
422         * protocols which are already in use.
423         */
424        private String[] getAlreadyInUseProtocolPortsVersion155( ServerConfigurationV155 serverConfiguration )
425        {
426            List<String> alreadyInUseProtocolPortsList = new ArrayList<String>();
427    
428            // LDAP
429            if ( serverConfiguration.isEnableLdap() )
430            {
431                if ( !AvailablePortFinder.available( serverConfiguration.getLdapPort() ) )
432                {
433                    alreadyInUseProtocolPortsList.add( NLS.bind(
434                        Messages.getString( "RunAction.LDAPPort" ), new Object[] { serverConfiguration.getLdapPort() } ) ); //$NON-NLS-1$
435                }
436            }
437    
438            // LDAPS
439            if ( serverConfiguration.isEnableLdaps() )
440            {
441                if ( !AvailablePortFinder.available( serverConfiguration.getLdapsPort() ) )
442                {
443                    alreadyInUseProtocolPortsList.add( NLS.bind(
444                        Messages.getString( "RunAction.LDAPSPort" ), new Object[] { serverConfiguration.getLdapsPort() } ) ); //$NON-NLS-1$
445                }
446            }
447    
448            // Kerberos
449            if ( serverConfiguration.isEnableKerberos() )
450            {
451                if ( !AvailablePortFinder.available( serverConfiguration.getKerberosPort() ) )
452                {
453                    alreadyInUseProtocolPortsList
454                        .add( NLS
455                            .bind(
456                                Messages.getString( "RunAction.KerberosPort" ), new Object[] { serverConfiguration.getKerberosPort() } ) ); //$NON-NLS-1$
457                }
458            }
459    
460            // DNS
461            if ( serverConfiguration.isEnableDns() )
462            {
463                if ( !AvailablePortFinder.available( serverConfiguration.getDnsPort() ) )
464                {
465                    alreadyInUseProtocolPortsList.add( NLS.bind(
466                        Messages.getString( "RunAction.DNSPort" ), new Object[] { serverConfiguration.getDnsPort() } ) ); //$NON-NLS-1$
467                }
468            }
469    
470            // NTP
471            if ( serverConfiguration.isEnableNtp() )
472            {
473                if ( !AvailablePortFinder.available( serverConfiguration.getNtpPort() ) )
474                {
475                    alreadyInUseProtocolPortsList.add( NLS.bind( Messages.getString( "RunAction.NTPPort" ), new Object[] //$NON-NLS-1$
476                        { serverConfiguration.getNtpPort() } ) );
477                }
478            }
479    
480            // Change Password
481            if ( serverConfiguration.isEnableChangePassword() )
482            {
483                if ( !AvailablePortFinder.available( serverConfiguration.getChangePasswordPort() ) )
484                {
485                    alreadyInUseProtocolPortsList
486                        .add( NLS
487                            .bind(
488                                Messages.getString( "RunAction.ChangePasswordPort" ), new Object[] { serverConfiguration.getChangePasswordPort() } ) ); //$NON-NLS-1$
489                }
490            }
491    
492            return alreadyInUseProtocolPortsList.toArray( new String[0] );
493        }
494    
495    
496        /* (non-Javadoc)
497         * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
498         */
499        public void run( IAction action )
500        {
501            run();
502        }
503    
504    
505        /* (non-Javadoc)
506         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
507         */
508        public void dispose()
509        {
510            // Nothing to do
511        }
512    
513    
514        /* (non-Javadoc)
515         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
516         */
517        public void init( IWorkbenchWindow window )
518        {
519            // Nothing to do
520        }
521    
522    
523        /* (non-Javadoc)
524         * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
525         */
526        public void selectionChanged( IAction action, ISelection selection )
527        {
528            // Nothing to do
529        }
530    }