001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2003-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ModifierFactory.java 8253 2006-04-18 11:40:19Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.modifier;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.jar.JarFile;
029:
030: import org.objectweb.jonas_lib.I18n;
031: import org.objectweb.jonas_lib.genbase.GenBaseException;
032: import org.objectweb.jonas_lib.genbase.archive.Application;
033: import org.objectweb.jonas_lib.genbase.archive.Archive;
034: import org.objectweb.jonas_lib.genbase.archive.Client;
035: import org.objectweb.jonas_lib.genbase.archive.EjbJar;
036: import org.objectweb.jonas_lib.genbase.archive.FileArchive;
037: import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
038: import org.objectweb.jonas_lib.genbase.archive.JarArchive;
039: import org.objectweb.jonas_lib.genbase.archive.WebApp;
040: import org.objectweb.jonas_lib.genbase.modifier.AbsModifierFactory;
041: import org.objectweb.jonas_lib.genbase.modifier.ArchiveModifier;
042:
043: import org.objectweb.jonas_ws.wsgen.WsGenException;
044:
045: /**
046: * Used to create the right ArchiveModifier from a given filename (ear, jar,
047: * war, ...)
048: *
049: * @author Guillaume Sauthier
050: */
051: public class ModifierFactory extends AbsModifierFactory {
052:
053: /** i18n */
054: private static I18n i18n = I18n.getInstance(ModifierFactory.class);
055:
056: /**
057: * Empty Constructor for utility class
058: */
059: private ModifierFactory() {
060: }
061:
062: /**
063: * Returns an <code>ArchiveModifier</code> according to archive type
064: * (application, ejbjar, webapp or client).
065: *
066: * @param filename input filename.
067: *
068: * @return an <code>ArchiveModifier</code>.
069: *
070: * @exception GenBaseException when archive creation fails.
071: */
072: public static ArchiveModifier getModifier(String filename)
073: throws GenBaseException {
074: return getModifier(filename, true);
075: }
076:
077: /**
078: * Returns an <code>ArchiveModifier</code> according to archive type
079: * (application, ejbjar, webapp or client).
080: *
081: * @param filename input filename.
082: * @param init Initialize the archive.
083: *
084: * @return an <code>ArchiveModifier</code>.
085: *
086: * @exception GenBaseException when archive creation fails.
087: */
088: public static ArchiveModifier getModifier(String filename,
089: boolean init) throws GenBaseException {
090: Archive archive = null;
091: J2EEArchive j2eeArchive = null;
092: ArchiveModifier modifier = null;
093: int mode;
094:
095: File file = new File(filename);
096:
097: if (file.exists()) {
098: if (file.isFile()) {
099: // should be a jar file
100: // test jar file
101: JarFile jarfile;
102:
103: try {
104: jarfile = new JarFile(file);
105: } catch (IOException ioe) {
106: // not a jar file
107: String err = i18n.getMessage(
108: "ModifierFactory.getModifier.notjar",
109: filename);
110: throw new WsGenException(err);
111: }
112:
113: // Create the inner Archive
114: archive = new JarArchive(file);
115:
116: // now we are sure that filename is a correct jar
117: if (isApplication(jarfile)) {
118: // Application J2EE Archive
119: j2eeArchive = new Application(archive);
120: mode = APPLICATION;
121:
122: } else if (isEjbJar(jarfile)) {
123: // EjbJar J2EE Archive
124: j2eeArchive = new EjbJar(archive);
125: mode = EJBJAR;
126:
127: } else if (isWebApp(jarfile)) {
128: // WebApp J2EE Archive
129: j2eeArchive = new WebApp(archive);
130: mode = WEBAPP;
131:
132: } else if (isClient(jarfile)) {
133: // Client J2EE Archive
134: j2eeArchive = new Client(archive);
135: mode = CLIENT;
136:
137: } else {
138: // unsupported archive type
139: String err = i18n.getMessage(
140: "ModifierFactory.getModifier.unsupported",
141: filename);
142: throw new WsGenException(err);
143: }
144: } else {
145: // directory unpacked
146: // Create the inner Archive
147: archive = new FileArchive(file);
148:
149: // now we are sure that filename is a correct jar
150: if (isApplication(file)) {
151: // Application J2EE Archive
152: j2eeArchive = new Application(archive);
153: mode = APPLICATION;
154:
155: } else if (isEjbJar(file)) {
156: // EjbJar J2EE Archive
157: j2eeArchive = new EjbJar(archive);
158: mode = EJBJAR;
159:
160: } else if (isWebApp(file)) {
161: // WebApp J2EE Archive
162: j2eeArchive = new WebApp(archive);
163: mode = WEBAPP;
164:
165: } else if (isClient(file)) {
166: // Client J2EE Archive
167: j2eeArchive = new Client(archive);
168: mode = CLIENT;
169:
170: } else {
171: // unsupported archive type
172: String err = i18n.getMessage(
173: "ModifierFactory.getModifier.unsupported",
174: filename);
175: throw new WsGenException(err);
176: }
177: }
178: } else {
179: String err = i18n.getMessage(
180: "ModifierFactory.getModifier.notfound", filename);
181: throw new WsGenException(err);
182: }
183:
184: // init the archive
185: if (init) {
186: j2eeArchive.initialize();
187: }
188:
189: //create the modifier
190: switch (mode) {
191: case APPLICATION:
192: modifier = new ApplicationModifier(
193: (Application) j2eeArchive);
194:
195: break;
196:
197: case EJBJAR:
198: modifier = new EjbJarModifier((EjbJar) j2eeArchive);
199:
200: break;
201:
202: case WEBAPP:
203: modifier = new WebAppModifier((WebApp) j2eeArchive);
204:
205: break;
206:
207: case CLIENT:
208: modifier = new ClientModifier((Client) j2eeArchive);
209:
210: break;
211:
212: default:
213: // cannot happen
214: }
215:
216: return modifier;
217:
218: }
219: }
|