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