View Javadoc

1   /***
2    * 
3    */
4   package atg.test.configuration;
5   
6   import java.io.File;
7   import java.io.FileWriter;
8   import java.io.IOException;
9   import java.util.HashMap;
10  import java.util.Map;
11  
12  import org.apache.log4j.Logger;
13  
14  import atg.adapter.gsa.InitializingGSA;
15  import atg.adapter.gsa.event.GSAEventServer;
16  import atg.dtm.TransactionDemarcationLogging;
17  import atg.dtm.TransactionManagerImpl;
18  import atg.dtm.UserTransactionImpl;
19  import atg.service.idgen.SQLIdGenerator;
20  import atg.service.jdbc.FakeXADataSource;
21  import atg.service.jdbc.MonitoredDataSource;
22  import atg.test.util.FileUtil;
23  
24  /***
25   * <i>This class is a merger of atg.test.util.DBUtils and
26   * atg.adapter.gsa.GSATestUtils. The result will hopefully be a class that just
27   * has the bare minimums needed for testing against an existing and/or in-memory
28   * database.</i>
29   * <p>
30   * This class will created all properties files needed for repository based
31   * tests.
32   * </p>
33   * 
34   * @author robert
35   * 
36   */
37  public final class RepositoryConfiguration {
38  
39  	// TODO-1 []: re-add versioned repository support?
40  	// TODO-2 []: better/more uniform way of handling properties file creation
41  
42  	protected String isDebug = Boolean.FALSE.toString();
43  
44  	protected final Map<String, String> settings = new HashMap<String, String>();
45  
46  	private static Logger log = Logger.getLogger(RepositoryConfiguration.class);
47  
48  	public void setDebug(final boolean isDebug) {
49  		this.isDebug = Boolean.toString(isDebug);
50  	}
51  
52  	public RepositoryConfiguration() {
53  		super();
54  	}
55  
56  	/***
57  	 * 
58  	 * @param root
59  	 * @throws IOException
60  	 */
61  	public void createPropertiesByConfigurationLocation(final File root)
62  			throws IOException {
63  		this.createTransactionManager(root);
64  		this.createUserTransaction(root);
65  		this.createIdGenerator(root);
66  		this.createIdSpaces(root);
67  		this.createSQLRepositoryEventServer(root);
68  		this.createJtdDataSource(root);
69  
70  		log.info("Created repository configuration fileset");
71  	}
72  
73  	/***
74  	 * 
75  	 * @param root
76  	 * @param jdbcSettings
77  	 * @throws IOException
78  	 */
79  	public void createFakeXADataSource(final File root,
80  			Map<String, String> jdbcSettings) throws IOException {
81  
82  		// TODO: Something expects the url property name in upper case... still
83  		// have
84  		// to investigate.
85  		jdbcSettings.put("URL", jdbcSettings.get("url"));
86  
87  		// remove the lower case url key/value pair so the generated
88  		// FakeXADataSource.properties only contains the upper case URL
89  		// key/value.
90  		jdbcSettings.remove("url");
91  
92  		jdbcSettings.put("transactionManager",
93  				"/atg/dynamo/transaction/TransactionManager");
94  
95  		FileUtil.createPropertyFile("FakeXADataSource", new File(root
96  				.getAbsolutePath()
97  				+ "/atg/dynamo/service/jdbc"), FakeXADataSource.class,
98  				jdbcSettings);
99  
100 		// restore the settings state (re-add url and remove URL)
101 		jdbcSettings.put("url", jdbcSettings.get("URL"));
102 		jdbcSettings.remove("URL");
103 	}
104 
105 	/***
106 	 * 
107 	 * @param root
108 	 * @throws IOException
109 	 */
110 	private void createJtdDataSource(final File root) throws IOException {
111 		this.settings.clear();
112 		settings.put("dataSource", "/atg/dynamo/service/jdbc/FakeXADataSource");
113 		settings.put("transactionManager",
114 				"/atg/dynamo/transaction/TransactionManager");
115 		settings.put("min", "10");
116 		settings.put("max", "20");
117 		settings.put("blocking", "true");
118 		settings.put("loggingSQLWarning", isDebug);
119 		settings.put("loggingSQLInfo", isDebug);
120 		settings.put("loggingSQLDebug", isDebug);
121 
122 		FileUtil.createPropertyFile("JTDataSource", new File(root
123 				.getAbsolutePath()
124 				+ "/atg/dynamo/service/jdbc"), MonitoredDataSource.class,
125 				settings);
126 	}
127 
128 	/***
129 	 * 
130 	 * @param root
131 	 * @throws IOException
132 	 */
133 	private void createIdGenerator(final File root) throws IOException {
134 		this.settings.clear();
135 		settings.put("dataSource", "/atg/dynamo/service/jdbc/JTDataSource");
136 		settings.put("transactionManager",
137 				"/atg/dynamo/transaction/TransactionManager");
138 		settings.put("XMLToolsFactory",
139 				"/atg/dynamo/service/xml/XMLToolsFactory");
140 		FileUtil.createPropertyFile("IdGenerator", new File(root
141 				.getAbsolutePath()
142 				+ "/atg/dynamo/service/"), SQLIdGenerator.class, settings);
143 	}
144 
145 	/***
146 	 * 
147 	 * @param root
148 	 * @throws IOException
149 	 */
150 	private void createIdSpaces(final File root) throws IOException {
151 		final String idspaces = "<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE id-spaces SYSTEM \"http://www.atg.com/dtds/idgen/idgenerator_1.0.dtd\"><id-spaces><id-space name=\"__default__\" seed=\"1\" batch-size=\"100000\"/><id-space name=\"jms_msg_ids\" seed=\"0\" batch-size=\"10000\"/><id-space name=\"layer\" seed=\"0\" batch-size=\"100\"/></id-spaces>";
152 		final File idspacesFile = new File(root.getAbsolutePath()
153 				+ "/atg/dynamo/service/idspaces.xml");
154 
155 		idspacesFile.delete();
156 		idspacesFile.getParentFile().mkdirs();
157 		idspacesFile.createNewFile();
158 		FileWriter out = new FileWriter(idspacesFile);
159 		out.write(idspaces);
160 		out.write("\n");
161 		out.flush();
162 		out.close();
163 	}
164 
165 	/***
166 	 * 
167 	 * @param root
168 	 * @param repositoryPath
169 	 * @param droptables
170 	 *            <code>true</code> then existing tables will be dropped after
171 	 *            the test run, if <code>false</code> then leave the existing
172 	 *            tables alone
173 	 * @param createTables
174 	 *            if set to <code>true</code> all non existing tables needed for
175 	 *            the current test run will be created, if set to
176 	 *            <code>false</code> this class expects all needed tables for
177 	 *            this test run are already created
178 	 * @param definitionFiles
179 	 * @throws IOException
180 	 */
181 	public void createRepositoryConfiguration(final File root,
182 			final String repositoryPath, final boolean droptables,
183 			final boolean createTables, final String... definitionFiles)
184 			throws IOException {
185 
186 		this.settings.clear();
187 
188 		final StringBuilder defFiles = new StringBuilder();
189 		for (int i = 0; i < definitionFiles.length; i++) {
190 			defFiles.append("/" + definitionFiles[i]);
191 		}
192 		settings.put("definitionFiles", defFiles.toString());
193 
194 		settings.put("XMLToolsFactory",
195 				"/atg/dynamo/service/xml/XMLToolsFactory");
196 		settings.put("transactionManager",
197 				"/atg/dynamo/transaction/TransactionManager");
198 		settings.put("idGenerator", "/atg/dynamo/service/IdGenerator");
199 		settings.put("dataSource", "/atg/dynamo/service/jdbc/JTDataSource");
200 
201 		settings.put("lockManager", "/atg/dynamo/service/ClientLockManager");
202 		settings.put("idspaces", "/atg/dynamo/service/idspaces.xml");
203 		settings.put("groupContainerPath", "/atg/registry/RepositoryGroups");
204 		settings.put("restartingAfterTableCreation", "false");
205 		settings.put("createTables", Boolean.toString(createTables));
206 		settings.put("loggingError", "true");
207 		settings.put("loggingDebug", isDebug);
208 		settings.put("loggingCreateTables", isDebug);
209 		settings.put("debugLevel", "7");
210 
211 		// InitializingGSA specific properties
212 		settings.put("dropTablesIfExist", Boolean.toString(droptables));
213 		settings.put("dropTablesAtShutdown", Boolean.toString(droptables));
214 		settings.put("stripReferences", "true");
215 		final int endIndex = repositoryPath.lastIndexOf("/");
216 		final String repositoryDir = repositoryPath.substring(0, endIndex);
217 		final String repositoryName = repositoryPath.substring(endIndex + 1,
218 				repositoryPath.length());
219 		final File newRoot = new File(root, repositoryDir);
220 		newRoot.mkdirs();
221 		FileUtil.createPropertyFile(repositoryName, newRoot,
222 				InitializingGSA.class, settings);
223 	}
224 
225 	/***
226 	 * 
227 	 * @param root
228 	 * @throws IOException
229 	 */
230 	private void createSQLRepositoryEventServer(final File root)
231 			throws IOException {
232 		this.settings.clear();
233 		settings.put("handlerCount", "0");
234 		FileUtil.createPropertyFile("SQLRepositoryEventServer", new File(root
235 				.getAbsolutePath()
236 				+ "/atg/dynamo/server"), GSAEventServer.class, settings);
237 	}
238 
239 	/***
240 	 * 
241 	 * @param root
242 	 * @throws IOException
243 	 */
244 	private void createTransactionManager(final File root) throws IOException {
245 		this.settings.clear();
246 		settings.put("loggingDebug", isDebug);
247 		final File newRoot = new File(root, "/atg/dynamo/transaction");
248 		newRoot.mkdirs();
249 		FileUtil.createPropertyFile("TransactionDemarcationLogging", newRoot,
250 				TransactionDemarcationLogging.class, settings);
251 
252 		FileUtil.createPropertyFile("TransactionManager", newRoot,
253 				TransactionManagerImpl.class, settings);
254 	}
255 
256 	/***
257 	 * 
258 	 * @param root
259 	 * @throws IOException
260 	 */
261 	private void createUserTransaction(final File root) throws IOException {
262 		this.settings.clear();
263 		settings.put("transactionManager",
264 				"/atg/dynamo/transaction/TransactionManager");
265 		FileUtil
266 				.createPropertyFile("UserTransaction", new File(root,
267 						"/atg/dynamo/transaction"), UserTransactionImpl.class,
268 						settings);
269 	}
270 
271 }