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