View Javadoc

1   /*
2    * Copyright © 2012 Eirik Bjornset.
3    * 
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  
17  package net.grinderscript.dotnet.scriptengine;
18  
19  import grinderscript.net.core.IGrinderContext;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.net.URL;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import net.grinder.util.ClassLoaderUtilities;
28  
29  public abstract class DotNetUtils {
30  
31  	static final String J4N_PROPERTIES_RESOURCE_NAME = "META-INF/grinderscript-dotnet.j4n.properties";
32  	static final String PROPERTY_PREFIX = "grinderscript-dotnet.";
33  	static final String J4N_LOCATION_PROPERTY_SUFFIX = "j4nLocation";
34  	static final String SCRIPT_ASSEMBLY_PROPERTY_SUFFIX = "scriptAssembly";
35  	static final String J4N_ASSEMBLY_PROPERTY_SUFFIX = "j4nAssembly";
36  	static final String J4N_JAR_PROPERTY_SUFFIX = "j4nJar";
37  	static final String JNI4NET_ASSEMBLY_PROPERTY_SUFFIX = "jni4NetAssembly";
38  
39  	static Properties getPropertiesFromResources(String resourceName,
40  			ClassLoader classLoader) throws IOException {
41  		Properties properties = new Properties();
42  		List<String> propertyStrings = ClassLoaderUtilities.allResourceLines(
43  				classLoader, resourceName);
44  		for (String propertyString : propertyStrings) {
45  			String[] propertyContent = propertyString.split("=");
46  			if (propertyContent.length != 2) {
47  				throw new IllegalArgumentException(String.format(
48  						"Not a property spesification: \"%1$s\"", propertyString));
49  			}
50  			if (!properties.containsKey(propertyContent[0].trim())) {
51  				properties.put(propertyContent[0].trim(),
52  						propertyContent[1].trim());
53  			}
54  		}
55  		return properties;
56  	}
57  
58  	static Properties getPropertiesFromResources(String resourceName)
59  			throws IOException {
60  		return getPropertiesFromResources(resourceName,
61  				DotNetUtils.class.getClassLoader());
62  	}
63  
64  	static Properties getPropertiesFromResources() throws IOException {
65  		return getPropertiesFromResources(J4N_PROPERTIES_RESOURCE_NAME);
66  	}
67  
68  	static String getJ4nAssemblyLocationFromProperties(Properties properties)
69  			throws IOException {
70  		return getCanonicalLocationFilePath(DotNetUtils.getProperty(properties,
71  				J4N_LOCATION_PROPERTY_SUFFIX), DotNetUtils.getProperty(
72  				properties, J4N_ASSEMBLY_PROPERTY_SUFFIX));
73  	}
74  
75  	static String getScriptAssemblyLocationFromProperties(Properties properties)
76  			throws IOException {
77  		return getCanonicalLocationFilePath(DotNetUtils.getProperty(properties,
78  				J4N_LOCATION_PROPERTY_SUFFIX), DotNetUtils.getProperty(
79  				properties, SCRIPT_ASSEMBLY_PROPERTY_SUFFIX));
80  	}
81  
82  	static String getJni4NetAssemblyLocationFromProperties(Properties properties)
83  			throws IOException {
84  		return getCanonicalLocationFilePath(DotNetUtils.getProperty(properties,
85  				J4N_LOCATION_PROPERTY_SUFFIX), DotNetUtils.getProperty(
86  				properties, JNI4NET_ASSEMBLY_PROPERTY_SUFFIX));
87  	}
88  
89  	static String getCanonicalLocationFilePath(String location,
90  			String file) throws IOException {
91  		return new File(location, file).getCanonicalPath();
92  	}
93  
94  	static String getDirectoryFromJ4nCodeLocation() throws IOException {
95  		URL codeLocationUrl = IGrinderContext.class.getProtectionDomain()
96  				.getCodeSource().getLocation();
97  		String codeLocation = codeLocationUrl.getFile();
98  		File file = new File(codeLocation);
99  		while (!file.isDirectory()) {
100 			file = file.getParentFile();
101 		}
102 		codeLocation = file.getCanonicalPath();
103 		return codeLocation;
104 	}
105 
106 	static String getFileNameFromJ4nCodeLocation(String baseName)
107 			throws DotNetScriptExecutionException, IOException {
108 		String codeLocation = getDirectoryFromJ4nCodeLocation();
109 		File file = new File(codeLocation, baseName);
110 		if (file.exists()) {
111 			return file.getCanonicalPath();
112 		}
113 		throw new DotNetScriptExecutionException(String.format(
114 				"Could not find file \"%1$s\" in code loction \"%2$s\"", baseName,
115 				codeLocation));
116 	}
117 
118 	static String getProperty(Properties properties, String suffix) {
119 		return properties.getProperty(getPropertyName(suffix));
120 	}
121 
122 	static boolean containsPropertyKey(Properties properties, String suffix) {
123 		return properties.containsKey(getPropertyName(suffix));
124 	}
125 
126 	static String getPropertyName(String suffix) {
127 		return PROPERTY_PREFIX + suffix;
128 	}
129 }