View Javadoc
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  package org.apache.commons.vfs2.tasks;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.vfs2.FileObject;
21  import org.apache.commons.vfs2.FileSystemException;
22  import org.apache.commons.vfs2.impl.StandardFileSystemManager;
23  import org.apache.tools.ant.BuildEvent;
24  import org.apache.tools.ant.Project;
25  import org.apache.tools.ant.SubBuildListener;
26  import org.apache.tools.ant.Task;
27  
28  /**
29   * Base class for the VFS Ant tasks. Takes care of creating a FileSystemManager, and for cleaning it up at the end of
30   * the build. Also provides some utility methods.
31   */
32  public class VfsTask extends Task {
33  
34      /**
35       * A commons-logging wrapper for Ant logging.
36       */
37      private final class AntLogger implements Log {
38          @Override
39          public void debug(final Object o) {
40              log(String.valueOf(o), Project.MSG_DEBUG);
41          }
42  
43          @Override
44          public void debug(final Object o, final Throwable throwable) {
45              debug(o);
46          }
47  
48          @Override
49          public void error(final Object o) {
50              log(String.valueOf(o), Project.MSG_ERR);
51          }
52  
53          @Override
54          public void error(final Object o, final Throwable throwable) {
55              error(o);
56          }
57  
58          @Override
59          public void fatal(final Object o) {
60              log(String.valueOf(o), Project.MSG_ERR);
61          }
62  
63          @Override
64          public void fatal(final Object o, final Throwable throwable) {
65              fatal(o);
66          }
67  
68          @Override
69          public void info(final Object o) {
70              log(String.valueOf(o), Project.MSG_INFO);
71          }
72  
73          @Override
74          public void info(final Object o, final Throwable throwable) {
75              info(o);
76          }
77  
78          @Override
79          public boolean isDebugEnabled() {
80              return true;
81          }
82  
83          @Override
84          public boolean isErrorEnabled() {
85              return true;
86          }
87  
88          @Override
89          public boolean isFatalEnabled() {
90              return true;
91          }
92  
93          @Override
94          public boolean isInfoEnabled() {
95              return true;
96          }
97  
98          @Override
99          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 }