Author: niclas
Date: Thu Sep 16 10:35:59 2004
New Revision: 46195

Added:
   avalon/trunk/tools/magic-installer/
   avalon/trunk/tools/magic-installer/build.properties   (contents, props changed)
   avalon/trunk/tools/magic-installer/build.xml   (contents, props changed)
   avalon/trunk/tools/magic-installer/src/
   avalon/trunk/tools/magic-installer/src/main/
   avalon/trunk/tools/magic-installer/src/main/org/
   avalon/trunk/tools/magic-installer/src/main/org/apache/
   avalon/trunk/tools/magic-installer/src/main/org/apache/metro/
   avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/
   avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/
   
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/ConsoleInstaller.java
   (contents, props changed)
   
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/Main.java 
  (contents, props changed)
   
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/ProgressIndicator.java
   (contents, props changed)
   
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/SwingInstaller.java
   (contents, props changed)
   
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/Worker.java
   (contents, props changed)
Log:
The beginning of a Installer for Magic.

Added: avalon/trunk/tools/magic-installer/build.properties
==============================================================================
--- (empty file)
+++ avalon/trunk/tools/magic-installer/build.properties Thu Sep 16 10:35:59 2004
@@ -0,0 +1,5 @@
+
+project.jar.main.class=org.apache.metro.installer.magic.Main
+
+project.jar.classpath=avalon-tools-magic.jar 
+

Added: avalon/trunk/tools/magic-installer/build.xml
==============================================================================
--- (empty file)
+++ avalon/trunk/tools/magic-installer/build.xml        Thu Sep 16 10:35:59 2004
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<project name="magic-installer" default="install" basedir="."
+    xmlns:x="antlib:org.apache.avalon.tools"
+>
+  <property file="build.properties"/>
+  <x:home/>
+  <import file="${magic.templates}/standard.xml"/>
+</project>

Added: 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/ConsoleInstaller.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/ConsoleInstaller.java
  Thu Sep 16 10:35:59 2004
@@ -0,0 +1,73 @@
+/* 
+ * Copyright 2004 Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.metro.installer.magic;
+
+import java.io.File;
+
+public class ConsoleInstaller
+{
+    private static final int BUFFER_SIZE = 10000;
+    
+    public ConsoleInstaller()
+    {
+    }
+    
+    public void start()
+        throws Exception
+    {
+        File userHome = new File( System.getProperty( "user.home" ) );
+        File magicHome = new File( userHome, ".magic" );
+        File antLibDir = new File( userHome, ".ant/lib" );
+        
+        ProgressIndicator indicator = new ConsoleProgress();
+        Worker w = new Worker( magicHome, antLibDir );
+        w.start( indicator );
+    }
+    
+    public class ConsoleProgress
+        implements ProgressIndicator
+    {
+        public void message( String message )
+        {
+            System.out.println( message );
+        }
+        
+        public void start()
+        {
+            System.out.print( "[                                                  ]  
0%" );
+        }        
+        
+        public void progress( int percentage )
+        {
+            int size = percentage / 2;
+            System.out.print( "\r[" );
+            for( int i = 0 ; i < size ; i++ )
+                System.out.print( "*" );
+            for( int i = 0 ; i < 50-size ; i++ )
+                System.out.print( " " );
+            System.out.print( "]  " + percentage + "%" );
+        }
+        
+        public void finished()
+        {
+            System.out.print( "[**************************************************]  
100%" );
+            System.out.println();
+        }
+    }
+}
+ 

Added: 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/Main.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/Main.java 
     Thu Sep 16 10:35:59 2004
@@ -0,0 +1,45 @@
+/* 
+ * Copyright 2004 Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.metro.installer.magic;
+
+public class Main
+{
+    static public void main( String[] args )
+        throws Exception
+    {
+        if( args.length == 0 )
+        {
+            SwingInstaller installer = new SwingInstaller();
+            installer.start();
+            return;
+        }
+        if( args[0].equals( "-console" ) )
+        {
+            ConsoleInstaller installer = new ConsoleInstaller();
+            installer.start();
+            return;
+        }
+        
+        if( args[0].equals( "-gui" ) )
+        {
+            SwingInstaller installer = new SwingInstaller();
+            installer.start();
+            return;
+        }
+    }
+} 

Added: 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/ProgressIndicator.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/ProgressIndicator.java
 Thu Sep 16 10:35:59 2004
@@ -0,0 +1,30 @@
+/* 
+ * Copyright 2004 Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.metro.installer.magic;
+
+public interface ProgressIndicator
+{
+    void message( String message );
+    
+    void start();
+    
+    void progress( int percentage );
+    
+    void finished();
+}
+ 

Added: 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/SwingInstaller.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/SwingInstaller.java
    Thu Sep 16 10:35:59 2004
@@ -0,0 +1,164 @@
+/* 
+ * Copyright 2004 Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.metro.installer.magic;
+
+import java.awt.Container;
+import java.awt.Dimension;
+
+import java.io.File;
+
+import javax.swing.BoxLayout;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JProgressBar;
+import javax.swing.JTextArea;
+
+public class SwingInstaller
+{
+    private JFrame m_ProgressWindow;
+    private JProgressBar m_ProgressBar;
+    private JTextArea  m_TextArea;
+        
+    public SwingInstaller()
+    {
+        m_ProgressWindow = new JFrame();
+        Container pane = m_ProgressWindow.getContentPane();
+        final BoxLayout layout = new BoxLayout( pane, BoxLayout.X_AXIS );
+        pane.setLayout( layout );
+        
+        JPanel leadColumn = new JPanel();
+        leadColumn.setMinimumSize( new Dimension( 10, 10 ) );
+        pane.add( leadColumn );
+
+        JPanel column = new JPanel();
+        final BoxLayout layout2 = new BoxLayout( column, BoxLayout.Y_AXIS );
+        column.setLayout( layout2 );
+        pane.add( column );
+
+        JPanel trailColumn = new JPanel();
+        trailColumn.setMinimumSize( new Dimension( 10, 10 ) );
+        pane.add( trailColumn );
+                
+        JPanel leadRow = new JPanel();
+        leadRow.setMinimumSize( new Dimension( 10, 10 ) );
+        column.add( leadRow );
+        
+        final JLabel label = new JLabel( "Installing Magic - The new age of build 
systems." );
+        column.add( label );
+        
+        m_ProgressWindow.setTitle( "Magic Installer" );
+        final JPanel spring1 = new JPanel();
+        setSizes( spring1, 0, 10, 100 );
+        column.add( spring1 );
+
+        m_ProgressBar = createProgressBar();
+        column.add( m_ProgressBar );
+        final JPanel spring2 = new JPanel();
+        setSizes( spring2, 0, 10, 100 );
+        column.add( spring2 );
+        
+        final JPanel spring3 = new JPanel();
+        setSizes( spring3, 0, 10, 100 );
+        column.add( spring3 );
+
+        m_TextArea = createTextArea();
+        column.add( m_TextArea );
+        final JPanel spring4 = new JPanel();
+        setSizes( spring4, 0, 10, 100 );
+        column.add( spring4 );
+        
+        m_ProgressWindow.pack();
+        m_ProgressWindow.setVisible( true );
+    }
+    
+    public void start()
+        throws Exception
+    {
+        File userHome = new File( System.getProperty( "user.home" ) );
+        File magicHome = new File( userHome, ".magic" );
+        File antLibDir = new File( userHome, ".ant/lib" );
+        
+        ProgressIndicator indicator = new SwingProgress();
+        Worker w = new Worker( magicHome, antLibDir );
+        w.start( indicator );
+    }
+
+    private void setSizes( JPanel panel, int min, int pref, int max )
+    {
+        Dimension minDim = new Dimension( min, min );
+        panel.setMinimumSize( minDim );
+        Dimension prefDim = new Dimension( pref, pref );
+        panel.setPreferredSize( prefDim );
+        Dimension maxDim = new Dimension( max, max );
+        panel.setMaximumSize( maxDim );
+    }
+
+    private JProgressBar createProgressBar()
+    {
+        final Dimension minDim = new Dimension( 50, 10 );
+        final Dimension prefDim = new Dimension( 300, 25 );
+        final Dimension maxDim = new Dimension( 1200, 50 );
+        JProgressBar bar = new JProgressBar();
+        bar.setStringPainted( true );
+        bar.setMinimumSize( minDim );
+        bar.setPreferredSize( prefDim );
+        bar.setMaximumSize( maxDim );
+        bar.setMinimum( 0 );
+        bar.setMaximum( 100 );
+        return bar;
+    }
+            
+    private JTextArea createTextArea()
+    {
+        final Dimension minDim = new Dimension( 50, 50 );
+        final Dimension prefDim = new Dimension( 300, 300 );
+        final Dimension maxDim = new Dimension( 1200, 1000 );
+        JTextArea area = new JTextArea();
+        area.setMinimumSize( minDim );
+        area.setPreferredSize( prefDim );
+        area.setMaximumSize( maxDim );
+        return area;
+    }
+    
+    public class SwingProgress
+        implements ProgressIndicator
+    {
+        public void message( String message )
+        {
+            m_TextArea.append( message );
+            m_TextArea.append( "\n" );
+        }
+        
+        public void start()
+        {
+        }
+    
+        public void progress( int percentage )
+        {
+            m_ProgressBar.setValue( percentage );
+            m_ProgressBar.setString( percentage + "%" );
+        }
+    
+        public void finished()
+        {
+        }
+    }
+}
+ 
+ 

Added: 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/Worker.java
==============================================================================
--- (empty file)
+++ 
avalon/trunk/tools/magic-installer/src/main/org/apache/metro/installer/magic/Worker.java
    Thu Sep 16 10:35:59 2004
@@ -0,0 +1,160 @@
+/* 
+ * Copyright 2004 Apache Software Foundation
+ * Licensed  under the  Apache License,  Version 2.0  (the "License");
+ * you may not use  this file  except in  compliance with the License.
+ * You may obtain a copy of the License at 
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed  under the  License is distributed on an "AS IS" BASIS,
+ * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
+ * implied.
+ * 
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.metro.installer.magic;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import java.net.URL;
+import java.net.URLConnection;
+
+import java.util.Enumeration;
+
+import java.util.jar.JarFile;
+import java.util.jar.JarEntry;
+
+public class Worker
+{
+    private static final int BUFFER_SIZE = 10000;
+    
+    private File m_MagicHome;
+    private File m_AntLibDir;
+    
+    public Worker( File magicHome, File antLib )
+    {
+        m_MagicHome = magicHome;
+        m_AntLibDir = antLib;
+    }
+    
+    public void start( ProgressIndicator indicator )
+        throws Exception
+    {
+        /* Not sure what to do with these yet.
+        File md5File = download( 
"http://www.dpml.net/avalon/tools/bars/avalon-tools-magic.bar.md5"; );
+        File ascFile = download( 
"http://www.dpml.net/avalon/tools/bars/avalon-tools-magic.bar.asc"; );
+        */
+        
+        String url = "http://www.dpml.net/avalon/tools/bars/avalon-tools-magic.bar";;
+        File barFile = download( url, indicator );
+        unjar( barFile, m_MagicHome, indicator );
+        
+        File toolsJar = new File( m_MagicHome, "jars/avalon-tools-magic.jar" );
+        
+        copy( toolsJar, m_AntLibDir, indicator );
+        indicator.message( "Cleaning up." );
+        File jarsDir = toolsJar.getParentFile();
+        toolsJar.delete();
+        jarsDir.delete();
+    }
+    
+    private void unjar( File barFile, File toDir, ProgressIndicator indicator )
+        throws IOException
+    {
+        indicator.message( "Unzipping " + barFile );
+        indicator.start();
+        JarFile jar = new JarFile( barFile );
+        Enumeration entries = jar.entries();
+        while( entries.hasMoreElements() )
+        {
+            JarEntry entry = (JarEntry) entries.nextElement();
+            String name = entry.getName();
+            InputStream in = jar.getInputStream( entry );
+            File file = new File( toDir, name );
+            FileOutputStream out = new FileOutputStream( file );
+            long size = entry.getSize();
+            copy( in, out, indicator, size );
+            in.close();
+            out.close();
+        }
+        indicator.finished();
+    }
+    
+    private File download( String url, ProgressIndicator indicator )
+        throws IOException
+    {        
+        URL download = new URL( url );
+        indicator.message( "Connecting to " + download.getHost() );
+
+        URLConnection conn = download.openConnection();
+        conn.connect();
+        int size = conn.getContentLength();
+        
+        indicator.message( "Downloading " + url );
+        indicator.start();
+        InputStream in = conn.getInputStream();
+        
+        File tmp = File.createTempFile( "magic", null );
+        tmp.deleteOnExit();
+        
+        FileOutputStream out = new FileOutputStream( tmp );
+        
+        copy( in, out, indicator, size );
+        in.close();
+        out.close();
+        indicator.finished();
+        return tmp;   
+    }
+    
+    
+    private void copy( InputStream from, OutputStream to, 
+                       ProgressIndicator indicator, long size )
+        throws IOException
+    {
+        BufferedOutputStream out = new BufferedOutputStream( to );
+        BufferedInputStream in = new BufferedInputStream( from, BUFFER_SIZE );
+        int bytesRead = 0;
+        int counter = 0;
+        do
+        {
+            byte[] data = new byte[ BUFFER_SIZE ];
+            bytesRead = in.read( data, 0, BUFFER_SIZE );
+            counter = counter + bytesRead;
+            if( size != 0 )
+                indicator.progress( (int) ( ( counter * 100 ) / size ) );
+            if( bytesRead != -1 )
+                out.write( data, 0, bytesRead );
+        } while( bytesRead != -1 );
+        out.flush();
+    }
+    
+    private void copy( File file, File toDir, ProgressIndicator indicator )
+        throws IOException
+    {
+        indicator.message( "Copying " + file + " to " + file );
+        indicator.start();
+        toDir.mkdirs();
+        String name = file.getName();
+        File destFile = new File( toDir, name );
+        
+        FileInputStream in = new FileInputStream( file );
+        FileOutputStream out = new FileOutputStream( destFile );
+        
+        long size = file.length();
+        copy( in, out, indicator, size );
+        
+        in.close();
+        out.close();
+        indicator.finished();
+    }
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to