KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > wizard > ArchiveType


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive.wizard;
25
26 // ToolBox
27
import org.enhydra.tool.ToolBoxInfo;
28 import org.enhydra.tool.archive.Descriptor;
29 import org.enhydra.tool.archive.EarPlan;
30 import org.enhydra.tool.archive.EjbPlan;
31 import org.enhydra.tool.archive.JarPlan;
32 import org.enhydra.tool.archive.ServicePlan;
33 import org.enhydra.tool.archive.WarPlan;
34 import org.enhydra.tool.archive.ArchiveException;
35 import org.enhydra.tool.archive.ArchiveTool;
36 import org.enhydra.tool.archive.ManifestKeys;
37 import org.enhydra.tool.common.PathHandle;
38 import org.enhydra.tool.common.SwingUtil;
39 import org.enhydra.tool.common.ToolException;
40
41 // JDK
42
import javax.swing.*;
43 import javax.swing.border.*;
44 import java.awt.*;
45 import java.awt.event.ActionEvent JavaDoc;
46 import java.awt.event.ActionListener JavaDoc;
47 import java.io.BufferedInputStream JavaDoc;
48 import java.io.File JavaDoc;
49 import java.io.FileInputStream JavaDoc;
50 import java.io.FileNotFoundException JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.util.Properties JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.ResourceBundle JavaDoc;
55 import java.util.jar.JarInputStream JavaDoc;
56 import java.util.jar.JarEntry JavaDoc;
57 import java.util.jar.Manifest JavaDoc;
58
59 //
60
abstract public class ArchiveType implements ManifestKeys {
61     private boolean mru = true;
62     private JarPlan plan = null;
63     private ArchivePanel[] panels = new ArchivePanel[0];
64
65     static public ArchiveType createType(JarPlan plan) {
66         ArchiveType type = null;
67
68         if (plan instanceof WarPlan) {
69             type = new WarType();
70             type.setPlan(plan);
71         } else if (plan instanceof EjbPlan) {
72             type = new EjbType();
73             type.setPlan(plan);
74         } else if (plan instanceof EarPlan) {
75             type = new EarType();
76             type.setPlan(plan);
77         } else if (plan instanceof ServicePlan) {
78             type = new ServiceType();
79             type.setPlan(plan);
80         } else {
81
82             // default JarPlan
83
type = new E3Type();
84             type.setPlan(plan);
85         }
86         return type;
87     }
88
89     //
90
static protected ArchiveType[] getAllTypes() {
91         ArchiveType[] types = new ArchiveType[0];
92
93         if (ToolBoxInfo.isEnhydra3()) {
94             types = new ArchiveType[2];
95             types[0] = new WarType();
96             types[1] = new E3Type();
97         } else {
98             types = new ArchiveType[5];
99             types[0] = new WarType();
100             types[1] = new EjbType();
101             types[2] = new EarType();
102             types[3] = new ServiceType();
103             types[4] = new E3Type();
104         }
105         return types;
106     }
107
108     //
109
public void setMostRecentlyUsed(boolean b) {
110       mru = b;
111     }
112     public boolean isMostRecentlyUsed() {
113       return mru;
114     }
115
116     public String JavaDoc toString() {
117         return getSelectionName();
118     }
119
120     public void readArchive(String JavaDoc archive) {
121         JarInputStream JavaDoc jar = null;
122         BufferedInputStream JavaDoc buf = null;
123         Manifest JavaDoc manifest = null;
124         JarEntry JavaDoc entry = null;
125         ArrayList JavaDoc list = new ArrayList JavaDoc();
126         String JavaDoc[] entries = new String JavaDoc[0];
127         PathHandle path = PathHandle.createPathHandle(archive);
128
129         if (path.isFile()) {
130             try {
131                 getPlan().setArchivePath(path.getPath());
132                 buf =
133                     new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(path.getFile()));
134                 jar = new JarInputStream JavaDoc(buf);
135                 manifest = jar.getManifest();
136                 entry = jar.getNextJarEntry();
137                 while (entry != null) {
138                     list.add(entry.getName());
139                     entry = jar.getNextJarEntry();
140                 }
141                 list.trimToSize();
142                 entries = new String JavaDoc[list.size()];
143                 entries = (String JavaDoc[]) list.toArray(entries);
144                 list.clear();
145                 readArchive(manifest, entries);
146                 jar.close();
147                 buf.close();
148             } catch (FileNotFoundException JavaDoc e) {
149                 e.printStackTrace(System.err);
150             } catch (IOException JavaDoc e) {
151                 e.printStackTrace(System.err);
152             } catch (ArchiveException e) {
153                 e.printStackTrace(System.err);
154             }
155         }
156     }
157
158     public JarPlan getPlan() throws ArchiveException {
159         if (plan == null) {
160             initPlan();
161         }
162         return plan;
163     }
164
165     //
166
abstract protected String JavaDoc getSelectionName();
167     abstract protected String JavaDoc getDescription();
168     abstract protected String JavaDoc getExtension();
169     abstract protected String JavaDoc getType();
170     abstract protected void initPanels() throws ArchiveException;
171     abstract protected void initPlan() throws ArchiveException;
172     protected void setPlan(JarPlan p) {
173         plan = p;
174     }
175
176     //
177
protected String JavaDoc getClassIncludeRoot() {
178         return new String JavaDoc();
179     }
180
181     protected boolean readArchive(Manifest JavaDoc manifest, String JavaDoc[] entries) {
182         boolean read = true;
183         PathHandle root = null;
184         PathHandle cursor = null;
185         String JavaDoc[] files = new String JavaDoc[0];
186         Descriptor[] dd = new Descriptor[0];
187         ArrayList JavaDoc list = new ArrayList JavaDoc();
188
189         if ((manifest == null)) {
190             read = false;
191         } else {
192             String JavaDoc value = null;
193
194             try {
195
196                 // classes
197
value = manifest.getMainAttributes().getValue(CLASS_ROOT);
198                 root = PathHandle.createPathHandle(value);
199                 if (root.isDirectory()) {
200                     getPlan().setClassRoot(root.getPath());
201                     for (int i = 0; i < entries.length; i++) {
202                         if (entries[i].toUpperCase().startsWith(getClassIncludeRoot())) {
203                             cursor =
204                                 PathHandle.createPathHandle(root.getPath()
205                                                             + File.separator
206                                                             + entries[i].substring(getClassIncludeRoot().length()));
207                             if (cursor.isFile()) {
208                                 list.add(cursor.getPath());
209                             }
210                         }
211                     }
212                     list.trimToSize();
213                     files = new String JavaDoc[list.size()];
214                     files = (String JavaDoc[]) list.toArray(files);
215                     list.clear();
216                     getPlan().setClassFiles(files);
217                 }
218
219                 // libraries (0 - 99)
220
for (int i = 0; i < 100; i++) {
221                     value = manifest.getMainAttributes().getValue(LIBRARY
222                             + i);
223                     cursor = PathHandle.createPathHandle(value);
224                     if (cursor.isFile()) {
225                         list.add(cursor.getPath());
226                     }
227                 }
228                 files = new String JavaDoc[list.size()];
229                 files = (String JavaDoc[]) list.toArray(files);
230                 list.clear();
231                 getPlan().setLibFiles(files);
232
233                 // deployment descriptors
234
dd = getPlan().getDescriptors();
235                 for (int i = 0; i < dd.length; i++) {
236                     value =
237                         manifest.getMainAttributes().getValue(dd[i].getType());
238                     cursor = PathHandle.createPathHandle(value);
239                     if (cursor.isFile()) {
240                         dd[i].setPath(cursor.getPath());
241                     }
242                 }
243                 getPlan().setDescriptors(dd);
244             } catch (ArchiveException e) {
245                 e.printStackTrace(System.err);
246             }
247         }
248         return read;
249     }
250
251     //
252
protected String JavaDoc getWizardTitle() {
253         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
254
255         buf.append(getSelectionName());
256         buf.append(" Wizard");
257         return buf.toString();
258     }
259
260     //
261
protected ArchivePanel[] getWizardPanels() throws ArchiveException {
262         if (panels.length == 0) {
263             initPanels();
264         }
265         return panels;
266     }
267
268     protected void setWizardPanels(ArchivePanel[] p) throws ArchiveException {
269         panels = p;
270         for (int i = 0; i < panels.length; i++) {
271             panels[i].readPlan(getPlan());
272         }
273     }
274
275     protected File JavaDoc build() throws ArchiveException {
276         ArchiveTool tool = new ArchiveTool();
277
278         return tool.buildArchive(getPlan());
279     }
280
281     protected void saveDefaults() {
282         saveDefaultArchivePath();
283     }
284
285     private void saveDefaultArchivePath() {
286         Properties JavaDoc props = null;
287
288         try {
289             props = ToolBoxInfo.loadProperties();
290             props.setProperty(getType() + ".archive",
291                               plan.getArchivePath());
292             ToolBoxInfo.storeProperties(props);
293         } catch (ToolException e) {
294             e.printStackTrace(System.err);
295             props = new Properties JavaDoc();
296         }
297     }
298
299     protected String JavaDoc getDefaultArchivePath() {
300         Properties JavaDoc props = null;
301         StringBuffer JavaDoc def = new StringBuffer JavaDoc();
302         String JavaDoc path = null;
303
304         def.append(System.getProperty("user.dir"));
305         def.append(File.separator);
306         def.append("untitled.");
307         def.append(getExtension());
308         try {
309             props = ToolBoxInfo.loadProperties();
310         } catch (ToolException e) {
311             e.printStackTrace(System.err);
312             props = new Properties JavaDoc();
313         }
314         if (isMostRecentlyUsed()) {
315           path = props.getProperty(getType() + ".archive", def.toString());
316         } else {
317           path = def.toString();
318         }
319         path = PathHandle.createPathString(path);
320         return path;
321     }
322
323 }
324
Popular Tags