View Javadoc

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   * Standard Module defining standard settings.
25   * 
26   * @author Sebastian Graf, University of Konstanz
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  //         properties.setProperty(ConstructorProps.JCLOUDSTYPE, "aws-s3");
46          properties.setProperty(ConstructorProps.JCLOUDSTYPE, "filesystem");
47          // Path not to main storage but to any to simulate remote cloud location.
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          // Class name for painter for imagehost
62          // properties.setProperty(ImageStoreConstants.PROPERTY_BYTEPAINTER,
63          // "org.jclouds.imagestore.imagegenerator.bytepainter.HexadecimalBytesToImagePainter");
64          // Class name for imagehost
65          // properties.setProperty(ImageStoreConstants.PROPERTY_IMAGEHOSTER,
66          // "org.jclouds.imagestore.imagehoster.file.ImageHostFile");
67          // properties.setProperty(ImageStoreConstants.PROPERTY_IMAGEHOSTER,
68          // "org.jclouds.imagestore.imagehoster.flickr.ImageHostFlickr");
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  }