1 26 27 package org.objectweb.jonas_rar.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 javax.naming.Context ; 39 import javax.naming.NamingException ; 40 41 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 42 import org.objectweb.jonas_lib.deployment.digester.JDigester; 43 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager; 44 45 import org.objectweb.jonas_rar.deployment.api.RarDeploymentDesc; 46 import org.objectweb.jonas_rar.deployment.api.RarDeploymentDescException; 47 import org.objectweb.jonas_rar.deployment.rules.ConnectorRuleSet; 48 import org.objectweb.jonas_rar.deployment.rules.JonasConnectorRuleSet; 49 import org.objectweb.jonas_rar.deployment.xml.Connector; 50 import org.objectweb.jonas_rar.deployment.xml.JonasConnector; 51 52 import org.objectweb.jonas.common.Log; 53 import org.objectweb.jonas.naming.CompNamingContext; 54 55 import org.objectweb.util.monolog.api.BasicLevel; 56 import org.objectweb.util.monolog.api.Logger; 57 58 62 public class RarDeploymentDescManager extends AbsDeploymentDescManager { 63 64 67 public static final String RA_FILE_NAME = "META-INF/ra.xml"; 68 69 72 public static final String JONAS_RA_FILE_NAME = "META-INF/jonas-ra.xml"; 73 74 77 private static boolean parsingWithValidation = true; 78 79 82 private static JDigester connectorDigester = null; 83 84 87 private static JDigester jonasConnectorDigester = null; 88 89 92 private static ConnectorRuleSet connectorRuleSet = new ConnectorRuleSet(); 93 94 97 private static JonasConnectorRuleSet jonasConnectorRuleSet = new JonasConnectorRuleSet(); 98 99 102 private static Logger logger = Log.getLogger(Log.JONAS_DBM_PREFIX); 103 104 107 private RarDeploymentDescManager() { 108 } 109 110 121 public static RarDeploymentDesc getInstance(String rarFileName, ClassLoader classLoader) 122 throws DeploymentDescException { 123 124 Context contctx = null; 127 try { 128 contctx = new CompNamingContext(rarFileName); 129 contctx.rebind("rarFileName", rarFileName); 130 contctx.rebind("classloader", classLoader); 131 } catch (Exception e) { 132 137 throw new RarDeploymentDescException("Error during the deployment", e); 138 } 139 return getInstance(contctx); 141 } 142 143 156 public static RarDeploymentDesc getInstance(Context ctx) throws DeploymentDescException { 157 158 String xmlContent = ""; 160 String jonasXmlContent = ""; 161 boolean altDD = false; 162 163 String rarFileName = null; 164 ClassLoader classLoader = null; 165 try { 166 rarFileName = (String ) ctx.lookup("rarFileName"); 167 classLoader = (ClassLoader ) ctx.lookup("classloader"); 168 } catch (NamingException e) { 169 String err = "Error while getting parameter from context param "; 170 throw new RarDeploymentDescException(err, e); 171 } 172 173 String raDeployDesc; 175 try { 176 raDeployDesc = (String ) ctx.lookup("altDD"); 177 altDD = true; 178 } catch (NamingException e) { 179 raDeployDesc = ""; 181 } 182 183 JarFile rarFile = null; 185 186 InputStream raInputStream = null; 188 InputStream jonasRaInputStream = null; 189 190 ZipEntry raZipEntry = null; 192 ZipEntry jonasRaZipEntry = null; 193 194 Connector connector = null; 195 JonasConnector jonasConnector = null; 196 197 File fRar = new File (rarFileName); 199 200 if (!(fRar.exists())) { 202 throw new RarDeploymentDescException("The file '" + rarFileName + "' was not found."); 203 } 204 205 boolean setupRa = true; 207 boolean setupJonasRa = true; 208 try { 209 if (!altDD) { 210 211 if (fRar.isDirectory()) { 214 File rarXmlF = new File (rarFileName, RA_FILE_NAME); 215 if (!rarXmlF.exists()) { 216 connector = null; 217 setupRa = false; 218 } else { 219 raInputStream = new FileInputStream (rarXmlF); 220 xmlContent = xmlContent(raInputStream); 221 raInputStream = new FileInputStream (rarXmlF); 222 } 223 } else { 224 rarFile = new JarFile (rarFileName); 225 226 raZipEntry = rarFile.getEntry(RA_FILE_NAME); 228 if (raZipEntry == null) { 229 connector = null; 230 setupRa = false; 231 } else { 232 raInputStream = rarFile.getInputStream(raZipEntry); 234 xmlContent = xmlContent(raInputStream); 235 raInputStream = rarFile.getInputStream(raZipEntry); 236 } 237 } 238 } else { 239 raInputStream = new FileInputStream (raDeployDesc); 240 xmlContent = xmlContent(raInputStream); 241 raInputStream = new FileInputStream (raDeployDesc); 242 } 243 244 if (fRar.isDirectory()) { 245 File rarJXmlF = new File (rarFileName, JONAS_RA_FILE_NAME); 247 if (rarJXmlF.exists()) { 248 jonasRaInputStream = new FileInputStream (rarJXmlF); 249 jonasXmlContent = xmlContent(jonasRaInputStream); 250 jonasRaInputStream = new FileInputStream (rarJXmlF); 251 } 252 } else { 253 rarFile = new JarFile (rarFileName); 255 jonasRaZipEntry = rarFile.getEntry(JONAS_RA_FILE_NAME); 256 if (jonasRaZipEntry == null) { 257 jonasConnector = null; 258 setupJonasRa = false; 259 } else { 260 jonasRaInputStream = rarFile.getInputStream(jonasRaZipEntry); 262 jonasXmlContent = xmlContent(jonasRaInputStream); 263 jonasRaInputStream = rarFile.getInputStream(jonasRaZipEntry); 264 } 265 } 266 } catch (Exception e) { 267 if (rarFile != null) { 268 try { 269 rarFile.close(); 270 } catch (IOException ioe) { 271 logger.log(BasicLevel.WARN, "Can't close '" + rarFileName + "'"); 273 } 274 } 275 throw new RarDeploymentDescException("Cannot read the XML deployment descriptors of the rar file '" 276 + rarFileName + "'.", e); 277 } 278 279 if (setupRa) { 280 connector = loadConnector(new InputStreamReader (raInputStream), raDeployDesc); 281 try { 282 raInputStream.close(); 283 } catch (IOException e) { 284 logger.log(BasicLevel.WARN, "Can't close META-INF/ra.xml of '" + rarFileName + "'"); 286 } 287 } 288 289 if (setupJonasRa) { 290 jonasConnector = loadJonasConnector(new InputStreamReader (jonasRaInputStream), JONAS_RA_FILE_NAME); 291 try { 292 jonasRaInputStream.close(); 293 } catch (IOException e) { 294 logger.log(BasicLevel.WARN, "Can't close META-INF/jonas-ra.xml of '" + rarFileName + "'"); 296 } 297 } 298 299 RarDeploymentDesc rdd = new RarDeploymentDesc(classLoader, connector, jonasConnector); 301 rdd.setXmlContent(xmlContent); 302 rdd.setJOnASXmlContent(jonasXmlContent); 303 return rdd; 304 } 305 306 314 public static Connector loadConnector(Reader reader, String fileName) throws DeploymentDescException { 315 316 Connector connector = new Connector(); 317 318 if (connectorDigester == null) { 320 connectorDigester = new JDigester(connectorRuleSet, parsingWithValidation, true, new ConnectorDTDs(), 321 new ConnectorSchemas()); 322 } 323 324 try { 325 connectorDigester.parse(reader, fileName, connector); 326 } catch (DeploymentDescException e) { 327 throw e; 328 } finally { 329 connectorDigester.push(null); 330 } 331 return connector; 332 } 333 334 342 public static JonasConnector loadJonasConnector(Reader reader, String fileName) throws DeploymentDescException { 343 344 JonasConnector jonasConnector = new JonasConnector(); 345 346 if (jonasConnectorDigester == null) { 348 jonasConnectorDigester = new JDigester(jonasConnectorRuleSet, parsingWithValidation, true, 349 new JonasConnectorDTDs(), new JonasConnectorSchemas()); 350 } 351 352 try { 353 jonasConnectorDigester.parse(reader, fileName, jonasConnector); 354 } catch (DeploymentDescException e) { 355 throw e; 356 } finally { 357 jonasConnectorDigester.push(null); 358 } 359 return jonasConnector; 360 } 361 362 366 public static boolean getParsingWithValidation() { 367 return RarDeploymentDescManager.parsingWithValidation; 368 } 369 370 374 public static void setParsingWithValidation(boolean validation) { 375 RarDeploymentDescManager.parsingWithValidation = validation; 376 } 377 378 } | Popular Tags |