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