You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2007/01/07 21:59:27 UTC

svn commit: r493837 - /incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/JarUtils.java

Author: jlaskowski
Date: Sun Jan  7 12:59:27 2007
New Revision: 493837

URL: http://svn.apache.org/viewvc?view=rev&rev=493837
Log:
OPENEJB-434 Remove dependency on Sun Classes in JarUtils

Submitted by: Karan Malhi

Modified:
    incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/JarUtils.java

Modified: incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/JarUtils.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/JarUtils.java?view=diff&rev=493837&r1=493836&r2=493837
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/JarUtils.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/util/JarUtils.java Sun Jan  7 12:59:27 2007
@@ -16,19 +16,25 @@
  */
 package org.apache.openejb.util;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.PrintStream;
 import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Hashtable;
+import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
 
 import org.apache.openejb.OpenEJBException;
 
+/**
+ * @version $Rev$ $Date$
+ */
 public class JarUtils {
 
     private static Messages messages = new Messages("org.apache.openejb.util.resources");
@@ -39,21 +45,22 @@
 
     private static boolean alreadySet = false;
 
+    @SuppressWarnings("unchecked")
     public static void setHandlerSystemProperty() {
         if (!alreadySet) {
             /*
              * Setup the java protocol handler path to include org.apache.openejb.util.urlhandler
              * so that org.apache.openejb.util.urlhandler.resource.Handler will be used for URLs
              * of the form "resource:/path".
-             */
+             */ 
             /*try {
                 String oldPkgs = System.getProperty( "java.protocol.handler.pkgs" );
-
+            
                 if ( oldPkgs == null )
                     System.setProperty( "java.protocol.handler.pkgs", "org.apache.openejb.util.urlhandler" );
                 else if ( oldPkgs.indexOf( "org.apache.openejb.util.urlhandler" ) < 0 )
                     System.setProperty( "java.protocol.handler.pkgs", oldPkgs + "|" + "org.apache.openejb.util.urlhandler" );
-
+            
             } catch ( SecurityException ex ) {
             }*/
             Hashtable urlHandlers = (Hashtable) AccessController.doPrivileged(
@@ -111,44 +118,35 @@
     }
 
     public static void addFileToJar(String jarFile, String file) throws OpenEJBException {
-        ByteArrayOutputStream errorBytes = new ByteArrayOutputStream();
-
-        /* NOTE: Sadly, we have to play this little game 
-         * with temporarily switching the standard error
-         * stream to capture the errors.
-         * Although you can pass in an error stream in 
-         * the constructor of the jar tool, they are not
-         * used when an error occurs.
-         */
-        PrintStream newErr = new PrintStream(errorBytes);
-        PrintStream oldErr = System.err;
-        System.setErr(newErr);
-
-        sun.tools.jar.Main jarTool = new sun.tools.jar.Main(newErr, newErr, "config_utils");
-
-        String[] args = new String[]{"uf", jarFile, file};
-        jarTool.run(args);
-
-        System.setErr(oldErr);
-
         try {
-            errorBytes.close();
-            newErr.close();
-        } catch (Exception e) {
-            throw new OpenEJBException(messages.format("file.0020", jarFile, e.getLocalizedMessage()));
-        }
-
-        String error = new String(errorBytes.toByteArray());
-        if (error.indexOf("java.io.IOException") != -1) {
+            JarInputStream jis = new JarInputStream(new FileInputStream(jarFile));
+            File tempJar = File.createTempFile("temp", "jar");
+            JarOutputStream jos = new JarOutputStream(new FileOutputStream(tempJar));
+            JarEntry nextJarEntry = null;
+            while ((nextJarEntry = jis.getNextJarEntry()) != null) {
+                jos.putNextEntry(nextJarEntry);
+            }
+            jis.close();
+            jos.putNextEntry(new JarEntry(file));
+            FileInputStream fis = new FileInputStream(file);
+            for (int c = fis.read(); c != -1; c = fis.read()) {
+                jos.write(c);
+            }
+            fis.close();
+            jos.close();
 
-            int begin = error.indexOf(':') + 1;
-            int end = error.indexOf('\n');
-            String message = error.substring(begin, end);
-            throw new OpenEJBException(messages.format("file.0003", file, jarFile, message));
+            File oldJar = new File(jarFile);
+            oldJar.delete();
+            tempJar.renameTo(oldJar);
+        } catch (FileNotFoundException e) {
+            throw new OpenEJBException(messages.format("file.0003", file, jarFile, e.getMessage()));
+        } catch (IOException e) {
+            throw new OpenEJBException(messages.format("file.0003", file, jarFile, e.getMessage()));
         }
 
     }
 
+    @SuppressWarnings("unchecked")
     public static JarFile getJarFile(String jarFile) throws OpenEJBException {
         /*[1.1]  Get the jar ***************/
         JarFile jar = null;
@@ -163,6 +161,7 @@
         return jar;
     }
 
+    @SuppressWarnings("unchecked")
     public static ClassLoader getContextClassLoader() {
         return (ClassLoader) java.security.AccessController.doPrivileged(
                 new java.security.PrivilegedAction() {
@@ -170,7 +169,6 @@
                         return Thread.currentThread().getContextClassLoader();
                     }
                 }
-        );
+        );    
     }
-
 }



Re: Review of patch for OPENEJB-434

Posted by Dain Sundstrom <da...@iq80.com>.
Me 2.  This is why I always try to copy in some other working IO code :)

-dain

On Jan 17, 2007, at 7:28 PM, David Blevins wrote:

> Aha! Yep, that'll do it :)  That's the kind of bug I would make.  I  
> do that kind of stuff all the time.
>
> -David
>
> On Jan 17, 2007, at 7:15 PM, Karan Malhi wrote:
>
>> Ok, there was a bug in my code. When i was writing the bytes to  
>> the output
>> stream, i was writing some extra bytes. here is what i had earlier
>>                byte buffer[] = new byte[4096];
>>                for (int c = jis.read(buffer); c != -1; c = jis.read 
>> (buffer))
>> {
>>                    jos.write(buffer); // i was writing all 4096  
>> bytes here
>>                }
>>
>> And here is what i have changed it into
>>                byte buffer[] = new byte[4096];
>>                for (int c = jis.read(buffer); c != -1; c = jis.read 
>> (buffer))
>> {
>>                    jos.write(buffer,0,c); // just writing the  
>> number of
>> bytes read from the inputstream
>>                }
>>
>> Works fine now. I will submit a patch
>>
>> On 1/17/07, Karan Malhi <ka...@gmail.com> wrote:
>>>
>>> David,
>>>
>>> 1. you sure you're using compression?
>>>
>>> --I am using the JarInputStream and JarOutputStream. I also tried  
>>> to set
>>> the compression level to 9 but didnt acheive anything.
>>>
>>> 2. if suggestion #1 doesn't lead anywhere, feel free to take the
>>> chunk of Ant code you need and trim it down, clean it up and use  
>>> that.
>>>
>>> --I was thinking that before digging into ANT, if somebody could  
>>> look at
>>> the code and see whats wrong, then that could save some trouble.  
>>> I am
>>> probably missing something in the code. This is the first time i  
>>> have worked
>>> with Jar input and outputstreams. Dain did send me a link to the
>>> DeploymentUtil of Geronimo, I will further look into that and see  
>>> how it was
>>> done in DeploymentUtil.
>>>
>>> The code behaves a bit differently with different jars. For  
>>> example, i
>>> took ant.jar and tried to add files to it. even if i dont add a  
>>> file and
>>> keep running my program on ant.jar, it keeps increasing its size  
>>> (but
>>> stops increasing the size after 4 or 5 runs). however, if i pick  
>>> another jar
>>> and try to add a file to it, the code works fine (size is the  
>>> same as
>>> produced by the "jar" command). I probably need to do some more  
>>> testing
>>> before i can come up with a definitive conclusion, but for now i  
>>> am guessing
>>> maybe it is something with ant.jar or something wrong with my code.
>>>
>>> karan
>>>
>>> On Jan 17, 2007, at 8:39 AM, Karan Malhi wrote:
>>>
>>> > Please do not review the patch yet. i did not svn update before
>>> > creating the
>>> > patch, will do it tonight and submit the correct patch. Apologize
>>> > for the
>>> > confusion
>>> >
>>> > On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
>>> >>
>>> >> I have attached another patch, without the ANT libraries. Please
>>> >> review
>>> >> it. It works fine, I am not happy with the size of the jar it  
>>> creates
>>> >> though. Any thoughts or input would be a good learning experience
>>> >> for me.
>>> >>
>>> >> https://issues.apache.org/jira/browse/OPENEJB-434
>>> >>
>>> >>
>>> >> On 1/16/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
>>> >> >
>>> >> > On 1/16/07, Karan Malhi < karan.malhi@gmail.com> wrote:
>>> >> >
>>> >> > > I then tried to use the Jar class from ANT and it works  
>>> perfectly
>>> >> > (same as
>>> >> > > the jar command) and is much simpler to use. Do you think we
>>> >> could use
>>> >> > the
>>> >> > > ant classes for openejb project.?
>>> >> >
>>> >> > I think Ant's fine as long as we don't use it as if we're
>>> >> executed it
>>> >> > from the command line, but using its classes. I'm not  
>>> convinced,
>>> >> > though, it's the best bet as Ant is more than copying jars  
>>> and for
>>> >> > such a small task we should not need it at all.
>>> >> >
>>> >> > Jacek
>>> >> >
>>> >> > --
>>> >> > Jacek Laskowski
>>> >> > http://www.JacekLaskowski.pl <http://www.jaceklaskowski.pl/>
>>> >> >
>>> >>
>>> >>
>>> >>
>>> >> --
>>> >> Karan Malhi
>>> >>
>>> >
>>> >
>>> >
>>> > --
>>> > Karan Malhi
>>>
>>>
>>>
>>>
>>>
>>> --
>>> Karan Malhi
>>>
>>
>>
>>
>> -- 
>> Karan Malhi
>


Re: Review of patch for OPENEJB-434

Posted by David Blevins <da...@visi.com>.
Aha! Yep, that'll do it :)  That's the kind of bug I would make.  I  
do that kind of stuff all the time.

-David

On Jan 17, 2007, at 7:15 PM, Karan Malhi wrote:

> Ok, there was a bug in my code. When i was writing the bytes to the  
> output
> stream, i was writing some extra bytes. here is what i had earlier
>                byte buffer[] = new byte[4096];
>                for (int c = jis.read(buffer); c != -1; c = jis.read 
> (buffer))
> {
>                    jos.write(buffer); // i was writing all 4096  
> bytes here
>                }
>
> And here is what i have changed it into
>                byte buffer[] = new byte[4096];
>                for (int c = jis.read(buffer); c != -1; c = jis.read 
> (buffer))
> {
>                    jos.write(buffer,0,c); // just writing the  
> number of
> bytes read from the inputstream
>                }
>
> Works fine now. I will submit a patch
>
> On 1/17/07, Karan Malhi <ka...@gmail.com> wrote:
>>
>> David,
>>
>> 1. you sure you're using compression?
>>
>> --I am using the JarInputStream and JarOutputStream. I also tried  
>> to set
>> the compression level to 9 but didnt acheive anything.
>>
>> 2. if suggestion #1 doesn't lead anywhere, feel free to take the
>> chunk of Ant code you need and trim it down, clean it up and use  
>> that.
>>
>> --I was thinking that before digging into ANT, if somebody could  
>> look at
>> the code and see whats wrong, then that could save some trouble. I am
>> probably missing something in the code. This is the first time i  
>> have worked
>> with Jar input and outputstreams. Dain did send me a link to the
>> DeploymentUtil of Geronimo, I will further look into that and see  
>> how it was
>> done in DeploymentUtil.
>>
>> The code behaves a bit differently with different jars. For  
>> example, i
>> took ant.jar and tried to add files to it. even if i dont add a  
>> file and
>> keep running my program on ant.jar, it keeps increasing its size (but
>> stops increasing the size after 4 or 5 runs). however, if i pick  
>> another jar
>> and try to add a file to it, the code works fine (size is the same as
>> produced by the "jar" command). I probably need to do some more  
>> testing
>> before i can come up with a definitive conclusion, but for now i  
>> am guessing
>> maybe it is something with ant.jar or something wrong with my code.
>>
>> karan
>>
>> On Jan 17, 2007, at 8:39 AM, Karan Malhi wrote:
>>
>> > Please do not review the patch yet. i did not svn update before
>> > creating the
>> > patch, will do it tonight and submit the correct patch. Apologize
>> > for the
>> > confusion
>> >
>> > On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
>> >>
>> >> I have attached another patch, without the ANT libraries. Please
>> >> review
>> >> it. It works fine, I am not happy with the size of the jar it  
>> creates
>> >> though. Any thoughts or input would be a good learning experience
>> >> for me.
>> >>
>> >> https://issues.apache.org/jira/browse/OPENEJB-434
>> >>
>> >>
>> >> On 1/16/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
>> >> >
>> >> > On 1/16/07, Karan Malhi < karan.malhi@gmail.com> wrote:
>> >> >
>> >> > > I then tried to use the Jar class from ANT and it works  
>> perfectly
>> >> > (same as
>> >> > > the jar command) and is much simpler to use. Do you think we
>> >> could use
>> >> > the
>> >> > > ant classes for openejb project.?
>> >> >
>> >> > I think Ant's fine as long as we don't use it as if we're
>> >> executed it
>> >> > from the command line, but using its classes. I'm not convinced,
>> >> > though, it's the best bet as Ant is more than copying jars  
>> and for
>> >> > such a small task we should not need it at all.
>> >> >
>> >> > Jacek
>> >> >
>> >> > --
>> >> > Jacek Laskowski
>> >> > http://www.JacekLaskowski.pl <http://www.jaceklaskowski.pl/>
>> >> >
>> >>
>> >>
>> >>
>> >> --
>> >> Karan Malhi
>> >>
>> >
>> >
>> >
>> > --
>> > Karan Malhi
>>
>>
>>
>>
>>
>> --
>> Karan Malhi
>>
>
>
>
> -- 
> Karan Malhi


Re: Review of patch for OPENEJB-434

Posted by Karan Malhi <ka...@gmail.com>.
Ok, there was a bug in my code. When i was writing the bytes to the output
stream, i was writing some extra bytes. here is what i had earlier
                byte buffer[] = new byte[4096];
                for (int c = jis.read(buffer); c != -1; c = jis.read(buffer))
{
                    jos.write(buffer); // i was writing all 4096 bytes here
                }

And here is what i have changed it into
                byte buffer[] = new byte[4096];
                for (int c = jis.read(buffer); c != -1; c = jis.read(buffer))
{
                    jos.write(buffer,0,c); // just writing the number of
bytes read from the inputstream
                }

Works fine now. I will submit a patch

On 1/17/07, Karan Malhi <ka...@gmail.com> wrote:
>
> David,
>
> 1. you sure you're using compression?
>
> --I am using the JarInputStream and JarOutputStream. I also tried to set
> the compression level to 9 but didnt acheive anything.
>
> 2. if suggestion #1 doesn't lead anywhere, feel free to take the
> chunk of Ant code you need and trim it down, clean it up and use that.
>
> --I was thinking that before digging into ANT, if somebody could look at
> the code and see whats wrong, then that could save some trouble. I am
> probably missing something in the code. This is the first time i have worked
> with Jar input and outputstreams. Dain did send me a link to the
> DeploymentUtil of Geronimo, I will further look into that and see how it was
> done in DeploymentUtil.
>
> The code behaves a bit differently with different jars. For example, i
> took ant.jar and tried to add files to it. even if i dont add a file and
> keep running my program on ant.jar, it keeps increasing its size (but
> stops increasing the size after 4 or 5 runs). however, if i pick another jar
> and try to add a file to it, the code works fine (size is the same as
> produced by the "jar" command). I probably need to do some more testing
> before i can come up with a definitive conclusion, but for now i am guessing
> maybe it is something with ant.jar or something wrong with my code.
>
> karan
>
> On Jan 17, 2007, at 8:39 AM, Karan Malhi wrote:
>
> > Please do not review the patch yet. i did not svn update before
> > creating the
> > patch, will do it tonight and submit the correct patch. Apologize
> > for the
> > confusion
> >
> > On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
> >>
> >> I have attached another patch, without the ANT libraries. Please
> >> review
> >> it. It works fine, I am not happy with the size of the jar it creates
> >> though. Any thoughts or input would be a good learning experience
> >> for me.
> >>
> >> https://issues.apache.org/jira/browse/OPENEJB-434
> >>
> >>
> >> On 1/16/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
> >> >
> >> > On 1/16/07, Karan Malhi < karan.malhi@gmail.com> wrote:
> >> >
> >> > > I then tried to use the Jar class from ANT and it works perfectly
> >> > (same as
> >> > > the jar command) and is much simpler to use. Do you think we
> >> could use
> >> > the
> >> > > ant classes for openejb project.?
> >> >
> >> > I think Ant's fine as long as we don't use it as if we're
> >> executed it
> >> > from the command line, but using its classes. I'm not convinced,
> >> > though, it's the best bet as Ant is more than copying jars and for
> >> > such a small task we should not need it at all.
> >> >
> >> > Jacek
> >> >
> >> > --
> >> > Jacek Laskowski
> >> > http://www.JacekLaskowski.pl <http://www.jaceklaskowski.pl/>
> >> >
> >>
> >>
> >>
> >> --
> >> Karan Malhi
> >>
> >
> >
> >
> > --
> > Karan Malhi
>
>
>
>
>
> --
> Karan Malhi
>



-- 
Karan Malhi

Re: Review of patch for OPENEJB-434

Posted by Karan Malhi <ka...@gmail.com>.
David,

1. you sure you're using compression?

--I am using the JarInputStream and JarOutputStream. I also tried to set the
compression level to 9 but didnt acheive anything.

2. if suggestion #1 doesn't lead anywhere, feel free to take the
chunk of Ant code you need and trim it down, clean it up and use that.

--I was thinking that before digging into ANT, if somebody could look at the
code and see whats wrong, then that could save some trouble. I am probably
missing something in the code. This is the first time i have worked with Jar
input and outputstreams. Dain did send me a link to the DeploymentUtil of
Geronimo, I will further look into that and see how it was done in
DeploymentUtil.

The code behaves a bit differently with different jars. For example, i took
ant.jar and tried to add files to it. even if i dont add a file and keep
running my program on ant.jar, it keeps increasing its size (but stops
increasing the size after 4 or 5 runs). however, if i pick another jar and
try to add a file to it, the code works fine (size is the same as produced
by the "jar" command). I probably need to do some more testing before i can
come up with a definitive conclusion, but for now i am guessing maybe it is
something with ant.jar or something wrong with my code.

karan

On Jan 17, 2007, at 8:39 AM, Karan Malhi wrote:

> Please do not review the patch yet. i did not svn update before
> creating the
> patch, will do it tonight and submit the correct patch. Apologize
> for the
> confusion
>
> On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
>>
>> I have attached another patch, without the ANT libraries. Please
>> review
>> it. It works fine, I am not happy with the size of the jar it creates
>> though. Any thoughts or input would be a good learning experience
>> for me.
>>
>> https://issues.apache.org/jira/browse/OPENEJB-434
>>
>>
>> On 1/16/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
>> >
>> > On 1/16/07, Karan Malhi < karan.malhi@gmail.com> wrote:
>> >
>> > > I then tried to use the Jar class from ANT and it works perfectly
>> > (same as
>> > > the jar command) and is much simpler to use. Do you think we
>> could use
>> > the
>> > > ant classes for openejb project.?
>> >
>> > I think Ant's fine as long as we don't use it as if we're
>> executed it
>> > from the command line, but using its classes. I'm not convinced,
>> > though, it's the best bet as Ant is more than copying jars and for
>> > such a small task we should not need it at all.
>> >
>> > Jacek
>> >
>> > --
>> > Jacek Laskowski
>> > http://www.JacekLaskowski.pl <http://www.jaceklaskowski.pl/>
>> >
>>
>>
>>
>> --
>> Karan Malhi
>>
>
>
>
> --
> Karan Malhi





-- 
Karan Malhi

Re: Review of patch for OPENEJB-434

Posted by David Blevins <da...@visi.com>.
On the note about using the Ant library and avoiding strangely large  
jar files:

  1. you sure you're using compression?
  2. if suggestion #1 doesn't lead anywhere, feel free to take the  
chunk of Ant code you need and trim it down, clean it up and use that.

-David


On Jan 17, 2007, at 8:39 AM, Karan Malhi wrote:

> Please do not review the patch yet. i did not svn update before  
> creating the
> patch, will do it tonight and submit the correct patch. Apologize  
> for the
> confusion
>
> On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
>>
>> I have attached another patch, without the ANT libraries. Please  
>> review
>> it. It works fine, I am not happy with the size of the jar it creates
>> though. Any thoughts or input would be a good learning experience  
>> for me.
>>
>> https://issues.apache.org/jira/browse/OPENEJB-434
>>
>>
>> On 1/16/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
>> >
>> > On 1/16/07, Karan Malhi < karan.malhi@gmail.com> wrote:
>> >
>> > > I then tried to use the Jar class from ANT and it works perfectly
>> > (same as
>> > > the jar command) and is much simpler to use. Do you think we  
>> could use
>> > the
>> > > ant classes for openejb project.?
>> >
>> > I think Ant's fine as long as we don't use it as if we're  
>> executed it
>> > from the command line, but using its classes. I'm not convinced,
>> > though, it's the best bet as Ant is more than copying jars and for
>> > such a small task we should not need it at all.
>> >
>> > Jacek
>> >
>> > --
>> > Jacek Laskowski
>> > http://www.JacekLaskowski.pl <http://www.jaceklaskowski.pl/>
>> >
>>
>>
>>
>> --
>> Karan Malhi
>>
>
>
>
> -- 
> Karan Malhi


Re: Review of patch for OPENEJB-434

Posted by Karan Malhi <ka...@gmail.com>.
Please do not review the patch yet. i did not svn update before creating the
patch, will do it tonight and submit the correct patch. Apologize for the
confusion

On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
>
> I have attached another patch, without the ANT libraries. Please review
> it. It works fine, I am not happy with the size of the jar it creates
> though. Any thoughts or input would be a good learning experience for me.
>
> https://issues.apache.org/jira/browse/OPENEJB-434
>
>
> On 1/16/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
> >
> > On 1/16/07, Karan Malhi < karan.malhi@gmail.com> wrote:
> >
> > > I then tried to use the Jar class from ANT and it works perfectly
> > (same as
> > > the jar command) and is much simpler to use. Do you think we could use
> > the
> > > ant classes for openejb project.?
> >
> > I think Ant's fine as long as we don't use it as if we're executed it
> > from the command line, but using its classes. I'm not convinced,
> > though, it's the best bet as Ant is more than copying jars and for
> > such a small task we should not need it at all.
> >
> > Jacek
> >
> > --
> > Jacek Laskowski
> > http://www.JacekLaskowski.pl <http://www.jaceklaskowski.pl/>
> >
>
>
>
> --
> Karan Malhi
>



-- 
Karan Malhi

Re: Review of patch for OPENEJB-434

Posted by Karan Malhi <ka...@gmail.com>.
I have attached another patch, without the ANT libraries. Please review it.
It works fine, I am not happy with the size of the jar it creates though.
Any thoughts or input would be a good learning experience for me.

https://issues.apache.org/jira/browse/OPENEJB-434


On 1/16/07, Jacek Laskowski <ja...@laskowski.net.pl> wrote:
>
> On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:
>
> > I then tried to use the Jar class from ANT and it works perfectly (same
> as
> > the jar command) and is much simpler to use. Do you think we could use
> the
> > ant classes for openejb project.?
>
> I think Ant's fine as long as we don't use it as if we're executed it
> from the command line, but using its classes. I'm not convinced,
> though, it's the best bet as Ant is more than copying jars and for
> such a small task we should not need it at all.
>
> Jacek
>
> --
> Jacek Laskowski
> http://www.JacekLaskowski.pl
>



-- 
Karan Malhi

Re: Review of patch for OPENEJB-434

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On 1/16/07, Karan Malhi <ka...@gmail.com> wrote:

> I then tried to use the Jar class from ANT and it works perfectly (same as
> the jar command) and is much simpler to use. Do you think we could use the
> ant classes for openejb project.?

I think Ant's fine as long as we don't use it as if we're executed it
from the command line, but using its classes. I'm not convinced,
though, it's the best bet as Ant is more than copying jars and for
such a small task we should not need it at all.

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl

Re: Review of patch for OPENEJB-434

Posted by Karan Malhi <ka...@gmail.com>.
I am currently working on this patch, however i found out that my new patch
does not work properly in that it increases the size of the jar file
considerably. For example , if i add a new file of size 300 kb to an
existing jar file of size 1 mb, then it adds the new file to the jar, but
increases the jar file to 1.9 mb. I tried to use jar command and add the
file , which increased the jar files size to 1.25 mb.

I then tried to use the Jar class from ANT and it works perfectly (same as
the jar command) and is much simpler to use. Do you think we could use the
ant classes for openejb project.? if yes, then below is the code which i
have written which adds a file to the jar file. i still needs to handle
exceptions e.g  file not found , if renaming files doesnt work etc, but just
as a POC, could you please look into it and let me know if it is ok and if i
can submit a patch  using ANT libraries

import java.io.File;
import java.io.IOException;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Jar;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.ZipFileSet;

public class JarUtilAnt {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        addFileToJar("c:/jars/ant.jar", "c:/test.xml");


    }
    public static void addFileToJar(String jarFile, String file) throws
IOException{
        Jar jar = new Jar();
        File f = new File(jarFile);
        File parentFile = f.getParentFile();
       File renamedFile = new File(parentFile,"temp.jar");
       f.renameTo(renamedFile);
        FileSet newFile = new FileSet();
        newFile.setFile(new File(file));
        ZipFileSet fileSet = new ZipFileSet();
        fileSet.setSrc(renamedFile);
        jar.addFileset(fileSet);
        jar.addFileset(newFile);
        File destFile = new File(jarFile);
        jar.setDestFile(destFile);
        Project p = new Project();
        jar.setProject(p);
        jar.executeMain();
        renamedFile.delete();

    }

}
On 1/11/07, David Blevins <da...@visi.com> wrote:
>
>
> On Jan 8, 2007, at 6:09 PM, Karan Malhi wrote:
>
> > Thanks Dain,
> >
> > I will make the suggested changes and submit another patch.
>
> I just have to point out.... Most people tend to say "sorry" when
> they get feedback, but you said "thanks."  Definitely the right
> attitude to have!  Big thumbs up!
>
> Thanks Karan and Dain.
>
> -David
>
> >
> > On 1/8/07, Dain Sundstrom <da...@iq80.com> wrote:
> >>
> >> This is a good start but I see some bugs :)
> >>
> >> comments in line...
> >>
> >> -dain
> >>
> >> On Jan 7, 2007, at 12:59 PM, jlaskowski@apache.org wrote:
> >>
> >> > +            JarInputStream jis = new JarInputStream(new
> >> > FileInputStream(jarFile));
> >> > +            File tempJar = File.createTempFile("temp", "jar");
> >>
> >> The temp file should be created in the same directory as the final
> >> original file because on some platforms the temp directory is on a
> >> separate drive and rename only works when the source and target file
> >> are on the same drive.
> >>
> >> > +            JarOutputStream jos = new JarOutputStream(new
> >> > FileOutputStream(tempJar));
> >> > +            JarEntry nextJarEntry = null;
> >> > +            while ((nextJarEntry = jis.getNextJarEntry()) !=
> >> null) {
> >> > +                jos.putNextEntry(nextJarEntry);
> >> > +            }
> >>
> >> You need to copy the contents from jis to jos after putNextEntry.
> >>
> >> > +            jis.close();
> >> > +            jos.putNextEntry(new JarEntry(file));
> >> > +            FileInputStream fis = new FileInputStream(file);
> >> > +            for (int c = fis.read(); c != -1; c = fis.read()) {
> >> > +                jos.write(c);
> >> > +            }
> >>
> >> Byte at a time is quite slow.  Instead I suggest using 4k blocks.
> >>
> >> > +            fis.close();
> >> > +            jos.close();
> >>
> >> Close should be in a finally block.
> >>
> >> > +            File oldJar = new File(jarFile);
> >> > +            oldJar.delete();
> >> > +            tempJar.renameTo(oldJar);
> >>
> >> It is safer to move the old file aside, rename the temp jar, then
> >> delete the old file.  That way if the rename fails, you can back
> >> it out.
> >>
> >> > +        } catch (FileNotFoundException e) {
> >> > +            throw new OpenEJBException(messages.format("file.
> >> > 0003", file, jarFile, e.getMessage()));
> >> > +        } catch (IOException e) {
> >> > +            throw new OpenEJBException(messages.format("file.
> >> > 0003", file, jarFile, e.getMessage()));
> >> >          }
> >> >
> >> >      }
> >>
> >>
> >
> >
> > --
> > Karan Malhi
>
>


-- 
Karan Malhi

Re: Review of patch for OPENEJB-434

Posted by David Blevins <da...@visi.com>.
On Jan 8, 2007, at 6:09 PM, Karan Malhi wrote:

> Thanks Dain,
>
> I will make the suggested changes and submit another patch.

I just have to point out.... Most people tend to say "sorry" when  
they get feedback, but you said "thanks."  Definitely the right  
attitude to have!  Big thumbs up!

Thanks Karan and Dain.

-David

>
> On 1/8/07, Dain Sundstrom <da...@iq80.com> wrote:
>>
>> This is a good start but I see some bugs :)
>>
>> comments in line...
>>
>> -dain
>>
>> On Jan 7, 2007, at 12:59 PM, jlaskowski@apache.org wrote:
>>
>> > +            JarInputStream jis = new JarInputStream(new
>> > FileInputStream(jarFile));
>> > +            File tempJar = File.createTempFile("temp", "jar");
>>
>> The temp file should be created in the same directory as the final
>> original file because on some platforms the temp directory is on a
>> separate drive and rename only works when the source and target file
>> are on the same drive.
>>
>> > +            JarOutputStream jos = new JarOutputStream(new
>> > FileOutputStream(tempJar));
>> > +            JarEntry nextJarEntry = null;
>> > +            while ((nextJarEntry = jis.getNextJarEntry()) !=  
>> null) {
>> > +                jos.putNextEntry(nextJarEntry);
>> > +            }
>>
>> You need to copy the contents from jis to jos after putNextEntry.
>>
>> > +            jis.close();
>> > +            jos.putNextEntry(new JarEntry(file));
>> > +            FileInputStream fis = new FileInputStream(file);
>> > +            for (int c = fis.read(); c != -1; c = fis.read()) {
>> > +                jos.write(c);
>> > +            }
>>
>> Byte at a time is quite slow.  Instead I suggest using 4k blocks.
>>
>> > +            fis.close();
>> > +            jos.close();
>>
>> Close should be in a finally block.
>>
>> > +            File oldJar = new File(jarFile);
>> > +            oldJar.delete();
>> > +            tempJar.renameTo(oldJar);
>>
>> It is safer to move the old file aside, rename the temp jar, then
>> delete the old file.  That way if the rename fails, you can back  
>> it out.
>>
>> > +        } catch (FileNotFoundException e) {
>> > +            throw new OpenEJBException(messages.format("file.
>> > 0003", file, jarFile, e.getMessage()));
>> > +        } catch (IOException e) {
>> > +            throw new OpenEJBException(messages.format("file.
>> > 0003", file, jarFile, e.getMessage()));
>> >          }
>> >
>> >      }
>>
>>
>
>
> -- 
> Karan Malhi


Re: Review of patch for OPENEJB-434

Posted by Karan Malhi <ka...@gmail.com>.
Thanks Dain,

I will make the suggested changes and submit another patch.

On 1/8/07, Dain Sundstrom <da...@iq80.com> wrote:
>
> This is a good start but I see some bugs :)
>
> comments in line...
>
> -dain
>
> On Jan 7, 2007, at 12:59 PM, jlaskowski@apache.org wrote:
>
> > +            JarInputStream jis = new JarInputStream(new
> > FileInputStream(jarFile));
> > +            File tempJar = File.createTempFile("temp", "jar");
>
> The temp file should be created in the same directory as the final
> original file because on some platforms the temp directory is on a
> separate drive and rename only works when the source and target file
> are on the same drive.
>
> > +            JarOutputStream jos = new JarOutputStream(new
> > FileOutputStream(tempJar));
> > +            JarEntry nextJarEntry = null;
> > +            while ((nextJarEntry = jis.getNextJarEntry()) != null) {
> > +                jos.putNextEntry(nextJarEntry);
> > +            }
>
> You need to copy the contents from jis to jos after putNextEntry.
>
> > +            jis.close();
> > +            jos.putNextEntry(new JarEntry(file));
> > +            FileInputStream fis = new FileInputStream(file);
> > +            for (int c = fis.read(); c != -1; c = fis.read()) {
> > +                jos.write(c);
> > +            }
>
> Byte at a time is quite slow.  Instead I suggest using 4k blocks.
>
> > +            fis.close();
> > +            jos.close();
>
> Close should be in a finally block.
>
> > +            File oldJar = new File(jarFile);
> > +            oldJar.delete();
> > +            tempJar.renameTo(oldJar);
>
> It is safer to move the old file aside, rename the temp jar, then
> delete the old file.  That way if the rename fails, you can back it out.
>
> > +        } catch (FileNotFoundException e) {
> > +            throw new OpenEJBException(messages.format("file.
> > 0003", file, jarFile, e.getMessage()));
> > +        } catch (IOException e) {
> > +            throw new OpenEJBException(messages.format("file.
> > 0003", file, jarFile, e.getMessage()));
> >          }
> >
> >      }
>
>


-- 
Karan Malhi

Review of patch for OPENEJB-434

Posted by Dain Sundstrom <da...@iq80.com>.
This is a good start but I see some bugs :)

comments in line...

-dain

On Jan 7, 2007, at 12:59 PM, jlaskowski@apache.org wrote:

> +            JarInputStream jis = new JarInputStream(new  
> FileInputStream(jarFile));
> +            File tempJar = File.createTempFile("temp", "jar");

The temp file should be created in the same directory as the final  
original file because on some platforms the temp directory is on a  
separate drive and rename only works when the source and target file  
are on the same drive.

> +            JarOutputStream jos = new JarOutputStream(new  
> FileOutputStream(tempJar));
> +            JarEntry nextJarEntry = null;
> +            while ((nextJarEntry = jis.getNextJarEntry()) != null) {
> +                jos.putNextEntry(nextJarEntry);
> +            }

You need to copy the contents from jis to jos after putNextEntry.

> +            jis.close();
> +            jos.putNextEntry(new JarEntry(file));
> +            FileInputStream fis = new FileInputStream(file);
> +            for (int c = fis.read(); c != -1; c = fis.read()) {
> +                jos.write(c);
> +            }

Byte at a time is quite slow.  Instead I suggest using 4k blocks.

> +            fis.close();
> +            jos.close();

Close should be in a finally block.

> +            File oldJar = new File(jarFile);
> +            oldJar.delete();
> +            tempJar.renameTo(oldJar);

It is safer to move the old file aside, rename the temp jar, then  
delete the old file.  That way if the rename fails, you can back it out.

> +        } catch (FileNotFoundException e) {
> +            throw new OpenEJBException(messages.format("file. 
> 0003", file, jarFile, e.getMessage()));
> +        } catch (IOException e) {
> +            throw new OpenEJBException(messages.format("file. 
> 0003", file, jarFile, e.getMessage()));
>          }
>
>      }