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 5590 2004-10-11 13:16:15Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.genclientstub.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: import org.objectweb.jonas_lib.genclientstub.ClientStubGenException;
043:
044: /**
045: * Used to create the right ArchiveModifier from a given filename (ear, jar,
046: * war, ...)
047: *
048: * @author Guillaume Sauthier
049: */
050: public class ModifierFactory extends AbsModifierFactory {
051:
052: /** i18n */
053: private static I18n i18n = I18n.getInstance(ModifierFactory.class);
054:
055: /**
056: * Empty Constructor for utility class
057: */
058: private ModifierFactory() {
059: super ();
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: Archive archive = null;
075: J2EEArchive j2eeArchive = null;
076: ArchiveModifier modifier = null;
077: int mode;
078:
079: File file = new File(filename);
080:
081: if (file.exists()) {
082: if (file.isFile()) {
083: // should be a jar file
084: // test jar file
085: JarFile jarfile;
086:
087: try {
088: jarfile = new JarFile(file);
089: } catch (IOException ioe) {
090: // not a jar file
091: String err = i18n.getMessage(
092: "ModifierFactory.getModifier.notjar",
093: filename);
094: throw new ClientStubGenException(err);
095: }
096:
097: // Create the inner Archive
098: archive = new JarArchive(file);
099:
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 err = i18n.getMessage(
124: "ModifierFactory.getModifier.unsupported",
125: filename);
126: throw new ClientStubGenException(err);
127: }
128: } else {
129: // directory unpacked
130: // Create the inner Archive
131: archive = new FileArchive(file);
132:
133: // now we are sure that filename is a correct jar
134: if (isApplication(file)) {
135: // Application J2EE Archive
136: j2eeArchive = new Application(archive);
137: mode = APPLICATION;
138:
139: } else if (isEjbJar(file)) {
140: // EjbJar J2EE Archive
141: j2eeArchive = new EjbJar(archive);
142: mode = EJBJAR;
143:
144: } else if (isWebApp(file)) {
145: // WebApp J2EE Archive
146: j2eeArchive = new WebApp(archive);
147: mode = WEBAPP;
148:
149: } else if (isClient(file)) {
150: // Client J2EE Archive
151: j2eeArchive = new Client(archive);
152: mode = CLIENT;
153:
154: } else {
155: // unsupported archive type
156: String err = i18n.getMessage(
157: "ModifierFactory.getModifier.unsupported",
158: filename);
159: throw new ClientStubGenException(err);
160: }
161: }
162: } else {
163: String err = i18n.getMessage(
164: "ModifierFactory.getModifier.notfound", filename);
165: throw new ClientStubGenException(err);
166: }
167:
168: // init the archive
169: j2eeArchive.initialize();
170:
171: //create the modifier
172: switch (mode) {
173: case APPLICATION:
174: modifier = new ApplicationModifier(
175: (Application) j2eeArchive);
176:
177: break;
178:
179: case EJBJAR:
180: modifier = new EjbJarModifier((EjbJar) j2eeArchive);
181:
182: break;
183:
184: case WEBAPP:
185: modifier = new WebAppModifier((WebApp) j2eeArchive);
186:
187: break;
188:
189: case CLIENT:
190: modifier = new ClientModifier((Client) j2eeArchive);
191:
192: break;
193:
194: default:
195: // cannot happen
196: }
197:
198: return modifier;
199:
200: }
201: }
|