1 22 package org.objectweb.petals.jbi.management.deployment.deploy; 23 24 import java.io.IOException ; 25 import java.net.URI ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.zip.ZipFile ; 31 32 import javax.xml.namespace.QName ; 33 34 import org.objectweb.petals.PetalsException; 35 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants; 36 import org.objectweb.petals.jbi.management.deployment.DeploymentServiceImpl; 37 import org.objectweb.petals.jbi.management.deployment.ServiceAssemblyDataHandler; 38 import org.objectweb.petals.processor.Task; 39 import org.objectweb.petals.tools.jbicommon.descriptor.Connection; 40 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor; 41 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceAssembly; 42 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit; 43 import org.objectweb.petals.util.LoggingUtil; 44 import org.objectweb.petals.util.StringHelper; 45 import org.objectweb.petals.util.ZipUtil; 46 47 54 public class SAPackageCheckingTask implements Task { 55 56 59 protected LoggingUtil log; 60 61 protected DeploymentServiceImpl deploymentService; 62 63 public SAPackageCheckingTask(LoggingUtil log, 64 DeploymentServiceImpl deploymentService) { 65 super(); 66 this.log = log; 67 this.deploymentService = deploymentService; 68 } 69 70 public void execute(HashMap context) throws Exception { 71 String msg = null; 72 73 JBIDescriptor descriptor = (JBIDescriptor) context 74 .get(DeploymentContextConstants.SA_DESCRIPTOR); 75 76 URI saArchiveURI = (URI ) context 77 .get(DeploymentContextConstants.ARCHIVE_URI); 78 79 82 ServiceAssembly assembly = descriptor.getServiceAssembly(); 83 if (assembly == null) { 84 msg = "Given archive isn't a service assembly archive : " 85 + "missing service assembly production element into jbi descriptor"; 86 log.error(msg); 87 throw new Exception (msg); 88 } 89 90 String saName = assembly.getIdentification().getName(); 91 92 95 if (isSANameAlreadyUsed(saName)) { 96 msg = "You are trying to deploy a sa with an identifier " 97 + "that is used by a previously successfuly deployed sa (" 98 + saName + ")."; 99 log.error(msg); 100 throw new Exception (msg); 101 } 102 103 106 if (StringHelper.isEmpty(saName)) { 107 msg = "Unique identifier of a service assembly must be non null and non empty (" 108 + saArchiveURI.getPath() + ")."; 109 log.error(msg); 110 throw new Exception (msg); 111 } 112 113 116 List <Connection> connections = assembly.getConnections(); 117 if (connections != null) { 118 checkConnections(connections); 119 } 120 121 124 List <ServiceUnit> sus = assembly.getServiceUnits(); 125 if (sus != null) { 126 checkServiceUnits(sus, saName, saArchiveURI); 127 } 128 129 } 130 131 141 protected void checkConnections(List <Connection> connections) 142 throws Exception { 143 String msg = ""; 144 145 for (Connection connection : connections) { 146 QName consInterface = connection.getConsumerInterfaceName(); 147 QName consService = connection.getConsumerServiceName(); 148 String consEndpoint = connection.getConsumerEndpointName(); 149 QName provService = connection.getProviderServiceName(); 150 String provEndpoint = connection.getProviderEndpointName(); 151 152 if (consInterface == null 153 || StringHelper.isEmpty(consInterface.getLocalPart())) { 154 if (StringHelper.isEmpty(consEndpoint) || consService == null 155 || StringHelper.isEmpty(consService.getLocalPart())) { 156 msg = "Consumer interface and (consumer service or consumer endpoint) " 157 + "must not be null or empty at the same time."; 158 log.error(msg); 159 throw new Exception (msg); 160 } 161 } 162 if (StringHelper.isEmpty(provEndpoint) 163 || (provService == null || StringHelper.isEmpty(provService 164 .getLocalPart()))) { 165 msg = "Provider service and provider endpoint) " 166 + "must not be null or empty."; 167 log.error(msg); 168 throw new Exception (msg); 169 } 170 } 171 } 172 173 189 protected void checkServiceUnits(List <ServiceUnit> sus, String saName, 190 URI saArchiveURI) throws Exception { 191 String msg = ""; 192 193 List <String > suIds = new ArrayList <String >(); 194 for (ServiceUnit su : sus) { 195 String suIdentifier = su.getIdentification().getName(); 196 197 checkSUIdentifier(suIds, suIdentifier, saName); 200 201 suIds.add(suIdentifier); 202 203 String artifactZip = su.getTargetArtifactsZip(); 206 String targetComponent = su.getTargetComponentName(); 207 checkTargetComponentAndZip(artifactZip, suIdentifier, 208 targetComponent); 209 210 checkSUZipInSAZip(artifactZip, saArchiveURI); 212 213 if (isSUAlreadyDeployedOnTargetedComponent(su)) { 216 msg = "You are trying to deploy a su (" 217 + su.getIdentification().getName() 218 + ")that have already been deployed on the targeted component (" 219 + su.getTargetComponentName() + ")."; 220 log.error(msg); 221 throw new Exception (msg); 222 } 223 224 } 225 } 226 227 240 protected void checkSUIdentifier(List <String > suIds, String suIdentifier, 241 String saName) throws Exception { 242 String msg = ""; 243 if (StringHelper.isEmpty(suIdentifier)) { 244 msg = "Identifier of a service unit must be non null and non empty (SA name : " 245 + saName + ")."; 246 log.error(msg); 247 throw new Exception (msg); 248 } 249 if (suIds.contains(suIdentifier)) { 250 msg = "Identifier of a service unit must be unique within a single SA (SA name : " 251 + saName + ")."; 252 log.error(msg); 253 throw new Exception (msg); 254 } 255 } 256 257 271 protected void checkTargetComponentAndZip(String artifactZip, 272 String suIdentifier, String targetComponent) throws Exception { 273 String msg = ""; 274 if (StringHelper.isEmpty(artifactZip)) { 275 msg = "Target artifact zip of a service unit must be non null " 276 + "and non empty (SU name : " + suIdentifier + ")."; 277 log.error(msg); 278 throw new Exception (msg); 279 } 280 if (StringHelper.isEmpty(targetComponent)) { 281 msg = "Target component name of a service unit must be non null " 282 + "and non empty (SU name : " + suIdentifier + ")."; 283 log.error(msg); 284 throw new Exception (msg); 285 } 286 } 287 288 299 protected void checkSUZipInSAZip(String artifactZip, URI saArchiveURI) 300 throws Exception { 301 String msg = ""; 302 ZipFile zipArchive = openZipFile(saArchiveURI); 303 try { 304 if (!hasEntry(zipArchive, artifactZip)) { 305 msg = "The specified target artifact zip in JBI " 306 + "Descriptor's service unit declaration was not " 307 + "found within deployment package Zip archive file : " 308 + saArchiveURI.getPath() + "."; 309 log.error(msg); 310 throw new Exception (msg); 311 } 312 } finally { 313 try { 314 zipArchive.close(); 315 } catch (IOException e) { 316 throw new Exception ( 317 "Error while closing the Zip archive file.", e); 318 } 319 } 320 } 321 322 331 protected ZipFile openZipFile(URI archiveURI) throws PetalsException { 332 return ZipUtil.openZipFile(archiveURI); 333 } 334 335 344 protected boolean hasEntry(ZipFile zipArchive, String artifactZip) { 345 return ZipUtil.hasEntry(zipArchive, artifactZip); 346 } 347 348 359 protected boolean isSUAlreadyDeployedOnTargetedComponent(ServiceUnit su) 360 throws Exception { 361 return deploymentService.isDeployedServiceUnit(su 362 .getTargetComponentName(), su.getIdentification().getName()); 363 } 364 365 373 protected boolean isSANameAlreadyUsed(String saName) { 374 375 Map <String , ServiceAssemblyDataHandler> deployedSACache = deploymentService 376 .getDeployedSACache(); 377 378 return deployedSACache.containsKey(saName); 379 } 380 381 public void undo(HashMap context) { 382 384 } 385 386 } 387 | Popular Tags |