KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2003-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: ModifierFactory.java,v 1.1 2004/10/11 13:16:15 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_lib.genclientstub.modifier;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.jar.JarFile JavaDoc;
32
33 import org.objectweb.jonas_lib.I18n;
34 import org.objectweb.jonas_lib.genbase.GenBaseException;
35 import org.objectweb.jonas_lib.genbase.archive.Application;
36 import org.objectweb.jonas_lib.genbase.archive.Archive;
37 import org.objectweb.jonas_lib.genbase.archive.Client;
38 import org.objectweb.jonas_lib.genbase.archive.EjbJar;
39 import org.objectweb.jonas_lib.genbase.archive.FileArchive;
40 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
41 import org.objectweb.jonas_lib.genbase.archive.JarArchive;
42 import org.objectweb.jonas_lib.genbase.archive.WebApp;
43 import org.objectweb.jonas_lib.genbase.modifier.AbsModifierFactory;
44 import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
45 import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;
46
47
48 /**
49  * Used to create the right ArchiveModifier from a given filename (ear, jar,
50  * war, ...)
51  *
52  * @author Guillaume Sauthier
53  */

54 public class ModifierFactory extends AbsModifierFactory {
55
56     /** i18n */
57     private static I18n i18n = I18n.getInstance(ModifierFactory.class);
58
59      /**
60      * Empty Constructor for utility class
61      */

62     private ModifierFactory() {
63         super();
64     }
65
66     /**
67      * Returns an <code>ArchiveModifier</code> according to archive type
68      * (application, ejbjar, webapp or client).
69      *
70      * @param filename input filename.
71      *
72      * @return an <code>ArchiveModifier</code>.
73      *
74      * @exception GenBaseException when archive creation fails.
75      */

76     public static ArchiveModifier getModifier(String JavaDoc filename) throws GenBaseException {
77         Archive archive = null;
78         J2EEArchive j2eeArchive = null;
79         ArchiveModifier modifier = null;
80         int mode;
81
82         File JavaDoc file = new File JavaDoc(filename);
83
84         if (file.exists()) {
85             if (file.isFile()) {
86                 // should be a jar file
87
// test jar file
88
JarFile JavaDoc jarfile;
89
90                 try {
91                     jarfile = new JarFile JavaDoc(file);
92                 } catch (IOException JavaDoc ioe) {
93                     // not a jar file
94
String JavaDoc err = i18n.getMessage("ModifierFactory.getModifier.notjar", filename);
95                     throw new ClientStubGenException(err);
96                 }
97
98                 // Create the inner Archive
99
archive = new JarArchive(file);
100
101                 // now we are sure that filename is a correct jar
102
if (isApplication(jarfile)) {
103                     // Application J2EE Archive
104
j2eeArchive = new Application(archive);
105                     mode = APPLICATION;
106
107                 } else if (isEjbJar(jarfile)) {
108                     // EjbJar J2EE Archive
109
j2eeArchive = new EjbJar(archive);
110                     mode = EJBJAR;
111
112                 } else if (isWebApp(jarfile)) {
113                     // WebApp J2EE Archive
114
j2eeArchive = new WebApp(archive);
115                     mode = WEBAPP;
116
117                 } else if (isClient(jarfile)) {
118                     // Client J2EE Archive
119
j2eeArchive = new Client(archive);
120                     mode = CLIENT;
121
122                 } else {
123                     // unsupported archive type
124
String JavaDoc err = i18n.getMessage("ModifierFactory.getModifier.unsupported", filename);
125                     throw new ClientStubGenException(err);
126                 }
127             } else {
128                 // directory unpacked
129
// Create the inner Archive
130
archive = new FileArchive(file);
131
132                 // now we are sure that filename is a correct jar
133
if (isApplication(file)) {
134                     // Application J2EE Archive
135
j2eeArchive = new Application(archive);
136                     mode = APPLICATION;
137
138                 } else if (isEjbJar(file)) {
139                     // EjbJar J2EE Archive
140
j2eeArchive = new EjbJar(archive);
141                     mode = EJBJAR;
142
143                 } else if (isWebApp(file)) {
144                     // WebApp J2EE Archive
145
j2eeArchive = new WebApp(archive);
146                     mode = WEBAPP;
147
148                 } else if (isClient(file)) {
149                     // Client J2EE Archive
150
j2eeArchive = new Client(archive);
151                     mode = CLIENT;
152
153                 } else {
154                     // unsupported archive type
155
String JavaDoc err = i18n.getMessage("ModifierFactory.getModifier.unsupported", filename);
156                     throw new ClientStubGenException(err);
157                 }
158             }
159         } else {
160             String JavaDoc err = i18n.getMessage("ModifierFactory.getModifier.notfound", filename);
161             throw new ClientStubGenException(err);
162         }
163
164         // init the archive
165
j2eeArchive.initialize();
166
167         //create the modifier
168
switch (mode) {
169             case APPLICATION:
170                 modifier = new ApplicationModifier((Application) j2eeArchive);
171
172                 break;
173
174             case EJBJAR:
175                 modifier = new EjbJarModifier((EjbJar) j2eeArchive);
176
177                 break;
178
179             case WEBAPP:
180                 modifier = new WebAppModifier((WebApp) j2eeArchive);
181
182                 break;
183
184             case CLIENT:
185                 modifier = new ClientModifier((Client) j2eeArchive);
186
187                 break;
188
189             default:
190                 // cannot happen
191
}
192
193         return modifier;
194
195     }
196 }
Popular Tags