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.vfs.tasks;
    18
    19  import org.apache.commons.logging.Log;
    20  import org.apache.commons.vfs.FileObject;
    21  import org.apache.commons.vfs.FileSystemException;
    22  import org.apache.commons.vfs.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,
    30   * and for cleaning it up at the end of the build.  Also provides some
    31   * utility methods.
    32   *
    33   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    34   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    35   */
    36  public class VfsTask
    37      extends Task
    38  {
    39      private static StandardFileSystemManager manager;
    40
    41      /***
    42       * Resolves a URI to a file, relative to the project's base directory.
    43       *
    44       * @param uri The URI to resolve.
    45       */
    46      protected FileObject resolveFile(final String uri)
    47          throws FileSystemException
    48      {
    49          if (manager == null)
    50          {
    51              manager = new StandardFileSystemManager();
    52              manager.setLogger(new AntLogger());
    53              manager.init();
    54              getProject().addBuildListener(new CloseListener());
    55          }
    56          return manager.resolveFile(getProject().getBaseDir(), uri);
    57      }
    58
    59      /***
    60       * Close the manager
    61       */
    62      protected void closeManager()
    63      {
    64          if (manager != null)
    65          {
    66              manager.close();
    67              manager = null;
    68          }
    69      }
    70
    71      /***
    72       * Closes the VFS manager when the project finishes.
    73       */
    74      private class CloseListener
    75          implements SubBuildListener
    76      {
    77          public void subBuildStarted(BuildEvent buildEvent)
    78          {
    79          }
    80
    81          public void subBuildFinished(BuildEvent buildEvent)
    82          {
    83              closeManager();
    84          }
    85
    86          public void buildFinished(BuildEvent event)
    87          {
    88              closeManager();
    89          }
    90
    91          public void buildStarted(BuildEvent event)
    92          {
    93          }
    94
    95          public void messageLogged(BuildEvent event)
    96          {
    97          }
    98
    99          public void targetFinished(BuildEvent event)
    100         {
    101         }
    102
    103         public void targetStarted(BuildEvent event)
    104         {
    105         }
    106
    107         public void taskFinished(BuildEvent event)
    108         {
    109         }
    110
    111         public void taskStarted(BuildEvent event)
    112         {
    113         }
    114     }
    115
    116     /***
    117      * A commons-logging wrapper for Ant logging.
    118      */
    119     private class AntLogger
    120         implements Log
    121     {
    122         public void debug(final Object o)
    123         {
    124             log(String.valueOf(o), Project.MSG_DEBUG);
    125         }
    126
    127         public void debug(Object o, Throwable throwable)
    128         {
    129             debug(o);
    130         }
    131
    132         public void error(Object o)
    133         {
    134             log(String.valueOf(o), Project.MSG_ERR);
    135         }
    136
    137         public void error(Object o, Throwable throwable)
    138         {
    139             error(o);
    140         }
    141
    142         public void fatal(Object o)
    143         {
    144             log(String.valueOf(o), Project.MSG_ERR);
    145         }
    146
    147         public void fatal(Object o, Throwable throwable)
    148         {
    149             fatal(o);
    150         }
    151
    152         public void info(Object o)
    153         {
    154             log(String.valueOf(o), Project.MSG_INFO);
    155         }
    156
    157         public void info(Object o, Throwable throwable)
    158         {
    159             info(o);
    160         }
    161
    162         public void trace(Object o)
    163         {
    164         }
    165
    166         public void trace(Object o, Throwable throwable)
    167         {
    168         }
    169
    170         public void warn(Object o)
    171         {
    172             log(String.valueOf(o), Project.MSG_WARN);
    173         }
    174
    175         public void warn(Object o, Throwable throwable)
    176         {
    177             warn(o);
    178         }
    179
    180         public boolean isDebugEnabled()
    181         {
    182             return true;
    183         }
    184
    185         public boolean isErrorEnabled()
    186         {
    187             return true;
    188         }
    189
    190         public boolean isFatalEnabled()
    191         {
    192             return true;
    193         }
    194
    195         public boolean isInfoEnabled()
    196         {
    197             return true;
    198         }
    199
    200         public boolean isTraceEnabled()
    201         {
    202             return false;
    203         }
    204
    205         public boolean isWarnEnabled()
    206         {
    207             return true;
    208         }
    209     }
    210 }