View Javadoc

1   /**
2    * Copyright 2008-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.codehaus.mojo.wagon.shared;
17  
18  /*
19   * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
20   * agreements. See the NOTICE file distributed with this work for additional information regarding
21   * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
22   * "License"); you may not use this file except in compliance with the License. You may obtain a
23   * copy of the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software distributed under the License
28   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29   * or implied. See the License for the specific language governing permissions and limitations under
30   * the License.
31   */
32  
33  import org.apache.maven.artifact.manager.WagonConfigurationException;
34  import org.apache.maven.artifact.manager.WagonManager;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.logging.Log;
37  import org.apache.maven.settings.Proxy;
38  import org.apache.maven.settings.Server;
39  import org.apache.maven.settings.Settings;
40  import org.apache.maven.wagon.UnsupportedProtocolException;
41  import org.apache.maven.wagon.Wagon;
42  import org.apache.maven.wagon.WagonException;
43  import org.apache.maven.wagon.observers.Debug;
44  import org.apache.maven.wagon.proxy.ProxyInfo;
45  import org.apache.maven.wagon.repository.Repository;
46  import org.apache.maven.wagon.repository.RepositoryPermissions;
47  import org.codehaus.plexus.util.StringUtils;
48  
49  public class WagonUtils {
50  
51      /**
52       * Convenient method to create a wagon
53       *
54       * @param id
55       * @param url
56       * @param wagonManager
57       * @param settings
58       * @param logger
59       * @return
60       * @throws MojoExecutionException
61       */
62      public static Wagon createWagon(String id, String url, WagonManager wagonManager, Settings settings, Log logger)
63              throws WagonException, UnsupportedProtocolException, WagonConfigurationException {
64          Wagon wagon = null;
65  
66          final Repository repository = new Repository(id, url);
67          repository.setPermissions(getPermissions(id, settings));
68  
69          wagon = wagonManager.getWagon(repository);
70  
71          if (logger.isDebugEnabled()) {
72              Debug debug = new Debug();
73              wagon.addSessionListener(debug);
74              wagon.addTransferListener(debug);
75          }
76  
77          ProxyInfo proxyInfo = getProxyInfo(settings);
78          if (proxyInfo != null) {
79              wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()), proxyInfo);
80          } else {
81              wagon.connect(repository, wagonManager.getAuthenticationInfo(repository.getId()));
82          }
83  
84          return wagon;
85      }
86  
87      protected static RepositoryPermissions getPermissions(String id, Settings settings) {
88          // May not have an id
89          if (StringUtils.isBlank(id)) {
90              return null;
91          }
92  
93          // May not be a server matching that id
94          Server server = settings.getServer(id);
95          if (server == null) {
96              return null;
97          }
98  
99          // Extract permissions (if there are any)
100         String filePerms = server.getFilePermissions();
101         String dirPerms = server.getDirectoryPermissions();
102 
103         // Check to see if custom permissions were supplied
104         if (StringUtils.isBlank(filePerms) && StringUtils.isBlank(dirPerms)) {
105             return null;
106         }
107 
108         // There are custom permissions specified in settings.xml for this server
109         RepositoryPermissions permissions = new RepositoryPermissions();
110         permissions.setFileMode(filePerms);
111         permissions.setDirectoryMode(dirPerms);
112         return permissions;
113     }
114 
115     public static WagonFileSet getWagonFileSet(String includes, String excludes) {
116         return getWagonFileSet(null, includes, excludes, true, null);
117     }
118 
119     public static WagonFileSet getWagonFileSet(String fromDir, String includes, String excludes,
120             boolean isCaseSensitive, String toDir) {
121         WagonFileSet fileSet = new WagonFileSet();
122         fileSet.setDirectory(fromDir);
123 
124         if (!StringUtils.isBlank(includes)) {
125             fileSet.setIncludes(StringUtils.split(includes, ","));
126         }
127 
128         if (!StringUtils.isBlank(excludes)) {
129             fileSet.setExcludes(StringUtils.split(excludes, ","));
130         }
131 
132         fileSet.setCaseSensitive(isCaseSensitive);
133 
134         fileSet.setOutputDirectory(toDir);
135 
136         return fileSet;
137 
138     }
139 
140     /**
141      * Convenience method to map a <code>Proxy</code> object from the user system settings to a <code>ProxyInfo</code>
142      * object.
143      *
144      * @return a proxyInfo object or null if no active proxy is define in the settings.xml
145      */
146     public static ProxyInfo getProxyInfo(Settings settings) {
147         ProxyInfo proxyInfo = null;
148         if (settings != null && settings.getActiveProxy() != null) {
149             Proxy settingsProxy = settings.getActiveProxy();
150 
151             proxyInfo = new ProxyInfo();
152             proxyInfo.setHost(settingsProxy.getHost());
153             proxyInfo.setType(settingsProxy.getProtocol());
154             proxyInfo.setPort(settingsProxy.getPort());
155             proxyInfo.setNonProxyHosts(settingsProxy.getNonProxyHosts());
156             proxyInfo.setUserName(settingsProxy.getUsername());
157             proxyInfo.setPassword(settingsProxy.getPassword());
158         }
159 
160         return proxyInfo;
161     }
162 
163 }