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      private static StandardFileSystemManager manager;
35  
36      /**
37       * Resolves a URI to a file, relative to the project's base directory.
38       *
39       * @param uri The URI to resolve.
40       * @return resolved file object.
41       * @throws FileSystemException If an error occurred.
42       */
43      protected FileObject resolveFile(final String uri) throws FileSystemException {
44          if (manager == null) {
45              final StandardFileSystemManagerystemManager.html#StandardFileSystemManager">StandardFileSystemManager mngr = new StandardFileSystemManager();
46              mngr.setLogger(new AntLogger());
47              mngr.init();
48              manager = mngr;
49              getProject().addBuildListener(new CloseListener());
50          }
51          return manager.resolveFile(getProject().getBaseDir(), uri);
52      }
53  
54      /**
55       * Close the manager
56       */
57      protected void closeManager() {
58          if (manager != null) {
59              manager.close();
60              manager = null;
61          }
62      }
63  
64      /**
65       * Closes the VFS manager when the project finishes.
66       */
67      private class CloseListener implements SubBuildListener {
68          @Override
69          public void subBuildStarted(final BuildEvent buildEvent) {
70          }
71  
72          @Override
73          public void subBuildFinished(final BuildEvent buildEvent) {
74              closeManager();
75          }
76  
77          @Override
78          public void buildFinished(final BuildEvent event) {
79              closeManager();
80          }
81  
82          @Override
83          public void buildStarted(final BuildEvent event) {
84          }
85  
86          @Override
87          public void messageLogged(final BuildEvent event) {
88          }
89  
90          @Override
91          public void targetFinished(final BuildEvent event) {
92          }
93  
94          @Override
95          public void targetStarted(final BuildEvent event) {
96          }
97  
98          @Override
99          public void taskFinished(final BuildEvent event) {
100         }
101 
102         @Override
103         public void taskStarted(final BuildEvent event) {
104         }
105     }
106 
107     /**
108      * A commons-logging wrapper for Ant logging.
109      */
110     private class AntLogger implements Log {
111         @Override
112         public void debug(final Object o) {
113             log(String.valueOf(o), Project.MSG_DEBUG);
114         }
115 
116         @Override
117         public void debug(final Object o, final Throwable throwable) {
118             debug(o);
119         }
120 
121         @Override
122         public void error(final Object o) {
123             log(String.valueOf(o), Project.MSG_ERR);
124         }
125 
126         @Override
127         public void error(final Object o, final Throwable throwable) {
128             error(o);
129         }
130 
131         @Override
132         public void fatal(final Object o) {
133             log(String.valueOf(o), Project.MSG_ERR);
134         }
135 
136         @Override
137         public void fatal(final Object o, final Throwable throwable) {
138             fatal(o);
139         }
140 
141         @Override
142         public void info(final Object o) {
143             log(String.valueOf(o), Project.MSG_INFO);
144         }
145 
146         @Override
147         public void info(final Object o, final Throwable throwable) {
148             info(o);
149         }
150 
151         @Override
152         public void trace(final Object o) {
153         }
154 
155         @Override
156         public void trace(final Object o, final Throwable throwable) {
157         }
158 
159         @Override
160         public void warn(final Object o) {
161             log(String.valueOf(o), Project.MSG_WARN);
162         }
163 
164         @Override
165         public void warn(final Object o, final Throwable throwable) {
166             warn(o);
167         }
168 
169         @Override
170         public boolean isDebugEnabled() {
171             return true;
172         }
173 
174         @Override
175         public boolean isErrorEnabled() {
176             return true;
177         }
178 
179         @Override
180         public boolean isFatalEnabled() {
181             return true;
182         }
183 
184         @Override
185         public boolean isInfoEnabled() {
186             return true;
187         }
188 
189         @Override
190         public boolean isTraceEnabled() {
191             return false;
192         }
193 
194         @Override
195         public boolean isWarnEnabled() {
196             return true;
197         }
198     }
199 }