1+ /*
2+ * ******************************************************************************
3+ * Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+ * this file except in compliance with the License. A copy of the License is located at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * or in the "license" file accompanying this file.
10+ * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+ * specific language governing permissions and limitations under the License.
13+ * ****************************************************************************
14+ */
15+
16+ // This code is auto-generated, do not modify
117package com .spectralogic .ds3client .metadata ;
218
319
420import com .google .common .collect .ImmutableMap ;
521import com .spectralogic .ds3client .helpers .Ds3ClientHelpers ;
22+ import com .spectralogic .ds3client .metadata .interfaces .MetaDataStore ;
23+ import com .spectralogic .ds3client .metadata .interfaces .MetaDataStoreListner ;
624import com .spectralogic .ds3client .utils .MetaDataUtil ;
725import org .slf4j .Logger ;
826import org .slf4j .LoggerFactory ;
927
1028import java .io .IOException ;
1129import java .nio .file .Files ;
1230import java .nio .file .Path ;
13- import java .nio .file .attribute .*;
14- import java .util .*;
31+ import java .nio .file .attribute .BasicFileAttributes ;
32+ import java .nio .file .attribute .PosixFileAttributes ;
33+ import java .util .Map ;
34+ import java .util .Set ;
1535
1636/**
1737 * Implementation of MetaDataAcess Interface
2040public class MetaDataAccessImpl implements Ds3ClientHelpers .MetadataAccess {
2141 static private final Logger LOG = LoggerFactory .getLogger (MetaDataAccessImpl .class );
2242 private final Map <String , Path > fileMapper ;
43+ private final MetaDataStoreListner metaDataStoreListner ;
2344
24- public MetaDataAccessImpl (final Map <String , Path > fileMapper ) {
45+ public MetaDataAccessImpl (final Map <String , Path > fileMapper , final MetaDataStoreListner metaDataStoreListner ) {
2546 this .fileMapper = fileMapper ;
47+ this .metaDataStoreListner = metaDataStoreListner ;
2648 }
2749
2850 @ Override
@@ -32,134 +54,57 @@ public Map<String, String> getMetadataValue(final String filename) {
3254 return storeMetaData (file ).build ();
3355 } catch (final Exception e ) {
3456 LOG .error ("failed to store Metadata" , e );
57+ metaDataStoreListner .onMetaDataFailed ("Unable to get MetaData" +e .getMessage ());
3558 return null ;
3659 }
3760 }
3861
3962 /**
40- * @param file
41- * @return
63+ * @param file local path of file
64+ * @return map builder containing the data to be stored on server
4265 */
4366 private ImmutableMap .Builder <String , String > storeMetaData (final Path file ) {
4467 final ImmutableMap .Builder <String , String > metadata = new ImmutableMap .Builder <>();
4568 new ImmutableMap .Builder <String , String >();
46-
4769 try {
48- final MetaDataUtil metadataUtil = new MetaDataUtil (metadata );
49- final Set <String > sets = metadataUtil .getSupportedFileAttributes (file );
50- final String os = metadataUtil .getOS ();
51- metadataUtil .saveOSMetaData ();
52-
53- for (final String set : sets ) {
54- switch (set ) {
70+ //get local os name
71+ final String localOSName = MetaDataUtil .getOS ();
72+ final Set <String > setFileAttributes = MetaDataUtil .getSupportedFileAttributes (file );
73+ //get metadata store based on os type
74+ final MetaDataStore metaDataStore = new MetaDataStoreFactory ().getOsSpecificMetadataStore (localOSName , metadata );
75+ metaDataStore .saveOSMetaData (localOSName );
76+ for (final String fileAttributeType : setFileAttributes ) {
77+ switch (fileAttributeType ) {
5578 case "basic" :
5679 final BasicFileAttributes attr = Files .readAttributes (file , BasicFileAttributes .class );
57- metadataUtil .saveCreationTimeMetaData (attr );
58- metadataUtil .saveAccessTimeMetaData (attr );
59- metadataUtil .saveLastModifiedTime (attr );
60-
61- if (os .contains ("Windows" )) {
62- saveWindowsfilePermissions (file , metadata );
63- }
64-
65- break ;
80+ metaDataStore .saveCreationTimeMetaData (attr );
81+ metaDataStore .saveAccessTimeMetaData (attr );
82+ metaDataStore .saveLastModifiedTime (attr );
83+ if (metaDataStore instanceof WindowsMetaDataStore ) {
84+ ((WindowsMetaDataStore ) metaDataStore ).saveWindowsfilePermissions (file );
85+ ((WindowsMetaDataStore ) metaDataStore ).saveWindowsDescriptors (file );
86+ ((WindowsMetaDataStore ) metaDataStore ).saveFlagMetaData (file );
6687
67- case "owner" :
68- if (os .contains ("Windows" )) {
69- metadataUtil .saveWindowsDescriptors (file );
7088 }
71-
72- break ;
73-
74- case "user" :
75- break ;
76-
77- case "dos" :
78- if (os .contains ("Windows" )) {
79- metadataUtil .saveFlagMetaData (file );
80- }
81-
8289 break ;
83-
8490 case "posix" :
8591 final PosixFileAttributes attrPosix = Files .readAttributes (file , PosixFileAttributes .class );
86- metadataUtil .saveUserId (file );
87- metadataUtil .saveGroupId (file );
88- metadataUtil .saveModeMetaData (file );
89- metadataUtil .saveOwnerNameMetaData (attrPosix );
90- metadataUtil .saveGroupNameMetaData (attrPosix );
91- metadataUtil .savePosixPermssionsMeta (attrPosix );
92-
92+ if (metaDataStore instanceof PosixMetaDataStore ) {
93+ ((PosixMetaDataStore ) metaDataStore ).saveUserId (file );
94+ ((PosixMetaDataStore ) metaDataStore ).saveGroupId (file );
95+ ((PosixMetaDataStore ) metaDataStore ).saveModeMetaData (file );
96+ ((PosixMetaDataStore ) metaDataStore ).saveOwnerNameMetaData (attrPosix );
97+ ((PosixMetaDataStore ) metaDataStore ).saveGroupNameMetaData (attrPosix );
98+ ((PosixMetaDataStore ) metaDataStore ).savePosixPermssionsMeta (attrPosix );
99+ }
93100 break ;
94101 }
95102 }
96103 } catch (final IOException ioe ) {
97104 LOG .error ("unable to get metadata" , ioe );
105+ metaDataStoreListner .onMetaDataFailed ("Unable to get MetaData" +ioe .getMessage ());
98106 }
99107 return metadata ;
100108 }
101109
102-
103- /**
104- * if os is windows then posix will not be called and we need to find permission in different manner
105- *
106- * @param file
107- * @param metadata
108- */
109- private void saveWindowsfilePermissions (final Path file , final ImmutableMap .Builder <String , String > metadata ) {
110- try {
111- final AclFileAttributeView view = Files .getFileAttributeView (file , AclFileAttributeView .class );
112- final List <AclEntry > aclEntries = view .getAcl ();
113- Set <AclEntryPermission > aclEntryPermissions ;
114- String userType = "" ;
115- String userDisplay = "" ;
116- StringBuilder permission = null ;
117- final StringBuilder userList = new StringBuilder ();
118- final StringBuilder userDisplayList = new StringBuilder ();
119- final Map <String , Set <Integer >> stringSetMap = new HashMap <String , Set <Integer >>();
120- for (final AclEntry aclEntry : aclEntries ) {
121- userDisplay = aclEntry .principal ().getName ().split ("\\ \\ " )[1 ];
122- permission = new StringBuilder ();
123- Set <Integer > newSet = stringSetMap .get (userDisplay );
124- aclEntryPermissions = aclEntry .permissions ();
125- if (newSet == null ) {
126- newSet = new HashSet <Integer >();
127- }
128- for (final AclEntryPermission aclEntryPermission : aclEntryPermissions ) {
129- newSet .add (aclEntryPermission .ordinal ());
130- }
131- stringSetMap .put (userDisplay , newSet );
132- }
133- final Set <String > keys = stringSetMap .keySet ();
134- Set <Integer > ordinals ;
135- int userCount = 1 ;
136- for (final String key : keys ) {
137- int index = 1 ;
138- ordinals = stringSetMap .get (key );
139- userType = key .replaceAll (" " , "" ).toLowerCase ();
140- permission = new StringBuilder ();
141- for (final int ord : ordinals ) {
142- if (ordinals .size () == index ) {
143- permission .append (ord );
144- } else {
145- permission .append (ord + "-" );
146- }
147- index ++;
148- }
149- if (keys .size () == userCount ) {
150- userDisplayList .append (key );
151- userList .append (userType );
152- } else {
153- userDisplayList .append (key + "-" );
154- userList .append (userType + "-" );
155- }
156- metadata .put ("x-amz-meta-ds3-" + userType , permission .toString ());
157- userCount ++;
158- }
159- metadata .put ("x-amz-meta-ds3-userList" , userList .toString ());
160- metadata .put ("x-amz-meta-ds3-userListDisplay" , userDisplayList .toString ());
161- } catch (final Exception e ) {
162- LOG .error ("Unable to get list of users or their permissions" , e );
163- }
164- }
165110}
0 commit comments