View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package sample.contact;
17  
18  import org.springframework.beans.factory.InitializingBean;
19  
20  import org.springframework.jdbc.core.JdbcTemplate;
21  
22  import org.springframework.util.Assert;
23  
24  import java.util.Random;
25  
26  import javax.sql.DataSource;
27  
28  
29  /***
30   * Populates the Contacts in-memory database with contact and ACL information.
31   *
32   * @author Ben Alex
33   * @version $Id: DataSourcePopulator.java,v 1.7 2005/11/17 00:56:08 benalex Exp $
34   */
35  public class DataSourcePopulator implements InitializingBean {
36      //~ Instance fields ========================================================
37  
38      Random rnd = new Random();
39      String[] firstNames = {"Bob", "Mary", "James", "Jane", "Kristy", "Kirsty", "Kate", "Jeni", "Angela", "Melanie", "Kent", "William", "Geoff", "Jeff", "Adrian", "Amanda", "Lisa", "Elizabeth", "Prue", "Richard", "Darin", "Phillip", "Michael", "Belinda", "Samantha", "Brian", "Greg", "Matthew"};
40      String[] lastNames = {"Smith", "Williams", "Jackson", "Rictor", "Nelson", "Fitzgerald", "McAlpine", "Sutherland", "Abbott", "Hall", "Edwards", "Gates", "Black", "Brown", "Gray", "Marwell", "Booch", "Johnson", "McTaggart", "Parklin", "Findlay", "Robinson", "Giugni", "Lang", "Chi", "Carmichael"};
41      private DataSource dataSource;
42      private int createEntities = 1000;
43  
44      //~ Methods ================================================================
45  
46      public void setCreateEntities(int createEntities) {
47          this.createEntities = createEntities;
48      }
49  
50      public int getCreateEntities() {
51          return createEntities;
52      }
53  
54      public void setDataSource(DataSource dataSource) {
55          this.dataSource = dataSource;
56      }
57  
58      public DataSource getDataSource() {
59          return dataSource;
60      }
61  
62      public void afterPropertiesSet() throws Exception {
63          Assert.notNull(dataSource, "dataSource required");
64  
65          JdbcTemplate template = new JdbcTemplate(dataSource);
66  
67          template.execute(
68              "CREATE TABLE CONTACTS(ID BIGINT NOT NULL PRIMARY KEY, CONTACT_NAME VARCHAR_IGNORECASE(50) NOT NULL, EMAIL VARCHAR_IGNORECASE(50) NOT NULL)");
69          template.execute(
70              "INSERT INTO contacts VALUES (1, 'John Smith', 'john@somewhere.com');"); // marissa
71          template.execute(
72              "INSERT INTO contacts VALUES (2, 'Michael Citizen', 'michael@xyz.com');"); // marissa
73          template.execute(
74              "INSERT INTO contacts VALUES (3, 'Joe Bloggs', 'joe@demo.com');"); // marissa
75          template.execute(
76              "INSERT INTO contacts VALUES (4, 'Karen Sutherland', 'karen@sutherland.com');"); // marissa + dianne + scott
77          template.execute(
78              "INSERT INTO contacts VALUES (5, 'Mitchell Howard', 'mitchell@abcdef.com');"); // dianne
79          template.execute(
80              "INSERT INTO contacts VALUES (6, 'Rose Costas', 'rose@xyz.com');"); // dianne + scott
81          template.execute(
82              "INSERT INTO contacts VALUES (7, 'Amanda Smith', 'amanda@abcdef.com');"); // scott
83          template.execute(
84              "INSERT INTO contacts VALUES (8, 'Cindy Smith', 'cindy@smith.com');"); // dianne + scott
85          template.execute(
86              "INSERT INTO contacts VALUES (9, 'Jonathan Citizen', 'jonathan@xyz.com');"); // scott
87  
88          for (int i = 10; i < createEntities; i++) {
89              String[] person = selectPerson();
90              template.execute("INSERT INTO contacts VALUES (" + i + ", '"
91                  + person[2] + "', '" + person[0].toLowerCase() + "@"
92                  + person[1].toLowerCase() + ".com');");
93          }
94  
95          template.execute(
96              "CREATE TABLE ACL_OBJECT_IDENTITY(ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 100)  NOT NULL PRIMARY KEY,OBJECT_IDENTITY VARCHAR_IGNORECASE(250) NOT NULL,PARENT_OBJECT BIGINT,ACL_CLASS VARCHAR_IGNORECASE(250) NOT NULL,CONSTRAINT UNIQUE_OBJECT_IDENTITY UNIQUE(OBJECT_IDENTITY),CONSTRAINT SYS_FK_3 FOREIGN KEY(PARENT_OBJECT) REFERENCES ACL_OBJECT_IDENTITY(ID))");
97          template.execute(
98              "INSERT INTO acl_object_identity VALUES (1, 'sample.contact.Contact:1', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
99          template.execute(
100             "INSERT INTO acl_object_identity VALUES (2, 'sample.contact.Contact:2', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
101         template.execute(
102             "INSERT INTO acl_object_identity VALUES (3, 'sample.contact.Contact:3', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
103         template.execute(
104             "INSERT INTO acl_object_identity VALUES (4, 'sample.contact.Contact:4', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
105         template.execute(
106             "INSERT INTO acl_object_identity VALUES (5, 'sample.contact.Contact:5', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
107         template.execute(
108             "INSERT INTO acl_object_identity VALUES (6, 'sample.contact.Contact:6', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
109         template.execute(
110             "INSERT INTO acl_object_identity VALUES (7, 'sample.contact.Contact:7', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
111         template.execute(
112             "INSERT INTO acl_object_identity VALUES (8, 'sample.contact.Contact:8', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
113         template.execute(
114             "INSERT INTO acl_object_identity VALUES (9, 'sample.contact.Contact:9', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
115 
116         for (int i = 10; i < createEntities; i++) {
117             template.execute("INSERT INTO acl_object_identity VALUES (" + i
118                 + ", 'sample.contact.Contact:" + i
119                 + "', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');");
120         }
121 
122         template.execute(
123             "CREATE TABLE ACL_PERMISSION(ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 100)  NOT NULL PRIMARY KEY,ACL_OBJECT_IDENTITY BIGINT NOT NULL,RECIPIENT VARCHAR_IGNORECASE(100) NOT NULL,MASK INTEGER NOT NULL,CONSTRAINT UNIQUE_RECIPIENT UNIQUE(ACL_OBJECT_IDENTITY,RECIPIENT),CONSTRAINT SYS_FK_7 FOREIGN KEY(ACL_OBJECT_IDENTITY) REFERENCES ACL_OBJECT_IDENTITY(ID))");
124         template.execute(
125             "INSERT INTO acl_permission VALUES (null, 1, 'marissa', 1);"); // administer
126         template.execute(
127             "INSERT INTO acl_permission VALUES (null, 2, 'marissa', 2);"); // read
128         template.execute(
129             "INSERT INTO acl_permission VALUES (null, 3, 'marissa', 22);"); // read+write+delete
130         template.execute(
131             "INSERT INTO acl_permission VALUES (null, 4, 'marissa', 1);"); // administer
132         template.execute(
133             "INSERT INTO acl_permission VALUES (null, 4, 'dianne', 1);"); // administer
134         template.execute(
135             "INSERT INTO acl_permission VALUES (null, 4, 'scott', 2);"); // read
136         template.execute(
137             "INSERT INTO acl_permission VALUES (null, 5, 'dianne', 2);"); // read
138         template.execute(
139             "INSERT INTO acl_permission VALUES (null, 6, 'dianne', 22);"); // read+write+delete
140         template.execute(
141             "INSERT INTO acl_permission VALUES (null, 6, 'scott', 2);"); // read
142         template.execute(
143             "INSERT INTO acl_permission VALUES (null, 7, 'scott', 1);"); // administer
144         template.execute(
145             "INSERT INTO acl_permission VALUES (null, 8, 'dianne', 2);"); // read
146         template.execute(
147             "INSERT INTO acl_permission VALUES (null, 8, 'scott', 2);"); // read
148         template.execute(
149             "INSERT INTO acl_permission VALUES (null, 9, 'scott', 22);"); // read+write+delete
150 
151         String[] users = {"bill", "bob", "jane"}; // don't want to mess around with consistent sample data
152         int[] permissions = {1, 2, 22};
153 
154         for (int i = 10; i < createEntities; i++) {
155             String user = users[rnd.nextInt(users.length)];
156             int permission = permissions[rnd.nextInt(permissions.length)];
157             template.execute("INSERT INTO acl_permission VALUES (null, " + i
158                 + ", '" + user + "', " + permission + ");");
159 
160             String user2 = users[rnd.nextInt(users.length)];
161             int permission2 = permissions[rnd.nextInt(permissions.length)];
162 
163             if (!user2.equals(user)) {
164                 template.execute("INSERT INTO acl_permission VALUES (null, "
165                     + i + ", '" + user2 + "', " + permission2 + ");");
166             }
167         }
168 
169         template.execute(
170             "CREATE TABLE USERS(USERNAME VARCHAR_IGNORECASE(50) NOT NULL PRIMARY KEY,PASSWORD VARCHAR_IGNORECASE(50) NOT NULL,ENABLED BOOLEAN NOT NULL);");
171         template.execute(
172             "CREATE TABLE AUTHORITIES(USERNAME VARCHAR_IGNORECASE(50) NOT NULL,AUTHORITY VARCHAR_IGNORECASE(50) NOT NULL,CONSTRAINT FK_AUTHORITIES_USERS FOREIGN KEY(USERNAME) REFERENCES USERS(USERNAME));");
173         template.execute(
174             "CREATE UNIQUE INDEX IX_AUTH_USERNAME ON AUTHORITIES(USERNAME,AUTHORITY);");
175 
176         /*
177            Passwords encoded using MD5, NOT in Base64 format, with null as salt
178            Encoded password for marissa is "koala"
179            Encoded password for dianne is "emu"
180            Encoded password for scott is "wombat"
181            Encoded password for peter is "opal" (but user is disabled)
182            Encoded password for bill is "wombat"
183            Encoded password for bob is "wombat"
184            Encoded password for jane is "wombat"
185         
186          */
187         template.execute(
188             "INSERT INTO USERS VALUES('marissa','a564de63c2d0da68cf47586ee05984d7',TRUE);");
189         template.execute(
190             "INSERT INTO USERS VALUES('dianne','65d15fe9156f9c4bbffd98085992a44e',TRUE);");
191         template.execute(
192             "INSERT INTO USERS VALUES('scott','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
193         template.execute(
194             "INSERT INTO USERS VALUES('peter','22b5c9accc6e1ba628cedc63a72d57f8',FALSE);");
195         template.execute(
196             "INSERT INTO USERS VALUES('bill','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
197         template.execute(
198             "INSERT INTO USERS VALUES('bob','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
199         template.execute(
200             "INSERT INTO USERS VALUES('jane','2b58af6dddbd072ed27ffc86725d7d3a',TRUE);");
201         template.execute(
202             "INSERT INTO AUTHORITIES VALUES('marissa','ROLE_USER');");
203         template.execute(
204             "INSERT INTO AUTHORITIES VALUES('marissa','ROLE_SUPERVISOR');");
205         template.execute(
206             "INSERT INTO AUTHORITIES VALUES('dianne','ROLE_USER');");
207         template.execute("INSERT INTO AUTHORITIES VALUES('scott','ROLE_USER');");
208         template.execute("INSERT INTO AUTHORITIES VALUES('peter','ROLE_USER');");
209         template.execute("INSERT INTO AUTHORITIES VALUES('bill','ROLE_USER');");
210         template.execute("INSERT INTO AUTHORITIES VALUES('bob','ROLE_USER');");
211         template.execute("INSERT INTO AUTHORITIES VALUES('jane','ROLE_USER');");
212     }
213 
214     private String[] selectPerson() {
215         String firstName = firstNames[rnd.nextInt(firstNames.length)];
216         String lastName = lastNames[rnd.nextInt(lastNames.length)];
217 
218         return new String[] {firstName, lastName, firstName + " " + lastName};
219     }
220 }