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    
021    package org.apache.directory.studio.apacheds.actions;
022    
023    
024    import java.io.IOException;
025    import java.util.PropertyResourceBundle;
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.model.Server;
036    import org.apache.directory.studio.apacheds.views.ServersView;
037    import org.eclipse.core.runtime.Platform;
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.ui.IWorkbenchWindow;
045    import org.eclipse.ui.IWorkbenchWindowActionDelegate;
046    import org.osgi.framework.Bundle;
047    
048    
049    /**
050     * This class implements the create connection 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 CreateConnectionAction extends Action implements IWorkbenchWindowActionDelegate
056    {
057        /** The associated view */
058        private ServersView view;
059    
060    
061        /**
062         * Creates a new instance of CreateConnectionAction.
063         */
064        public CreateConnectionAction()
065        {
066            super( Messages.getString( "CreateConnectionAction.CreateAConnection" ) ); //$NON-NLS-1$
067            init();
068        }
069    
070    
071        /**
072         * Creates a new instance of CreateConnectionAction.
073         * 
074         * @param view
075         *      the associated view
076         */
077        public CreateConnectionAction( ServersView view )
078        {
079            super( Messages.getString( "CreateConnectionAction.CreateAConnection" ) ); //$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_CREATE_CONNECTION );
091            setActionDefinitionId( ApacheDsPluginConstants.CMD_CREATE_CONNECTION );
092            setToolTipText( Messages.getString( "CreateConnectionAction.StopToolTip" ) ); //$NON-NLS-1$
093            setImageDescriptor( ApacheDsPlugin.getDefault().getImageDescriptor(
094                ApacheDsPluginConstants.IMG_CREATE_CONNECTION ) );
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                    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                    if ( isEnableLdapOrLdaps( serverConfiguration ) )
137                    {
138                        // Creating the connection using the helper class
139                        CreateConnectionActionHelper.createLdapBrowserConnection( server.getName(), serverConfiguration );
140                    }
141                    else
142                    {
143                        // LDAP and LDAPS protocols are disabled, we report this error to the user
144                        MessageDialog dialog = new MessageDialog( view.getSite().getShell(), Messages
145                            .getString( "CreateConnectionAction.UnableCreateConnection" ), null, //$NON-NLS-1$
146                            Messages.getString( "CreateConnectionAction.LDAPAndLDAPSDisabled" ), MessageDialog.ERROR, //$NON-NLS-1$
147                            new String[]
148                                { IDialogConstants.OK_LABEL }, MessageDialog.OK );
149                        dialog.open();
150                    }
151                }
152            }
153        }
154    
155    
156        /**
157         * Reports to the user an error message indicating the server 
158         * configuration could not be read correctly.
159         *
160         * @param errorMessage
161         *      an error message which can be <code>null</code>
162         */
163        private void reportErrorReadingServerConfiguration( String errorMessage )
164        {
165            String message = null;
166    
167            if ( errorMessage == null )
168            {
169                message = Messages.getString( "CreateConnectionAction.UnableReadServerConfiguration" ); //$NON-NLS-1$
170            }
171            else
172            {
173                message = Messages.getString( "CreateConnectionAction.UnableReadServerConfiguration" ) + ApacheDsPluginUtils.LINE_SEPARATOR //$NON-NLS-1$
174                    + ApacheDsPluginUtils.LINE_SEPARATOR
175                    + Messages.getString( "CreateConnectionAction.FollowingErrorOccurred" ) + errorMessage; //$NON-NLS-1$
176            }
177    
178            MessageDialog dialog = new MessageDialog( view.getSite().getShell(), Messages
179                .getString( "CreateConnectionAction.UnableReadServerConfiguration" ), //$NON-NLS-1$
180                null, message, MessageDialog.ERROR, new String[]
181                    { IDialogConstants.OK_LABEL }, MessageDialog.OK );
182            dialog.open();
183        }
184    
185    
186        /**
187         * Indicates if LDAP or LDAPS is enabled.
188         *
189         * @param serverConfiguration
190         *      the server configuration
191         * @return
192         *      <code>true</code> if LDAP or LDAPS is enabled, <code>false</code> if not
193         */
194        private boolean isEnableLdapOrLdaps( ServerConfiguration serverConfiguration )
195        {
196            if ( serverConfiguration instanceof ServerConfigurationV155 )
197            {
198                ServerConfigurationV155 serverConfiguration155 = ( ServerConfigurationV155 ) serverConfiguration;
199                return ( serverConfiguration155.isEnableLdap() ) || ( serverConfiguration155.isEnableLdaps() );
200            }
201            else if ( serverConfiguration instanceof ServerConfigurationV154 )
202            {
203                ServerConfigurationV154 serverConfiguration154 = ( ServerConfigurationV154 ) serverConfiguration;
204                return ( serverConfiguration154.isEnableLdap() ) || ( serverConfiguration154.isEnableLdaps() );
205            }
206            else if ( serverConfiguration instanceof ServerConfigurationV153 )
207            {
208                ServerConfigurationV153 serverConfiguration153 = ( ServerConfigurationV153 ) serverConfiguration;
209                return ( serverConfiguration153.isEnableLdap() ) || ( serverConfiguration153.isEnableLdaps() );
210            }
211    
212            return false;
213        }
214    
215    
216        /**
217         * Sets the enabled state of this action.
218         * <p>
219         * When an action is in the enabled state, the control associated with 
220         * it is active; triggering it will end up inkoking this action's 
221         * <code>run</code> method.
222         * </p>
223         * <p>
224         * Fires a property change event for the <code>ENABLED</code> property
225         * if the enabled state actually changes as a consequence.
226         * </p>
227         * <p>
228         * In the particular case of this action, when the enabled value equals
229         * <code>true</code>, a check on the presence of the necessary LDAP
230         * Browser plugins is executed. The action is enabled only if all the 
231         * required plugins are available.
232         * </p>
233         *
234         * @param enabled <code>true</code> to enable, and
235         *   <code>false</code> to disable
236         * @see #ENABLED
237         */
238        public void setEnabled( boolean enabled )
239        {
240            if ( enabled )
241            {
242                super.setEnabled( isLdapBrowserPluginsAvailable() );
243            }
244            else
245            {
246                super.setEnabled( enabled );
247            }
248        }
249    
250    
251        /**
252         * Indicates if the LDAP Browser plugins are available or not.
253         *
254         * @return
255         *  <code>true</code> if the LDAP Browser plugins are available, 
256         *  <code>false</code> if not.
257         */
258        private boolean isLdapBrowserPluginsAvailable()
259        {
260            PropertyResourceBundle properties = ApacheDsPlugin.getDefault().getPluginProperties();
261    
262            // Connection Core Plugin
263            Bundle connectionCoreBundle = Platform.getBundle( properties.getString( "Plugin_ConnectionCore_id" ) ); //$NON-NLS-1$
264            if ( connectionCoreBundle != null )
265            {
266                // Checking the state of the plugin
267                if ( connectionCoreBundle.getState() == Bundle.UNINSTALLED )
268                {
269                    return false;
270                }
271    
272                // Connection UI Plugin
273                Bundle connectionUiBundle = Platform.getBundle( properties.getString( "Plugin_ConnectionUi_id" ) ); //$NON-NLS-1$
274                if ( connectionUiBundle != null )
275                {
276                    // Checking the state of the plugin
277                    if ( connectionUiBundle.getState() == Bundle.UNINSTALLED )
278                    {
279                        return false;
280                    }
281    
282                    // LDAP Browser Common Plugin
283                    Bundle ldapBrowserCommonBundle = Platform.getBundle( properties
284                        .getString( "Plugin_LdapBrowserCommon_id" ) ); //$NON-NLS-1$
285                    if ( ldapBrowserCommonBundle != null )
286                    {
287                        // Checking the state of the plugin
288                        if ( ldapBrowserCommonBundle.getState() == Bundle.UNINSTALLED )
289                        {
290                            return false;
291                        }
292    
293                        // LDAP Browser Core Plugin
294                        Bundle ldapBrowserCoreBundle = Platform.getBundle( properties
295                            .getString( "Plugin_LdapBrowserCore_id" ) ); //$NON-NLS-1$
296                        if ( ldapBrowserCoreBundle != null )
297                        {
298                            // Checking the state of the plugin
299                            if ( ldapBrowserCoreBundle.getState() == Bundle.UNINSTALLED )
300                            {
301                                return false;
302                            }
303    
304                            // LDAP Browser UI Plugin
305                            Bundle ldapBrowserUiBundle = Platform.getBundle( properties
306                                .getString( "Plugin_LdapBrowserUi_id" ) ); //$NON-NLS-1$
307                            if ( ldapBrowserUiBundle != null )
308                            {
309                                // Checking the state of the plugin
310                                if ( ldapBrowserUiBundle.getState() == Bundle.UNINSTALLED )
311                                {
312                                    return false;
313                                }
314    
315                                // LDIF Editor Plugin
316                                Bundle ldifEditorBundle = Platform
317                                    .getBundle( properties.getString( "Plugin_LdifEditor_id" ) ); //$NON-NLS-1$
318                                if ( ldifEditorBundle != null )
319                                {
320                                    // Checking the state of the plugin
321                                    if ( ldifEditorBundle.getState() == Bundle.UNINSTALLED )
322                                    {
323                                        return false;
324                                    }
325    
326                                    // LDIF Parser Plugin
327                                    Bundle ldifParserBundle = Platform.getBundle( properties
328                                        .getString( "Plugin_LdifParser_id" ) ); //$NON-NLS-1$
329                                    if ( ldifParserBundle != null )
330                                    {
331                                        // Checking the state of the plugin
332                                        if ( ldifParserBundle.getState() == Bundle.UNINSTALLED )
333                                        {
334                                            return false;
335                                        }
336    
337                                        // Jars Plugin
338                                        Bundle jarsBundle = Platform.getBundle( properties.getString( "Plugin_Jars_id" ) ); //$NON-NLS-1$
339                                        if ( jarsBundle != null )
340                                        {
341                                            // Checking the state of the plugin
342                                            if ( jarsBundle.getState() == Bundle.UNINSTALLED )
343                                            {
344                                                return false;
345                                            }
346    
347                                            return true;
348                                        }
349                                    }
350                                }
351                            }
352                        }
353                    }
354                }
355            }
356    
357            return false;
358        }
359    
360    
361        /* (non-Javadoc)
362         * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
363         */
364        public void run( IAction action )
365        {
366            run();
367        }
368    
369    
370        /* (non-Javadoc)
371         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
372         */
373        public void dispose()
374        {
375            // Nothing to do
376        }
377    
378    
379        /* (non-Javadoc)
380         * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
381         */
382        public void init( IWorkbenchWindow window )
383        {
384            // Nothing to do
385        }
386    
387    
388        /* (non-Javadoc)
389         * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
390         */
391        public void selectionChanged( IAction action, ISelection selection )
392        {
393            // Nothing to do
394        }
395    }