KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genclientstub > modifier > AbsArchiveModifier


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbsArchiveModifier.java,v 1.2 2004/12/14 17:26:32 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_lib.genclientstub.modifier;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.objectweb.jonas_lib.genbase.GenBaseException;
32 import org.objectweb.jonas_lib.genbase.archive.Archive;
33 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
34 import org.objectweb.jonas_lib.genbase.archive.WebApp;
35 import org.objectweb.jonas_lib.genbase.generator.Config;
36 import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
37 import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;
38 import org.objectweb.jonas_lib.genclientstub.generator.Generator;
39
40 /**
41  * Defines common methods used by all modifier
42  */

43 public abstract class AbsArchiveModifier extends ArchiveModifier {
44
45     /**
46      * Constructor
47      * @param archive the j2ee archive
48      */

49     public AbsArchiveModifier(J2EEArchive archive) {
50         super(archive);
51     }
52
53     /**
54     * Modify the current archive and return a modified archive.
55      * @return a modified archive.
56      * @throws GenBaseException When Client Generation or storing phase fails.
57      */

58     public abstract Archive modify() throws GenBaseException;
59
60     /**
61      * Generates stub/tie classes for a given archive with its config
62      * @param config configuration to use for the generator
63      * @param archive the file containing the remote class
64      * @throws ClientStubGenException if the generation fails
65      */

66     protected void generateFoundStubs(Config config, Archive archive) throws ClientStubGenException {
67
68         // List of classes on which generate stubs
69
List JavaDoc classesStub = new ArrayList JavaDoc();
70         try {
71             List JavaDoc files = archive.getContainedFiles();
72
73             for (Iterator JavaDoc itFiles = files.iterator(); itFiles.hasNext();) {
74                 String JavaDoc clName = (String JavaDoc) itFiles.next();
75                 if (clName.endsWith("_Stub.class") || clName.endsWith("_Tie.class")) {
76                     // Extract classname from stub and add entry in the list
77
if (archive instanceof WebApp) {
78                         if (clName.startsWith("WEB-INF/classes/")) {
79                             classesStub.add(clName.substring("WEB-INF/classes/".length()));
80                         }
81                     } else {
82                         classesStub.add(clName);
83                     }
84                 }
85             }
86
87             // launch generation for all classes found
88
for (Iterator JavaDoc itClass = classesStub.iterator(); itClass.hasNext();) {
89                 String JavaDoc clName = (String JavaDoc) itClass.next();
90
91                 // Remove stub name
92
int removeStubIdx = 0;
93                 if (clName.endsWith("_Stub.class")) {
94                     removeStubIdx = clName.length() - "_Stub.class".length();
95                 } else if (clName.endsWith("_Tie.class")) {
96                     removeStubIdx = clName.length() - "_Tie.class".length();
97                 } else {
98                     continue;
99                 }
100                 clName = clName.substring(0, removeStubIdx);
101
102                 // Remove the _ from the className
103
int idx = clName.lastIndexOf('/') + 1;
104                 if (clName.charAt(idx) == '_') {
105                     String JavaDoc pkgName = clName.substring(0, idx);
106                     clName = pkgName + clName.substring(idx + 1, clName.length());
107                 }
108
109                 // Translate / into . (/ is in jar entry, classname should contain .)
110
clName = clName.replaceAll("/", ".");
111
112                 // javax object and existing omg stubs
113
if (clName.indexOf("javax.") != -1 || clName.indexOf("org.omg.stub") != -1) {
114                     continue;
115                 }
116
117
118                 // Doesn't take into account Home and Remote
119
if (clName.indexOf("Home") != -1 || clName.indexOf("Remote") != -1) {
120                     continue;
121                 }
122
123                 Generator g = new Generator(config, null, clName, archive);
124                 try {
125                     g.generate();
126                     g.compile();
127                 } catch (ClientStubGenException cstge) {
128                     continue;
129                 }
130                 // add files in archive
131
g.addFiles(archive);
132             }
133         } catch (Exception JavaDoc e) {
134             throw new ClientStubGenException("Cannot find/build stubs from the file " + archive.getRootFile(), e);
135         }
136
137     }
138
139 }
Popular Tags