KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > tasks > VfsTask


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.tasks;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.impl.StandardFileSystemManager;
22 import org.apache.tools.ant.BuildEvent;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.SubBuildListener;
25 import org.apache.tools.ant.Task;
26
27 /**
28  * Base class for the VFS Ant tasks. Takes care of creating a FileSystemManager,
29  * and for cleaning it up at the end of the build. Also provides some
30  * utility methods.
31  *
32  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
33  */

34 public class VfsTask
35     extends Task
36 {
37     private static StandardFileSystemManager manager;
38
39     /**
40      * Resolves a URI to a file, relative to the project's base directory.
41      *
42      * @param uri The URI to resolve.
43      */

44     protected FileObject resolveFile(final String JavaDoc uri)
45         throws FileSystemException
46     {
47         if (manager == null)
48         {
49             manager = new StandardFileSystemManager();
50             manager.setLogger(new AntLogger());
51             manager.init();
52             getProject().addBuildListener(new CloseListener());
53         }
54         return manager.resolveFile(getProject().getBaseDir(), uri);
55     }
56
57     /**
58      * Close the manager
59      */

60     protected void closeManager()
61     {
62         if (manager != null)
63         {
64             manager.close();
65             manager = null;
66         }
67     }
68
69     /**
70      * Closes the VFS manager when the project finishes.
71      */

72     private class CloseListener
73         implements SubBuildListener
74     {
75         public void subBuildStarted(BuildEvent buildEvent)
76         {
77         }
78
79         public void subBuildFinished(BuildEvent buildEvent)
80         {
81             closeManager();
82         }
83
84         public void buildFinished(BuildEvent event)
85         {
86             closeManager();
87         }
88
89         public void buildStarted(BuildEvent event)
90         {
91         }
92
93         public void messageLogged(BuildEvent event)
94         {
95         }
96
97         public void targetFinished(BuildEvent event)
98         {
99         }
100
101         public void targetStarted(BuildEvent event)
102         {
103         }
104
105         public void taskFinished(BuildEvent event)
106         {
107         }
108
109         public void taskStarted(BuildEvent event)
110         {
111         }
112     }
113
114     /**
115      * A commons-logging wrapper for Ant logging.
116      */

117     private class AntLogger
118         implements Log
119     {
120         public void debug(final Object JavaDoc o)
121         {
122             log(String.valueOf(o), Project.MSG_DEBUG);
123         }
124
125         public void debug(Object JavaDoc o, Throwable JavaDoc throwable)
126         {
127             debug(o);
128         }
129
130         public void error(Object JavaDoc o)
131         {
132             log(String.valueOf(o), Project.MSG_ERR);
133         }
134
135         public void error(Object JavaDoc o, Throwable JavaDoc throwable)
136         {
137             error(o);
138         }
139
140         public void fatal(Object JavaDoc o)
141         {
142             log(String.valueOf(o), Project.MSG_ERR);
143         }
144
145         public void fatal(Object JavaDoc o, Throwable JavaDoc throwable)
146         {
147             fatal(o);
148         }
149
150         public void info(Object JavaDoc o)
151         {
152             log(String.valueOf(o), Project.MSG_INFO);
153         }
154
155         public void info(Object JavaDoc o, Throwable JavaDoc throwable)
156         {
157             info(o);
158         }
159
160         public void trace(Object JavaDoc o)
161         {
162         }
163
164         public void trace(Object JavaDoc o, Throwable JavaDoc throwable)
165         {
166         }
167
168         public void warn(Object JavaDoc o)
169         {
170             log(String.valueOf(o), Project.MSG_WARN);
171         }
172
173         public void warn(Object JavaDoc o, Throwable JavaDoc throwable)
174         {
175             warn(o);
176         }
177
178         public boolean isDebugEnabled()
179         {
180             return true;
181         }
182
183         public boolean isErrorEnabled()
184         {
185             return true;
186         }
187
188         public boolean isFatalEnabled()
189         {
190             return true;
191         }
192
193         public boolean isInfoEnabled()
194         {
195             return true;
196         }
197
198         public boolean isTraceEnabled()
199         {
200             return false;
201         }
202
203         public boolean isWarnEnabled()
204         {
205             return true;
206         }
207     }
208 }
209
Popular Tags