View Javadoc
1   /**
2    * Copyright 2010-2014 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.kuali.common.util.project.model;
17  
18  import static com.google.common.base.Preconditions.checkArgument;
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import java.io.File;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.kuali.common.util.file.CanonicalFile;
25  import org.kuali.common.util.metainf.service.MetaInfUtils;
26  import org.springframework.util.ResourceUtils;
27  
28  public final class ProjectResource {
29  
30  	private final String prefix;
31  	private final ProjectIdentifier project;
32  	private final String path;
33  
34  	private ProjectResource(Builder builder) {
35  		this.project = builder.project;
36  		this.prefix = builder.prefix;
37  		this.path = builder.path;
38  	}
39  
40  	/**
41  	 * Create a {@code ProjectResource} with the prefix set to {@code classpath:}
42  	 */
43  	public static ProjectResource create(ProjectIdentifier project, String path) {
44  		return builder(project, path).build();
45  	}
46  
47  	/**
48  	 * Create a {@code ProjectResource} with the corresponding prefix
49  	 */
50  	public static ProjectResource create(String prefix, ProjectIdentifier project, String path) {
51  		return builder(project, path).prefix(prefix).build();
52  	}
53  
54  	/**
55  	 * Create a {@code ProjectResource} with the prefix set to {@code classpath:}
56  	 */
57  	public static ProjectResource classpath(ProjectIdentifier project, String path) {
58  		return classpath(project, path, false);
59  	}
60  
61  	/**
62  	 * Create a {@code ProjectResource} with the prefix set to {@code classpath:} or {@code classpath:META-INF/}
63  	 */
64  	public static ProjectResource classpath(ProjectIdentifier project, String path, boolean metainf) {
65  		return builder(project, path).classpathPrefix(metainf).build();
66  	}
67  
68  	/**
69  	 * Create a {@code ProjectResource} with the prefix set to {@code directory} and optionally further prefixed with {@code META-INF}
70  	 */
71  	public static ProjectResource directory(File directory, ProjectIdentifier project, String path, boolean metainf) {
72  		return builder(project, path).directoryPrefix(directory, metainf).build();
73  	}
74  
75  	/**
76  	 * Create a {@code ProjectResource} with the prefix set to {@code directory}
77  	 */
78  	public static ProjectResource directory(File directory, ProjectIdentifier project, String path) {
79  		return directory(directory, project, path, false);
80  	}
81  
82  	public static Builder builder(ProjectIdentifier project, String path) {
83  		return new Builder(project, path);
84  	}
85  
86  	public static class Builder {
87  
88  		// Required
89  		private final ProjectIdentifier project;
90  		private final String path;
91  
92  		// Optional
93  		private String prefix = ResourceUtils.CLASSPATH_URL_PREFIX;
94  
95  		public Builder(ProjectIdentifier project, String path) {
96  			this.project = project;
97  			this.path = path;
98  		}
99  
100 		/**
101 		 * {@code classpath:}
102 		 */
103 		public Builder classpathPrefix() {
104 			return prefix(ResourceUtils.CLASSPATH_URL_PREFIX);
105 		}
106 
107 		/**
108 		 * {@code classpath:} or {@code classpath:META-INF/}
109 		 */
110 		public Builder classpathPrefix(boolean metainf) {
111 			if (metainf) {
112 				return prefix(ResourceUtils.CLASSPATH_URL_PREFIX + MetaInfUtils.METAINF_DIRECTORY_NAME + "/");
113 			} else {
114 				return classpathPrefix();
115 			}
116 		}
117 
118 		/**
119 		 * {@code /tmp/}
120 		 */
121 		public Builder directoryPrefix(File directory) {
122 			return directoryPrefix(directory, false);
123 		}
124 
125 		/**
126 		 * {@code /tmp/} or {@code /tmp/META-INF/}
127 		 */
128 		public Builder directoryPrefix(File directory, boolean metainf) {
129 			String path = new CanonicalFile(directory).getPath() + File.pathSeparator;
130 			if (metainf) {
131 				return prefix(path + MetaInfUtils.METAINF_DIRECTORY_NAME + File.pathSeparator);
132 			} else {
133 				return prefix(path);
134 			}
135 		}
136 
137 		/**
138 		 * Typically {@code classpath:}
139 		 */
140 		public Builder prefix(String prefix) {
141 			this.prefix = prefix;
142 			return this;
143 		}
144 
145 		public ProjectResource build() {
146 			ProjectResource instance = new ProjectResource(this);
147 			validate(instance);
148 			return instance;
149 		}
150 
151 		private static void validate(ProjectResource instance) {
152 			checkNotNull(instance.project, "'project' cannot be null");
153 			checkArgument(!StringUtils.isBlank(instance.path), "'path' cannot be blank");
154 			checkArgument(!StringUtils.isBlank(instance.prefix), "'prefix' cannot be blank");
155 		}
156 	}
157 
158 	public ProjectIdentifier getProject() {
159 		return project;
160 	}
161 
162 	public String getPrefix() {
163 		return prefix;
164 	}
165 
166 	public String getPath() {
167 		return path;
168 	}
169 
170 }