View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.tools.ant.BuildException;
26  import org.apache.tools.ant.types.DataType;
27  
28  /**
29   * Local to remote filename mapping (Experimental).
30   *
31   */
32  public final class DistributerMap extends DataType {
33    /**
34     * if property.
35     */
36    private String ifCond;
37  
38    /**
39     * unless property.
40     */
41    private String unlessCond;
42  
43    /**
44     * local directory name.
45     *
46     */
47    private File localName;
48  
49    /**
50     * Canonical local file name.
51     */
52    private String canonicalPath;
53  
54    /**
55     * remote name.
56     *
57     */
58    private String remoteName;
59  
60    /**
61     * Separator (/ or \) character on remote system.
62     */
63    private char remoteSeparator = File.separatorChar;
64  
65    /**
66     * hosts that for which this map is valid.
67     *
68     */
69    private String hosts;
70  
71    /**
72     * Constructor.
73     *
74     */
75    public DistributerMap() {
76    }
77  
78    /**
79     * Required by documentation generator.
80     */
81    public void execute() {
82      throw new org.apache.tools.ant.BuildException("Not an actual task, but looks like one for documentation purposes");
83    }
84  
85    /**
86     * Gets local directory.
87     * 
88     * @return local directory, may be null.
89     *
90     */
91    public File getLocal() {
92      return this.localName;
93    }
94  
95    /**
96     * Gets remote name for directory.
97     * 
98     * @return remote name, may be null.
99     *
100    */
101   public String getRemote() {
102     return this.remoteName;
103   }
104 
105   /**
106    * Returns true if the if and unless conditions (if any) are
107    * satisfied.
108    *
109    * @return true if this object is active.
110    */
111   public boolean isActive() {
112     return CUtil.isActive(getProject(), this.ifCond, this.unlessCond);
113   }
114 
115   /**
116    * Sets hosts for which this mapping is valid.
117    *
118    * @param value
119    *          hosts
120    */
121   public void setHosts(final String value) {
122     this.hosts = value;
123   }
124 
125   /**
126    * Sets the property name for the 'if' condition.
127    *
128    * This object will be ignored unless the property is defined.
129    *
130    * The value of the property is insignificant, but values that would imply
131    * misinterpretation ("false", "no") will throw an exception when
132    * evaluated.
133    *
134    * @param propName
135    *          property name
136    */
137   public void setIf(final String propName) {
138     this.ifCond = propName;
139   }
140 
141   /**
142    * Sets local directory for base of mapping.
143    *
144    * @param value
145    *          value
146    */
147   public void setLocal(final File value) {
148     if (value == null) {
149       throw new NullPointerException("value");
150     }
151     if (value.exists() && !value.isDirectory()) {
152       throw new BuildException("local should be a directory");
153     }
154     this.localName = value;
155     try {
156       this.canonicalPath = this.localName.getCanonicalPath();
157     } catch (final IOException ex) {
158       throw new BuildException(ex);
159     }
160   }
161 
162   /**
163    * Sets remote name for directory.
164    * 
165    * @param value
166    *          remote name for directory
167    */
168   public void setRemote(final String value) {
169     this.remoteName = value;
170   }
171 
172   /**
173    * Sets the separator character (/ or \) for the remote system.
174    * 
175    * @param value
176    *          separator character
177    */
178   public void setRemoteSeparator(final String value) {
179     if (value != null && value.length() != 1) {
180       throw new BuildException("remote separator must be a single character");
181     }
182     this.remoteSeparator = value.charAt(0);
183   }
184 
185   /**
186    * Set the property name for the 'unless' condition.
187    *
188    * If named property is set, the define will be ignored.
189    *
190    * The value of the property is insignificant, but values that would imply
191    * misinterpretation ("false", "no") of the behavior will throw an
192    * exception when evaluated.
193    *
194    * @param propName
195    *          name of property
196    */
197   public void setUnless(final String propName) {
198     this.unlessCond = propName;
199   }
200 
201   /**
202    * Converts the local file name to the remote name for the same file.
203    *
204    * @param host
205    *          host
206    * @param localFile
207    *          local file
208    * @return remote name for local file, null if unknown.
209    */
210   public String toRemote(final String host, final File localFile) {
211     if (this.remoteName != null && (this.hosts == null || this.hosts.contains(host))) {
212       try {
213         final String canonical = localFile.getCanonicalPath();
214         if (canonical.startsWith(this.canonicalPath)) {
215           if (isActive()) {
216             return this.remoteName
217                 + canonical.substring(this.canonicalPath.length()).replace(File.separatorChar, this.remoteSeparator);
218           }
219         }
220       } catch (final IOException ex) {
221         return null;
222       }
223     }
224     return null;
225   }
226 
227 }