001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.serializer;
018:
019: import java.io.File;
020: import java.io.FilenameFilter;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Map;
024: import java.util.StringTokenizer;
025:
026: import org.apache.commons.configuration.PropertiesConfiguration;
027: import org.apache.jetspeed.components.jndi.SpringJNDIStarter;
028: import org.apache.log4j.Level;
029: import org.apache.log4j.Logger;
030:
031: /**
032: * Jetspeed Serializer Application
033: *
034: * invoke with mandatory
035: * <p>-E filename or -I filename to denote the export or the import file</p>
036: * <p>-I filename | directory, if a directory will process all XML files of pattern "*seed.xml"</p>
037: *
038: * invoke with (optional) parameters as
039: * <p>-p propertyFilename : overwrite the default filename defined in System.getProperty JetSpeed.Serializer.Configuration</p>
040: * <p>-a ApplicationPath : overwrite the default ./ or ApplicationPath property in properties file)</p>
041: * <p>-b bootPath : directory to Spring boot files, overwrite the default assembly/boot/ or bootPath property in properties file)</p>
042: * <p>-c configPath : directory to Spring config files, overwrite the default assembly/ or configPath property in properties file)</p>
043: *
044: * <p>-O optionstring : overwrite default "ALL,REPLACE"</p>
045: * <p>optionstring:
046: * ALL - extract/import all (with exception of PREFERENCES)
047: * USER - extract/import users, groups, roles
048: * CAPABILITIES - extract/import capabilities
049: * PROFILE = extract/import profile settings (for export requires USER)
050: * PERMISSIONS = extract/import permissions
051: * PREFS = extract/import portlet preferences (ignored if any of the above is set)
052: *
053: * NOOVERWRITE = don't overwrite existing file (for export)
054: * BACKUP = backup before process
055: * </p>
056: * <p>
057: * -dc driverClass, for example com.mysql.jdbc.Driver
058: * </p>
059: * <p>
060: * -ds url, ruls according to the driver used, URL needs to point to the correct
061: * database
062: * </p>
063: * <p>
064: * -du user, user with create/drop etc. rights on the database
065: * </p>
066: * <p>
067: * -dp password
068: * </p>
069: *
070: * <p>
071: * -l log4j-level, ERROR (default), WARN, INFO
072: * </p>
073: *
074: * @author <a href="mailto:hajo@bluesunrise.com">Hajo Birthelmer</a>
075: * @version $Id: $
076: */
077: public class JetspeedSerializerApplication implements
078: JetspeedSerializerFactory {
079: public static final String JNDI_DS_NAME = "jetspeed";
080:
081: public JetspeedSerializerApplication() {
082: }
083:
084: public JetspeedSerializer create(String serializerType) {
085: if (serializerType.equals(JetspeedSerializerFactory.SECONDARY))
086: return new JetspeedSerializerSecondaryImpl();
087: else
088: return new JetspeedSerializerImpl();
089: }
090:
091: public static void main(String[] args) {
092: String propertyFileName = null;
093:
094: String fileName = null; // XML filename - mandatory on command line
095: String applicationPath = null; // configuration.getProperties("applicationPath");
096: String bootConfigFiles = null; // configuration.getProperties("bootConfigFiles");
097: String configFiles = null; // configuration.getProperties("configFiles");
098:
099: String name = null;
100:
101: String options = null;
102:
103: PropertiesConfiguration configuration = null;
104:
105: String defaultIndent = null;
106:
107: String driverClass = null; // jdbc driver
108: String url = null; // jdbc url to database
109: String user = null; // user
110: String password = null; // password
111:
112: String logLevel = null;
113:
114: boolean doImport = false;
115: boolean doExport = false;
116:
117: if (args == null)
118: throw new IllegalArgumentException(
119: "Either import or export have to be defined (-I or -E follwoed by the filename");
120:
121: // Parse all the command-line arguments
122: for (int n = 0; n < args.length; n++) {
123: if (args[n].equals("-p"))
124: propertyFileName = args[++n];
125: else if (args[n].equals("-a"))
126: applicationPath = args[++n];
127: else if (args[n].equals("-b"))
128: bootConfigFiles = args[++n];
129: else if (args[n].equals("-c"))
130: configFiles = args[++n];
131: else if (args[n].equals("-E")) {
132: doExport = true;
133: fileName = args[++n];
134: } else if (args[n].equals("-I")) {
135: doImport = true;
136: fileName = args[++n];
137: } else if (args[n].equals("-N")) {
138: name = args[++n];
139: } else if (args[n].equals("-l"))
140: logLevel = args[++n];
141: else if (args[n].equals("-O"))
142: options = args[++n];
143: else if (args[n].equals("-dc"))
144: driverClass = args[++n];
145: else if (args[n].equals("-ds"))
146: url = args[++n];
147: else if (args[n].equals("-du")) {
148: if (((n + 1) >= args.length)
149: || args[n + 1].startsWith("-d")) {
150: user = "";
151: } else {
152: user = args[++n];
153: }
154: } else if (args[n].equals("-dp")) {
155: if (((n + 1) >= args.length)
156: || args[n + 1].startsWith("-d")) {
157: password = "";
158: } else {
159: password = args[++n];
160: }
161: } else {
162: throw new IllegalArgumentException("Unknown argument: "
163: + args[n]);
164: }
165: }
166:
167: /** The only required argument is the filename for either export or import*/
168: if ((!doImport) && (!doExport))
169: throw new IllegalArgumentException(
170: "Either import or export have to be defined (-I or -E follwoed by the filename");
171:
172: /** But not both*/
173: if ((doImport) && (doExport))
174: throw new IllegalArgumentException(
175: "Only one - either import or export - can be requested");
176:
177: if (name == null)
178: name = fileName;
179:
180: /** get system property definition */
181: if (propertyFileName == null)
182: propertyFileName = System.getProperty(
183: "org.apache.jetspeed.xml.importer.configuration",
184: null);
185:
186: if (propertyFileName != null) {
187: try {
188: configuration = new PropertiesConfiguration(
189: propertyFileName);
190: } catch (Exception e) {
191: e.printStackTrace();
192: System.exit(1);
193: }
194: if (configuration != null) {
195: /** only read what was not defined on the command line */
196:
197: if (applicationPath == null)
198: applicationPath = configuration
199: .getString("applicationPath");
200: if (bootConfigFiles == null)
201: bootConfigFiles = configuration
202: .getString("bootConfigFiles");
203: if (configFiles == null)
204: configFiles = configuration
205: .getString("configFiles");
206: if (options == null)
207: options = configuration.getString("options");
208: if (defaultIndent == null)
209: defaultIndent = configuration
210: .getString("defaultIndent");
211:
212: if (driverClass == null)
213: driverClass = configuration
214: .getString("driverClass");
215: if (url == null)
216: url = configuration.getString("url");
217: if (user == null)
218: user = configuration.getString("user");
219: if (password == null)
220: password = configuration.getString("password");
221: if (logLevel == null)
222: logLevel = configuration.getString("loglevel");
223:
224: }
225: }
226:
227: // if we still miss some settings, use hardoced defaults
228: if (applicationPath == null)
229: applicationPath = "./";
230: if (bootConfigFiles == null)
231: bootConfigFiles = "assembly/boot/";
232: if (configFiles == null)
233: configFiles = "assembly/";
234: if (logLevel == null)
235: logLevel = "ERROR";
236:
237: bootConfigFiles = bootConfigFiles + "*.xml";
238: configFiles = configFiles + "*.xml";
239:
240: // ok - we are ready to rumble....
241:
242: /** create the instruction map */
243:
244: Map settings = null;
245: int processHelper = 1; // default process SEED
246: if (options != null) {
247: settings = new HashMap();
248: settings.put(JetspeedSerializer.KEY_PROCESS_USERS,
249: Boolean.FALSE);
250: settings.put(JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
251: Boolean.FALSE);
252: settings.put(JetspeedSerializer.KEY_PROCESS_PROFILER,
253: Boolean.FALSE);
254: settings.put(
255: JetspeedSerializer.KEY_PROCESS_USER_PREFERENCES,
256: Boolean.FALSE);
257: settings.put(JetspeedSerializer.KEY_OVERWRITE_EXISTING,
258: Boolean.TRUE);
259: settings.put(JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
260: Boolean.FALSE);
261: String[] optionSet = getTokens(options);
262:
263: processHelper = 0;
264:
265: for (int i = 0; i < optionSet.length; i++) {
266: String o = optionSet[i];
267: if (o.equalsIgnoreCase("all")) {
268: settings.put(JetspeedSerializer.KEY_PROCESS_USERS,
269: Boolean.TRUE);
270: settings
271: .put(
272: JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
273: Boolean.TRUE);
274: settings.put(
275: JetspeedSerializer.KEY_PROCESS_PROFILER,
276: Boolean.TRUE);
277: settings.put(
278: JetspeedSerializer.KEY_PROCESS_PERMISSIONS,
279: Boolean.TRUE);
280: settings
281: .put(
282: JetspeedSerializer.KEY_PROCESS_USER_PREFERENCES,
283: Boolean.FALSE);
284: processHelper = 1;
285: } else if (o.equalsIgnoreCase("user")) {
286: settings.put(JetspeedSerializer.KEY_PROCESS_USERS,
287: Boolean.TRUE);
288: processHelper = 1;
289: } else if (o.equalsIgnoreCase("PREFS")) {
290: settings
291: .put(
292: JetspeedSerializer.KEY_PROCESS_USER_PREFERENCES,
293: Boolean.TRUE);
294: processHelper = 2;
295: } else if (o.equalsIgnoreCase("CAPABILITIES")) {
296: settings
297: .put(
298: JetspeedSerializer.KEY_PROCESS_CAPABILITIES,
299: Boolean.TRUE);
300: processHelper = 1;
301: } else if (o.equalsIgnoreCase("PROFILE")) {
302: settings.put(
303: JetspeedSerializer.KEY_PROCESS_PROFILER,
304: Boolean.TRUE);
305: processHelper = 1;
306: } else if (o.equalsIgnoreCase("PERMISSIONS")) {
307: settings.put(
308: JetspeedSerializer.KEY_PROCESS_PERMISSIONS,
309: Boolean.TRUE);
310: processHelper = 1;
311: } else if (o.equalsIgnoreCase("NOOVERWRITE"))
312: settings.put(
313: JetspeedSerializer.KEY_OVERWRITE_EXISTING,
314: Boolean.FALSE);
315: else if (o.equalsIgnoreCase("BACKUP"))
316: settings
317: .put(
318: JetspeedSerializer.KEY_BACKUP_BEFORE_PROCESS,
319: Boolean.TRUE);
320:
321: }
322:
323: }
324: JetspeedSerializer serializer = null;
325:
326: if (driverClass == null)
327: driverClass = System.getProperty(
328: "org.apache.jetspeed.database.driverClass",
329: "com.mysql.jdbc.Driver");
330: if (url == null)
331: url = System.getProperty(
332: "org.apache.jetspeed.database.url",
333: "jdbc:mysql://localhost/j2test");
334: if (user == null)
335: user = System.getProperty(
336: "org.apache.jetspeed.database.user", "user");
337: if (password == null)
338: password = System
339: .getProperty(
340: "org.apache.jetspeed.database.password",
341: "password");
342:
343: if (driverClass == null)
344: throw new IllegalArgumentException(
345: "Can't proceed without a valid driver");
346: if (url == null)
347: throw new IllegalArgumentException(
348: "Can't proceed without a valid url to the target database");
349: if (user == null)
350: throw new IllegalArgumentException(
351: "Can't proceed without a valid database user");
352:
353: HashMap context = new HashMap();
354:
355: context.put(SpringJNDIStarter.DATASOURCE_DRIVER, driverClass);
356: context.put(SpringJNDIStarter.DATASOURCE_URL, url);
357: context.put(SpringJNDIStarter.DATASOURCE_USERNAME, user);
358: context.put(SpringJNDIStarter.DATASOURCE_PASSWORD, password);
359:
360: Logger logger = Logger.getLogger("org.springframework");
361: Level level = logger.getLevel();
362: if (logLevel.equalsIgnoreCase("INFO"))
363: logger.setLevel(Level.INFO);
364: else if (logLevel.equalsIgnoreCase("WARN"))
365: logger.setLevel(Level.WARN);
366: else
367: logger.setLevel(Level.ERROR);
368:
369: /**
370: * set the application root
371: */
372: System.out.println("APP ROOT is " + applicationPath);
373: System.setProperty("applicationRoot", applicationPath);
374: System.setProperty("portal.name", "jetspped");
375: SpringJNDIStarter starter = new SpringJNDIStarter(context,
376: applicationPath, getTokens(bootConfigFiles),
377: getTokens(configFiles));
378:
379: System.out.println("starter framework created " + starter);
380:
381: try {
382: starter.setUp();
383: } catch (Exception e) {
384: e.printStackTrace();
385: System.exit(1);
386: }
387: System.out.println("starter framework established " + starter);
388: String[] importList = null;
389:
390: if (doImport)
391: importList = parseFiles(fileName);
392:
393: if ((doImport) && (importList != null)
394: && (importList.length > 0)) {
395: for (int i = 0; i < importList.length; i++) {
396: try {
397: System.out.println("processing import "
398: + importList[i]);
399: if (processHelper == 2) {
400: serializer = new JetspeedSerializerSecondaryImpl(
401: starter.getComponentManager());
402: } else
403: serializer = new JetspeedSerializerImpl(starter
404: .getComponentManager());
405: serializer.importData(importList[i], settings);
406: System.out.println("processing import "
407: + importList[i] + " done");
408:
409: } catch (Exception e) {
410: System.err
411: .println("Failed to process XML import for "
412: + importList[i] + ":" + e);
413: e.printStackTrace();
414: } finally {
415: if (serializer != null)
416: serializer.closeUp();
417: }
418: }
419: }
420: if (doExport) {
421: try {
422: System.out.println("processing export to " + fileName);
423: if (processHelper == 2) {
424: serializer = new JetspeedSerializerSecondaryImpl(
425: starter.getComponentManager());
426: } else
427: serializer = new JetspeedSerializerImpl(starter
428: .getComponentManager());
429:
430: serializer.exportData(name, fileName, settings);
431: } catch (Exception e) {
432: System.err.println("Failed to process XML export of "
433: + fileName + ": " + e);
434: e.printStackTrace();
435: } finally {
436: if (serializer != null)
437: serializer.closeUp();
438: }
439:
440: }
441: try {
442: starter.tearDown();
443: logger.setLevel(level);
444: ;
445: } catch (Exception e1) {
446: System.out
447: .println("starter framework teardown caused exception "
448: + e1.getLocalizedMessage());
449: e1.printStackTrace();
450:
451: }
452: System.out.println("DONE performing "
453: + (doExport ? "export" : "import") + " with "
454: + fileName);
455: }
456:
457: /**
458: * process provided filename or directory name
459: *
460: * @return one or more files to be processed
461: */
462: static private String[] parseFiles(String schemaDirectory) {
463: String[] fileList = null;
464: try {
465: File dir = new File(schemaDirectory);
466: if (!(dir.exists())) {
467: return fileList;
468: }
469: if (!(dir.isDirectory())) {
470: fileList = new String[1];
471: fileList[0] = schemaDirectory;
472: return fileList;
473: }
474: // Handling a directory
475: File[] files = dir.listFiles(new FilenameFilter() {
476: public boolean accept(File dir, String name) {
477: String n = name.toLowerCase();
478: return n.endsWith("seed.xml");
479: }
480: });
481: if (files == null)
482: return fileList;
483:
484: fileList = new String[files.length];
485: for (int i = 0; i < files.length; i++) {
486: fileList[i] = files[i].getAbsolutePath();
487: }
488: return fileList;
489: } catch (Exception e) {
490: e.printStackTrace();
491: throw new IllegalArgumentException(
492: "Processing the schema-directory "
493: + schemaDirectory + " caused exception "
494: + e.getLocalizedMessage());
495: }
496:
497: }
498:
499: private static String[] getTokens(String _line) {
500: if ((_line == null) || (_line.length() == 0))
501: return null;
502:
503: StringTokenizer st = new StringTokenizer(_line, ",");
504: ArrayList list = new ArrayList();
505:
506: while (st.hasMoreTokens())
507: list.add(st.nextToken());
508: String[] s = new String[list.size()];
509: for (int i = 0; i < list.size(); i++)
510: s[i] = (String) list.get(i);
511: return s;
512: }
513:
514: }
|