KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > installer > Install


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.installer;
37
38 import java.awt.*;
39 import java.awt.event.*;
40 import java.io.File JavaDoc;
41 import java.util.*;
42
43 import javax.swing.*;
44 import javax.swing.border.BevelBorder JavaDoc;
45
46 import org.ungoverned.oscar.installer.artifact.*;
47 import org.ungoverned.oscar.installer.editor.*;
48 import org.ungoverned.oscar.installer.property.*;
49 import org.ungoverned.oscar.util.OscarConstants;
50
51 public class Install extends JFrame
52 {
53     private static transient final String JavaDoc PROPERTY_FILE = "property.xml";
54     private static transient final String JavaDoc ARTIFACT_FILE = "artifact.xml";
55
56     public static transient final String JavaDoc JAVA_DIR = "Java directory";
57     public static transient final String JavaDoc INSTALL_DIR = "Install directory";
58
59     private PropertyPanel m_propPanel = null;
60     private JButton m_okayButton = null;
61     private JButton m_cancelButton = null;
62     private JLabel m_statusLabel = null;
63
64     private java.util.List JavaDoc m_propList = null;
65     private java.util.List JavaDoc m_artifactList = null;
66
67     public Install()
68         throws Exception JavaDoc
69     {
70         super("Install");
71
72         // Load properties before resources, because resources
73
// refer to properties.
74
m_propList = loadPropertyList();
75         m_artifactList = loadArtifactList();
76
77         getContentPane().setLayout(new BorderLayout());
78         getContentPane().add(
79             m_propPanel = new PropertyPanel(m_propList), BorderLayout.CENTER);
80         getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
81         pack();
82         setResizable(true);
83         centerWindow(this);
84
85         // Make window closeable.
86
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
87         addWindowListener(new WindowAdapter() {
88             public void windowClosing(WindowEvent event)
89             {
90                 doCancel();
91             }
92         });
93     }
94
95     public java.util.List JavaDoc loadPropertyList()
96     {
97         String JavaDoc installDir = System.getProperty("user.home");
98         if (!installDir.endsWith(File.separator))
99         {
100             installDir = installDir + File.separator;
101         }
102
103         Property prop = null;
104
105         // Eventually these should be read from a file.
106
java.util.List JavaDoc list = new ArrayList();
107
108         // Add the shell choice property.
109
prop = new BooleanPropertyImpl("Shell", true);
110         prop.setEditor(new BooleanEditor((BooleanProperty) prop, "Text", "GUI"));
111         list.add(prop);
112
113         // Add the java directory property.
114
prop = new StringPropertyImpl(JAVA_DIR, System.getProperty("java.home"));
115         prop.setEditor(new FileEditor((StringProperty) prop, true));
116         list.add(prop);
117
118         // Add the installation directory property.
119
prop = new StringPropertyImpl(INSTALL_DIR, installDir + "Oscar");
120         prop.setEditor(new FileEditor((StringProperty) prop, true));
121         list.add(prop);
122
123         // Add the documentation URL property.
124
prop = new BooleanStringPropertyImpl(
125             "User documentation",
126             true,
127             "http://download.forge.objectweb.org/oscar/oscar-doc-"
128             + OscarConstants.OSCAR_VERSION_VALUE + ".jar");
129         list.add(prop);
130
131         // Add the documentation URL property.
132
prop = new BooleanStringPropertyImpl(
133             "API documentation",
134             true,
135             "http://download.forge.objectweb.org/oscar/oscar-api-"
136             + OscarConstants.OSCAR_VERSION_VALUE + ".jar");
137         list.add(prop);
138
139         return list;
140     }
141
142     public java.util.List JavaDoc loadArtifactList() throws Exception JavaDoc
143     {
144         // Eventually I will changed these to be read from a file.
145
java.util.List JavaDoc list = new ArrayList();
146         list.add(
147             new ArtifactHolder(
148                 (BooleanProperty) getProperty("User documentation"),
149                 new URLJarArtifact(
150                     (StringProperty) getProperty("User documentation"))));
151         list.add(
152             new ArtifactHolder(
153                 (BooleanProperty) getProperty("API documentation"),
154                 new URLJarArtifact(
155                     (StringProperty) getProperty("API documentation"))));
156         list.add(
157             new ArtifactHolder(
158                 new ResourceJarArtifact(
159                     new StringPropertyImpl("sourceName", "package.jar"))));
160         list.add(
161             new ArtifactHolder(
162                 new ResourceFileArtifact(
163                     new StringPropertyImpl("sourceName", "src.jar"))));
164         list.add(
165             new ArtifactHolder(
166                 new ResourceFileArtifact(
167                     new StringPropertyImpl("sourceName", "LICENSE.txt"))));
168         list.add(
169             new ArtifactHolder(
170                 (BooleanProperty) getProperty("Shell"),
171                 new ResourceFileArtifact(
172                     new StringPropertyImpl("sourceName", "system.properties.text"),
173                     new StringPropertyImpl("destName", "system.properties"),
174                     new StringPropertyImpl("destDir", "lib"))));
175         list.add(
176             new ArtifactHolder(
177                 new NotBooleanPropertyImpl((BooleanProperty) getProperty("Shell")),
178                 new ResourceFileArtifact(
179                     new StringPropertyImpl("sourceName", "system.properties.gui"),
180                     new StringPropertyImpl("destName", "system.properties"),
181                     new StringPropertyImpl("destDir", "lib"))));
182         list.add(
183             new ArtifactHolder(
184                 new ResourceFileArtifact(
185                     new StringPropertyImpl("sourceName", "example.policy"))));
186         list.add(
187             new ArtifactHolder(
188                 new ResourceFileArtifact(
189                     new StringPropertyImpl("sourceName", "oscar.bat"),
190                     new StringPropertyImpl("destName" , "oscar.bat"),
191                     new StringPropertyImpl("destDir", ""),
192                     true)));
193         list.add(
194             new ArtifactHolder(
195                 new ResourceFileArtifact(
196                     new StringPropertyImpl("sourceName", "oscar.sh"),
197                     new StringPropertyImpl("destName" , "oscar.sh"),
198                     new StringPropertyImpl("destDir", ""),
199                     true)));
200
201         return list;
202     }
203
204     private Property getProperty(String JavaDoc name)
205     {
206         for (int i = 0; i < m_propList.size(); i++)
207         {
208             Property prop = (Property) m_propList.get(i);
209             if (prop.getName().equals(name))
210             {
211                 return prop;
212             }
213         }
214         return null;
215     }
216
217     protected void doOkay()
218     {
219         m_propPanel.setEnabled(false);
220         m_okayButton.setEnabled(false);
221         m_cancelButton.setEnabled(false);
222         new Thread JavaDoc(new InstallRunnable()).start();
223     }
224
225     protected void doCancel()
226     {
227         System.exit(0);
228     }
229
230     protected JPanel createButtonPanel()
231     {
232         JPanel buttonPanel = new JPanel();
233         buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
234
235         // Create and set layout.
236
GridBagLayout grid = new GridBagLayout();
237         GridBagConstraints c = new GridBagConstraints();
238
239         buttonPanel.setLayout(grid);
240
241         // Create labels and fields.
242
c.insets = new Insets(2, 2, 2, 2);
243
244         // Okay button.
245
c.gridx = 0;
246         c.gridy = 0;
247         c.gridwidth = 1;
248         c.gridheight = 1;
249         c.anchor = GridBagConstraints.EAST;
250         grid.setConstraints(m_okayButton = new JButton("OK"), c);
251         buttonPanel.add(m_okayButton);
252         m_okayButton.setDefaultCapable(true);
253         getRootPane().setDefaultButton(m_okayButton);
254
255         // Cancel button.
256
c.gridx = 1;
257         c.gridy = 0;
258         c.gridwidth = 1;
259         c.gridheight = 1;
260         c.anchor = GridBagConstraints.WEST;
261         grid.setConstraints(m_cancelButton = new JButton("Cancel"), c);
262         buttonPanel.add(m_cancelButton);
263
264         // Add action listeners.
265
m_okayButton.addActionListener(new ActionListener() {
266             public void actionPerformed(ActionEvent event)
267             {
268                 doOkay();
269             }
270         });
271
272         m_cancelButton.addActionListener(new ActionListener() {
273             public void actionPerformed(ActionEvent event)
274             {
275                 doCancel();
276             }
277         });
278
279         // Status label.
280
m_statusLabel = new JLabel("Oscar installation");
281         m_statusLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
282
283         // Complete panel.
284
JPanel panel = new JPanel(new BorderLayout());
285         panel.add(buttonPanel, BorderLayout.CENTER);
286         panel.add(m_statusLabel, BorderLayout.SOUTH);
287         return panel;
288     }
289
290     public static void centerWindow(Component window)
291     {
292         Toolkit toolkit = Toolkit.getDefaultToolkit();
293         Dimension dim = toolkit.getScreenSize();
294         int screenWidth = dim.width;
295         int screenHeight = dim.height;
296         int x = (screenWidth - window.getSize().width) / 2;
297         int y = (screenHeight - window.getSize().height) / 2;
298         window.setLocation(x, y);
299     }
300
301     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
302     {
303         String JavaDoc msg = "<html>"
304             + "<center><h1>Oscar " + OscarConstants.OSCAR_VERSION_VALUE + "</h1></center>"
305             + "You can download example bundles at the Oscar shell prompt by<br>"
306             + "using the <b><tt>obr</tt></b> command to access the Oscar Bundle Repository;<br>"
307             + "type <b><tt>obr help</tt></b> at the Oscar shell prompt for details."
308             + "</html>";
309         JLabel label = new JLabel(msg);
310         label.setFont(new Font("SansSerif", Font.PLAIN, 11));
311         final JDialog dlg = new JDialog((Frame) null, "Oscar Install", true);
312         dlg.getContentPane().setLayout(new BorderLayout(10, 10));
313         dlg.getContentPane().add(label, BorderLayout.CENTER);
314         JPanel panel = new JPanel();
315         JButton button = new JButton("OK");
316         button.addActionListener(new ActionListener() {
317             public void actionPerformed(ActionEvent event)
318             {
319                 dlg.hide();
320             }
321         });
322         panel.add(button);
323         dlg.getContentPane().add(panel, BorderLayout.SOUTH);
324         // For spacing purposes...
325
dlg.getContentPane().add(new JPanel(), BorderLayout.NORTH);
326         dlg.getContentPane().add(new JPanel(), BorderLayout.EAST);
327         dlg.getContentPane().add(new JPanel(), BorderLayout.WEST);
328         dlg.pack();
329         centerWindow(dlg);
330         dlg.show();
331
332         Install obj = new Install();
333         obj.setVisible(true);
334     }
335
336     class InstallRunnable implements Runnable JavaDoc
337     {
338         public void run()
339         {
340             Map propMap = new HashMap();
341             for (int i = 0; i < m_propList.size(); i++)
342             {
343                 Property prop = (Property) m_propList.get(i);
344                 propMap.put(prop.getName(), prop);
345             }
346
347             String JavaDoc installDir = ((StringProperty) propMap.get(INSTALL_DIR)).getStringValue();
348
349             // Make sure the install directory ends with separator char.
350
if (!installDir.endsWith(File.separator))
351             {
352                 installDir = installDir + File.separator;
353             }
354
355             // Make sure the install directory exists and
356
// that is actually a directory.
357
File JavaDoc file = new File JavaDoc(installDir);
358             if (!file.exists())
359             {
360                 if (!file.mkdirs())
361                 {
362                     JOptionPane.showMessageDialog(Install.this,
363                         "Unable to create install directory.",
364                         "Error", JOptionPane.ERROR_MESSAGE);
365                     System.exit(-1);
366                 }
367             }
368             else if (!file.isDirectory())
369             {
370                 JOptionPane.showMessageDialog(Install.this,
371                     "The selected install location is not a directory.",
372                     "Error", JOptionPane.ERROR_MESSAGE);
373                 System.exit(-1);
374             }
375
376             // Status updater runnable.
377
StatusRunnable sr = new StatusRunnable();
378
379             // Loop through and process resources.
380
for (int i = 0; i < m_artifactList.size(); i++)
381             {
382                 ArtifactHolder ah = (ArtifactHolder) m_artifactList.get(i);
383                 if (ah.isIncluded())
384                 {
385                     if (!ah.getArtifact().process(sr, propMap))
386                     {
387                         JOptionPane.showMessageDialog(Install.this,
388                             "An error occurred while processing the resources.",
389                             "Error", JOptionPane.ERROR_MESSAGE);
390                         System.exit(-1);
391                     }
392                 }
393             }
394
395             System.exit(0);
396         }
397     }
398
399     class StatusRunnable implements Status, Runnable JavaDoc
400     {
401         private String JavaDoc text = null;
402
403         public void setText(String JavaDoc s)
404         {
405             text = s;
406             try {
407                 SwingUtilities.invokeAndWait(this);
408             } catch (Exception JavaDoc ex) {
409                 // Ignore.
410
}
411         }
412
413         public void run()
414         {
415             m_statusLabel.setText(text);
416         }
417     }
418
419     // Re-usable static member for ResourceHolder inner class.
420
private static BooleanProperty m_trueProp =
421         new BooleanPropertyImpl("mandatory", true);
422
423     class ArtifactHolder
424     {
425         private BooleanProperty m_isIncluded = null;
426         private Artifact m_artifact = null;
427         
428         public ArtifactHolder(Artifact artifact)
429         {
430             this(m_trueProp, artifact);
431         }
432         
433         public ArtifactHolder(BooleanProperty isIncluded, Artifact artifact)
434         {
435             m_isIncluded = isIncluded;
436             m_artifact = artifact;
437         }
438         
439         public boolean isIncluded()
440         {
441             return m_isIncluded.getBooleanValue();
442         }
443         
444         public Artifact getArtifact()
445         {
446             return m_artifact;
447         }
448     }
449 }
450
Popular Tags