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