KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > management > service > PackageHandler


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: PackageHandler.java 154 2 mai 2006 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.management.service;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.zip.ZipFile JavaDoc;
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 /**
43  * Utility class for handling packages and package URIs
44  *
45  * @author Olivier Fabre - EBM Websourcing
46  *
47  */

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 JavaDoc META_INF_DIR = "META-INF";
57
58     private final static String JavaDoc 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     /**
68      * Load the jbi descriptor from the given archive
69      *
70      * @param archiveURI
71      * the archive URI
72      * @return the {@link JBIDescriptor} object
73      * @throws PetalsException
74      */

75     public JBIDescriptor loadDescriptor(URI JavaDoc archiveURI) throws PetalsException {
76         log.start();
77
78         String JavaDoc msg;
79         JBIDescriptor result = null;
80
81         /*
82          * extract descriptor file to parse and validate
83          */

84
85         File JavaDoc tmpJbiXml = null;
86
87         try {
88             /*
89              * open the package Zip archive file and extract jbi.xml temp
90              */

91             try {
92                 ZipFile JavaDoc zipArchive = ZipUtil.openZipFile(archiveURI);
93
94                 tmpJbiXml = ZipUtil.getEntryAsTemp(zipArchive,
95                         JBIDescriptorBuilder.JBI_DESCRIPTOR_RESOURCE);
96
97                 zipArchive.close();
98             } catch (IOException JavaDoc 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             /*
115              * use builder to parse and validate jbi.xml against the schema
116              */

117
118             JBIDescriptorBuilder descBuilder = new JBIDescriptorBuilder(
119                     tmpJbiXml.toURI(), (java.util.logging.Logger JavaDoc) 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             /*
129              * delete temporary file
130              */

131             if (tmpJbiXml != null) {
132                 tmpJbiXml.delete();
133             }
134         }
135         log.end();
136
137         return result;
138     }
139
140     /**
141      * Load the jbi descriptor from the repository folder of the given entity
142      * (component, shared lib or service assembly).
143      *
144      * @param entityName
145      * name of entity
146      * @param entityType
147      * type of entity (component, shared lib or service assembly)
148      * @return the {@link JBIDescriptor} object
149      * @throws PetalsException
150      */

151     public JBIDescriptor loadDescriptor(String JavaDoc entityName, EntityType entityType)
152             throws PetalsException {
153         log.start();
154
155         String JavaDoc msg;
156         JBIDescriptor result = null;
157
158         /*
159          * extract descriptor file to parse and validate
160          */

161         File JavaDoc tmpJbiXml = null;
162
163         try {
164             /*
165              * Retrieve jbi.xml file from repository
166              */

167             switch (entityType) {
168             case COMPONENT_TYPE:
169                 tmpJbiXml = new File JavaDoc(repositorySrv
170                         .getComponentInstallRoot(entityName), META_INF_DIR
171                         + File.separator + JBI_XML_FILE_NAME);
172                 break;
173             case SA_TYPE:
174                 tmpJbiXml = new File JavaDoc(repositorySrv
175                         .getServiceAssemblyInstallRoot(entityName),
176                         META_INF_DIR + File.separator + JBI_XML_FILE_NAME);
177                 break;
178             case SL_TYPE:
179                 tmpJbiXml = new File JavaDoc(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             /*
194              * use builder to parse and validate jbi.xml against the schema
195              */

196
197             JBIDescriptorBuilder descBuilder = new JBIDescriptorBuilder(
198                     tmpJbiXml.toURI(), (java.util.logging.Logger JavaDoc) 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     /**
213      * Process the given package Zip archive URL string and transform into a
214      * ready-to-use URI object. Additionally, checks if the point specified
215      * exist.
216      *
217      * @return <code>URI</code> the proper package URI.
218      *
219      */

220     public URI JavaDoc processAndGetPackageURI(String JavaDoc archiveZipURL,
221             boolean checkIfExist) {
222         log.start(archiveZipURL);
223
224         String JavaDoc msg;
225         URI JavaDoc uri = null;
226
227         /*
228          * Create URI
229          */

230         // Replace all space by %20
231
archiveZipURL = archiveZipURL.replaceAll(" ", "%20");
232         try {
233             URL JavaDoc url = new URL JavaDoc(archiveZipURL);
234
235             uri = url.toURI();
236         } catch (MalformedURLException JavaDoc mue) {
237             msg = "Wrong package archive location specified. URL: "
238                     + archiveZipURL;
239
240             log.error(msg, mue);
241
242             throw new IllegalArgumentException JavaDoc(msg, mue);
243         } catch (URISyntaxException JavaDoc use) {
244             msg = "Syntax error converting to URI from given location. URL: "
245                     + archiveZipURL;
246             log.error(msg, use);
247
248             throw new IllegalArgumentException JavaDoc(msg, use);
249         }
250
251         /*
252          * check if exists
253          */

254         if (checkIfExist) {
255             File JavaDoc archive = new File JavaDoc(uri);
256
257             if (!archive.exists()) {
258                 msg = "JBI package archive not found. URL: " + archiveZipURL;
259                 log.error(msg);
260
261                 throw new IllegalArgumentException JavaDoc(msg);
262             }
263         }
264         log.end();
265
266         return uri;
267     }
268
269 }
270
Popular Tags