001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import java.io.File;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.io.FileUtils;
023import org.apache.commons.lang3.StringUtils;
024import org.kuali.common.util.nullify.NullUtils;
025
026/**
027 * @deprecated
028 */
029@Deprecated
030public class RepositoryUtils {
031
032        private static final String FS = File.separator;
033        private static final String DEFAULT_MAVEN_REPO_PATH = ".m2" + FS + "repository";
034        private static final String GAV_DELIMITER = ":";
035
036        public static final void copyArtifact(String repository, Artifact artifact) {
037                File file = getFile(artifact);
038                copyArtifactToFile(repository, artifact, file);
039        }
040
041        public static final void copyArtifactToDirectory(String repository, Artifact artifact, File directory) {
042                String filename = getFilename(artifact);
043                File file = new File(directory, filename);
044                copyArtifactToFile(repository, artifact, file);
045        }
046
047        public static final void copyArtifactToFile(String repository, Artifact artifact, File file) {
048                String location = repository + getRepositoryPath(artifact);
049                LocationUtils.copyLocationToFile(location, file);
050        }
051
052        /**
053         * <p>
054         * Order is <code>groupId:artifactId:version:classifier:type</code>. The ordering here matches the order Maven uses to create actual files. Which is different from what the
055         * toString() method on Maven's Artifact object produces.
056         * </p>
057         * 
058         * <p>
059         * Trailing <code>:</code>'s are omitted.
060         * </p>
061         * 
062         * <p>
063         * If every field is left blank, <code>::::</code> is returned.
064         * </p>
065         * 
066         * <pre>
067         *   org.kuali.common:kuali-jdbc:1.0.0:webapp:jar  - groupId + artifactId + version + classifier + type
068         *   org.kuali.common:kuali-jdbc:1.0.0::jar        - no classifier
069         *   ::::                                          - Every field is blank
070         *   org.kuali.common                              - groupId only
071         *   ::::jar                                       - type only
072         *   :kuali-jdbc:::jar                             - no groupId, version, classifier, or type 
073         *   org.kuali.common:kuali-jdbc                   - groupId + artifactId
074         *   org.kuali.common:kuali-jdbc:1.0.0             - groupId + artifactId + version 
075         *   org.kuali.common:kuali-jdbc:1.0.0:webapp      - no type
076         *   org.kuali.common:kuali-jdbc:1.0.0             - no classifier or type
077         *   org.kuali.common:kuali-jdbc::webapp:jar       - no version
078         * </pre>
079         */
080        public static final String toString(Artifact artifact) {
081                List<String> tokens = new ArrayList<String>();
082                tokens.add(toEmpty(artifact.getGroupId()));
083                tokens.add(toEmpty(artifact.getArtifactId()));
084                tokens.add(toEmpty(artifact.getVersion()));
085                tokens.add(toEmpty(artifact.getClassifier()));
086                tokens.add(toEmpty(artifact.getType()));
087                int delimiterCount = getDelimiterCount(tokens);
088                return getDelimitedString(tokens, delimiterCount, GAV_DELIMITER);
089        }
090
091        /**
092         * <p>
093         * Order is <code>groupId:artifactId:version:classifier:type:scope</code>. The ordering here matches the order Maven uses to create actual files. As opposed to what the
094         * toString() method on Maven's Dependency object produces.
095         * </p>
096         * 
097         * <p>
098         * Trailing <code>:</code>'s are omitted.
099         * </p>
100         * 
101         * <p>
102         * If every field is left blank, <code>:::::</code> is returned.
103         * </p>
104         * 
105         * <pre>
106         *   org.kuali.common:kuali-jdbc:1.0.0:webapp:jar:compile - groupId + artifactId + version + classifier + type + scope
107         *   org.kuali.common:kuali-jdbc:1.0.0::jar:compile       - no classifier
108         *   org.kuali.common:kuali-jdbc:1.0.0:webapp:jar:        - no scope
109         *   :::::                                                - Every field is blank
110         *   org.kuali.common                                     - groupId only
111         *   :::::compile                                         - scope only
112         *   :kuali-jdbc:::jar                                    - artifactId + type 
113         *   org.kuali.common:kuali-jdbc                          - groupId + artifactId
114         *   org.kuali.common:kuali-jdbc:1.0.0                    - groupId + artifactId + version 
115         *   org.kuali.common:kuali-jdbc:1.0.0:webapp             - groupId + artifactId + version + classifier
116         *   org.kuali.common:kuali-jdbc:1.0.0:::compile          - no classifier or type
117         *   org.kuali.common:kuali-jdbc::webapp:jar:compile      - no version
118         * </pre>
119         */
120        public static final String toString(Dependency dependency) {
121                List<String> tokens = new ArrayList<String>();
122                tokens.add(toEmpty(dependency.getGroupId()));
123                tokens.add(toEmpty(dependency.getArtifactId()));
124                tokens.add(toEmpty(dependency.getVersion()));
125                tokens.add(toEmpty(dependency.getClassifier()));
126                tokens.add(toEmpty(dependency.getType()));
127                tokens.add(toEmpty(dependency.getScope()));
128                int delimiterCount = getDelimiterCount(tokens);
129                return getDelimitedString(tokens, delimiterCount, GAV_DELIMITER);
130        }
131
132        /**
133         * <p>
134         * Order is <code>groupId:artifactId:version:classifier:type:scope</code>.
135         * </p>
136         */
137        public static final Artifact parseArtifact(String gav) {
138                Assert.hasText(gav, "gav has no text");
139
140                String[] tokens = StringUtils.splitPreserveAllTokens(gav, GAV_DELIMITER);
141                int len = tokens.length;
142                for (int i = 0; i < len; i++) {
143                        tokens[i] = toNull(tokens[i]);
144                }
145
146                Artifact a = new Artifact();
147                if (len > 0) {
148                        a.setGroupId(tokens[0]);
149                }
150                if (len > 1) {
151                        a.setArtifactId(tokens[1]);
152                }
153                if (len > 2) {
154                        a.setVersion(tokens[2]);
155                }
156                if (len > 3) {
157                        a.setClassifier(tokens[3]);
158                }
159                if (len > 4) {
160                        a.setType(tokens[4]);
161                }
162                return a;
163        }
164
165        /**
166         * <p>
167         * Order is <code>groupId:artifactId:version:classifier:type:scope</code>.
168         * </p>
169         */
170        public static final Dependency parseDependency(String gav) {
171                Assert.hasText(gav, "gav has no text");
172
173                String[] tokens = StringUtils.splitPreserveAllTokens(gav, GAV_DELIMITER);
174                int len = tokens.length;
175                for (int i = 0; i < len; i++) {
176                        tokens[i] = toNull(tokens[i]);
177                }
178
179                Dependency d = new Dependency();
180                if (len > 0) {
181                        d.setGroupId(tokens[0]);
182                }
183                if (len > 1) {
184                        d.setArtifactId(tokens[1]);
185                }
186                if (len > 2) {
187                        d.setVersion(tokens[2]);
188                }
189                if (len > 3) {
190                        d.setClassifier(tokens[3]);
191                }
192                if (len > 4) {
193                        d.setType(tokens[4]);
194                }
195                if (len > 5) {
196                        d.setScope(tokens[5]);
197                }
198                return d;
199        }
200
201        protected static final String getDelimitedString(List<String> tokens, int delimiterCount, String delimiter) {
202                StringBuilder sb = new StringBuilder();
203                for (int i = 0; i < tokens.size(); i++) {
204                        if (i != 0 && i < delimiterCount) {
205                                sb.append(delimiter);
206                        }
207                        sb.append(tokens.get(i));
208                }
209                return sb.toString();
210        }
211
212        protected static final int getDelimiterCount(List<String> tokens) {
213                int count = 0;
214                for (int i = 0; i < tokens.size(); i++) {
215                        String token = toEmpty(tokens.get(i));
216                        if (!StringUtils.isEmpty(token)) {
217                                count = i + 1;
218                        }
219                }
220                return count == 0 ? tokens.size() : count;
221        }
222
223        /**
224         * Return null if token is blank, "NULL", or "NONE"
225         */
226        public static String toNull(String token) {
227                if (StringUtils.isBlank(token)) {
228                        return null;
229                }
230                if (NullUtils.isNullOrNone(token)) {
231                        return null;
232                }
233                return token;
234        }
235
236        /**
237         * Return the empty string if token is blank, "NULL", or "NONE"
238         */
239        public static String toEmpty(String token) {
240                if (StringUtils.isBlank(token)) {
241                        return "";
242                }
243                if (NullUtils.isNullOrNone(token)) {
244                        return "";
245                }
246                return token;
247        }
248
249        public static final String getRepositoryPath(Artifact artifact) {
250                StringBuilder sb = new StringBuilder();
251                sb.append(Str.getPath(artifact.getGroupId()));
252                sb.append(FS);
253                sb.append(artifact.getArtifactId());
254                sb.append(FS);
255                sb.append(artifact.getVersion());
256                return sb.toString();
257        }
258
259        /**
260         * Return true if classifier should become part of the filename
261         */
262        protected static boolean addClassifierToFilename(String classifier) {
263                return !StringUtils.isBlank(classifier) && !NullUtils.isNullOrNone(classifier);
264        }
265
266        public static final String getFilename(Artifact artifact) {
267                StringBuilder sb = new StringBuilder();
268                sb.append(artifact.getArtifactId());
269                sb.append("-");
270                sb.append(artifact.getVersion());
271                if (addClassifierToFilename(artifact.getClassifier())) {
272                        sb.append("-");
273                        sb.append(artifact.getClassifier());
274                }
275                sb.append(".");
276                sb.append(artifact.getType());
277                return sb.toString();
278        }
279
280        public static final File getDefaultLocalRepositoryDir() {
281                return new File(FileUtils.getUserDirectoryPath() + FS + DEFAULT_MAVEN_REPO_PATH);
282        }
283
284        public static final File getFile(Artifact artifact) {
285                return getFile(getDefaultLocalRepositoryDir(), artifact);
286        }
287
288        public static final File getFile(File localRepositoryDir, Artifact artifact) {
289                String path = getRepositoryPath(artifact);
290                String filename = getFilename(artifact);
291                return new File(localRepositoryDir.getAbsolutePath() + FS + path, filename);
292        }
293
294}