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 */ 017package org.apache.commons.vfs2.tasks; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.vfs2.FileObject; 021import org.apache.commons.vfs2.FileSystemException; 022import org.apache.commons.vfs2.impl.StandardFileSystemManager; 023import org.apache.tools.ant.BuildEvent; 024import org.apache.tools.ant.Project; 025import org.apache.tools.ant.SubBuildListener; 026import org.apache.tools.ant.Task; 027 028/** 029 * Base class for the VFS Ant tasks. Takes care of creating a FileSystemManager, and for cleaning it up at the end of 030 * the build. Also provides some utility methods. 031 */ 032public class VfsTask extends Task { 033 034 /** 035 * A commons-logging wrapper for Ant logging. 036 */ 037 private final class AntLogger implements Log { 038 @Override 039 public void debug(final Object o) { 040 log(String.valueOf(o), Project.MSG_DEBUG); 041 } 042 043 @Override 044 public void debug(final Object o, final Throwable throwable) { 045 debug(o); 046 } 047 048 @Override 049 public void error(final Object o) { 050 log(String.valueOf(o), Project.MSG_ERR); 051 } 052 053 @Override 054 public void error(final Object o, final Throwable throwable) { 055 error(o); 056 } 057 058 @Override 059 public void fatal(final Object o) { 060 log(String.valueOf(o), Project.MSG_ERR); 061 } 062 063 @Override 064 public void fatal(final Object o, final Throwable throwable) { 065 fatal(o); 066 } 067 068 @Override 069 public void info(final Object o) { 070 log(String.valueOf(o), Project.MSG_INFO); 071 } 072 073 @Override 074 public void info(final Object o, final Throwable throwable) { 075 info(o); 076 } 077 078 @Override 079 public boolean isDebugEnabled() { 080 return true; 081 } 082 083 @Override 084 public boolean isErrorEnabled() { 085 return true; 086 } 087 088 @Override 089 public boolean isFatalEnabled() { 090 return true; 091 } 092 093 @Override 094 public boolean isInfoEnabled() { 095 return true; 096 } 097 098 @Override 099 public boolean isTraceEnabled() { 100 return false; 101 } 102 103 @Override 104 public boolean isWarnEnabled() { 105 return true; 106 } 107 108 @Override 109 public void trace(final Object o) { 110 // no-op 111 } 112 113 @Override 114 public void trace(final Object o, final Throwable throwable) { 115 // no-op 116 } 117 118 @Override 119 public void warn(final Object o) { 120 log(String.valueOf(o), Project.MSG_WARN); 121 } 122 123 @Override 124 public void warn(final Object o, final Throwable throwable) { 125 warn(o); 126 } 127 } 128 129 /** 130 * Closes the VFS manager when the project finishes. 131 */ 132 private final class CloseListener implements SubBuildListener { 133 @Override 134 public void buildFinished(final BuildEvent event) { 135 closeManager(); 136 } 137 138 @Override 139 public void buildStarted(final BuildEvent event) { 140 // no-op 141 } 142 143 @Override 144 public void messageLogged(final BuildEvent event) { 145 // no-op 146 } 147 148 @Override 149 public void subBuildFinished(final BuildEvent buildEvent) { 150 closeManager(); 151 } 152 153 @Override 154 public void subBuildStarted(final BuildEvent buildEvent) { 155 // no-op 156 } 157 158 @Override 159 public void targetFinished(final BuildEvent event) { 160 // no-op 161 } 162 163 @Override 164 public void targetStarted(final BuildEvent event) { 165 // no-op 166 } 167 168 @Override 169 public void taskFinished(final BuildEvent event) { 170 // no-op 171 } 172 173 @Override 174 public void taskStarted(final BuildEvent event) { 175 // no-op 176 } 177 } 178 179 private static StandardFileSystemManager manager; 180 181 /** 182 * Constructs a new instance. 183 */ 184 public VfsTask() { 185 // empty 186 } 187 188 /** 189 * Close the manager 190 */ 191 protected void closeManager() { 192 if (manager != null) { 193 manager.close(); 194 manager = null; 195 } 196 } 197 198 /** 199 * Resolves a URI to a file, relative to the project's base directory. 200 * 201 * @param uri The URI to resolve. 202 * @return resolved file object. 203 * @throws FileSystemException If an error occurred. 204 */ 205 protected FileObject resolveFile(final String uri) throws FileSystemException { 206 if (manager == null) { 207 final StandardFileSystemManager mngr = new StandardFileSystemManager(); 208 mngr.setLogger(new AntLogger()); 209 mngr.init(); 210 manager = mngr; 211 getProject().addBuildListener(new CloseListener()); 212 } 213 return manager.resolveFile(getProject().getBaseDir(), uri); 214 } 215}