You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Kevin Jackson <fo...@gmail.com> on 2006/12/12 08:17:47 UTC

Java6 possible condition HasFreeSpace

Hi all,

I've coded up a new condition to take advantage of (one of) the new
features of the the java.io.File api

Currently this works and I have an antunit test and everything!

>>>>
public class HasFreeSpace implements Condition {

	private String partition;
	private String needed;
	
	public boolean eval() throws BuildException {
		try {
			if (JavaEnvUtils.isAtLeastJavaVersion("1.6")) {
				long free = Java6FileUtils.freeSpace(partition);
				return free >= StringUtils.parseHumanSizes(needed);
			} else {
				throw new BuildException("HasFreeSpace condition not supported on
Java5 or less.");
			}
		} catch (Exception e) {
			throw new BuildException(e);
		}
	}

<<<<
>>>>
<?xml version="1.0"?>
<project name="hasfreespace-test" default="all" basedir="."
xmlns:au="antlib:org.apache.ant.antunit">

  <target name="test-not-enough-space-human">
    <au:assertFalse>
      <hasfreespace partition="c:" needed="1P"/>			
    </au:assertFalse>	
  </target>

  <target name="test-enough-space-human">
    <au:assertTrue>
      <hasfreespace partition="c:" needed="1K"/>			
    </au:assertTrue>	
  </target>

  <target name="test-not-enough-space">
  	<property name="long.max-value" value="9223372036854775807"/>
    <au:assertFalse>
   	  <hasfreespace partition="c:" needed="${long.max-value}"/>
    </au:assertFalse>
  </target>
		
  <target name="test-enough-space">
    <au:assertTrue>
      <hasfreespace partition="c:" needed="1"/>			
    </au:assertTrue>	
  </target>

  <target name="all">
    <au:antunit>
      <fileset file="${ant.file}"/>
      <au:plainlistener/>
    </au:antunit>
  </target>

</project>
<<<<

Now the problem is that I currently use a Java6FileUtils to allow me
to keep the special Java6 methods out of the base FileUtils, but none
of this code will compile on <jdk6 (well ok the StringUtils code is
fine).  I'd like to separate out the java6 specific stuff in a way
that allows me to bootstrap/build correctly on jdk5, but right now it
fails as the bootstrap script expects everything in the conditions
package to be bwc with jdk1.2.

I want to check in the StringUtils changes as they will build fine on
pre-jdk6, but they'd be unused unless I checked in my new condition.
So any helpful suggestions as to how to structure everything to work
in a bwc way and ignore my new condition when on jdk5 & lower?

(I've added a o.a.t.a.u.java6 package which my Java6FileUtils resides
in, so I can conditionally compile that, it's just that the
HasFreeSpace condition is in with the rest of the conditions and this
is failing on Java5)

Thanks,
Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: Java6 possible condition HasFreeSpace

Posted by Kevin Jackson <fo...@gmail.com>.
Hi all,

> I just use reflection for the javax.scripting stuff.
> It is not too bad - see o.a.t.a.util.ReflectUtil and ReflectWrapper.

Thanks Peter, I was considering reflection, but needed a poke to
actually use it.

Well it's all checked in (and I uncovered a minor bug in the build
file while I was at it).  Please feel free to -1 this if you feel it's
frivolous or shouldn't go in 1.7.0 etc ;)

Also if there are tabs instead of spaces (which just viewing the svn
messages in gmail looks likely), please scream loudly, I've triple
checked eclipse, but after installing java6 this morning, everything
went a bit screwy and eclipse is certainly behaving strange at the
moment, so I'm not sure which of the profiles I have is actually being
used to ant.  Java6 & Eclipse == very slow ant editor, seems as if
something broke somewhere.

Thanks,
Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: Java6 possible condition HasFreeSpace

Posted by Peter Reilly <pe...@gmail.com>.
On 12/12/06, Kevin Jackson <fo...@gmail.com> wrote:
> Hi all,
>
> I've coded up a new condition to take advantage of (one of) the new
> features of the the java.io.File api
>
> Currently this works and I have an antunit test and everything!
>
> >>>>
> public class HasFreeSpace implements Condition {
>
>         private String partition;
>         private String needed;
>
>         public boolean eval() throws BuildException {
>                 try {
>                         if (JavaEnvUtils.isAtLeastJavaVersion("1.6")) {
>                                 long free = Java6FileUtils.freeSpace(partition);
>                                 return free >= StringUtils.parseHumanSizes(needed);
>                         } else {
>                                 throw new BuildException("HasFreeSpace condition not supported on
> Java5 or less.");
>                         }
>                 } catch (Exception e) {
>                         throw new BuildException(e);
>                 }
>         }
>
> <<<<
> >>>>
> <?xml version="1.0"?>
> <project name="hasfreespace-test" default="all" basedir="."
> xmlns:au="antlib:org.apache.ant.antunit">
>
>   <target name="test-not-enough-space-human">
>     <au:assertFalse>
>       <hasfreespace partition="c:" needed="1P"/>
>     </au:assertFalse>
>   </target>
>
>   <target name="test-enough-space-human">
>     <au:assertTrue>
>       <hasfreespace partition="c:" needed="1K"/>
>     </au:assertTrue>
>   </target>
>
>   <target name="test-not-enough-space">
>         <property name="long.max-value" value="9223372036854775807"/>
>     <au:assertFalse>
>           <hasfreespace partition="c:" needed="${long.max-value}"/>
>     </au:assertFalse>
>   </target>
>
>   <target name="test-enough-space">
>     <au:assertTrue>
>       <hasfreespace partition="c:" needed="1"/>
>     </au:assertTrue>
>   </target>
>
>   <target name="all">
>     <au:antunit>
>       <fileset file="${ant.file}"/>
>       <au:plainlistener/>
>     </au:antunit>
>   </target>
>
> </project>
> <<<<
>
> Now the problem is that I currently use a Java6FileUtils to allow me
> to keep the special Java6 methods out of the base FileUtils, but none
> of this code will compile on <jdk6 (well ok the StringUtils code is
> fine).  I'd like to separate out the java6 specific stuff in a way
> that allows me to bootstrap/build correctly on jdk5, but right now it
> fails as the bootstrap script expects everything in the conditions
> package to be bwc with jdk1.2.

I just use reflection for the javax.scripting stuff.
It is not too bad - see o.a.t.a.util.ReflectUtil and ReflectWrapper.

Peter

>
> I want to check in the StringUtils changes as they will build fine on
> pre-jdk6, but they'd be unused unless I checked in my new condition.
> So any helpful suggestions as to how to structure everything to work
> in a bwc way and ignore my new condition when on jdk5 & lower?
>
> (I've added a o.a.t.a.u.java6 package which my Java6FileUtils resides
> in, so I can conditionally compile that, it's just that the
> HasFreeSpace condition is in with the rest of the conditions and this
> is failing on Java5)
>
> Thanks,
> Kev
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org