001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.ant;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.net.URL;
029: import java.util.ArrayList;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import org.apache.tools.ant.BuildException;
034: import org.apache.tools.ant.Task;
035:
036: import biz.hammurapi.config.Command;
037: import biz.hammurapi.config.ConfigurationException;
038: import biz.hammurapi.config.Context;
039: import biz.hammurapi.config.DomConfigFactory;
040: import biz.hammurapi.config.DomConfigurableContainer;
041: import biz.hammurapi.logging.AntLogger;
042:
043: /**
044: * PV Container wrapped in Ant task.
045: * Instantiates container, mounts /logger and /taks and then
046: * executes nested <command> entries
047: * @author Pavel Vlasov
048: * @revision $Revision$
049: */
050: public class ContainerTask extends Task {
051: private Collection executes = new ArrayList();
052:
053: /**
054: * Context to "execute".
055: * @author Pavel Vlasov
056: * @revision $Revision$
057: */
058: public class Execute extends ObjectEntry {
059: private String path;
060:
061: public void setPath(String path) {
062: this .path = path;
063: }
064: }
065:
066: public Execute createExecute() {
067: Execute ret = new Execute();
068: executes.add(ret);
069: return ret;
070: }
071:
072: public ContainerTask() {
073: super ();
074: }
075:
076: private String url;
077: private File file;
078: private String resource;
079:
080: /**
081: * URL to load configuration from.
082: * One of URL, file or resource is mandatory.
083: * These attributes are mutually exclusive.
084: * @ant.not-required
085: * @param url
086: */
087: public void setUrl(String url) {
088: checkAlreadySet();
089: this .url = url;
090: }
091:
092: /**
093: * File to load configuration from.
094: * One of URL, file or resource is mandatory.
095: * These attributes are mutually exclusive.
096: * @ant.not-required
097: * @param url
098: */
099: public void setFile(File file) {
100: checkAlreadySet();
101: this .file = file;
102: }
103:
104: /**
105: * Classloader resource to load configuration from.
106: * One of URL, file or resource is mandatory.
107: * These attributes are mutually exclusive.
108: * @ant.not-required
109: * @param url
110: */
111: public void setResource(String resource) {
112: checkAlreadySet();
113: this .resource = resource;
114: }
115:
116: private void checkAlreadySet() {
117: if (url != null) {
118: throw new BuildException("url already set");
119: }
120:
121: if (file != null) {
122: throw new BuildException("file already set");
123: }
124:
125: if (resource != null) {
126: throw new BuildException("resource already set");
127: }
128: }
129:
130: public void execute() throws BuildException {
131: try {
132: ClassLoader classLoader = getClass().getClassLoader();
133: DomConfigFactory factory = new DomConfigFactory(
134: classLoader, (Context) null);
135: Object o;
136: if (url != null) {
137: o = factory.create(new URL(url), null);
138: } else if (file != null) {
139: o = factory.create(file, null);
140: } else if (resource != null) {
141: InputStream in = classLoader
142: .getResourceAsStream(resource);
143: if (in == null) {
144: throw new BuildException("Resource " + resource
145: + " not found");
146: }
147: o = factory.create(in, null);
148: } else {
149: throw new BuildException(
150: "One of url, file or resource attributes must be set");
151: }
152:
153: if (o instanceof DomConfigurableContainer) {
154: DomConfigurableContainer container = (DomConfigurableContainer) o;
155: if (container.get("logger") == null) {
156: container.addComponent("logger",
157: new AntLogger(this ));
158: }
159: if (container.get("task") == null) {
160: container.addComponent("task", this );
161: }
162:
163: container.start();
164: try {
165: Iterator it = executes.iterator();
166: while (it.hasNext()) {
167: Execute execute = (Execute) it.next();
168: Object target = container.get(execute.path);
169: if (target instanceof Command) {
170: ((Command) target).execute(execute
171: .getObject(classLoader));
172: }
173: }
174: } finally {
175: container.stop();
176: }
177: }
178: } catch (IOException e) {
179: throw new BuildException(e.toString(), e);
180: } catch (ConfigurationException e) {
181: throw new BuildException(e.toString(), e);
182: }
183:
184: }
185:
186: }
|