|
From: <cw...@us...> - 2007-08-07 17:49:38
|
Revision: 110
http://mptstore.svn.sourceforge.net/mptstore/?rev=110&view=rev
Author: cwilper
Date: 2007-08-07 10:49:35 -0700 (Tue, 07 Aug 2007)
Log Message:
-----------
added ability to auto-grant select privs for users and groups
via a system property or via constructor parameter
Modified Paths:
--------------
trunk/src/java/org/nsdl/mptstore/impl/postgres/PostgresDDLGenerator.java
Modified: trunk/src/java/org/nsdl/mptstore/impl/postgres/PostgresDDLGenerator.java
===================================================================
--- trunk/src/java/org/nsdl/mptstore/impl/postgres/PostgresDDLGenerator.java 2007-05-01 03:28:53 UTC (rev 109)
+++ trunk/src/java/org/nsdl/mptstore/impl/postgres/PostgresDDLGenerator.java 2007-08-07 17:49:35 UTC (rev 110)
@@ -1,68 +1,107 @@
-package org.nsdl.mptstore.impl.postgres;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.nsdl.mptstore.core.AbstractDDLGenerator;
-
-/**
- * A <code>DDLGenerator</code> that works with Postgres.
- * <p>
- * The map table DDL defines pKey as a <code>SERIAL</code> value
- * and p as a <code>TEXT</code> value, with indexes on each column.
- * </p>
- * <p>
- * The relationship table DDL defines s and o
- * as <code>TEXT</code> values, with indexes on each column.
- * </p>
- * <p>
- * Since Postgres automatically drops associated indexes
- * and sequences when the table is dropped, the AbstractDDLGenerator
- * implementation is used for the drop methods.
- * </p>
- *
- * @author cw...@cs...
- */
-public class PostgresDDLGenerator extends AbstractDDLGenerator {
-
- /**
- * Construct a PostgresDDLGenerator.
- */
- public PostgresDDLGenerator() {
- }
-
- /** {@inheritDoc} */
- public List<String> getCreateMapTableDDL(final String table) {
-
- List<String> cmds = new ArrayList<String>();
-
- cmds.add("CREATE TABLE " + table + " (\n"
- + " pKey SERIAL,\n"
- + " p TEXT NOT NULL\n"
- + ")");
- cmds.add("CREATE INDEX " + table + "_pKey "
- + " on " + table + " (pKey)");
- cmds.add("CREATE INDEX " + table + "_p "
- + " on " + table + " (p)");
-
- return cmds;
- }
-
+package org.nsdl.mptstore.impl.postgres;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.nsdl.mptstore.core.AbstractDDLGenerator;
+
+/**
+ * A <code>DDLGenerator</code> that works with Postgres.
+ * <p>
+ * The map table DDL defines pKey as a <code>SERIAL</code> value
+ * and p as a <code>TEXT</code> value, with indexes on each column.
+ * </p>
+ * <p>
+ * The relationship table DDL defines s and o
+ * as <code>TEXT</code> values, with indexes on each column.
+ * </p>
+ * <p>
+ * Since Postgres automatically drops associated indexes
+ * and sequences when the table is dropped, the AbstractDDLGenerator
+ * implementation is used for the drop methods.
+ * </p>
+ *
+ * @author cw...@cs...
+ */
+public class PostgresDDLGenerator extends AbstractDDLGenerator {
+
+ private final String[] _users;
+
+ private final String[] _groups;
+
+ /**
+ * Construct a PostgresDDLGenerator.
+ */
+ public PostgresDDLGenerator() {
+ _users = splitProperty("mptstore.postgres.autoGrantUsers");
+ _groups = splitProperty("mptstore.postgres.autoGrantGroups");
+ }
+
+ public PostgresDDLGenerator(String[] users, String[] groups) {
+ if (users == null) {
+ _users = new String[0];
+ } else {
+ _users = users;
+ }
+ if (groups == null) {
+ _groups = new String[0];
+ } else {
+ _groups = groups;
+ }
+ }
+
+ private static String[] splitProperty(String name) {
+ String val = System.getProperty(name);
+ if (val == null || val.trim().length() == 0) {
+ return new String[0];
+ } else {
+ return val.trim().split(" +");
+ }
+ }
+
+ private void addSelectGrants(List<String> cmds, String table) {
+ for (String name : _users) {
+ cmds.add("GRANT SELECT ON TABLE " + table + " TO " + name);
+ }
+ for (String name : _groups) {
+ cmds.add("GRANT SELECT ON TABLE " + table + " TO GROUP " + name);
+ }
+ }
+
/** {@inheritDoc} */
- public List<String> getCreateSOTableDDL(final String table) {
-
- List<String> cmds = new ArrayList<String>();
-
- cmds.add("CREATE TABLE " + table + " (\n"
- + " s TEXT NOT NULL,\n"
- + " o TEXT NOT NULL\n"
- + ")");
- cmds.add("CREATE INDEX " + table + "_s "
- + " on " + table + " (s)");
- cmds.add("CREATE INDEX " + table + "_o "
- + " on " + table + " (o)");
-
- return cmds;
- }
-
-}
+ public List<String> getCreateMapTableDDL(final String table) {
+
+ List<String> cmds = new ArrayList<String>();
+
+ cmds.add("CREATE TABLE " + table + " (\n"
+ + " pKey SERIAL,\n"
+ + " p TEXT NOT NULL\n"
+ + ")");
+ cmds.add("CREATE INDEX " + table + "_pKey "
+ + " on " + table + " (pKey)");
+ cmds.add("CREATE INDEX " + table + "_p "
+ + " on " + table + " (p)");
+ addSelectGrants(cmds, table);
+
+ return cmds;
+ }
+
+ /** {@inheritDoc} */
+ public List<String> getCreateSOTableDDL(final String table) {
+
+ List<String> cmds = new ArrayList<String>();
+
+ cmds.add("CREATE TABLE " + table + " (\n"
+ + " s TEXT NOT NULL,\n"
+ + " o TEXT NOT NULL\n"
+ + ")");
+ cmds.add("CREATE INDEX " + table + "_s "
+ + " on " + table + " (s)");
+ cmds.add("CREATE INDEX " + table + "_o "
+ + " on " + table + " (o)");
+ addSelectGrants(cmds, table);
+
+ return cmds;
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|