1 26 27 package org.objectweb.jonas_ear.deployment.lib; 28 29 import java.io.File ; 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.InputStreamReader ; 34 import java.io.Reader ; 35 import java.util.jar.JarFile ; 36 import java.util.zip.ZipEntry ; 37 38 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc; 39 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDescException; 40 import org.objectweb.jonas_ear.deployment.rules.ApplicationRuleSet; 41 import org.objectweb.jonas_ear.deployment.rules.JonasApplicationRuleSet; 42 import org.objectweb.jonas_ear.deployment.xml.Application; 43 import org.objectweb.jonas_ear.deployment.xml.JonasApplication; 44 45 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 46 import org.objectweb.jonas_lib.deployment.digester.JDigester; 47 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager; 48 49 import org.objectweb.jonas.common.Log; 50 51 import org.objectweb.util.monolog.api.BasicLevel; 52 import org.objectweb.util.monolog.api.Logger; 53 54 61 public class EarDeploymentDescManager extends AbsDeploymentDescManager { 62 63 66 public static final String APPLICATION_FILE_NAME = "META-INF/application.xml"; 67 68 71 public static final String JONAS_APPLICATION_FILE_NAME = "META-INF/jonas-application.xml"; 72 73 74 77 private static JDigester earDigester = null; 78 79 82 private static JDigester jonasEarDigester = null; 83 84 87 private static ApplicationRuleSet appRuleSet = new ApplicationRuleSet(); 88 89 92 private static JonasApplicationRuleSet jonasApplicationRuleSet = new JonasApplicationRuleSet(); 93 94 97 private static Logger logger = Log.getLogger(Log.JONAS_EAR_PREFIX); 98 99 102 private static boolean parsingWithValidation = true; 103 104 107 private EarDeploymentDescManager() { 108 } 109 110 121 public static EarDeploymentDesc getDeploymentDesc(String earFileName, ClassLoader classLoaderForCls) 122 throws EarDeploymentDescException { 123 124 JarFile earFile = null; 126 127 InputStream appInputStream = null; 129 InputStream jonasApplicationInputStream = null; 130 131 ZipEntry appZipEntry = null; 133 ZipEntry jonasApplicationZipEntry = null; 134 135 Application application = null; 136 JonasApplication jonasApplication = null; 137 138 String xmlContent = ""; 139 String jonasXmlContent = ""; 140 141 File earF = new File (earFileName); 143 if (!earF.exists()) { 144 throw new EarDeploymentDescException("The file '" + earFileName + "' was not found."); 145 } 146 147 try { 149 if (earF.isDirectory()) { 151 File earXmlF = new File (earFileName, APPLICATION_FILE_NAME); 153 if (!earXmlF.exists()) { 154 String err = "You have choose to deploy an ear directory but there is no " + APPLICATION_FILE_NAME 155 + " file in the directory " + earFileName; 156 throw new EarDeploymentDescException(err); 157 } 158 appInputStream = new FileInputStream (earXmlF); 159 xmlContent = xmlContent(appInputStream); 160 appInputStream = new FileInputStream (earXmlF); 162 163 File jonasEarXmlF = new File (earFileName, JONAS_APPLICATION_FILE_NAME); 165 if (jonasEarXmlF.exists()) { 167 jonasApplicationInputStream = new FileInputStream (jonasEarXmlF); 168 jonasXmlContent = xmlContent(jonasApplicationInputStream); 169 jonasApplicationInputStream = new FileInputStream (jonasEarXmlF); 171 } 172 } else { 173 earFile = new JarFile (earFileName); 175 176 appZipEntry = earFile.getEntry(APPLICATION_FILE_NAME); 178 if (appZipEntry == null) { 179 throw new EarDeploymentDescException("The entry '" + APPLICATION_FILE_NAME 180 + "' was not found in the file '" + earFileName + "'."); 181 } 182 183 appInputStream = earFile.getInputStream(appZipEntry); 185 xmlContent = xmlContent(appInputStream); 186 appInputStream = earFile.getInputStream(appZipEntry); 188 189 jonasApplicationZipEntry = earFile.getEntry(JONAS_APPLICATION_FILE_NAME); 191 if (jonasApplicationZipEntry != null) { 192 jonasApplicationInputStream = earFile.getInputStream(jonasApplicationZipEntry); 194 jonasXmlContent = xmlContent(jonasApplicationInputStream); 195 jonasApplicationInputStream = earFile.getInputStream(jonasApplicationZipEntry); 197 } 198 } 199 } catch (Exception e) { 200 if (earFile != null) { 201 try { 202 earFile.close(); 203 } catch (IOException ioe) { 204 logger.log(BasicLevel.WARN, "Cannot close InputStream for '" + earFileName + "'"); 206 } 207 } 208 throw new EarDeploymentDescException("Cannot read the XML deployment descriptors of the ear file '" 209 + earFileName + "'.", e); 210 } 211 212 application = loadApplication(new InputStreamReader (appInputStream), APPLICATION_FILE_NAME); 213 try { 214 appInputStream.close(); 215 } catch (IOException e) { 216 logger.log(BasicLevel.WARN, "Cannot close InputStream for META-INF/application.xml in '" + earFileName 218 + "'"); 219 } 220 221 if (jonasApplicationInputStream != null) { 224 jonasApplication = loadJonasApplication(new InputStreamReader (jonasApplicationInputStream), JONAS_APPLICATION_FILE_NAME); 225 try { 226 jonasApplicationInputStream.close(); 227 } catch (IOException e) { 228 logger.log(BasicLevel.WARN, "Cannot close InputStream for '" + earFileName + "'"); 230 } 231 } else { 232 jonasApplication = new JonasApplication(); 233 } 234 235 236 EarDeploymentDesc earDD = new EarDeploymentDesc(classLoaderForCls, application, jonasApplication); 238 earDD.setXmlContent(xmlContent); 239 earDD.setJonasXmlContent(jonasXmlContent); 240 return earDD; 241 } 242 243 251 public static Application loadApplication(Reader reader, String fileName) throws EarDeploymentDescException { 252 253 Application app = new Application(); 254 if (earDigester == null) { 256 try { 257 earDigester = new JDigester(appRuleSet, getParsingWithValidation(), true, new EarDTDs(), 259 new EarSchemas()); 260 } catch (DeploymentDescException e) { 261 throw new EarDeploymentDescException(e); 262 } 263 } 264 265 try { 266 earDigester.parse(reader, fileName, app); 267 } catch (DeploymentDescException e) { 268 throw new EarDeploymentDescException(e); 269 } finally { 270 earDigester.push(null); 271 } 272 273 return app; 274 } 275 276 285 public static JonasApplication loadJonasApplication(Reader reader, String fileName) throws EarDeploymentDescException { 286 287 JonasApplication ja = new JonasApplication(); 288 289 if (jonasEarDigester == null) { 291 try { 292 jonasEarDigester = new JDigester(jonasApplicationRuleSet, getParsingWithValidation(), true, 293 null, new JonasEarSchemas()); 294 } catch (DeploymentDescException e) { 295 throw new EarDeploymentDescException(e); 296 } 297 } 298 299 try { 300 jonasEarDigester.parse(reader, fileName, ja); 301 302 } catch (DeploymentDescException e) { 303 throw new EarDeploymentDescException(e); 304 } finally { 305 jonasEarDigester.push(null); 306 } 307 return ja; 308 } 309 310 314 public static boolean getParsingWithValidation() { 315 return parsingWithValidation; 316 } 317 318 322 public static void setParsingWithValidation(boolean validation) { 323 EarDeploymentDescManager.parsingWithValidation = validation; 324 } 325 } | Popular Tags |