001: package org.objectweb.jonas.tools.migration.jboss;
002:
003: import java.io.*;
004: import java.util.*;
005: import javax.xml.parsers.*;
006: import org.w3c.dom.*;
007: import org.xml.sax.*;
008:
009: /**
010: * Migrate
011: *
012: * @author Rafael H. Schloming <rhs@mit.edu>
013: **/
014:
015: public class Migrate extends Transformer {
016:
017: private Document m_ejbjar;
018: private Document m_jboss;
019: private Document m_jbosscmp;
020:
021: public Migrate(Document ejbjar, Document jboss, Document jbosscmp) {
022: m_ejbjar = ejbjar;
023: m_jboss = jboss;
024: m_jbosscmp = jbosscmp;
025: }
026:
027: private Collection getBeanNames(String type) {
028: List docs = new ArrayList();
029: docs.add(m_ejbjar);
030: docs.add(m_jboss);
031: docs.add(m_jbosscmp);
032:
033: LinkedHashSet result = new LinkedHashSet();
034: for (int i = 0; i < docs.size(); i++) {
035: Document doc = (Document) docs.get(i);
036: Node root = doc.getDocumentElement();
037: if (root == null) {
038: continue;
039: }
040: result.addAll(values(root, "enterprise-beans/" + type
041: + "/ejb-name"));
042: }
043: return result;
044: }
045:
046: private Node getBean(Document doc, final String name) {
047: Node root = doc.getDocumentElement();
048: if (root == null) {
049: return null;
050: }
051: LinkedHashSet result = new LinkedHashSet();
052: String[] paths = { "enterprise-beans/session",
053: "enterprise-beans/entity",
054: "enterprise-beans/message-driven" };
055: for (int i = 0; i < paths.length; i++) {
056: query(root, paths[i], result, new NodeFilter() {
057: public boolean accept(Node node) {
058: return name.equals(value(node, "ejb-name"));
059: }
060: });
061: }
062: return singleton(result);
063: }
064:
065: public void transform() {
066: open("jonas-ejb-jar");
067:
068: text(LINE);
069: text(LINE);
070: comment(" this file is autogenerated by jboss2jonas ");
071: text(LINE);
072:
073: Collection ssn = getBeanNames("session");
074: for (Iterator it = ssn.iterator(); it.hasNext();) {
075: String name = (String) it.next();
076: session(name);
077: }
078:
079: Collection entity = getBeanNames("entity");
080: if (!entity.isEmpty()) {
081: text(LINE);
082: }
083: for (Iterator it = entity.iterator(); it.hasNext();) {
084: String name = (String) it.next();
085: entity(name);
086: }
087:
088: Collection message = getBeanNames("message-driven");
089: if (!message.isEmpty()) {
090: text(LINE);
091: }
092: for (Iterator it = message.iterator(); it.hasNext();) {
093: String name = (String) it.next();
094: message(name);
095: }
096:
097: Collection relations = nodes(m_jbosscmp,
098: "jbosscmp-jdbc/relationships/ejb-relation");
099: if (!relations.isEmpty()) {
100: text(LINE);
101: }
102: rename(relations, new Mapper().rename("ejb-relation",
103: "jonas-ejb-relation").copy("ejb-relation-name").remove(
104: "relation-table-mapping").rename("table-name",
105: "jdbc-table-name").rename("ejb-relationship-role",
106: "jonas-ejb-relationship-role").copy(
107: "ejb-relationship-role-name").remove("key-fields")
108: .remove("key-field").rename("column-name",
109: "foreign-key-jdbc-name"));
110: text(LINE);
111: close();
112: }
113:
114: private void openBean(String type, String name) {
115: open("jonas-" + type);
116: tag("ejb-name", name);
117:
118: Node bean = getBean(m_jboss, name);
119: if (bean == null) {
120: return;
121: }
122:
123: tag("jndi-name", value(bean, "jndi-name"));
124: tag("jndi-local-name", value(bean, "local-jndi-name"));
125: tag("run-as", value(bean, "security-identity/run-as-principal"));
126:
127: rename(nodes(bean, "resource-ref"), new Mapper().rename(
128: "resource-ref", "jonas-resource").copy("res-ref-name")
129: .copy("jndi-name").rename("resource-name", "jndi-name")
130: .rename("res-url", "jndi-name"));
131:
132: rename(nodes(bean, "ejb-ref"), new Mapper().rename("ejb-ref",
133: "jonas-ejb-ref").copy("ejb-ref-name").copy("jndi-name"));
134:
135: rename(nodes(bean, "resource-env-ref"), new Mapper().rename(
136: "resource-env-ref", "jonas-resource-env").copy(
137: "resource-env-ref-name").copy("jndi-name"));
138: }
139:
140: private void session(String name) {
141: openBean("session", name);
142: close();
143: }
144:
145: private String value(Node primary, Node secondary, String path) {
146: if (primary == null) {
147: return null;
148: }
149: String result = value(primary, path);
150: if (result != null) {
151: return result;
152: }
153: if (secondary == null) {
154: return null;
155: }
156: return value(secondary, path);
157: }
158:
159: private boolean isTrue(String value, boolean def) {
160: if (value == null || value.equals("")) {
161: return def;
162: }
163: return value.equalsIgnoreCase("true");
164: }
165:
166: private String ds2jndi(String ds) {
167: if (ds == null) {
168: return "jdbc_1";
169: }
170: if (ds.equals("java:/DefaultDS")) {
171: return "jdbc_1";
172: } else {
173: return ds;
174: }
175: }
176:
177: private void entity(String name) {
178: openBean("entity", name);
179:
180: Node bean = getBean(m_jbosscmp, name);
181:
182: open("jdbc-mapping");
183: Node defaults = node(m_jbosscmp, "jbosscmp-jdbc/defaults");
184: // XXX: I'm guessing at these defaults
185: boolean create = isTrue(value(bean, defaults, "create-table"),
186: true);
187: boolean remove = isTrue(value(bean, defaults, "remove-table"),
188: false);
189: if (remove) {
190: tag("cleanup", "removeall");
191: } else if (!create) {
192: tag("cleanup", "none");
193: }
194:
195: tag("jndi-name", ds2jndi(value(bean, defaults, "datasource")));
196: tag("jdbc-table-name", value(bean, "table-name"));
197:
198: if (bean != null) {
199: rename(nodes(bean, "cmp-field"), new Mapper().rename(
200: "cmp-field", "cmp-field-jdbc-mapping").copy(
201: "field-name").rename("column-name",
202: "jdbc-field-name")
203: // XXX: not sure if this is the right mapping, jonas
204: // sql type may be the same as jboss's jdbc-type
205: .copy("sql-type"));
206: }
207: close();
208:
209: close();
210: }
211:
212: private void message(String name) {
213: openBean("message-driven", name);
214: Node bean = getBean(m_jboss, name);
215: if (bean != null) {
216: String jndi = value(bean, "destination-jndi-name");
217: if (!isEmpty(jndi)) {
218: open("jonas-message-driven-destination");
219: tag("jndi-name", jndi);
220: close();
221: }
222: rename(node(bean, "activation-config"), new Mapper().copy(
223: "activation-config").copy(
224: "activation-config-property").copy(
225: "activation-config-property-name").copy(
226: "activation-config-property-value"));
227: }
228: close();
229: }
230:
231: private static final Map ARGS = new LinkedHashMap();
232:
233: private static final String arg(String arg, String usage) {
234: ARGS.put(arg, usage);
235: return arg;
236: }
237:
238: private static final String HELP = arg("--help",
239: "display usage information");
240: private static final String EJBJAR = arg("--ejb-jar",
241: "the path to the ejb-jar.xml file");
242: private static final String JBOSS = arg("--jboss",
243: "the path to the jboss.xml file");
244: private static final String JBOSSCMP = arg("--jbosscmp-jdbc",
245: "the path to the jbosscmp-jdbc.xml file");
246:
247: private static void usage(PrintStream out) {
248: out.println("Usage: jboss2jonas <options> > jonas-ejb-jar.xml");
249: out.println();
250: out
251: .println(" This utility can be used to generate a JOnAS specific deployment ");
252: out
253: .println(" descriptor based on an ejb-jar file and various JBoss specific ");
254: out.println(" configuration files.");
255: out.println();
256: out.println("Options:");
257: for (Iterator it = ARGS.entrySet().iterator(); it.hasNext();) {
258: Map.Entry me = (Map.Entry) it.next();
259: String arg = (String) me.getKey();
260: String usage = (String) me.getValue();
261: out.println();
262: out.println(" " + arg);
263: out.println(" " + usage);
264: }
265: }
266:
267: public static final void main(String[] args) {
268: List unprocessed = new ArrayList(Arrays.asList(args));
269: Map files = new HashMap();
270: while (!unprocessed.isEmpty()) {
271: String arg = (String) unprocessed.remove(0);
272: if (!ARGS.containsKey(arg)) {
273: System.err.println("unrecognized arg: " + arg);
274: System.exit(1);
275: }
276: if (arg.equals(HELP)) {
277: usage(System.out);
278: System.exit(0);
279: } else if (unprocessed.isEmpty()) {
280: System.err.println(arg + ": requires file");
281: System.exit(1);
282: }
283: files.put(arg, unprocessed.remove(0));
284: }
285: if (files.size() == 0) {
286: usage(System.err);
287: System.exit(1);
288: }
289: DocumentBuilderFactory dbf = DocumentBuilderFactory
290: .newInstance();
291: DocumentBuilder db;
292: try {
293: db = dbf.newDocumentBuilder();
294: } catch (ParserConfigurationException e) {
295: throw new RuntimeException(e);
296: }
297: Document ejbjar = parse(db, (String) files.get(EJBJAR));
298: Document jboss = parse(db, (String) files.get(JBOSS));
299: Document jbosscmp = parse(db, (String) files.get(JBOSSCMP));
300: Migrate mig = new Migrate(ejbjar, jboss, jbosscmp);
301: mig.transform();
302: System.out.println(mig.getDocument().getDocumentElement());
303: }
304:
305: private static final Document parse(DocumentBuilder db, String file) {
306: if (file == null) {
307: return db.newDocument();
308: } else {
309: try {
310: return db.parse(new File(file));
311: } catch (SAXException e) {
312: System.err.println(file + ": " + e.getMessage());
313: System.exit(1);
314: return null;
315: } catch (IOException e) {
316: System.err.println(e.getMessage());
317: System.exit(1);
318: return null;
319: }
320: }
321: }
322:
323: }
|