Author: peterreilly Date: Tue Sep 26 14:03:42 2006 New Revision: 450204 URL: http://svn.apache.org/viewvc?view=rev&rev=450204 Log: New files for service element for <jar>
Added: ant/core/trunk/src/main/org/apache/tools/ant/types/spi/ ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Provider.java ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Service.java ant/core/trunk/src/tests/antunit/taskdefs/jar-spi-test.xml Added: ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Provider.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Provider.java?view=auto&rev=450204 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Provider.java (added) +++ ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Provider.java Tue Sep 26 14:03:42 2006 @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.tools.ant.types.spi; + +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.BuildException; + +/** + * ANT Jar-Task SPI extension + * This class corresponds to the nested element + * <provider type="type"> in the <service type=""> + * nested element of the jar task. + * @see http://issues.apache.org/bugzilla/show_bug.cgi?id=31520 + */ +public class Provider extends ProjectComponent { + private String type; + + /** + * @return the class name for + */ + public String getClassName(){ + return type; + } + + /** + * Set the provider classname. + * @param type the value to set. + */ + public void setClassName(String type){ + this.type = type; + } + + /** + * Check if the component has been configured correctly. + */ + public void check() { + if (type == null) { + throw new BuildException( + "classname attribute must be set for provider element", + getLocation()); + } + if (type.length() == 0) { + throw new BuildException( + "Invalid empty classname", getLocation()); + } + } +} Added: ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Service.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Service.java?view=auto&rev=450204 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Service.java (added) +++ ant/core/trunk/src/main/org/apache/tools/ant/types/spi/Service.java Tue Sep 26 14:03:42 2006 @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.tools.ant.types.spi; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.tools.ant.ProjectComponent; +import org.apache.tools.ant.BuildException; + +/** + * ANT Jar-Task SPI extension + * + * @see http://issues.apache.org/bugzilla/show_bug.cgi?id=31520 + */ +public class Service extends ProjectComponent { + private List providerList = new ArrayList(); + private String type; + + /** + * Set the provider classname. + * @param className the classname of a provider of this service. + */ + public void setProvider(String className) { + Provider provider = new Provider(); + provider.setClassName(className); + providerList.add(provider); + } + + /** + * Add a nested provider element. + * @param provider a provider element. + */ + public void addConfiguredProvider(Provider provider) { + provider.check(); + providerList.add(provider); + } + + /** + * @return the service type. + */ + public String getType(){ + return type; + } + + /** + * Set the service type. + * @param type the service type, a classname of + * an interface or a class (normally + * abstract). + */ + public void setType(String type){ + this.type = type; + } + + /** + * Return the implementations of this + * services as an inputstream. + * @return an inputstream of the classname names + * encoded as UTF-8. + */ + public InputStream getAsStream() throws IOException { + ByteArrayOutputStream arrayOut; + Writer writer; + Iterator providerIterator; + Provider provider; + + arrayOut = new ByteArrayOutputStream(); + writer = new OutputStreamWriter(arrayOut, "UTF-8"); + providerIterator = providerList.iterator(); + while (providerIterator.hasNext()){ + provider = (Provider) providerIterator.next(); + writer.write(provider.getClassName()); + writer.write("\n"); + } + writer.close(); + return new ByteArrayInputStream(arrayOut.toByteArray()); + } + + /** + * Check if this object is configured correctly as a nested + * element. + */ + public void check() { + if (type == null) { + throw new BuildException( + "type attribute must be set for service element", + getLocation()); + } + if (type.length() == 0) { + throw new BuildException( + "Invalid empty type classname", getLocation()); + } + if (providerList.size() == 0) { + throw new BuildException( + "provider attribute or nested provider element must be set!", + getLocation()); + } + } +} Added: ant/core/trunk/src/tests/antunit/taskdefs/jar-spi-test.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/jar-spi-test.xml?view=auto&rev=450204 ============================================================================== --- ant/core/trunk/src/tests/antunit/taskdefs/jar-spi-test.xml (added) +++ ant/core/trunk/src/tests/antunit/taskdefs/jar-spi-test.xml Tue Sep 26 14:03:42 2006 @@ -0,0 +1,124 @@ +<?xml version="1.0" encoding="utf-8"?> +<project xmlns:au="antlib:org.apache.ant.antunit"> + <property name="jar.dir" location="jar_spi_dir"/> + <property name="jar.src.dir" location="jar_spi_dir/src"/> + <property name="jar.src.file" location="jar_spi_dir/src/a_file"/> + + <target name="init"> + <mkdir dir="${jar.src.dir}"/> + <delete quiet="yes" file="${jar.src.file}"/> + <touch file="${jar.src.file}"/> + <delete quiet="yes" file="${jar.dir}/file.jar"/> + <delete quiet="yes" dir="${jar.dir}/output"/> + </target> + + <target name="test-simple" depends="init"> + + <jar jarfile="${jar.dir}/file.jar"> + <fileset dir="${jar.src.dir}"/> + <service type="a.b.c" provider="a.b.c.d"/> + </jar> + + <unjar src="${jar.dir}/file.jar" + dest="${jar.dir}/output"/> + + <loadfile property="simple" + srcfile="${jar.dir}/output/META-INF/service/a.b.c" + encoding="UTF-8"/> + + <au:assertTrue> + <matches string="${simple}" pattern="^a\.b\.c\.d\n$"/> + </au:assertTrue> + + </target> + + <target name="test-providers" depends="init"> + + <jar jarfile="${jar.dir}/file.jar"> + <fileset dir="${jar.src.dir}"/> + <service type="a.b.c"> + <provider classname="a.X"/> + <provider classname="a.D"/> + </service> + </jar> + + <unjar src="${jar.dir}/file.jar" + dest="${jar.dir}/output"/> + + <loadfile property="providers" + srcfile="${jar.dir}/output/META-INF/service/a.b.c" + encoding="UTF-8"/> + <au:assertTrue> + <matches string="${providers}" pattern="^a\.X\na\.D$"/> + </au:assertTrue> + + </target> + + <target name="test-multi" depends="init"> + + <jar jarfile="${jar.dir}/file.jar"> + <fileset dir="${jar.src.dir}"/> + <service type="a.b.c"> + <provider classname="a.X"/> + <provider classname="a.D"/> + </service> + <service type="javax.a.service"> + <provider classname="a.O.T"/> + <provider classname="a.B"/> + </service> + </jar> + + <unjar src="${jar.dir}/file.jar" + dest="${jar.dir}/output"/> + + <loadfile property="multi-a" + srcfile="${jar.dir}/output/META-INF/service/a.b.c" + encoding="UTF-8"/> + + <au:assertTrue> + <matches string="${multi-a}" pattern="^a\.X\na\.D$"/> + </au:assertTrue> + + <loadfile property="multi-b" + srcfile="${jar.dir}/output/META-INF/service/javax.a.service" + encoding="UTF-8"/> + + <au:assertTrue> + <matches string="${multi-b}" pattern="^a\.O\.T\na\.B$"/> + </au:assertTrue> + + </target> + + <target name="test-reject-no-type" depends="init"> + <au:expectfailure> + <jar jarfile="${jar.dir}/file.jar"> + <fileset dir="${jar.src.dir}"/> + <service provider="a.X"/> + </jar> + </au:expectfailure> + </target> + + <target name="test-reject-no-provider" depends="init"> + <au:expectfailure> + <jar jarfile="${jar.dir}/file.jar"> + <fileset dir="${jar.src.dir}"/> + <service type="a.X"/> + </jar> + </au:expectfailure> + </target> + + <target name="test-reject-no-classname" depends="init"> + <au:expectfailure> + <jar jarfile="${jar.dir}/file.jar"> + <fileset dir="${jar.src.dir}"/> + <service type="a.X"> + <provider/> + </service> + </jar> + </au:expectfailure> + </target> + + <target name="tearDown"> + <delete quiet="yes" dir="${jar.dir}"/> + </target> +</project> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]