001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.spi;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStreamWriter;
025: import java.io.Writer;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.apache.tools.ant.ProjectComponent;
031: import org.apache.tools.ant.BuildException;
032:
033: /**
034: * ANT Jar-Task SPI extension
035: *
036: * @see <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=31520">
037: * http://issues.apache.org/bugzilla/show_bug.cgi?id=31520</a>
038: */
039: public class Service extends ProjectComponent {
040: private List providerList = new ArrayList();
041: private String type;
042:
043: /**
044: * Set the provider classname.
045: * @param className the classname of a provider of this service.
046: */
047: public void setProvider(String className) {
048: Provider provider = new Provider();
049: provider.setClassName(className);
050: providerList.add(provider);
051: }
052:
053: /**
054: * Add a nested provider element.
055: * @param provider a provider element.
056: */
057: public void addConfiguredProvider(Provider provider) {
058: provider.check();
059: providerList.add(provider);
060: }
061:
062: /**
063: * @return the service type.
064: */
065: public String getType() {
066: return type;
067: }
068:
069: /**
070: * Set the service type.
071: * @param type the service type, a classname of
072: * an interface or a class (normally
073: * abstract).
074: */
075: public void setType(String type) {
076: this .type = type;
077: }
078:
079: /**
080: * Return the implementations of this
081: * services as an inputstream.
082: * @return an inputstream of the classname names
083: * encoded as UTF-8.
084: * @throws IOException if there is an error.
085: */
086: public InputStream getAsStream() throws IOException {
087: ByteArrayOutputStream arrayOut;
088: Writer writer;
089: Iterator providerIterator;
090: Provider provider;
091:
092: arrayOut = new ByteArrayOutputStream();
093: writer = new OutputStreamWriter(arrayOut, "UTF-8");
094: providerIterator = providerList.iterator();
095: while (providerIterator.hasNext()) {
096: provider = (Provider) providerIterator.next();
097: writer.write(provider.getClassName());
098: writer.write("\n");
099: }
100: writer.close();
101: return new ByteArrayInputStream(arrayOut.toByteArray());
102: }
103:
104: /**
105: * Check if this object is configured correctly as a nested
106: * element.
107: */
108: public void check() {
109: if (type == null) {
110: throw new BuildException(
111: "type attribute must be set for service element",
112: getLocation());
113: }
114: if (type.length() == 0) {
115: throw new BuildException("Invalid empty type classname",
116: getLocation());
117: }
118: if (providerList.size() == 0) {
119: throw new BuildException(
120: "provider attribute or nested provider element must be set!",
121: getLocation());
122: }
123: }
124: }
|