001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.tools.importexport.util;
017:
018: import org.outerj.daisy.util.VersionHelper;
019: import org.outerj.daisy.xmlutil.DocumentHelper;
020: import org.outerj.daisy.repository.Repository;
021: import org.outerj.daisy.repository.RepositoryException;
022: import org.w3c.dom.Element;
023:
024: import java.util.*;
025: import java.io.*;
026: import java.text.SimpleDateFormat;
027: import java.text.DateFormat;
028: import java.lang.reflect.Method;
029: import java.lang.reflect.Modifier;
030:
031: public class ImportExportUtil {
032: public static Properties getVersionProperties() throws IOException {
033: return VersionHelper
034: .getVersionProperties(ImportExportUtil.class
035: .getClassLoader(),
036: "org/outerj/daisy/tools/importexport/versioninfo.properties");
037: }
038:
039: public static void logError(Throwable throwable,
040: String baseFileName, PrintStream out) {
041: out.println();
042: out.println("*** An error occured ***");
043: out.println();
044:
045: Throwable current = throwable;
046: do {
047: out.println(current.getMessage());
048: current = current.getCause();
049: } while (current != null);
050: out.println();
051:
052: try {
053: File logFile;
054: SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat
055: .getDateTimeInstance();
056: dateFormat.applyPattern("yyyyMMddHHmmss");
057: String baseName = baseFileName + "_"
058: + dateFormat.format(new Date());
059: int c = 1;
060: while (true) {
061: File file = new File(baseName + c + ".txt");
062: if (file.createNewFile()) {
063: logFile = file;
064: break;
065: }
066: c++;
067: }
068:
069: FileOutputStream fos = new FileOutputStream(logFile);
070: PrintWriter writer = new PrintWriter(fos);
071: throwable.printStackTrace(writer);
072: writer.close();
073: fos.close();
074: out.println("Error details written to "
075: + logFile.getAbsolutePath());
076: out.println();
077: } catch (Throwable e) {
078: // Something fails writing the error to a file, just dump it on standard out then
079: throwable.printStackTrace(System.out);
080: }
081: }
082:
083: public static Object useFactory(Element element,
084: Repository repository) throws Exception {
085: String factoryClassName = DocumentHelper.getAttribute(element,
086: "factoryClass", true);
087: Class factoryClass = ImportExportUtil.class.getClassLoader()
088: .loadClass(factoryClassName);
089: Method createMethod = factoryClass.getMethod("create",
090: Element.class, Repository.class);
091: if (!Modifier.isStatic(createMethod.getModifiers())) {
092: throw new Exception("The create method of class "
093: + factoryClassName + " is not static.");
094: }
095: return createMethod.invoke(null, element, repository);
096: }
097:
098: public static long[] parseRoles(String roles, Repository repository)
099: throws RepositoryException {
100: List<Long> roleIdList = new ArrayList<Long>(5);
101: StringTokenizer tokenizer = new StringTokenizer(roles, ",");
102: while (tokenizer.hasMoreTokens()) {
103: String token = tokenizer.nextToken().trim();
104: if (token.length() > 0) {
105: roleIdList.add(repository.getUserManager().getRole(
106: token, false).getId());
107: }
108: }
109:
110: long[] roleIds = new long[roleIdList.size()];
111: for (int i = 0; i < roleIds.length; i++) {
112: roleIds[i] = roleIdList.get(i);
113: }
114: return roleIds;
115: }
116: }
|