View Javadoc

1   /**
2    * Copyright 2010-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.kuali.common.util.spring;
17  
18  import java.io.File;
19  
20  import org.kuali.common.util.Artifact;
21  import org.kuali.common.util.Assert;
22  import org.kuali.common.util.LocationUtils;
23  import org.kuali.common.util.RepositoryUtils;
24  import org.springframework.beans.factory.FactoryBean;
25  
26  public class ArtifactPathFactoryBean extends Artifact implements FactoryBean<String> {
27  
28  	File localRepositoryDir = RepositoryUtils.getDefaultLocalRepositoryDir();
29  	boolean mustExist;
30  
31  	@Override
32  	public String getObject() throws Exception {
33  
34  		Assert.notNull(localRepositoryDir);
35  		Assert.hasText(getGroupId());
36  		Assert.hasText(getArtifactId());
37  		Assert.hasText(getVersion());
38  		Assert.hasText(getType());
39  
40  		File file = RepositoryUtils.getFile(localRepositoryDir, this);
41  		validate(file);
42  		return LocationUtils.getCanonicalPath(file);
43  	}
44  
45  	protected void validate(File file) {
46  		if (!file.exists() && mustExist) {
47  			throw new IllegalStateException(file + " does not exist");
48  		}
49  	}
50  
51  	@Override
52  	public Class<String> getObjectType() {
53  		return String.class;
54  	}
55  
56  	@Override
57  	public boolean isSingleton() {
58  		return false;
59  	}
60  
61  	public File getLocalRepositoryDir() {
62  		return localRepositoryDir;
63  	}
64  
65  	public void setLocalRepositoryDir(File localRepositoryDir) {
66  		this.localRepositoryDir = localRepositoryDir;
67  	}
68  
69  	public boolean isMustExist() {
70  		return mustExist;
71  	}
72  
73  	public void setMustExist(boolean mustExist) {
74  		this.mustExist = mustExist;
75  	}
76  
77  }