KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > packaging > ControlJarTask


1 package org.apache.beehive.controls.runtime.packaging;
2
3 /*
4  * Copyright 2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * $Header:$
19  */

20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.taskdefs.Jar;
31 import org.apache.tools.ant.taskdefs.Manifest;
32 import org.apache.tools.ant.taskdefs.ManifestException;
33 import org.apache.tools.ant.types.Resource;
34 import org.apache.tools.ant.types.FileSet;
35 import org.apache.tools.ant.util.FileUtils;
36 import org.apache.tools.zip.ZipOutputStream;
37
38 /**
39  * The ControlTask class extends the standard ant Jar task to perform
40  * additional processing for JAR files that contain Beehive Controls.
41  */

42 public class ControlJarTask extends Jar
43 {
44     private static FileUtils fileUtils = FileUtils.newFileUtils();
45
46     /**
47      * Step #1: Wrap the implementation of Zip.grabResources. This method identifies the
48      * set of resources to be stored in the JAR file. For each added/updated resource,
49      * the overrided method will look for an associated .manifest file that any
50      * JAR manifest data to add/update in the JAR manifest.
51      */

52     protected Resource[][] grabResources(FileSet[] filesets)
53     {
54         //
55
// Get the list of resources being added/updated by calling Zip.grabResources
56
//
57
Resource [][] resources = super.grabResources(filesets);
58
59         //
60
// Iterate through the resources for each input FileSet, looking for an associated
61
// manifest file.
62
//
63
for (int i = 0; i < filesets.length; i++)
64         {
65             if (resources[i].length == 0)
66                 continue;
67
68             File JavaDoc base = filesets[i].getDir(getProject());
69             Resource [] setResources = resources[i];
70
71             for (int j = 0; j < setResources.length; j++)
72             {
73                 File JavaDoc manifestFile =
74                     fileUtils.resolveFile(base, setResources[j].getName() + ".manifest");
75                 if (manifestFile.exists())
76                     manifestFiles.add(manifestFile);
77             }
78         }
79
80         return resources;
81     }
82
83     /**
84      * Step #2: Override Jar.initZipOutputStream to inject manifest sections. This is done
85      * by treating them as if they were 'inline' entries, from a <jar> task perspective.
86      */

87     protected void initZipOutputStream(ZipOutputStream zOut) throws IOException JavaDoc, BuildException
88     {
89         if (manifestFiles.size() != 0)
90         {
91             //
92
// Create a default (empty) manifest
93
//
94
Manifest mergeManifest = Manifest.getDefaultManifest();
95
96             //
97
// Iterate through each found manifest file, merging its contents into the
98
// merge manifest
99
//
100
for (File JavaDoc manifestFile : manifestFiles)
101             {
102                 FileInputStream JavaDoc fis = null;
103                 try
104                 {
105                     fis = new FileInputStream JavaDoc(manifestFile);
106                     Manifest resourceManifest = new Manifest(new InputStreamReader JavaDoc(fis));
107                     mergeManifest.merge(resourceManifest);
108                 }
109                 catch (IOException JavaDoc ioe)
110                 {
111                     throw new BuildException("Unable to read manifest file:" + manifestFile, ioe);
112                 }
113                 catch (ManifestException me)
114                 {
115                     throw new BuildException("Unable to process manifest file: "+ manifestFile, me);
116                 }
117                 finally
118                 {
119                     if (fis != null) fis.close();
120                 }
121             }
122
123             //
124
// Set the merge manifest as the 'configured' manifest. This will treat its
125
// contents as if they had been included inline with the <jar> task, with
126
// similar precedence rules.
127
//
128
try
129             {
130                 addConfiguredManifest(mergeManifest);
131             }
132             catch (ManifestException me)
133             {
134                 throw new BuildException("Unable to add new manifest entries:" + me);
135             }
136         }
137
138         super.initZipOutputStream(zOut);
139     }
140
141     protected void addToManifest(Manifest jarManifest, List JavaDoc<File JavaDoc> mergeList)
142     {
143     }
144
145     /**
146      * Reset the manifest file list to be empty
147      */

148     protected void cleanUp()
149     {
150         manifestFiles = new Vector JavaDoc<File JavaDoc>();
151     }
152
153     /**
154      * Contains the set of manifest entries to merge into the JAR manifest
155      */

156     private List JavaDoc<File JavaDoc> manifestFiles = new Vector JavaDoc<File JavaDoc>();
157 }
158
Popular Tags