1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32 public class VfsTask extends Task {
33
34 private static StandardFileSystemManager manager;
35
36
37
38
39
40
41
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
56
57 protected void closeManager() {
58 if (manager != null) {
59 manager.close();
60 manager = null;
61 }
62 }
63
64
65
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
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 }