00001
00027 package org.objectweb.jonas_ear.deployment.lib;
00028
00029 import java.io.File;
00030 import java.io.FileInputStream;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.io.InputStreamReader;
00034 import java.io.Reader;
00035 import java.util.jar.JarFile;
00036 import java.util.zip.ZipEntry;
00037
00038 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
00039 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException;
00040 import org.objectweb.jonas_ear.deployment.rules.ApplicationRuleSet;
00041 import org.objectweb.jonas_ear.deployment.rules.JonasApplicationRuleSet;
00042 import org.objectweb.jonas_ear.deployment.xml.Application;
00043 import org.objectweb.jonas_ear.deployment.xml.JonasApplication;
00044
00045 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00046 import org.objectweb.jonas_lib.deployment.digester.JDigester;
00047 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
00048
00049 import org.objectweb.jonas.common.Log;
00050
00051 import org.objectweb.util.monolog.api.BasicLevel;
00052 import org.objectweb.util.monolog.api.Logger;
00053
00061 public class EarDeploymentDescManager extends AbsDeploymentDescManager {
00062
00066 public static final String APPLICATION_FILE_NAME = "META-INF/application.xml";
00067
00071 public static final String JONAS_APPLICATION_FILE_NAME = "META-INF/jonas-application.xml";
00072
00073
00077 private static JDigester earDigester = null;
00078
00082 private static JDigester jonasEarDigester = null;
00083
00087 private static ApplicationRuleSet appRuleSet = new ApplicationRuleSet();
00088
00092 private static JonasApplicationRuleSet jonasApplicationRuleSet = new JonasApplicationRuleSet();
00093
00097 private static Logger logger = Log.getLogger(Log.JONAS_EAR_PREFIX);
00098
00102 private static boolean parsingWithValidation = true;
00103
00107 private EarDeploymentDescManager() {
00108 }
00109
00121 public static EarDeploymentDesc getDeploymentDesc(String earFileName, ClassLoader classLoaderForCls)
00122 throws EarDeploymentDescException {
00123
00124
00125 JarFile earFile = null;
00126
00127
00128 InputStream appInputStream = null;
00129 InputStream jonasApplicationInputStream = null;
00130
00131
00132 ZipEntry appZipEntry = null;
00133 ZipEntry jonasApplicationZipEntry = null;
00134
00135 Application application = null;
00136 JonasApplication jonasApplication = null;
00137
00138 String xmlContent = "";
00139 String jonasXmlContent = "";
00140
00141
00142 File earF = new File(earFileName);
00143 if (!earF.exists()) {
00144 throw new EarDeploymentDescException("The file '" + earFileName + "' was not found.");
00145 }
00146
00147
00148 try {
00149
00150 if (earF.isDirectory()) {
00151
00152 File earXmlF = new File(earFileName, APPLICATION_FILE_NAME);
00153 if (!earXmlF.exists()) {
00154 String err = "You have choose to deploy an ear directory but there is no " + APPLICATION_FILE_NAME
00155 + " file in the directory " + earFileName;
00156 throw new EarDeploymentDescException(err);
00157 }
00158 appInputStream = new FileInputStream(earXmlF);
00159 xmlContent = xmlContent(appInputStream);
00160
00161 appInputStream = new FileInputStream(earXmlF);
00162
00163
00164 File jonasEarXmlF = new File(earFileName, JONAS_APPLICATION_FILE_NAME);
00165
00166 if (jonasEarXmlF.exists()) {
00167 jonasApplicationInputStream = new FileInputStream(jonasEarXmlF);
00168 jonasXmlContent = xmlContent(jonasApplicationInputStream);
00169
00170 jonasApplicationInputStream = new FileInputStream(jonasEarXmlF);
00171 }
00172 } else {
00173
00174 earFile = new JarFile(earFileName);
00175
00176
00177 appZipEntry = earFile.getEntry(APPLICATION_FILE_NAME);
00178 if (appZipEntry == null) {
00179 throw new EarDeploymentDescException("The entry '" + APPLICATION_FILE_NAME
00180 + "' was not found in the file '" + earFileName + "'.");
00181 }
00182
00183
00184 appInputStream = earFile.getInputStream(appZipEntry);
00185 xmlContent = xmlContent(appInputStream);
00186
00187 appInputStream = earFile.getInputStream(appZipEntry);
00188
00189
00190 jonasApplicationZipEntry = earFile.getEntry(JONAS_APPLICATION_FILE_NAME);
00191 if (jonasApplicationZipEntry != null) {
00192
00193 jonasApplicationInputStream = earFile.getInputStream(jonasApplicationZipEntry);
00194 jonasXmlContent = xmlContent(jonasApplicationInputStream);
00195
00196 jonasApplicationInputStream = earFile.getInputStream(jonasApplicationZipEntry);
00197 }
00198 }
00199 } catch (Exception e) {
00200 if (earFile != null) {
00201 try {
00202 earFile.close();
00203 } catch (IOException ioe) {
00204
00205 logger.log(BasicLevel.WARN, "Cannot close InputStream for '" + earFileName + "'");
00206 }
00207 }
00208 throw new EarDeploymentDescException("Cannot read the XML deployment descriptors of the ear file '"
00209 + earFileName + "'.", e);
00210 }
00211
00212 application = loadApplication(new InputStreamReader(appInputStream), APPLICATION_FILE_NAME);
00213 try {
00214 appInputStream.close();
00215 } catch (IOException e) {
00216
00217 logger.log(BasicLevel.WARN, "Cannot close InputStream for META-INF/application.xml in '" + earFileName
00218 + "'");
00219 }
00220
00221
00222
00223 if (jonasApplicationInputStream != null) {
00224 jonasApplication = loadJonasApplication(new InputStreamReader(jonasApplicationInputStream), JONAS_APPLICATION_FILE_NAME);
00225 try {
00226 jonasApplicationInputStream.close();
00227 } catch (IOException e) {
00228
00229 logger.log(BasicLevel.WARN, "Cannot close InputStream for '" + earFileName + "'");
00230 }
00231 } else {
00232 jonasApplication = new JonasApplication();
00233 }
00234
00235
00236
00237 EarDeploymentDesc earDD = new EarDeploymentDesc(classLoaderForCls, application, jonasApplication);
00238 earDD.setXmlContent(xmlContent);
00239 earDD.setJonasXmlContent(jonasXmlContent);
00240 return earDD;
00241 }
00242
00251 public static Application loadApplication(Reader reader, String fileName) throws EarDeploymentDescException {
00252
00253 Application app = new Application();
00254
00255 if (earDigester == null) {
00256 try {
00257
00258 earDigester = new JDigester(appRuleSet, getParsingWithValidation(), true, new EarDTDs(),
00259 new EarSchemas());
00260 } catch (DeploymentDescException e) {
00261 throw new EarDeploymentDescException(e);
00262 }
00263 }
00264
00265 try {
00266 earDigester.parse(reader, fileName, app);
00267 } catch (DeploymentDescException e) {
00268 throw new EarDeploymentDescException(e);
00269 } finally {
00270 earDigester.push(null);
00271 }
00272
00273 return app;
00274 }
00275
00285 public static JonasApplication loadJonasApplication(Reader reader, String fileName) throws EarDeploymentDescException {
00286
00287 JonasApplication ja = new JonasApplication();
00288
00289
00290 if (jonasEarDigester == null) {
00291 try {
00292 jonasEarDigester = new JDigester(jonasApplicationRuleSet, getParsingWithValidation(), true,
00293 null, new JonasEarSchemas());
00294 } catch (DeploymentDescException e) {
00295 throw new EarDeploymentDescException(e);
00296 }
00297 }
00298
00299 try {
00300 jonasEarDigester.parse(reader, fileName, ja);
00301
00302 } catch (DeploymentDescException e) {
00303 throw new EarDeploymentDescException(e);
00304 } finally {
00305 jonasEarDigester.push(null);
00306 }
00307 return ja;
00308 }
00309
00314 public static boolean getParsingWithValidation() {
00315 return parsingWithValidation;
00316 }
00317
00322 public static void setParsingWithValidation(boolean validation) {
00323 EarDeploymentDescManager.parsingWithValidation = validation;
00324 }
00325 }