From f35b23cd42ab8b80a1dbb288b4a34597559bac49 Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Tue, 26 Sep 2006 21:03:42 +0000 Subject: [PATCH] New files for service element for git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@450204 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/types/spi/Provider.java | 62 +++++++++ .../apache/tools/ant/types/spi/Service.java | 122 +++++++++++++++++ src/tests/antunit/taskdefs/jar-spi-test.xml | 124 ++++++++++++++++++ 3 files changed, 308 insertions(+) create mode 100644 src/main/org/apache/tools/ant/types/spi/Provider.java create mode 100644 src/main/org/apache/tools/ant/types/spi/Service.java create mode 100644 src/tests/antunit/taskdefs/jar-spi-test.xml diff --git a/src/main/org/apache/tools/ant/types/spi/Provider.java b/src/main/org/apache/tools/ant/types/spi/Provider.java new file mode 100644 index 000000000..6d0eefd92 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/spi/Provider.java @@ -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()); + } + } +} diff --git a/src/main/org/apache/tools/ant/types/spi/Service.java b/src/main/org/apache/tools/ant/types/spi/Service.java new file mode 100644 index 000000000..2229f33d6 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/spi/Service.java @@ -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()); + } + } +} diff --git a/src/tests/antunit/taskdefs/jar-spi-test.xml b/src/tests/antunit/taskdefs/jar-spi-test.xml new file mode 100644 index 000000000..616960d06 --- /dev/null +++ b/src/tests/antunit/taskdefs/jar-spi-test.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +