]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/CreateMdkPkg/src/org/tianocore/packaging/FrameworkPkg.java
e12b58438f185360b31cfe9f51d5396944b1515a
[mirror_edk2.git] / Tools / Source / CreateMdkPkg / src / org / tianocore / packaging / FrameworkPkg.java
1 /** @file
2
3 The file is used to install .jar file
4
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 package org.tianocore.packaging;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Enumeration;
23 import java.util.jar.JarEntry;
24 import java.util.jar.JarFile;
25
26 import javax.swing.JFrame;
27
28 /**
29 The class is used to install .jar file
30
31 @since CreateMdkPkg 1.0
32
33 **/
34 public class FrameworkPkg {
35 //
36 // Define class members
37 //
38 static JFrame frame;
39
40 private String pkg = null;
41
42 private JarFile jf = null;
43
44 /**
45 Main clase, used to test
46
47 @param args
48
49 **/
50 public static void main(String[] args) {
51 FrameworkPkg fp = new FrameworkPkg("C:\\Documents and Settings\\hchen30\\Desktop\\com.jar");
52 try {
53 fp.install("C:\\MyWorkspace" + System.getProperty("file.separator"));
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 }
58
59 /**
60 This is the default constructor
61
62 **/
63 public FrameworkPkg() {
64
65 }
66
67 /**
68 This is the override constructor
69
70 @param package_name
71
72 **/
73 public FrameworkPkg(String package_name) {
74 pkg = package_name;
75 }
76
77 /**
78 Get package name
79
80 @param package_name
81
82 **/
83 public void setPkg(String package_name) {
84 pkg = package_name;
85 }
86
87 /**
88 Set Jarfile
89
90 @throws IOException
91
92 **/
93 public void setJarFile() throws IOException {
94 jf = new JarFile(pkg);
95 }
96
97 /**
98 Install the jar file to specific path
99
100 @param dir The target path
101 @return 0 - success
102 @throws IOException
103 @throws BasePkgNotInstalled
104 @throws VerNotEqual
105 @throws GuidNotEqual
106 @throws SameAll
107
108 **/
109 public int install(final String dir) throws IOException, BasePkgNotInstalled, VerNotEqual, GuidNotEqual, SameAll {
110 pre_install();
111 extract(dir);
112 post_install();
113 return 0;
114 }
115
116 /**
117
118 @return
119
120 **/
121 public int uninstall() {
122
123 return 0;
124 }
125
126 /**
127 Check before install
128
129 @throws IOException
130 @throws BasePkgNotInstalled
131 @throws VerNotEqual
132 @throws GuidNotEqual
133 @throws SameAll
134
135 **/
136 protected void pre_install() throws IOException, BasePkgNotInstalled, VerNotEqual, GuidNotEqual, SameAll {
137 jf = new JarFile(pkg);
138 if (false) {
139 throw new BasePkgNotInstalled();
140 }
141 if (false) {
142 throw new VerNotEqual();
143 }
144 if (false) {
145 throw new GuidNotEqual();
146 }
147 if (false) {
148 throw new SameAll();
149 }
150 }
151
152 /**
153 End of install
154
155 @throws IOException
156
157 **/
158 protected void post_install() throws IOException {
159 jf.close();
160
161 }
162
163 /**
164 Extract the jar file to specific dir
165
166 @param dir The target path
167 @throws IOException
168
169 **/
170 private synchronized void extract(String dir) throws IOException {
171
172 int i = 0;
173 try {
174 for (Enumeration e = jf.entries(); e.hasMoreElements(); i++) {
175 JarEntry je = (JarEntry) e.nextElement();
176 if (je.getName().contains("META-INF"))
177 continue;
178 if (je.isDirectory()) {
179 new File(dir + je.getName()).mkdirs();
180 continue;
181 }
182
183 if (je != null) {
184 //
185 // Get an input stream for the entry.
186 //
187 InputStream entryStream = jf.getInputStream(je);
188
189 try {
190 //
191 // Create the output file (clobbering the file if it exists).
192 //
193 FileOutputStream file = new FileOutputStream(dir + je.getName());
194
195 try {
196 //
197 // Allocate a buffer for reading the entry data.
198 //
199 byte[] buffer = new byte[1024];
200 int bytesRead;
201
202 //
203 // Read the entry data and write it to the output file.
204 //
205 while ((bytesRead = entryStream.read(buffer)) != -1) {
206 file.write(buffer, 0, bytesRead);
207 }
208
209 System.out.println(je.getName() + " extracted.");
210 } finally {
211 file.close();
212 }
213 } finally {
214 entryStream.close();
215 }
216 }
217 }
218 } finally {
219 jf.close();
220 }
221 }
222 }
223
224 class BasePkgNotInstalled extends Exception {
225 final static long serialVersionUID = 0;
226 }
227
228 class VerNotEqual extends Exception {
229 final static long serialVersionUID = 0;
230 }
231
232 class GuidNotEqual extends Exception {
233 final static long serialVersionUID = 0;
234 }
235
236 class SameAll extends Exception {
237 final static long serialVersionUID = 0;
238 }