1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43 public static ProjectResource create(ProjectIdentifier project, String path) {
44 return builder(project, path).build();
45 }
46
47
48
49
50 public static ProjectResource create(String prefix, ProjectIdentifier project, String path) {
51 return builder(project, path).prefix(prefix).build();
52 }
53
54
55
56
57 public static ProjectResource classpath(ProjectIdentifier project, String path) {
58 return classpath(project, path, false);
59 }
60
61
62
63
64 public static ProjectResource classpath(ProjectIdentifier project, String path, boolean metainf) {
65 return builder(project, path).classpathPrefix(metainf).build();
66 }
67
68
69
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
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
89 private final ProjectIdentifier project;
90 private final String path;
91
92
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
102
103 public Builder classpathPrefix() {
104 return prefix(ResourceUtils.CLASSPATH_URL_PREFIX);
105 }
106
107
108
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
120
121 public Builder directoryPrefix(File directory) {
122 return directoryPrefix(directory, false);
123 }
124
125
126
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
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 }