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: package org.apache.commons.vfs.tasks;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.commons.vfs.FileSystemException;
022: import org.apache.commons.vfs.impl.StandardFileSystemManager;
023: import org.apache.tools.ant.BuildEvent;
024: import org.apache.tools.ant.Project;
025: import org.apache.tools.ant.SubBuildListener;
026: import org.apache.tools.ant.Task;
027:
028: /**
029: * Base class for the VFS Ant tasks. Takes care of creating a FileSystemManager,
030: * and for cleaning it up at the end of the build. Also provides some
031: * utility methods.
032: *
033: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
034: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
035: */
036: public class VfsTask extends Task {
037: private static StandardFileSystemManager manager;
038:
039: /**
040: * Resolves a URI to a file, relative to the project's base directory.
041: *
042: * @param uri The URI to resolve.
043: */
044: protected FileObject resolveFile(final String uri)
045: throws FileSystemException {
046: if (manager == null) {
047: manager = new StandardFileSystemManager();
048: manager.setLogger(new AntLogger());
049: manager.init();
050: getProject().addBuildListener(new CloseListener());
051: }
052: return manager.resolveFile(getProject().getBaseDir(), uri);
053: }
054:
055: /**
056: * Close the manager
057: */
058: protected void closeManager() {
059: if (manager != null) {
060: manager.close();
061: manager = null;
062: }
063: }
064:
065: /**
066: * Closes the VFS manager when the project finishes.
067: */
068: private class CloseListener implements SubBuildListener {
069: public void subBuildStarted(BuildEvent buildEvent) {
070: }
071:
072: public void subBuildFinished(BuildEvent buildEvent) {
073: closeManager();
074: }
075:
076: public void buildFinished(BuildEvent event) {
077: closeManager();
078: }
079:
080: public void buildStarted(BuildEvent event) {
081: }
082:
083: public void messageLogged(BuildEvent event) {
084: }
085:
086: public void targetFinished(BuildEvent event) {
087: }
088:
089: public void targetStarted(BuildEvent event) {
090: }
091:
092: public void taskFinished(BuildEvent event) {
093: }
094:
095: public void taskStarted(BuildEvent event) {
096: }
097: }
098:
099: /**
100: * A commons-logging wrapper for Ant logging.
101: */
102: private class AntLogger implements Log {
103: public void debug(final Object o) {
104: log(String.valueOf(o), Project.MSG_DEBUG);
105: }
106:
107: public void debug(Object o, Throwable throwable) {
108: debug(o);
109: }
110:
111: public void error(Object o) {
112: log(String.valueOf(o), Project.MSG_ERR);
113: }
114:
115: public void error(Object o, Throwable throwable) {
116: error(o);
117: }
118:
119: public void fatal(Object o) {
120: log(String.valueOf(o), Project.MSG_ERR);
121: }
122:
123: public void fatal(Object o, Throwable throwable) {
124: fatal(o);
125: }
126:
127: public void info(Object o) {
128: log(String.valueOf(o), Project.MSG_INFO);
129: }
130:
131: public void info(Object o, Throwable throwable) {
132: info(o);
133: }
134:
135: public void trace(Object o) {
136: }
137:
138: public void trace(Object o, Throwable throwable) {
139: }
140:
141: public void warn(Object o) {
142: log(String.valueOf(o), Project.MSG_WARN);
143: }
144:
145: public void warn(Object o, Throwable throwable) {
146: warn(o);
147: }
148:
149: public boolean isDebugEnabled() {
150: return true;
151: }
152:
153: public boolean isErrorEnabled() {
154: return true;
155: }
156:
157: public boolean isFatalEnabled() {
158: return true;
159: }
160:
161: public boolean isInfoEnabled() {
162: return true;
163: }
164:
165: public boolean isTraceEnabled() {
166: return false;
167: }
168:
169: public boolean isWarnEnabled() {
170: return true;
171: }
172: }
173: }
|