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