KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > repository > RepositoryImpl


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.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: RepositoryImpl.java 250 2006-04-21 12:20:57Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.repository;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.util.zip.ZipException JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29
30 import org.objectweb.petals.PetalsException;
31 import org.objectweb.petals.util.FileUtil;
32 import org.objectweb.petals.util.SystemUtil;
33 import org.objectweb.petals.util.ZipUtil;
34
35 /**
36  * Platform Component Repository service implementation.
37  *
38  * @version $Rev: 250 $ $Date: 2006-04-21 12:20:57Z $
39  * @since Petals 1.0
40  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
41  */

42 public class RepositoryImpl implements RepositoryService {
43
44     /**
45      * Path to repository location within Petals installation directory.
46      */

47     private final static String JavaDoc REPOSITORY_PATH = "repository";
48
49     /**
50      * Path to component install directory
51      */

52     private final static String JavaDoc INSTALL_PATH = "install";
53
54     /**
55      * Path to component working directory
56      */

57     private final static String JavaDoc WORK_PATH = "work";
58
59     
60     /**
61      * The complete repository directory path.
62      */

63     private static String JavaDoc repositoryDirectory = null;
64
65     
66     // ----------------------------------------------------------------------
67
// RepositoryService interface implementation
68
// ----------------------------------------------------------------------
69

70     /**
71      * @see org.objectweb.petals.repository.RepositoryService#containsPackage(java.lang.String)
72      */

73     public boolean containsPackage(String JavaDoc uuid) {
74
75         File JavaDoc installationRoot = getComponentRoot(uuid);
76
77         boolean contain = (installationRoot.exists() && installationRoot.isDirectory());
78
79         return contain;
80     }
81
82     /**
83      * @see org.objectweb.petals.repository.RepositoryService#addPackage(String, java.net.URI)
84      */

85     public URI JavaDoc addPackage(String JavaDoc uuid, URI JavaDoc zipArchiveLocation)
86             throws PetalsException {
87
88         // don't care about replacing existing data
89
if (containsPackage(uuid)) {
90             removePackage(uuid);
91         }
92
93         File JavaDoc installationRoot = new File JavaDoc(getComponentRoot(uuid),INSTALL_PATH);
94         
95         File JavaDoc workRoot = new File JavaDoc(new File JavaDoc(installationRoot,".."),WORK_PATH);
96         
97
98         // recreate dirs necessary for adding the package into the repository
99
if (!installationRoot.mkdirs()) {
100             throw new PetalsException(
101                     "Failed to create installation root dir: " +
102                     installationRoot.getAbsolutePath());
103         }
104         // recreate dirs necessary for adding the package into the repository
105
if (!workRoot.mkdirs()) {
106             throw new PetalsException(
107                     "Failed to create installation root dir: " +
108                     workRoot.getAbsolutePath());
109         }
110
111         // explode package Zip archive file into repository
112
ZipFile JavaDoc zipArchive = null;
113
114         try {
115             File JavaDoc archiveFile = new File JavaDoc(zipArchiveLocation);
116             zipArchive = new ZipFile JavaDoc(archiveFile);
117         } catch (ZipException JavaDoc ze) {
118             throw new PetalsException("Unable to open Zip archive file: " +
119                     zipArchiveLocation.toString(), ze);
120         } catch (IOException JavaDoc ioe) {
121             throw new PetalsException("Unexpected exception.", ioe);
122         }
123
124         ZipUtil.explodeIntoDirectory(zipArchive, installationRoot);
125         return installationRoot.toURI();
126
127     }
128     
129     /**
130      * @see org.objectweb.petals.repository.RepositoryService#explodeSUIntoSAInstallFolder(String, URI, String)
131      */

132     public URI JavaDoc explodeSUIntoSAInstallFolder(String JavaDoc suId,
133                                 URI JavaDoc suZipLocation,
134                                 String JavaDoc saId)
135             throws PetalsException {
136
137         File JavaDoc installationRoot = new File JavaDoc(getComponentInstallRoot(saId),suId);
138
139         // recreate dirs necessary for adding the package into the repository
140
if (!installationRoot.mkdirs()) {
141             throw new PetalsException(
142                     "Failed to create installation root dir: " +
143                     installationRoot.getAbsolutePath());
144         }
145
146         // explode package Zip archive file into repository
147
ZipFile JavaDoc zipArchive = null;
148
149         try {
150             File JavaDoc archiveFile = new File JavaDoc(suZipLocation);
151             zipArchive = new ZipFile JavaDoc(archiveFile);
152         } catch (ZipException JavaDoc ze) {
153             throw new PetalsException("Unable to open Zip archive file: " +
154                     suZipLocation.toString(), ze);
155         } catch (IOException JavaDoc ioe) {
156             throw new PetalsException("Unexpected exception.", ioe);
157         }
158
159         ZipUtil.explodeIntoDirectory(zipArchive, installationRoot);
160         return installationRoot.toURI();
161
162     }
163
164     /**
165      * @see org.objectweb.petals.repository.RepositoryService#removePackage(java.lang.String)
166      */

167     public boolean removePackage(String JavaDoc componentId) {
168
169         boolean removed = true;
170         File JavaDoc installationRoot = getComponentRoot(componentId);
171
172         // adding component don't care about replacing existing data
173
if (installationRoot.exists()) {
174             removed = FileUtil.removeDirectory(installationRoot);
175         }
176
177         return removed;
178     }
179
180     // ----------------------------------------------------------------------
181
// implementation specific methods
182
// ----------------------------------------------------------------------
183

184     private String JavaDoc getRepositoryDirectory() {
185         if (repositoryDirectory == null) {
186             repositoryDirectory = SystemUtil.getPetalsInstallDirectory()
187                     .getAbsolutePath() + File.separator + REPOSITORY_PATH;
188         }
189         return repositoryDirectory;
190     }
191
192     public File JavaDoc getComponentRoot(String JavaDoc componentId) {
193
194         String JavaDoc path = getRepositoryDirectory() + File.separator + componentId;
195
196         File JavaDoc installationRoot = new File JavaDoc(path);
197
198         return installationRoot;
199     }
200     
201     public File JavaDoc getComponentInstallRoot(String JavaDoc componentId) {
202
203         String JavaDoc path = getRepositoryDirectory() + File.separator + componentId + File.separator + INSTALL_PATH;
204
205         File JavaDoc installationRoot = new File JavaDoc(path);
206
207         return installationRoot;
208     }
209
210     public File JavaDoc getComponentWorkRoot(String JavaDoc componentId) {
211
212         String JavaDoc path = getRepositoryDirectory() + File.separator + componentId+ File.separator +WORK_PATH;
213
214         File JavaDoc installationRoot = new File JavaDoc(path);
215
216         return installationRoot;
217     }
218
219 }
220
Popular Tags