Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[368696] Add support for authorization identifiers.

Signed-off-by: Pierre Queinnec <pierre.queinnec@zenika.com>
  • Loading branch information...
commit 75a7207fadf5170f01b4cb512cbe8828f33b0791 1 parent 3a57a27
Pierre Queinnec authored February 22, 2012
24  plugins/org.eclipse.datatools.enablement.mysql/plugin.xml
@@ -385,6 +385,12 @@
385 385
              provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlRoutineLoader"
386 386
              version="5.1">
387 387
        </overrideLoader>
  388
+       <overrideLoader
  389
+             eclass="org.eclipse.datatools.modelbase.sql.accesscontrol.AuthorizationIdentifier"
  390
+             product="MySql"
  391
+             provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlAuthorizationIdentifierLoader"
  392
+             version="5.1">
  393
+       </overrideLoader>
388 394
     </extension>
389 395
     <extension
390 396
           point="org.eclipse.datatools.connectivity.sqm.core.catalog">
@@ -418,6 +424,12 @@
418 424
              provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlRoutineLoader"
419 425
              version="5.0">
420 426
        </overrideLoader>
  427
+       <overrideLoader
  428
+             eclass="org.eclipse.datatools.modelbase.sql.accesscontrol.AuthorizationIdentifier"
  429
+             product="MySql"
  430
+             provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlAuthorizationIdentifierLoader"
  431
+             version="5.0">
  432
+       </overrideLoader>
421 433
     </extension>
422 434
     <extension
423 435
           point="org.eclipse.datatools.connectivity.sqm.core.catalog">
@@ -451,6 +463,12 @@
451 463
              provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlRoutineLoader"
452 464
              version="4.1">
453 465
        </overrideLoader>
  466
+       <overrideLoader
  467
+             eclass="org.eclipse.datatools.modelbase.sql.accesscontrol.AuthorizationIdentifier"
  468
+             product="MySql"
  469
+             provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlAuthorizationIdentifierLoader"
  470
+             version="4.1">
  471
+       </overrideLoader>
454 472
     </extension>
455 473
     <extension
456 474
           point="org.eclipse.datatools.connectivity.sqm.core.catalog">
@@ -484,6 +502,12 @@
484 502
              provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlRoutineLoader"
485 503
              version="4.0">
486 504
        </overrideLoader>
  505
+       <overrideLoader
  506
+             eclass="org.eclipse.datatools.modelbase.sql.accesscontrol.AuthorizationIdentifier"
  507
+             product="MySql"
  508
+             provider="org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlAuthorizationIdentifierLoader"
  509
+             version="4.0">
  510
+       </overrideLoader>
487 511
     </extension>
488 512
     <extension
489 513
           point="org.eclipse.datatools.connectivity.sqm.core.mappings">
85  ....datatools.enablement.mysql/src/org/eclipse/datatools/enablement/mysql/catalog/MySqlCatalogDatabase.java
@@ -9,10 +9,19 @@
9 9
  ******************************************************************************/
10 10
 package org.eclipse.datatools.enablement.mysql.catalog;
11 11
 
  12
+import java.lang.ref.SoftReference;
12 13
 import java.sql.Connection;
  14
+import java.util.ArrayList;
  15
+import java.util.List;
13 16
 
  17
+import org.eclipse.datatools.connectivity.sqm.core.definition.DatabaseDefinition;
14 18
 import org.eclipse.datatools.connectivity.sqm.core.rte.RefreshManager;
15 19
 import org.eclipse.datatools.connectivity.sqm.core.rte.jdbc.JDBCDatabase;
  20
+import org.eclipse.datatools.connectivity.sqm.core.util.CatalogLoaderOverrideManager;
  21
+import org.eclipse.datatools.connectivity.sqm.internal.core.RDBCorePlugin;
  22
+import org.eclipse.datatools.connectivity.sqm.loader.JDBCBaseLoader;
  23
+import org.eclipse.datatools.enablement.mysql.catalog.loaders.MySqlAuthorizationIdentifierLoader;
  24
+import org.eclipse.datatools.modelbase.sql.accesscontrol.SQLAccessControlPackage;
16 25
 import org.eclipse.datatools.modelbase.sql.schema.SQLSchemaPackage;
17 26
 import org.eclipse.datatools.modelbase.sql.schema.Schema;
18 27
 import org.eclipse.emf.common.util.EList;
@@ -26,6 +35,9 @@
26 35
 	
27 36
 	private Boolean schemasLoaded = Boolean.FALSE;
28 37
 	private Boolean catalogsLoaded = Boolean.FALSE;
  38
+	private Boolean authorizationIdsLoaded = Boolean.FALSE;
  39
+
  40
+	private transient SoftReference authorizationIdLoaderRef;
29 41
 
30 42
 	public MySqlCatalogDatabase(Connection connection){
31 43
 		super(connection);
@@ -38,6 +50,12 @@ public void refresh() {
38 50
 			}
39 51
 		}
40 52
 
  53
+		synchronized (authorizationIdsLoaded) {
  54
+			if (authorizationIdsLoaded.booleanValue()) {
  55
+				authorizationIdsLoaded = Boolean.FALSE;
  56
+			}
  57
+		}
  58
+
41 59
 		RefreshManager.getInstance().referesh(this);
42 60
 	}
43 61
 
@@ -70,4 +88,71 @@ public EList getCatalogs() {
70 88
 		}
71 89
 		return catalogs;
72 90
 	}
  91
+
  92
+	public EList getAuthorizationIds() {
  93
+		synchronized (this.authorizationIdsLoaded) {
  94
+			if (!authorizationIdsLoaded.booleanValue())
  95
+				this.loadAuthorizationIdentifiers();
  96
+		}
  97
+
  98
+		return super.getAuthorizationIds();
  99
+	}
  100
+
  101
+	private void loadAuthorizationIdentifiers() {
  102
+		boolean deliver = eDeliver();
  103
+		try {
  104
+			List container = super.getAuthorizationIds();
  105
+			List existingAuthorizationIds = new ArrayList(container);
  106
+
  107
+			eSetDeliver(false);
  108
+
  109
+			container.clear();
  110
+			getAuthorizationIdentifierLoader().loadAuthorizationIdentifiers(container, existingAuthorizationIds);
  111
+			getAuthorizationIdentifierLoader().clearAuthorizationIdentifiers(existingAuthorizationIds);
  112
+
  113
+			authorizationIdsLoaded = Boolean.TRUE;
  114
+
  115
+		} catch (Exception e) {
  116
+			e.printStackTrace();
  117
+
  118
+		} finally {
  119
+			eSetDeliver(deliver);
  120
+		}
  121
+	}
  122
+
  123
+	protected final MySqlAuthorizationIdentifierLoader getAuthorizationIdentifierLoader() {
  124
+		// cache the AuthorizationIdentifierLoader for better performance
  125
+		if (authorizationIdLoaderRef == null || authorizationIdLoaderRef.get() == null) {
  126
+			authorizationIdLoaderRef = new SoftReference(createAuthorizationIdentifierLoader());
  127
+		}
  128
+
  129
+		return (MySqlAuthorizationIdentifierLoader) authorizationIdLoaderRef.get();
  130
+	}
  131
+
  132
+	/**
  133
+	 * Creates and returns an instance of the AuthorizationIdentifierLoader. By default an instance of the
  134
+	 * <code>MySqlAuthorizationIdentifierLoader</code> is returned. This behavior can be changed by providing an
  135
+	 * <code>overrideLoader</code> using the eclass org.eclipse.datatools.modelbase.sql.accesscontrol.
  136
+	 * AuthorizationIdentifier.
  137
+	 * 
  138
+	 * @return An instance of MySqlAuthorizationIdentifierLoader.
  139
+	 */
  140
+	private MySqlAuthorizationIdentifierLoader createAuthorizationIdentifierLoader() {
  141
+		// get the database definition for the actual database
  142
+		DatabaseDefinition databaseDefinition = RDBCorePlugin.getDefault().getDatabaseDefinitionRegistry()
  143
+				.getDefinition(this.getCatalogDatabase());
  144
+
  145
+		// see if someone is interested in providing an own authorization identifier loader
  146
+		JDBCBaseLoader loader = CatalogLoaderOverrideManager.INSTANCE.getLoaderForDatabase(databaseDefinition,
  147
+				SQLAccessControlPackage.eINSTANCE.getAuthorizationIdentifier().getInstanceClassName());
  148
+
  149
+		if (loader != null) {
  150
+			MySqlAuthorizationIdentifierLoader authorizationIdLoader = (MySqlAuthorizationIdentifierLoader) loader;
  151
+			authorizationIdLoader.setCatalogObject(this);
  152
+			return authorizationIdLoader;
  153
+		}
  154
+
  155
+		return new MySqlAuthorizationIdentifierLoader(this);
  156
+	}
  157
+
73 158
 }
141  ...mysql/src/org/eclipse/datatools/enablement/mysql/catalog/loaders/MySqlAuthorizationIdentifierLoader.java
... ...
@@ -0,0 +1,141 @@
  1
+/*******************************************************************************
  2
+ * Copyright (c) 2012 Zenika
  3
+ * 
  4
+ * All rights reserved. This program and the accompanying materials
  5
+ * are made available under the terms of the Eclipse Public License v1.0
  6
+ * which accompanies this distribution, and is available at
  7
+ * http://www.eclipse.org/legal/epl-v10.html
  8
+ * 
  9
+ * Contributors: queinnec - initial API and implementation
  10
+ *******************************************************************************/
  11
+package org.eclipse.datatools.enablement.mysql.catalog.loaders;
  12
+
  13
+import java.sql.PreparedStatement;
  14
+import java.sql.ResultSet;
  15
+import java.sql.SQLException;
  16
+import java.util.List;
  17
+
  18
+import org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionFilter;
  19
+import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject;
  20
+import org.eclipse.datatools.connectivity.sqm.loader.IConnectionFilterProvider;
  21
+import org.eclipse.datatools.connectivity.sqm.loader.JDBCBaseLoader;
  22
+import org.eclipse.datatools.connectivity.sqm.loader.SchemaObjectFilterProvider;
  23
+import org.eclipse.datatools.enablement.mysql.model.impl.MySqlUserImpl;
  24
+import org.eclipse.datatools.modelbase.sql.accesscontrol.User;
  25
+
  26
+/**
  27
+ * This class adds the ability to retrieve a list of authorization identifiers from a MySQL database.
  28
+ * 
  29
+ * @author pierre.queinnec@zenika.com
  30
+ */
  31
+public class MySqlAuthorizationIdentifierLoader extends JDBCBaseLoader {
  32
+
  33
+	private static final String USER_QUERY = "SELECT * FROM mysql.user;"; //$NON-NLS-1$
  34
+	private static final String USER_NAME = "User";
  35
+	private static final String HOST_NAME = "Host";
  36
+
  37
+	public MySqlAuthorizationIdentifierLoader() {
  38
+		this(null);
  39
+	}
  40
+
  41
+	public MySqlAuthorizationIdentifierLoader(ICatalogObject catalogObject) {
  42
+		this(catalogObject, new SchemaObjectFilterProvider(ConnectionFilter.SEQUENCE_FILTER));
  43
+	}
  44
+
  45
+	/**
  46
+	 * @param catalogObject
  47
+	 * @param connectionFilterProvider
  48
+	 */
  49
+	public MySqlAuthorizationIdentifierLoader(ICatalogObject catalogObject,
  50
+			IConnectionFilterProvider connectionFilterProvider) {
  51
+
  52
+		super(catalogObject, connectionFilterProvider);
  53
+	}
  54
+
  55
+	public void clearAuthorizationIdentifiers(List existingAuthorizationIds) {
  56
+		existingAuthorizationIds.clear();
  57
+	}
  58
+
  59
+	public void loadAuthorizationIdentifiers(List containmentList, List existingAuthorizationIds) throws SQLException {
  60
+		ResultSet rs = null;
  61
+		PreparedStatement stmt = null;
  62
+		try {
  63
+			// initActiveFilter();
  64
+
  65
+			stmt = getCatalogObject().getConnection().prepareStatement(USER_QUERY);
  66
+			rs = createResultSet(stmt);
  67
+
  68
+			while (rs.next()) {
  69
+				String userDisplayName = this.toDisplayLabel(rs);
  70
+
  71
+				if (userDisplayName == null || isFiltered(userDisplayName)) {
  72
+					continue;
  73
+				}
  74
+
  75
+				User user = (User) getAndRemoveSQLObject(existingAuthorizationIds, userDisplayName);
  76
+
  77
+				if (user == null) {
  78
+					user = processRow(rs);
  79
+					if (user != null) {
  80
+						containmentList.add(user);
  81
+					}
  82
+				} else {
  83
+					containmentList.add(user);
  84
+					if (user instanceof ICatalogObject) {
  85
+						((ICatalogObject) user).refresh();
  86
+					}
  87
+				}
  88
+			}
  89
+		} finally {
  90
+			try {
  91
+				if (rs != null) {
  92
+					rs.close();
  93
+				}
  94
+
  95
+			} catch (SQLException e) {
  96
+				// ignored
  97
+
  98
+			} finally {
  99
+				try {
  100
+					if (stmt != null) {
  101
+						stmt.close();
  102
+					}
  103
+				} catch (SQLException e) {
  104
+					// ignored
  105
+				}
  106
+			}
  107
+		}
  108
+	}
  109
+
  110
+	protected String toDisplayLabel(ResultSet rs) throws SQLException {
  111
+		String userName = rs.getString(USER_NAME).trim();
  112
+		String hostName = rs.getString(HOST_NAME).trim();
  113
+		String displayName = userName + " [" + hostName + ']';
  114
+
  115
+		return displayName;
  116
+	}
  117
+
  118
+	protected User processRow(ResultSet rs) throws SQLException {
  119
+		User user = new MySqlUserImpl();
  120
+		String displayName = this.toDisplayLabel(rs);
  121
+
  122
+		// user.setName(userName);
  123
+		user.setName(displayName);
  124
+		user.setLabel(displayName);
  125
+
  126
+		return user;
  127
+	}
  128
+
  129
+	protected ResultSet createResultSet(PreparedStatement stmt) throws SQLException {
  130
+		try {
  131
+			return stmt.executeQuery();
  132
+
  133
+		} catch (RuntimeException e) {
  134
+			SQLException error = new SQLException(
  135
+					"Error while retrieving database information (authorization identifiers)");
  136
+			error.initCause(e);
  137
+			throw error;
  138
+		}
  139
+	}
  140
+
  141
+}
22  ...s/org.eclipse.datatools.enablement.mysql/src/org/eclipse/datatools/enablement/mysql/model/MySqlUser.java
... ...
@@ -0,0 +1,22 @@
  1
+/*******************************************************************************
  2
+ * Copyright (c) 2012 Zenika
  3
+ * 
  4
+ * All rights reserved. This program and the accompanying materials
  5
+ * are made available under the terms of the Eclipse Public License v1.0
  6
+ * which accompanies this distribution, and is available at
  7
+ * http://www.eclipse.org/legal/epl-v10.html
  8
+ * 
  9
+ * Contributors: queinnec - initial API and implementation
  10
+ *******************************************************************************/
  11
+package org.eclipse.datatools.enablement.mysql.model;
  12
+
  13
+import org.eclipse.datatools.modelbase.sql.accesscontrol.User;
  14
+
  15
+/**
  16
+ * A MySQL user definition.
  17
+ * 
  18
+ * @author pierre.queinnec@zenika.com
  19
+ */
  20
+public interface MySqlUser extends User {
  21
+
  22
+}
23  ...ipse.datatools.enablement.mysql/src/org/eclipse/datatools/enablement/mysql/model/impl/MySqlUserImpl.java
... ...
@@ -0,0 +1,23 @@
  1
+/*******************************************************************************
  2
+ * Copyright (c) 2012 Zenika
  3
+ * 
  4
+ * All rights reserved. This program and the accompanying materials
  5
+ * are made available under the terms of the Eclipse Public License v1.0
  6
+ * which accompanies this distribution, and is available at
  7
+ * http://www.eclipse.org/legal/epl-v10.html
  8
+ * 
  9
+ * Contributors: queinnec - initial API and implementation
  10
+ *******************************************************************************/
  11
+package org.eclipse.datatools.enablement.mysql.model.impl;
  12
+
  13
+import org.eclipse.datatools.enablement.mysql.model.MySqlUser;
  14
+import org.eclipse.datatools.modelbase.sql.accesscontrol.impl.UserImpl;
  15
+
  16
+/**
  17
+ * A MySQL user definition.
  18
+ * 
  19
+ * @author pierre.queinnec@zenika.com
  20
+ */
  21
+public class MySqlUserImpl extends UserImpl implements MySqlUser {
  22
+
  23
+}

0 notes on commit 75a7207

Please sign in to comment.
Something went wrong with that request. Please try again.