1 package org.objectweb.jonas.tools.migration.jboss; 2 3 import java.io.*; 4 import java.util.*; 5 import javax.xml.parsers.*; 6 import org.w3c.dom.*; 7 import org.xml.sax.*; 8 9 14 15 public class Migrate extends Transformer { 16 17 private Document m_ejbjar; 18 private Document m_jboss; 19 private Document m_jbosscmp; 20 21 public Migrate(Document ejbjar, Document jboss, Document jbosscmp) { 22 m_ejbjar = ejbjar; 23 m_jboss = jboss; 24 m_jbosscmp = jbosscmp; 25 } 26 27 private Collection getBeanNames(String type) { 28 List docs = new ArrayList(); 29 docs.add(m_ejbjar); 30 docs.add(m_jboss); 31 docs.add(m_jbosscmp); 32 33 LinkedHashSet result = new LinkedHashSet(); 34 for (int i = 0; i < docs.size(); i++) { 35 Document doc = (Document) docs.get(i); 36 Node root = doc.getDocumentElement(); 37 if (root == null) { continue; } 38 result.addAll(values(root, 39 "enterprise-beans/" + type + "/ejb-name")); 40 } 41 return result; 42 } 43 44 private Node getBean(Document doc, final String name) { 45 Node root = doc.getDocumentElement(); 46 if (root == null) { return null; } 47 LinkedHashSet result = new LinkedHashSet(); 48 String [] paths = {"enterprise-beans/session", 49 "enterprise-beans/entity", 50 "enterprise-beans/message-driven"}; 51 for (int i = 0; i < paths.length; i++) { 52 query(root, paths[i], result, new NodeFilter() { 53 public boolean accept(Node node) { 54 return name.equals(value(node, "ejb-name")); 55 } 56 }); 57 } 58 return singleton(result); 59 } 60 61 public void transform() { 62 open("jonas-ejb-jar"); 63 64 text(LINE); 65 text(LINE); 66 comment(" this file is autogenerated by jboss2jonas "); 67 text(LINE); 68 69 Collection ssn = getBeanNames("session"); 70 for (Iterator it = ssn.iterator(); it.hasNext(); ) { 71 String name = (String ) it.next(); 72 session(name); 73 } 74 75 Collection entity = getBeanNames("entity"); 76 if (!entity.isEmpty()) { text(LINE); } 77 for (Iterator it = entity.iterator(); it.hasNext(); ) { 78 String name = (String ) it.next(); 79 entity(name); 80 } 81 82 Collection message = getBeanNames("message-driven"); 83 if (!message.isEmpty()) { text(LINE); } 84 for (Iterator it = message.iterator(); it.hasNext(); ) { 85 String name = (String ) it.next(); 86 message(name); 87 } 88 89 Collection relations = 90 nodes(m_jbosscmp, "jbosscmp-jdbc/relationships/ejb-relation"); 91 if (!relations.isEmpty()) { text(LINE); } 92 rename(relations, new Mapper() 93 .rename("ejb-relation", "jonas-ejb-relation") 94 .copy("ejb-relation-name") 95 .remove("relation-table-mapping") 96 .rename("table-name", "jdbc-table-name") 97 .rename("ejb-relationship-role", "jonas-ejb-relationship-role") 98 .copy("ejb-relationship-role-name") 99 .remove("key-fields") 100 .remove("key-field") 101 .rename("column-name", "foreign-key-jdbc-name")); 102 text(LINE); 103 close(); 104 } 105 106 private void openBean(String type, String name) { 107 open("jonas-" + type); 108 tag("ejb-name", name); 109 110 Node bean = getBean(m_jboss, name); 111 if (bean == null) { return; } 112 113 tag("jndi-name", value(bean, "jndi-name")); 114 tag("jndi-local-name", value(bean, "local-jndi-name")); 115 tag("run-as", value(bean, "security-identity/run-as-principal")); 116 117 rename(nodes(bean, "resource-ref"), new Mapper() 118 .rename("resource-ref", "jonas-resource") 119 .copy("res-ref-name") 120 .copy("jndi-name") 121 .rename("resource-name", "jndi-name") 122 .rename("res-url", "jndi-name")); 123 124 rename(nodes(bean, "ejb-ref"), new Mapper() 125 .rename("ejb-ref", "jonas-ejb-ref") 126 .copy("ejb-ref-name") 127 .copy("jndi-name")); 128 129 rename(nodes(bean, "resource-env-ref"), new Mapper() 130 .rename("resource-env-ref", "jonas-resource-env") 131 .copy("resource-env-ref-name") 132 .copy("jndi-name")); 133 } 134 135 private void session(String name) { 136 openBean("session", name); 137 close(); 138 } 139 140 private String value(Node primary, Node secondary, String path) { 141 if (primary == null) { return null; } 142 String result = value(primary, path); 143 if (result != null) { return result; } 144 if (secondary == null) { return null; } 145 return value(secondary, path); 146 } 147 148 private boolean isTrue(String value, boolean def) { 149 if (value == null || value.equals("")) { return def; } 150 return value.equalsIgnoreCase("true"); 151 } 152 153 private String ds2jndi(String ds) { 154 if (ds == null) { return "jdbc_1"; } 155 if (ds.equals("java:/DefaultDS")) { 156 return "jdbc_1"; 157 } else { 158 return ds; 159 } 160 } 161 162 private void entity(String name) { 163 openBean("entity", name); 164 165 Node bean = getBean(m_jbosscmp, name); 166 167 open("jdbc-mapping"); 168 Node defaults = node(m_jbosscmp, "jbosscmp-jdbc/defaults"); 169 boolean create = isTrue(value(bean, defaults, "create-table"), true); 171 boolean remove = isTrue(value(bean, defaults, "remove-table"), false); 172 if (remove) { 173 tag("cleanup", "removeall"); 174 } else if (!create) { 175 tag("cleanup", "none"); 176 } 177 178 tag("jndi-name", ds2jndi(value(bean, defaults, "datasource"))); 179 tag("jdbc-table-name", value(bean, "table-name")); 180 181 if (bean != null) { 182 rename(nodes(bean, "cmp-field"), new Mapper() 183 .rename("cmp-field", "cmp-field-jdbc-mapping") 184 .copy("field-name") 185 .rename("column-name", "jdbc-field-name") 186 .copy("sql-type")); 189 } 190 close(); 191 192 close(); 193 } 194 195 private void message(String name) { 196 openBean("message-driven", name); 197 Node bean = getBean(m_jboss, name); 198 if (bean != null) { 199 String jndi = value(bean, "destination-jndi-name"); 200 if (!isEmpty(jndi)) { 201 open("jonas-message-driven-destination"); 202 tag("jndi-name", jndi); 203 close(); 204 } 205 rename(node(bean, "activation-config"), new Mapper() 206 .copy("activation-config") 207 .copy("activation-config-property") 208 .copy("activation-config-property-name") 209 .copy("activation-config-property-value")); 210 } 211 close(); 212 } 213 214 private static final Map ARGS = new LinkedHashMap(); 215 216 private static final String arg(String arg, String usage) { 217 ARGS.put(arg, usage); 218 return arg; 219 } 220 221 private static final String HELP = 222 arg("--help", "display usage information"); 223 private static final String EJBJAR = 224 arg("--ejb-jar", "the path to the ejb-jar.xml file"); 225 private static final String JBOSS = 226 arg("--jboss", "the path to the jboss.xml file"); 227 private static final String JBOSSCMP = 228 arg("--jbosscmp-jdbc", "the path to the jbosscmp-jdbc.xml file"); 229 230 private static void usage(PrintStream out) { 231 out.println("Usage: jboss2jonas <options> > jonas-ejb-jar.xml"); 232 out.println(); 233 out.println(" This utility can be used to generate a JOnAS specific deployment "); 234 out.println(" descriptor based on an ejb-jar file and various JBoss specific "); 235 out.println(" configuration files."); 236 out.println(); 237 out.println("Options:"); 238 for (Iterator it = ARGS.entrySet().iterator(); it.hasNext(); ) { 239 Map.Entry me = (Map.Entry) it.next(); 240 String arg = (String ) me.getKey(); 241 String usage = (String ) me.getValue(); 242 out.println(); 243 out.println(" " + arg); 244 out.println(" " + usage); 245 } 246 } 247 248 public static final void main(String [] args) { 249 List unprocessed = new ArrayList(Arrays.asList(args)); 250 Map files = new HashMap(); 251 while (!unprocessed.isEmpty()) { 252 String arg = (String ) unprocessed.remove(0); 253 if (!ARGS.containsKey(arg)) { 254 System.err.println("unrecognized arg: " + arg); 255 System.exit(1); 256 } 257 if (arg.equals(HELP)) { 258 usage(System.out); 259 System.exit(0); 260 } else if (unprocessed.isEmpty()) { 261 System.err.println(arg + ": requires file"); 262 System.exit(1); 263 } 264 files.put(arg, unprocessed.remove(0)); 265 } 266 if (files.size() == 0) { 267 usage(System.err); 268 System.exit(1); 269 } 270 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 271 DocumentBuilder db; 272 try { 273 db = dbf.newDocumentBuilder(); 274 } catch (ParserConfigurationException e) { 275 throw new RuntimeException (e); 276 } 277 Document ejbjar = parse(db, (String ) files.get(EJBJAR)); 278 Document jboss = parse(db, (String ) files.get(JBOSS)); 279 Document jbosscmp = parse(db, (String ) files.get(JBOSSCMP)); 280 Migrate mig = new Migrate(ejbjar, jboss, jbosscmp); 281 mig.transform(); 282 System.out.println(mig.getDocument().getDocumentElement()); 283 } 284 285 private static final Document parse(DocumentBuilder db, String file) { 286 if (file == null) { 287 return db.newDocument(); 288 } else { 289 try { 290 return db.parse(new File(file)); 291 } catch (SAXException e) { 292 System.err.println(file + ": " + e.getMessage()); 293 System.exit(1); 294 return null; 295 } catch (IOException e) { 296 System.err.println(e.getMessage()); 297 System.exit(1); 298 return null; 299 } 300 } 301 } 302 303 } 304 | Popular Tags |