1
2
3
4 package org.treetank.access.conf;
5
6 import java.io.File;
7 import java.io.FileReader;
8 import java.io.IOException;
9 import java.nio.file.FileSystems;
10 import java.security.Key;
11 import java.util.Properties;
12
13 import javax.crypto.spec.SecretKeySpec;
14
15 import org.jclouds.Constants;
16 import org.jclouds.filesystem.reference.FilesystemConstants;
17 import org.treetank.exception.TTIOException;
18
19 import com.google.common.hash.HashFunction;
20 import com.google.common.hash.Hashing;
21 import com.google.common.io.Files;
22
23
24
25
26
27
28
29 public class StandardSettings {
30
31 private static byte[] keyValue = new byte[] {
32 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k', 'k'
33 };
34 public static final Key KEY = new SecretKeySpec(keyValue, "AES");
35 public static final HashFunction HASHFUNC = Hashing.sha512();
36
37 public static Properties getProps(final String pathToStorage, final String resource) throws TTIOException {
38 Properties properties = new Properties();
39 properties.setProperty(ConstructorProps.STORAGEPATH, pathToStorage);
40 properties.setProperty(ConstructorProps.RESOURCE, resource);
41 properties.setProperty(ConstructorProps.RESOURCEPATH, FileSystems.getDefault().getPath(pathToStorage,
42 StorageConfiguration.Paths.Data.getFile().getName(), resource).toString());
43 properties.setProperty(ConstructorProps.NUMBERTORESTORE, Integer.toString(4));
44
45
46 properties.setProperty(ConstructorProps.JCLOUDSTYPE, "filesystem");
47
48
49 properties.setProperty(FilesystemConstants.PROPERTY_BASEDIR,FileSystems.getDefault().getPath(pathToStorage,
50 StorageConfiguration.Paths.Data.getFile().getName(), resource, ResourceConfiguration.Paths.Data
51 .getFile().getName()).toString());
52
53 String[] awsCredentials = getCredentials();
54 if (awsCredentials.length == 0) {
55 properties.setProperty(Constants.PROPERTY_CREDENTIAL, "test");
56 } else {
57 properties.setProperty(Constants.PROPERTY_IDENTITY, awsCredentials[0]);
58 properties.setProperty(Constants.PROPERTY_CREDENTIAL, awsCredentials[1]);
59 }
60
61
62
63
64
65
66
67
68
69
70 return properties;
71
72 }
73
74 private static String[] getCredentials() {
75 File userStore =
76 new File(System.getProperty("user.home"), new StringBuilder(".credentials")
77 .append(File.separator).append("aws.properties").toString());
78 if (!userStore.exists()) {
79 return new String[0];
80 } else {
81 Properties props = new Properties();
82 try {
83 props.load(new FileReader(userStore));
84 return new String[] {
85 props.getProperty("access"), props.getProperty("secret")
86 };
87
88 } catch (IOException exc) {
89 throw new RuntimeException(exc);
90 }
91 }
92
93 }
94
95 }