1 22 package org.objectweb.petals.jbi.management.service; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.net.URL ; 30 import java.util.zip.ZipFile ; 31 32 import org.objectweb.petals.PetalsException; 33 import org.objectweb.petals.repository.RepositoryService; 34 import org.objectweb.petals.repository.RepositoryImpl.EntityType; 35 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 36 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorBuilder; 37 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptorException; 38 import org.objectweb.petals.util.LoggingUtil; 39 import org.objectweb.petals.util.ZipUtil; 40 import org.objectweb.util.monolog.api.Logger; 41 42 48 public class PackageHandler { 49 50 private LoggingUtil log; 51 52 private Logger logger; 53 54 private RepositoryService repositorySrv; 55 56 private final static String META_INF_DIR = "META-INF"; 57 58 private final static String JBI_XML_FILE_NAME = "jbi.xml"; 59 60 public PackageHandler(Logger logger, RepositoryService repositorySrv) { 61 super(); 62 this.logger = logger; 63 this.log = new LoggingUtil(logger); 64 this.repositorySrv = repositorySrv; 65 } 66 67 75 public JBIDescriptor loadDescriptor(URI archiveURI) throws PetalsException { 76 log.start(); 77 78 String msg; 79 JBIDescriptor result = null; 80 81 84 85 File tmpJbiXml = null; 86 87 try { 88 91 try { 92 ZipFile zipArchive = ZipUtil.openZipFile(archiveURI); 93 94 tmpJbiXml = ZipUtil.getEntryAsTemp(zipArchive, 95 JBIDescriptorBuilder.JBI_DESCRIPTOR_RESOURCE); 96 97 zipArchive.close(); 98 } catch (IOException e) { 99 msg = "Error during jbi.xml extraction from package : " 100 + archiveURI.getPath(); 101 log.error(msg); 102 103 throw new PetalsException(msg, e); 104 } 105 106 if (tmpJbiXml == null) { 107 msg = "Invalid JBI package Zip archive. jbi.xml descriptor " 108 + "entry was not found: " + archiveURI.toString(); 109 log.error(msg); 110 111 throw new PetalsException(msg); 112 } 113 114 117 118 JBIDescriptorBuilder descBuilder = new JBIDescriptorBuilder( 119 tmpJbiXml.toURI(), (java.util.logging.Logger ) logger); 120 121 result = descBuilder.build(); 122 123 } catch (JBIDescriptorException e) { 124 msg = "Bad JBI descriptor"; 125 log.error(msg, e); 126 throw new PetalsException(msg, e); 127 } finally { 128 131 if (tmpJbiXml != null) { 132 tmpJbiXml.delete(); 133 } 134 } 135 log.end(); 136 137 return result; 138 } 139 140 151 public JBIDescriptor loadDescriptor(String entityName, EntityType entityType) 152 throws PetalsException { 153 log.start(); 154 155 String msg; 156 JBIDescriptor result = null; 157 158 161 File tmpJbiXml = null; 162 163 try { 164 167 switch (entityType) { 168 case COMPONENT_TYPE: 169 tmpJbiXml = new File (repositorySrv 170 .getComponentInstallRoot(entityName), META_INF_DIR 171 + File.separator + JBI_XML_FILE_NAME); 172 break; 173 case SA_TYPE: 174 tmpJbiXml = new File (repositorySrv 175 .getServiceAssemblyInstallRoot(entityName), 176 META_INF_DIR + File.separator + JBI_XML_FILE_NAME); 177 break; 178 case SL_TYPE: 179 tmpJbiXml = new File (repositorySrv 180 .getSharedLibInstallRoot(entityName), META_INF_DIR 181 + File.separator + JBI_XML_FILE_NAME); 182 break; 183 } 184 185 if (tmpJbiXml == null) { 186 msg = "jbi.xml descriptor entry was not " 187 + "found in repository for entity: " + entityName; 188 log.error(msg); 189 190 throw new PetalsException(msg); 191 } 192 193 196 197 JBIDescriptorBuilder descBuilder = new JBIDescriptorBuilder( 198 tmpJbiXml.toURI(), (java.util.logging.Logger ) logger); 199 200 result = descBuilder.build(); 201 202 } catch (JBIDescriptorException e) { 203 msg = "Bad JBI descriptor"; 204 log.error(msg, e); 205 throw new PetalsException(msg, e); 206 } 207 log.end(); 208 209 return result; 210 } 211 212 220 public URI processAndGetPackageURI(String archiveZipURL, 221 boolean checkIfExist) { 222 log.start(archiveZipURL); 223 224 String msg; 225 URI uri = null; 226 227 230 archiveZipURL = archiveZipURL.replaceAll(" ", "%20"); 232 try { 233 URL url = new URL (archiveZipURL); 234 235 uri = url.toURI(); 236 } catch (MalformedURLException mue) { 237 msg = "Wrong package archive location specified. URL: " 238 + archiveZipURL; 239 240 log.error(msg, mue); 241 242 throw new IllegalArgumentException (msg, mue); 243 } catch (URISyntaxException use) { 244 msg = "Syntax error converting to URI from given location. URL: " 245 + archiveZipURL; 246 log.error(msg, use); 247 248 throw new IllegalArgumentException (msg, use); 249 } 250 251 254 if (checkIfExist) { 255 File archive = new File (uri); 256 257 if (!archive.exists()) { 258 msg = "JBI package archive not found. URL: " + archiveZipURL; 259 log.error(msg); 260 261 throw new IllegalArgumentException (msg); 262 } 263 } 264 log.end(); 265 266 return uri; 267 } 268 269 } 270 | Popular Tags |