KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > spi > Service


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types.spi;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.tools.ant.ProjectComponent;
31 import org.apache.tools.ant.BuildException;
32
33 /**
34  * ANT Jar-Task SPI extension
35  *
36  * @see <a HREF="http://issues.apache.org/bugzilla/show_bug.cgi?id=31520">
37  * http://issues.apache.org/bugzilla/show_bug.cgi?id=31520</a>
38  */

39 public class Service extends ProjectComponent {
40     private List JavaDoc providerList = new ArrayList JavaDoc();
41     private String JavaDoc type;
42
43     /**
44      * Set the provider classname.
45      * @param className the classname of a provider of this service.
46      */

47     public void setProvider(String JavaDoc className) {
48         Provider provider = new Provider();
49         provider.setClassName(className);
50         providerList.add(provider);
51     }
52
53     /**
54      * Add a nested provider element.
55      * @param provider a provider element.
56      */

57     public void addConfiguredProvider(Provider provider) {
58         provider.check();
59         providerList.add(provider);
60     }
61
62     /**
63      * @return the service type.
64      */

65     public String JavaDoc getType() {
66         return type;
67     }
68
69     /**
70      * Set the service type.
71      * @param type the service type, a classname of
72      * an interface or a class (normally
73      * abstract).
74      */

75     public void setType(String JavaDoc type) {
76         this.type = type;
77     }
78
79     /**
80      * Return the implementations of this
81      * services as an inputstream.
82      * @return an inputstream of the classname names
83      * encoded as UTF-8.
84      * @throws IOException if there is an error.
85      */

86     public InputStream JavaDoc getAsStream() throws IOException JavaDoc {
87         ByteArrayOutputStream JavaDoc arrayOut;
88         Writer JavaDoc writer;
89         Iterator JavaDoc providerIterator;
90         Provider provider;
91
92         arrayOut = new ByteArrayOutputStream JavaDoc();
93         writer = new OutputStreamWriter JavaDoc(arrayOut, "UTF-8");
94         providerIterator = providerList.iterator();
95         while (providerIterator.hasNext()) {
96             provider = (Provider) providerIterator.next();
97             writer.write(provider.getClassName());
98             writer.write("\n");
99         }
100         writer.close();
101         return new ByteArrayInputStream JavaDoc(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(
116                 "Invalid empty type classname", 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 }
125
Popular Tags