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 org.apache.directory.studio.apacheds.ApacheDsPlugin;
024    import org.apache.directory.studio.apacheds.configuration.model.ServerConfiguration;
025    import org.apache.directory.studio.apacheds.configuration.model.v153.ServerConfigurationV153;
026    import org.apache.directory.studio.apacheds.configuration.model.v154.ServerConfigurationV154;
027    import org.apache.directory.studio.connection.core.Connection;
028    import org.apache.directory.studio.connection.core.ConnectionCorePlugin;
029    import org.apache.directory.studio.connection.core.ConnectionParameter;
030    import org.apache.directory.studio.connection.core.ConnectionParameter.AuthenticationMethod;
031    import org.apache.directory.studio.connection.core.ConnectionParameter.EncryptionMethod;
032    import org.eclipse.jface.dialogs.IDialogConstants;
033    import org.eclipse.jface.dialogs.MessageDialog;
034    import org.eclipse.osgi.util.NLS;
035    import org.eclipse.ui.IPerspectiveDescriptor;
036    import org.eclipse.ui.IWorkbenchWindow;
037    import org.eclipse.ui.PlatformUI;
038    
039    
040    /**
041     * This class implements a helper class of the create connection action for a server.
042     *
043     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044     * @version $Rev$, $Date$
045     */
046    public class CreateConnectionActionHelper
047    {
048        /**
049         * Creates a connection in the LDAP Browser plugin.
050         *
051         * @param serverName
052         *      the name of the server
053         * @param serverConfiguration
054         *      the server configuration
055         */
056        public static void createLdapBrowserConnection( String serverName, ServerConfiguration serverConfiguration )
057        {
058            //Creating the connection parameter object
059            ConnectionParameter connectionParameter = new ConnectionParameter();
060    
061            // Auth method
062            connectionParameter.setAuthMethod( AuthenticationMethod.SIMPLE );
063    
064            // Encryption method and port
065            if ( serverConfiguration instanceof ServerConfigurationV154 )
066            {
067                ServerConfigurationV154 serverConfiguration154 = ( ServerConfigurationV154 ) serverConfiguration;
068                if ( serverConfiguration154.isEnableLdap() )
069                {
070                    connectionParameter.setEncryptionMethod( EncryptionMethod.NONE );
071                    connectionParameter.setPort( serverConfiguration154.getLdapPort() );
072                }
073                else if ( serverConfiguration154.isEnableLdaps() )
074                {
075                    connectionParameter.setEncryptionMethod( EncryptionMethod.LDAPS );
076                    connectionParameter.setPort( serverConfiguration154.getLdapsPort() );
077                }
078            }
079            else if ( serverConfiguration instanceof ServerConfigurationV153 )
080            {
081                ServerConfigurationV153 serverConfiguration153 = ( ServerConfigurationV153 ) serverConfiguration;
082                if ( serverConfiguration153.isEnableLdap() )
083                {
084                    connectionParameter.setEncryptionMethod( EncryptionMethod.NONE );
085                    connectionParameter.setPort( serverConfiguration153.getLdapPort() );
086                }
087                else if ( serverConfiguration153.isEnableLdaps() )
088                {
089                    connectionParameter.setEncryptionMethod( EncryptionMethod.LDAPS );
090                    connectionParameter.setPort( serverConfiguration153.getLdapsPort() );
091                }
092            }
093    
094            // Bind password
095            connectionParameter.setBindPassword( "secret" ); //$NON-NLS-1$
096    
097            // Bind principal
098            connectionParameter.setBindPrincipal( "uid=admin,ou=system" ); //$NON-NLS-1$
099    
100            // Host
101            connectionParameter.setHost( "localhost" ); //$NON-NLS-1$
102    
103            // Name
104            connectionParameter.setName( serverName );
105    
106            // Creating the connection
107            Connection connection = new Connection( connectionParameter );
108    
109            // Adding the connection to the connection manager
110            ConnectionCorePlugin.getDefault().getConnectionManager().addConnection( connection );
111    
112            // Adding the connection to the root connection folder
113            ConnectionCorePlugin.getDefault().getConnectionFolderManager().getRootConnectionFolder().addConnectionId(
114                connection.getId() );
115    
116            // Getting the window, LDAP perspective and current perspective
117            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
118            IPerspectiveDescriptor ldapPerspective = getLdapPerspective();
119            IPerspectiveDescriptor currentPerspective = window.getActivePage().getPerspective();
120    
121            // Checking if we are already in the LDAP perspective
122            if ( ( ldapPerspective != null ) && ( ldapPerspective.equals( currentPerspective ) ) )
123            {
124                // As we're already in the LDAP perspective, we only indicate to the user 
125                // the name of the connection that has been created
126                MessageDialog dialog = new MessageDialog(
127                    window.getShell(),
128                    Messages.getString( "CreateConnectionActionHelper.ConnectionCreated" ), null, //$NON-NLS-1$
129                    NLS
130                        .bind(
131                            Messages.getString( "CreateConnectionActionHelper.ConnectionCalledCreated" ), new String[] { connection.getName() } ), MessageDialog.INFORMATION, //$NON-NLS-1$
132                    new String[]
133                        { IDialogConstants.OK_LABEL }, MessageDialog.OK );
134                dialog.open();
135            }
136            else
137            {
138                // We're not already in the LDAP perspective, we indicate to the user
139                // the name of the connection that has been created and we ask him
140                // if we wants to switch to the LDAP perspective
141                MessageDialog dialog = new MessageDialog(
142                    window.getShell(),
143                    Messages.getString( "CreateConnectionActionHelper.ConnectionCreated" ), null, //$NON-NLS-1$
144                    NLS
145                        .bind(
146                            Messages.getString( "CreateConnectionActionHelper.ConnectionCalledCreatedSwitch" ), new String[] { connection.getName() } ), //$NON-NLS-1$
147                    MessageDialog.INFORMATION, new String[]
148                        { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, MessageDialog.OK );
149                if ( dialog.open() == MessageDialog.OK )
150                {
151                    // Switching to the LDAP perspective
152                    window.getActivePage().setPerspective( ldapPerspective );
153                }
154            }
155        }
156    
157    
158        /**
159         * Get the LDAP perspective.
160         *
161         * @return
162         *      the LDAP perspective
163         */
164        private static IPerspectiveDescriptor getLdapPerspective()
165        {
166            for ( IPerspectiveDescriptor perspective : PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives() )
167            {
168                if ( ApacheDsPlugin.getDefault().getPluginProperties().getString( "Perspective_LdapBrowserPerspective_id" ) //$NON-NLS-1$
169                    .equalsIgnoreCase( perspective.getId() ) )
170                {
171                    return perspective;
172                }
173            }
174    
175            return null;
176        }
177    }