Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5abe6ecab17029c91f0bff80d7797774a91a0891 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 *   Davide Marchignoli
 * </copyright>
 *
 * $Id: GenerateHBM.java,v 1.2 2007/02/01 12:35:55 mtaal Exp $
 */

package org.eclipse.emf.teneo.hibernate.mapper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.StoreException;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedModel;
import org.eclipse.emf.teneo.hibernate.hbannotation.util.MappingBuilder;

/**
 * Class is responsible for generating the hbm file. Is run through a launcher therefore the main methods.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.2 $
 */

public class GenerateHBM {
	/** The logger for this class */
	private static Log log = LogFactory.getLog(GenerateHBM.class);

	/** Parses the args and generates a jdo file */
	public static void main(String[] args) {
		// decode the args
		Properties options = new Properties();
		ArrayList ecores = new ArrayList();
		String targetFileName = null;
		for (int i = 0; i < args.length; i++) {
			if (i == 0) {
				targetFileName = args[0];
			} else if (args[i].startsWith("+")) { // option
				final String[] keyValue = args[i].substring(1).split(",");
				options.put(keyValue[0], keyValue[1]);
			} else if (args[i].startsWith("-")) { // epackage classnames
				final String[] epacks = args[i].substring(1).split(",");

				// the epackages load them all, hope for the best
				// when loading the epackage should be loaded
				for (int p = 0; p < epacks.length; p++) {
					try {
						log.debug("Loading class " + epacks[p] + " should be an epackage");

						Class epack = Class.forName(epacks[p]);
						if (!EPackage.class.isAssignableFrom(epack)) {
							log.warn("JDO generator found " + epack.getName()
									+ " but this is not an EPackage, ignoring it");
						}
					} catch (Throwable t) { // ignore everything but log it
						log.error("Exception while instantiating " + epacks[p] + ", message: " + t.getMessage());
					}
				}
			} else {
				ecores.add(args[i]);
			}
		}

		createORMapperFile(targetFileName, (String[]) ecores.toArray(new String[ecores.size()]), options);
	}

	/** Creates the mapping file */
	private static void createORMapperFile(String targetFileName, String[] ecores, Properties options) {
		try {
			// get the first ecore file
			File firstEcore = new File(ecores[0]);
			File file = new File(firstEcore.getParentFile(), targetFileName);
			final File archiveFile = new File(firstEcore.getParentFile(), targetFileName + "_old");

			if (file.exists()) {
				if (archiveFile.exists())
					archiveFile.delete();
				copyFile(file, archiveFile);
				file.delete();
			}
			file.createNewFile();

			final PersistenceOptions po = new PersistenceOptions(options);
			PAnnotatedModel paModel = MappingBuilder.INSTANCE.buildMapping(ecores, po);
			HibernateMappingGenerator hmg = new HibernateMappingGenerator(po);
			FileWriter writer = new FileWriter(file);
			writer.write(hmg.generateToString(paModel));
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace(System.err);
			log.error(e);
			throw new StoreException("IOException when creating or mapping file", e);
		}
	}

	/** Copies a file */
	public static void copyFile(File src, File dst) throws IOException {
		if (!dst.exists())
			dst.createNewFile();

		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dst);

		// Transfer bytes from in to out
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}
}

Back to the top