KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbiplugin > PackageServiceAssemblyMojo


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 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: PackageAssemblyMojo.java 16:56:43 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.tools.jbiplugin;
23
24 import java.io.DataInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipOutputStream JavaDoc;
30
31 import org.apache.maven.model.Dependency;
32 import org.apache.maven.plugin.MojoExecutionException;
33
34 /**
35  * Make a JBI Archive of a service assembly project
36  *
37  * @goal jbi-service-assembly
38  * @description Make a JBI Service Assembly Archive from a service assembly project
39  * @requiresDependencyResolution runtime
40  *
41  * @author cdeneux - Capgemini Sud
42  */

43 public class PackageServiceAssemblyMojo extends PackageAbstractMojo {
44
45     /**
46      * Execute the plugin
47      */

48     public void execute() throws MojoExecutionException {
49         try {
50             project.getArtifact().setFile(new File JavaDoc(outputDirectory
51                 + File.separator + jbiName + ".zip"));
52             if (jbiDirectory.exists()) {
53                 if (verbose) {
54                     System.out.println("Start building JBI Service Assembly archive "
55                         + new File JavaDoc(outputDirectory
56                             + File.separator + jbiName + ".zip")
57                             .getAbsolutePath());
58                 }
59                 
60                 new File JavaDoc(outputDirectory
61                     + File.separator + jbiName + ".zip").delete();
62                 
63                 outputDirectory.mkdirs();
64
65                 // Add the jbi files
66
ZipOutputStream JavaDoc zipOutputStream = new ZipOutputStream JavaDoc(
67                     new FileOutputStream JavaDoc(new File JavaDoc(outputDirectory
68                         + File.separator + jbiName + ".zip")));
69                 recurseDirectory(zipOutputStream, jbiDirectory, "");
70                 zipOutputStream.flush();
71                 
72                 // Add the service unit dependencies files
73
addServiceUnitDepencies(zipOutputStream);
74                 zipOutputStream.flush();
75
76                 zipOutputStream.close();
77                 if (verbose) {
78                     System.out.println("JBI Service Assembly archive building done.\n");
79                 }
80             }
81         } catch (Exception JavaDoc e) {
82             e.printStackTrace();
83             throw new MojoExecutionException(e.getLocalizedMessage(), e);
84         }
85     }
86     
87     /**
88      * Add all JBI service unit declared as dependencies
89      *
90      */

91     private void addServiceUnitDepencies(ZipOutputStream JavaDoc zipOutputStream) throws Exception JavaDoc {
92         
93         for (Object JavaDoc object : project.getDependencies()) {
94             if (object instanceof Dependency) {
95                 Dependency dep = (Dependency) object;
96                 // TODO: Change the condition using <packaging> tag of the dependency
97
if ("zip".equals(dep.getType())) {
98                     String JavaDoc zipDependencyFileName = dep.getArtifactId() + "-" + dep.getVersion() + ".zip";
99                     
100                     ZipEntry JavaDoc zipEntry = new ZipEntry JavaDoc(zipDependencyFileName);
101                     DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new FileInputStream JavaDoc(
102                         new File JavaDoc(localRepository.getBasedir()
103                             + File.separator
104                             + dep.getGroupId().replace(".", File.separator)
105                             + File.separator + dep.getArtifactId()
106                             + File.separator + dep.getVersion()
107                             + File.separator + zipDependencyFileName)));
108                     byte[] content = new byte[dis.available()];
109                     dis.readFully(content);
110                     zipOutputStream.putNextEntry(zipEntry);
111                     zipOutputStream.write(content);
112                     zipOutputStream.closeEntry();
113                     if (verbose) {
114                         System.out.println(" "
115                             + zipDependencyFileName + " added to JBI Service Assembly archive");
116                     }
117                 }
118             }
119         }
120     }
121 }
122
Popular Tags