KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > 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.5 2004/10/11 13:16:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

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

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

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

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