KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > Tools > CorrectOrdinaryAppl


1 /* $Id: CorrectOrdinaryAppl.java,v 1.1 2004/03/22 19:31:49 hnetynka Exp $ */
2 package SOFA.SOFAnode.Made.Tools;
3
4 /** Program that corrects ordinary (non component-based) application in order to it can call
5   * SOFA components. It augments java interfaces of components with correct versions.
6   * <br>
7   * <b>Arguments:</b><br>
8   * Components that application uses and all class files of the application (all can be mixed).
9   *
10   * @author Petr Hnetynka
11   */

12 public class CorrectOrdinaryAppl {
13
14
15   /** Main program. */
16   public static void main(String JavaDoc[] argv) {
17
18     java.util.Hashtable JavaDoc trTable = new java.util.Hashtable JavaDoc();
19     java.util.Hashtable JavaDoc verTable = new java.util.Hashtable JavaDoc();
20     java.util.ArrayList JavaDoc classes = new java.util.ArrayList JavaDoc();
21     
22     for (int i=0; i<argv.length; i++) {
23       if (argv[i].indexOf('[') != -1) {
24         // component name
25
SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor dd = getDeplDescr(argv[i], "index.dc");
26         java.util.Hashtable JavaDoc ent = dd.getCDLEntities();
27         for (java.util.Enumeration JavaDoc e = ent.keys(); e.hasMoreElements(); ) {
28           String JavaDoc javaName = (String JavaDoc) e.nextElement();
29           String JavaDoc fullName = (String JavaDoc) ent.get(javaName);
30           String JavaDoc specVersion = fullName.substring(fullName.indexOf('?')+1);
31           trTable.put(InstallToTR.toVMName(javaName), InstallToTR.toVMName(javaName+InstallToTR.specVersionToFileName(specVersion)));
32           verTable.put(InstallToTR.toVMName(javaName), InstallToTR.specVersionToFileName(specVersion));
33         }
34       } else if (argv[i].endsWith(".class")) {
35         // class name
36
classes.add(argv[i]);
37       } else {
38         // unknown name
39
System.out.println("Unrecognized type of name: "+argv[i]);
40         System.exit(1);
41       }
42     }
43     
44     SOFA.Tools.Resolver.Resolver resolver = new SOFA.Tools.Resolver.Resolver(trTable);
45     for (int i=0; i<classes.size(); i++) {
46       try {
47         String JavaDoc cur = (String JavaDoc) classes.get(i);
48         System.out.print("Correcting class \""+cur+"\"...");
49         String JavaDoc className = new SOFA.Tools.Resolver.ClassInfo(cur).className();
50         java.io.FileInputStream JavaDoc fis = new java.io.FileInputStream JavaDoc(cur);
51         byte [] b = resolver.process(fis);
52         fis.close();
53         String JavaDoc ver = (String JavaDoc) verTable.get(className);
54         if (ver != null) {
55           String JavaDoc old = cur;
56           cur = cur.replaceFirst("\\.class", ver+".class");
57           new java.io.File JavaDoc(old).delete();
58         }
59         java.io.FileOutputStream JavaDoc fos = new java.io.FileOutputStream JavaDoc(cur);
60         fos.write(b);
61         fos.close();
62         System.out.println("OK");
63       } catch (java.io.IOException JavaDoc e) {
64         System.out.println("IOException: "+e.getMessage());
65         System.exit(1);
66       }
67     }
68   }
69
70   static SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor getDeplDescr(String JavaDoc applname, String JavaDoc ddname) {
71     try {
72       String JavaDoc trString = System.getProperty("sofa.tr.url","file:/sofa/tr/");
73       String JavaDoc path = trString+"impl";
74       path = path + fullNameToFileName(applname, ddname);
75       java.net.URL JavaDoc url = new java.net.URL JavaDoc(path);
76 java.net.URLConnection JavaDoc con = url.openConnection();
77       java.io.InputStream JavaDoc is = con.getInputStream();
78       SOFA.SOFAnode.Run.Deployment.DeploymentDescriptor dd = new SOFA.SOFAnode.Run.Deployment.DeploymentDescriptorImpl(is);
79       is.close();
80       return dd;
81     } catch (java.net.MalformedURLException JavaDoc e) {
82       System.out.println("MalformedURLException: "+e.getMessage());
83       e.printStackTrace();
84       System.exit(1);
85     } catch (java.io.IOException JavaDoc e) {
86       System.out.println("IOException: "+e.getMessage());
87       System.exit(1);
88     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
89       System.out.println("ParserConfigurationExceptioni: "+e.getMessage() );
90       System.exit(1);
91     } catch (org.xml.sax.SAXException JavaDoc e) {
92       System.out.println("SAXException: "+e.getMessage() );
93       System.exit(1);
94     }
95     return null;
96   }
97
98   private static String JavaDoc fullNameToFileName(String JavaDoc clname, String JavaDoc ddname) {
99     clname = clname.replaceAll("::", "/");
100     clname = clname.replace('[', '/');
101     clname = clname.replace(']', '/');
102     clname = clname + ddname;
103     if (clname.charAt(0) != '/')
104       clname = "/" + clname;
105     return clname;
106   }
107 }
108
Popular Tags