KickJava   Java API By Example, From Geeks To Geeks.

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


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: SAAndSUExtractionTask.java 154 6 oct. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.deployment.deploy;
23
24 import java.net.URI JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.petals.PetalsException;
30 import org.objectweb.petals.jbi.management.deployment.DeploymentContextConstants;
31 import org.objectweb.petals.jbi.management.deployment.DeploymentUtils;
32 import org.objectweb.petals.jbi.management.service.ManagementException;
33 import org.objectweb.petals.jbi.management.service.PackageHandler;
34 import org.objectweb.petals.processor.Task;
35 import org.objectweb.petals.repository.RepositoryService;
36 import org.objectweb.petals.tools.jbicommon.descriptor.JBIDescriptor;
37 import org.objectweb.petals.tools.jbicommon.descriptor.ServiceUnit;
38 import org.objectweb.petals.util.LoggingUtil;
39
40 /**
41  * This task extracts service assembly package into the repository
42  *
43  * @author ofabre - EBM Websourcing
44  *
45  */

46 public class SAAndSUExtractionTask implements Task {
47
48     /**
49      * Package Handler
50      */

51     protected PackageHandler packageHandler;
52
53     /**
54      * Platform Component :: Repository Service
55      */

56     protected RepositoryService repositorySrv;
57
58     /**
59      * logger wrapper
60      */

61     protected LoggingUtil log;
62
63     public SAAndSUExtractionTask(PackageHandler packageHandler,
64             RepositoryService repositorySrv, LoggingUtil log) {
65         super();
66         this.packageHandler = packageHandler;
67         this.repositorySrv = repositorySrv;
68         this.log = log;
69     }
70
71     @SuppressWarnings JavaDoc("unchecked")
72     public void execute(HashMap JavaDoc context) throws Exception JavaDoc {
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          * Explode SA
81          */

82         URI JavaDoc saInstallRoot = explodeSA(saArchiveURI, descriptor);
83
84         /*
85          * Explodes SUs
86          */

87         Map JavaDoc<String JavaDoc, URI JavaDoc> suInstallRoots = expendSUS(saInstallRoot, descriptor);
88
89         /*
90          * Fill context
91          */

92         context.put(DeploymentContextConstants.SA_INSTALL_ROOT, saInstallRoot);
93         context
94                 .put(DeploymentContextConstants.SU_INSTALL_ROOTS,
95                         suInstallRoots);
96
97     }
98
99     /**
100      * Expend service assembly package into the repository
101      *
102      * @param saArchiveURI
103      * the service assembly archive {@link URI}
104      * @param descriptor
105      * the service assembly descriptor
106      * @return return installation {@link URI} of the expanded package, null if
107      * it fails
108      * @throws ManagementException
109      * when it fails to expand service assembly package into
110      * repository
111      */

112     protected URI JavaDoc explodeSA(URI JavaDoc saArchiveURI, JBIDescriptor descriptor)
113         throws ManagementException {
114
115         String JavaDoc msg;
116         URI JavaDoc installationRoot = null;
117
118         try {
119             installationRoot = repositorySrv.addServiceAssemblyPackage(
120                     DeploymentUtils.getServiceAssemblyName(descriptor),
121                     saArchiveURI);
122
123         } catch (PetalsException pe) {
124             msg = "Unexpected error adding JBI installation "
125                     + "package into repository. ";
126             log.error(msg, pe);
127             throw new ManagementException(msg, pe);
128         }
129
130         if (installationRoot == null) {
131             msg = "Unexpected Platform repository service error. The SA"
132                     + "installation root must NOT be null: "
133                     + DeploymentUtils.getServiceAssemblyName(descriptor);
134             log.error(msg);
135             throw new ManagementException(msg);
136         }
137
138         return installationRoot;
139     }
140
141     /**
142      * Explode service unit packages into the repository *
143      *
144      * @param saInstallRoot
145      * the root {@link URI} of the service assembly
146      * @param descriptor
147      * the service assembly descriptor
148      * @return the map containing all su install roots
149      * @throws ManagementException
150      * if the repository service doesn't manage to expend su archive
151      */

152     protected Map JavaDoc<String JavaDoc, URI JavaDoc> expendSUS(URI JavaDoc saInstallRoot,
153             JBIDescriptor descriptor) throws ManagementException {
154         Map JavaDoc<String JavaDoc, URI JavaDoc> suInstallRoots = new HashMap JavaDoc<String JavaDoc, URI JavaDoc>();
155         List JavaDoc<ServiceUnit> sus = descriptor.getServiceAssembly()
156                 .getServiceUnits();
157         for (ServiceUnit unit : sus) {
158             URI JavaDoc suArchiveURI = getSUArchiveURI(unit, saInstallRoot);
159             URI JavaDoc suInstallRoot = expandSUIntoSA(unit.getIdentification()
160                     .getName(), suArchiveURI, DeploymentUtils
161                     .getServiceAssemblyName(descriptor));
162             suInstallRoots.put(unit.getIdentification().getName(),
163                     suInstallRoot);
164         }
165         return suInstallRoots;
166     }
167
168     /**
169      * Retrieve the service unit archive {@link URI}
170      *
171      * @param su
172      * the {@link ServiceUnit} production element
173      * @param saInstallRoot
174      * the sa installation root {@link URI}
175      * @return the su archive {@link URI}
176      */

177     protected URI JavaDoc getSUArchiveURI(ServiceUnit su, URI JavaDoc saInstallRoot) {
178         String JavaDoc suZipNAme = su.getTargetArtifactsZip();
179         String JavaDoc suZipURL = "file://" + saInstallRoot.getPath() + suZipNAme;
180         return packageHandler.processAndGetPackageURI(suZipURL.replaceAll(" ",
181                 "%20"), true);
182     }
183
184     /**
185      * Expand a service unit package into its service assembly installRoot.
186      *
187      * @param suId
188      * the unique identifier of the su
189      * @param suZipLocation
190      * URI representing the su zip location
191      * @param saId
192      * the unique identifier of the sa
193      * @return SU installationRoot, null if it fails
194      * @throws ManagementException
195      * if it fails to expend the su package to the repository
196      */

197     protected URI JavaDoc expandSUIntoSA(String JavaDoc suId, URI JavaDoc suZipLocation, String JavaDoc saId)
198         throws ManagementException {
199         log.start();
200         String JavaDoc msg;
201         URI JavaDoc result = null;
202
203         try {
204             result = repositorySrv.explodeSUIntoSAInstallFolder(suId,
205                     suZipLocation, saId);
206         } catch (PetalsException pe) {
207             msg = "Unexpected error expending service JBI installation "
208                     + "package into repository.";
209             log.error(msg, pe);
210             throw new ManagementException(msg, pe);
211         }
212         if (result == null) {
213             msg = "Unexpected platform repository service error. The "
214                     + "su root must NOT be null: " + suId;
215             log.error(msg);
216             throw new ManagementException(msg);
217         }
218         log.end();
219         return result;
220     }
221
222     public void undo(HashMap JavaDoc context) {
223         JBIDescriptor descriptor = (JBIDescriptor) context
224                 .get(DeploymentContextConstants.SA_DESCRIPTOR);
225
226         try {
227             repositorySrv.removeServiceAssemblyPackage(DeploymentUtils
228                     .getServiceAssemblyName(descriptor));
229         } catch (PetalsException e) {
230             String JavaDoc msg = "Failed to revert a SAAndSUExtractionTask";
231             log.error(msg, e);
232         }
233
234     }
235
236 }
237
Popular Tags