KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > deployment > deploy > SAPackageCheckingTask


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SAPackageCheckingTask.java 154 6 oct. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.deployment.deploy;
23
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.zip.ZipFile JavaDoc;
31
32 import javax.xml.namespace.QName JavaDoc;
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 /**
48  * This task checks the validity of the service assembly package. Some checking
49  * steps are described in the specification (section 6.6 of the JSR 208).
50  *
51  * @author ofabre - EBM Websourcing
52  *
53  */

54 public class SAPackageCheckingTask implements Task {
55
56     /**
57      * logger wrapper
58      */

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 JavaDoc context) throws Exception JavaDoc {
71         String JavaDoc msg = null;
72
73         JBIDescriptor descriptor = (JBIDescriptor) context
74                 .get(DeploymentContextConstants.SA_DESCRIPTOR);
75
76         URI JavaDoc saArchiveURI = (URI JavaDoc) context
77                 .get(DeploymentContextConstants.ARCHIVE_URI);
78
79         /*
80          * Retrieve assembly production element into JBI descriptor
81          */

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 JavaDoc(msg);
88         }
89
90         String JavaDoc saName = assembly.getIdentification().getName();
91
92         /*
93          * Check if sa name is already used
94          */

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 JavaDoc(msg);
101         }
102
103         /*
104          * Check service assembly identifier
105          */

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 JavaDoc(msg);
111         }
112
113         /*
114          * Check potential connection elements
115          */

116         List JavaDoc<Connection> connections = assembly.getConnections();
117         if (connections != null) {
118             checkConnections(connections);
119         }
120
121         /*
122          * Check potential service unit elements
123          */

124         List JavaDoc<ServiceUnit> sus = assembly.getServiceUnits();
125         if (sus != null) {
126             checkServiceUnits(sus, saName, saArchiveURI);
127         }
128
129     }
130
131     /**
132      * Check the validity of connection elements
133      *
134      * @param connections
135      * a {@link List} of {@link Connection} element
136      * @throws Exception
137      * if consumer interface and consumer endpoint and consumer
138      * service are null or empty at the same time or if (provider
139      * endpoint and provider service) are null or empty
140      */

141     protected void checkConnections(List JavaDoc<Connection> connections)
142             throws Exception JavaDoc {
143         String JavaDoc msg = "";
144
145         for (Connection connection : connections) {
146             QName JavaDoc consInterface = connection.getConsumerInterfaceName();
147             QName JavaDoc consService = connection.getConsumerServiceName();
148             String JavaDoc consEndpoint = connection.getConsumerEndpointName();
149             QName JavaDoc provService = connection.getProviderServiceName();
150             String JavaDoc 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 JavaDoc(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 JavaDoc(msg);
169             }
170         }
171     }
172
173     /**
174      * Check the validity of service unit elements
175      *
176      * @param sus
177      * a {@link List} of {@link ServiceUnit} element
178      * @param saName
179      * the service assembly name
180      * @param saArchiveURI
181      * the service assembly archive URI
182      * @throws Exception
183      * if su identifier is null or empty or non unique within the
184      * sa, if target artifact zip and target component are null or
185      * empty, if not all declared su zip are presents in the sa
186      * package or if su has already been deployed to the target
187      * component
188      */

189     protected void checkServiceUnits(List JavaDoc<ServiceUnit> sus, String JavaDoc saName,
190             URI JavaDoc saArchiveURI) throws Exception JavaDoc {
191         String JavaDoc msg = "";
192
193         List JavaDoc<String JavaDoc> suIds = new ArrayList JavaDoc<String JavaDoc>();
194         for (ServiceUnit su : sus) {
195             String JavaDoc suIdentifier = su.getIdentification().getName();
196
197             // Check su identifier : must be non null, non empty and unique
198
// within the sa
199
checkSUIdentifier(suIds, suIdentifier, saName);
200
201             suIds.add(suIdentifier);
202
203             // check target artifact zip and target component entries : must
204
// be non null and non empty
205
String JavaDoc artifactZip = su.getTargetArtifactsZip();
206             String JavaDoc targetComponent = su.getTargetComponentName();
207             checkTargetComponentAndZip(artifactZip, suIdentifier,
208                     targetComponent);
209
210             // check that all declared su zip are presents in the sa package
211
checkSUZipInSAZip(artifactZip, saArchiveURI);
212
213             // Check if su hasn't already been deployed to the target
214
// component
215
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 JavaDoc(msg);
222             }
223
224         }
225     }
226
227     /**
228      * Check su identifier : must be non null, non empty and unique within the
229      * sa
230      *
231      * @param suIds
232      * the su identifier list within the sa
233      * @param suIdentifier
234      * the service unit identifier
235      * @param saName
236      * the service assembly name
237      * @throws Exception
238      * if su identifier is null, empty and non unique within the sa
239      */

240     protected void checkSUIdentifier(List JavaDoc<String JavaDoc> suIds, String JavaDoc suIdentifier,
241             String JavaDoc saName) throws Exception JavaDoc {
242         String JavaDoc 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 JavaDoc(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 JavaDoc(msg);
254         }
255     }
256
257     /**
258      * Check target artifact zip and target component entries : must be non null
259      * and non empty
260      *
261      * @param artifactZip
262      * the artefact zip path into the sa archive
263      * @param suIdentifier
264      * the service unit identifier
265      * @param targetComponent
266      * the targeted component name
267      * @throws Exception
268      * if target artifact zip or target component entries are null
269      * or empty
270      */

271     protected void checkTargetComponentAndZip(String JavaDoc artifactZip,
272             String JavaDoc suIdentifier, String JavaDoc targetComponent) throws Exception JavaDoc {
273         String JavaDoc 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 JavaDoc(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 JavaDoc(msg);
285         }
286     }
287
288     /**
289      * Check that all declared su zip are presents in the sa package
290      *
291      * @param artifactZip
292      * the artefact zip path into the sa archive
293      * @param saArchiveURI
294      * the service assembly archive URI
295      * @throws Exception
296      * if at least one su is missing into the sa archive or if zip
297      * archive can't be correctly close or opened
298      */

299     protected void checkSUZipInSAZip(String JavaDoc artifactZip, URI JavaDoc saArchiveURI)
300             throws Exception JavaDoc {
301         String JavaDoc msg = "";
302         ZipFile JavaDoc 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 JavaDoc(msg);
311             }
312         } finally {
313             try {
314                 zipArchive.close();
315             } catch (IOException JavaDoc e) {
316                 throw new Exception JavaDoc(
317                         "Error while closing the Zip archive file.", e);
318             }
319         }
320     }
321
322     /**
323      * Load a {@link ZipFile} from an {@link URI}
324      *
325      * @param archiveURI
326      * {@link URI} of the {@link ZipFile}
327      * @return the targeted {@link ZipFile}
328      * @throws PetalsException
329      * when {@link ZipFile} can't be opened
330      */

331     protected ZipFile JavaDoc openZipFile(URI JavaDoc archiveURI) throws PetalsException {
332         return ZipUtil.openZipFile(archiveURI);
333     }
334
335     /**
336      * Test if a zip file has a specific entry
337      *
338      * @param archive
339      * the sa {@link ZipFile}
340      * @param entryName
341      * the entry name
342      * @return true if the entry is in the zip file
343      */

344     protected boolean hasEntry(ZipFile JavaDoc zipArchive, String JavaDoc artifactZip) {
345         return ZipUtil.hasEntry(zipArchive, artifactZip);
346     }
347
348     /**
349      * Check if the su is already deployed on the targeted component.
350      *
351      * @param su
352      * the service unit production element
353      *
354      * @return true if the su is already deployed on the targeted component,
355      * false otherwise
356      * @throws Exception
357      * if deploymentService.isDeployedServiceUnit fails
358      */

359     protected boolean isSUAlreadyDeployedOnTargetedComponent(ServiceUnit su)
360             throws Exception JavaDoc {
361         return deploymentService.isDeployedServiceUnit(su
362                 .getTargetComponentName(), su.getIdentification().getName());
363     }
364
365     /**
366      * Check if the sa name is already used by a previously successfuly deployed
367      * sa.
368      *
369      * @param saName
370      * the service assembly name
371      * @return true if the sa name is already used, false otherwise
372      */

373     protected boolean isSANameAlreadyUsed(String JavaDoc saName) {
374
375         Map JavaDoc<String JavaDoc, ServiceAssemblyDataHandler> deployedSACache = deploymentService
376                 .getDeployedSACache();
377
378         return deployedSACache.containsKey(saName);
379     }
380
381     public void undo(HashMap JavaDoc context) {
382         // Nothing to do
383

384     }
385
386 }
387
Popular Tags