KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > ProjectConfigurator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.cayenne.conf.Configuration;
27 import org.apache.cayenne.util.Util;
28 import org.apache.cayenne.util.ZipUtil;
29
30 /**
31  * Performs on the fly reconfiguration of Cayenne projects.
32  *
33  * @author Andrus Adamchik
34  */

35 public class ProjectConfigurator {
36     protected ProjectConfigInfo info;
37
38     public ProjectConfigurator(ProjectConfigInfo info) {
39         this.info = info;
40     }
41
42     /**
43      * Performs reconfiguration of the project.
44      *
45      * @throws ProjectException
46      */

47     public void execute() throws ProjectException {
48         File JavaDoc tmpDir = null;
49         File JavaDoc tmpDest = null;
50         try {
51             // initialize default settings
52
if (info.getDestJar() == null) {
53                 info.setDestJar(info.getSourceJar());
54             }
55
56             // sanity check
57
validate();
58
59             // do the processing
60
tmpDir = makeTempDirectory();
61             ZipUtil.unzip(info.getSourceJar(), tmpDir);
62
63             reconfigureProject(tmpDir);
64
65             tmpDest = makeTempDestJar();
66             ZipUtil.zip(tmpDest, tmpDir, tmpDir.listFiles(), '/');
67
68             // finally, since everything goes well so far, rename temp file to final name
69
if (info.getDestJar().exists() && !info.getDestJar().delete()) {
70                 throw new IOException JavaDoc(
71                     "Can't delete old jar file: " + info.getDestJar());
72             }
73
74             if (!tmpDest.renameTo(info.getDestJar())) {
75                 throw new IOException JavaDoc(
76                     "Error renaming: " + tmpDest + " to " + info.getDestJar());
77             }
78         } catch (IOException JavaDoc ex) {
79             throw new ProjectException("Error performing reconfiguration.", ex);
80         } finally {
81             if (tmpDir != null) {
82                 cleanup(tmpDir);
83             }
84
85             if (tmpDest != null) {
86                 tmpDest.delete();
87             }
88         }
89     }
90
91     /**
92      * Performs reconfiguration of the unjarred project.
93      *
94      * @param projectDir a directory where a working copy of the project is
95      * located.
96      */

97     protected void reconfigureProject(File JavaDoc projectDir)
98         throws ProjectException {
99         File JavaDoc projectFile = new File JavaDoc(projectDir, Configuration.DEFAULT_DOMAIN_FILE);
100
101         // process alternative project file
102
if (info.getAltProjectFile() != null) {
103             if (!Util.copy(info.getAltProjectFile(), projectFile)) {
104                 throw new ProjectException(
105                     "Can't copy project file: " + info.getAltProjectFile());
106             }
107         }
108
109         // copy driver files, delete unused
110
Iterator JavaDoc it = info.getNodes().iterator();
111         boolean needFix = it.hasNext();
112         while (it.hasNext()) {
113             DataNodeConfigInfo nodeInfo = (DataNodeConfigInfo) it.next();
114             String JavaDoc name = nodeInfo.getName();
115
116             File JavaDoc targetDriverFile =
117                 new File JavaDoc(projectDir, name + DataNodeFile.LOCATION_SUFFIX);
118
119             // these are the two cases when the driver file must be deleted
120
if (nodeInfo.getDataSource() != null
121                 || nodeInfo.getDriverFile() != null) {
122                 if (targetDriverFile.exists()) {
123                     targetDriverFile.delete();
124                 }
125             }
126
127             if (nodeInfo.getDriverFile() != null
128                 && !nodeInfo.getDriverFile().equals(targetDriverFile)) {
129                 // need to copy file from another location
130
if (!Util.copy(nodeInfo.getDriverFile(), targetDriverFile)) {
131                     throw new ProjectException(
132                         "Can't copy driver file from "
133                             + nodeInfo.getDriverFile());
134                 }
135             }
136         }
137
138         // load project
139
if (needFix) {
140             // read the project and fix data nodes
141
PartialProject project = new PartialProject(projectFile);
142             project.updateNodes(info.getNodes());
143             project.save();
144         }
145     }
146
147     /**
148      * Returns a temporary file for the destination jar.
149      */

150     protected File JavaDoc makeTempDestJar() throws IOException JavaDoc {
151         File JavaDoc destFolder = info.getDestJar().getParentFile();
152         if (destFolder != null && !destFolder.isDirectory()) {
153             if (!destFolder.mkdirs()) {
154                 throw new IOException JavaDoc(
155                     "Can't create directory: " + destFolder.getCanonicalPath());
156             }
157         }
158
159         String JavaDoc baseName = "tmp_" + info.getDestJar().getName();
160
161         // seeting upper limit on a number of tries, though normally we would expect
162
// to succeed from the first attempt...
163
for (int i = 0; i < 50; i++) {
164             File JavaDoc tmpFile =
165                 (destFolder != null)
166                     ? new File JavaDoc(destFolder, baseName + i)
167                     : new File JavaDoc(baseName + i);
168             if (!tmpFile.exists()) {
169                 return tmpFile;
170             }
171         }
172
173         throw new IOException JavaDoc("Problems creating temporary file.");
174     }
175
176     /**
177      * Deletes a temporary directories and files created.
178      */

179     protected void cleanup(File JavaDoc dir) {
180         Util.delete(dir.getPath(), true);
181     }
182
183     /**
184      * Creates a temporary directory to unjar the jar file.
185      *
186      * @return File
187      * @throws IOException
188      */

189     protected File JavaDoc makeTempDirectory() throws IOException JavaDoc {
190         File JavaDoc destFolder = info.getDestJar().getParentFile();
191         if (destFolder != null && !destFolder.isDirectory()) {
192             if (!destFolder.mkdirs()) {
193                 throw new IOException JavaDoc(
194                     "Can't create directory: " + destFolder.getCanonicalPath());
195             }
196         }
197
198         String JavaDoc baseName = info.getDestJar().getName();
199         if (baseName.endsWith(".jar")) {
200             baseName = baseName.substring(0, baseName.length() - 4);
201         }
202
203         // seeting upper limit on a number of tries, though normally we would expect
204
// to succeed from the first attempt...
205
for (int i = 0; i < 50; i++) {
206             File JavaDoc tmpDir =
207                 (destFolder != null)
208                     ? new File JavaDoc(destFolder, baseName + i)
209                     : new File JavaDoc(baseName + i);
210             if (!tmpDir.exists()) {
211                 if (!tmpDir.mkdir()) {
212                     throw new IOException JavaDoc(
213                         "Can't create directory: " + tmpDir.getCanonicalPath());
214                 }
215
216                 return tmpDir;
217             }
218         }
219
220         throw new IOException JavaDoc("Problems creating temporary directory.");
221     }
222
223     /**
224      * Validates consistency of the reconfiguration information.
225      */

226     protected void validate() throws IOException JavaDoc, ProjectException {
227         if (info == null) {
228             throw new ProjectException("ProjectConfig info is not set.");
229         }
230
231         if (info.getSourceJar() == null) {
232             throw new ProjectException("Source jar file is not set.");
233         }
234
235         if (!info.getSourceJar().isFile()) {
236             throw new IOException JavaDoc(info.getSourceJar() + " is not a file.");
237         }
238
239         if (!info.getSourceJar().canRead()) {
240             throw new IOException JavaDoc("Can't read file: " + info.getSourceJar());
241         }
242     }
243 }
244
Popular Tags