/
PurRepository.java
2868 lines (2589 loc) · 120 KB
/
PurRepository.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright 2010 - 2015 Pentaho Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.pentaho.di.repository.pur;
import org.apache.commons.lang.StringUtils;
import org.pentaho.di.cluster.ClusterSchema;
import org.pentaho.di.cluster.SlaveServer;
import org.pentaho.di.core.Condition;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.ProgressMonitorListener;
import org.pentaho.di.core.annotations.RepositoryPlugin;
import org.pentaho.di.core.changed.ChangedFlagInterface;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.IdNotFoundException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleFileException;
import org.pentaho.di.core.exception.KettleSecurityException;
import org.pentaho.di.core.extension.ExtensionPointHandler;
import org.pentaho.di.core.extension.KettleExtensionPoint;
import org.pentaho.di.core.logging.LogChannel;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.partition.PartitionSchema;
import org.pentaho.di.repository.AbstractRepository;
import org.pentaho.di.repository.IRepositoryExporter;
import org.pentaho.di.repository.IRepositoryImporter;
import org.pentaho.di.repository.IRepositoryService;
import org.pentaho.di.repository.IUser;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.ObjectRevision;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectory;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.RepositoryElementInterface;
import org.pentaho.di.repository.RepositoryElementMetaInterface;
import org.pentaho.di.repository.RepositoryMeta;
import org.pentaho.di.repository.RepositoryObject;
import org.pentaho.di.repository.RepositoryObjectType;
import org.pentaho.di.repository.RepositorySecurityManager;
import org.pentaho.di.repository.RepositorySecurityProvider;
import org.pentaho.di.repository.StringObjectId;
import org.pentaho.di.repository.pur.metastore.PurRepositoryMetaStore;
import org.pentaho.di.repository.pur.model.EEJobMeta;
import org.pentaho.di.repository.pur.model.EERepositoryObject;
import org.pentaho.di.repository.pur.model.EETransMeta;
import org.pentaho.di.repository.pur.model.EEUserInfo;
import org.pentaho.di.repository.pur.model.RepositoryLock;
import org.pentaho.di.shared.SharedObjectInterface;
import org.pentaho.di.shared.SharedObjects;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.repository.pur.services.IAbsSecurityProvider;
import org.pentaho.di.ui.repository.pur.services.IAclService;
import org.pentaho.di.ui.repository.pur.services.ILockService;
import org.pentaho.di.ui.repository.pur.services.IRevisionService;
import org.pentaho.metastore.api.IMetaStore;
import org.pentaho.metastore.api.exceptions.MetaStoreException;
import org.pentaho.metastore.api.exceptions.MetaStoreNamespaceExistsException;
import org.pentaho.metastore.util.PentahoDefaults;
import org.pentaho.platform.api.repository2.unified.IUnifiedRepository;
import org.pentaho.platform.api.repository2.unified.RepositoryFile;
import org.pentaho.platform.api.repository2.unified.RepositoryFileAcl;
import org.pentaho.platform.api.repository2.unified.RepositoryFileTree;
import org.pentaho.platform.api.repository2.unified.VersionSummary;
import org.pentaho.platform.api.repository2.unified.data.node.DataNode;
import org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData;
import org.pentaho.platform.repository.RepositoryFilenameUtils;
import org.pentaho.platform.repository2.ClientRepositoryPaths;
import java.io.Serializable;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Implementation of {@link Repository} that delegates to the Pentaho unified repository (PUR), an instance of {@link
* IUnifiedRepository}.
*
* @author Matt
* @author mlowery
*/
@RepositoryPlugin( id = "PentahoEnterpriseRepository", name = "DI Repository",
description = "i18n:org.pentaho.di.ui.repository.pur:RepositoryType.Description.EnterpriseRepository",
metaClass = "org.pentaho.di.repository.pur.PurRepositoryMeta" )
public class PurRepository extends AbstractRepository implements Repository, java.io.Serializable {
private static final long serialVersionUID = 7460109109707189479L; /* EESOURCE: UPDATE SERIALVERUID */
private static Class<?> PKG = PurRepository.class;
// ~ Static fields/initializers ======================================================================================
// private static final Log logger = LogFactory.getLog(PurRepository.class);
private static final String REPOSITORY_VERSION = "1.0"; //$NON-NLS-1$
private static final boolean VERSION_SHARED_OBJECTS = true;
private static final String FOLDER_PDI = "pdi"; //$NON-NLS-1$
private static final String FOLDER_PARTITION_SCHEMAS = "partitionSchemas"; //$NON-NLS-1$
private static final String FOLDER_CLUSTER_SCHEMAS = "clusterSchemas"; //$NON-NLS-1$
private static final String FOLDER_SLAVE_SERVERS = "slaveServers"; //$NON-NLS-1$
private static final String FOLDER_DATABASES = "databases"; //$NON-NLS-1$
// ~ Instance fields =================================================================================================
/**
* Indicates that this code should be run in unit test mode (where PUR is passed in instead of created inside this
* class).
*/
private boolean test = false;
private IUnifiedRepository pur;
private IUser user;
private PurRepositoryMeta repositoryMeta;
private DatabaseDelegate databaseMetaTransformer = new DatabaseDelegate( this );
private PartitionDelegate partitionSchemaTransformer = new PartitionDelegate( this );
private SlaveDelegate slaveTransformer = new SlaveDelegate( this );
private ClusterDelegate clusterTransformer = new ClusterDelegate( this );
private ISharedObjectsTransformer transDelegate;
private ISharedObjectsTransformer jobDelegate;
private Map<RepositoryObjectType, SharedObjectAssembler<?>> sharedObjectAssemblerMap;
private RepositorySecurityManager securityManager;
private RepositorySecurityProvider securityProvider;
protected LogChannelInterface log;
protected Serializable cachedSlaveServerParentFolderId;
protected Serializable cachedPartitionSchemaParentFolderId;
protected Serializable cachedClusterSchemaParentFolderId;
protected Serializable cachedDatabaseMetaParentFolderId;
private final RootRef rootRef = new RootRef();
private UnifiedRepositoryLockService unifiedRepositoryLockService;
private Map<RepositoryObjectType, List<? extends SharedObjectInterface>> sharedObjectsByType = null;
private boolean connected = false;
private String connectMessage = null;
protected PurRepositoryMetaStore metaStore;
// The servers (DI Server, BA Server) that a user can authenticate to
protected enum RepositoryServers {
DIS, POBS
}
private IRepositoryConnector purRepositoryConnector;
private RepositoryServiceRegistry purRepositoryServiceRegistry = new RepositoryServiceRegistry();
// ~ Constructors ====================================================================================================
public PurRepository() {
super();
initSharedObjectAssemblerMap();
}
// ~ Methods =========================================================================================================
protected RepositoryDirectoryInterface getRootDir() throws KettleException {
RepositoryDirectoryInterface ref = rootRef.getRef();
return ref == null ? loadRepositoryDirectoryTree() : ref;
}
/**
* public for unit tests.
*/
public void setTest( final IUnifiedRepository pur ) {
this.pur = pur;
// set this to avoid NPE in connect()
this.repositoryMeta.setRepositoryLocation( new PurRepositoryLocation( "doesnotmatch" ) );
this.test = true;
}
private boolean isTest() {
return test;
}
@Override
public void init( final RepositoryMeta repositoryMeta ) {
this.log = new LogChannel( this );
this.repositoryMeta = (PurRepositoryMeta) repositoryMeta;
purRepositoryConnector = new PurRepositoryConnector( this, this.repositoryMeta, rootRef );
}
public void setPurRepositoryConnector( IRepositoryConnector purRepositoryConnector ) {
this.purRepositoryConnector = purRepositoryConnector;
}
public RootRef getRootRef() {
return rootRef;
}
@Override public void connect( final String username, final String password ) throws KettleException {
if ( isTest() ) {
connected = true;
purRepositoryServiceRegistry
.registerService( IRevisionService.class, new UnifiedRepositoryRevisionService( pur, getRootRef() ) );
purRepositoryServiceRegistry.registerService( ILockService.class, new UnifiedRepositoryLockService( pur ) );
purRepositoryServiceRegistry
.registerService( IAclService.class, new UnifiedRepositoryConnectionAclService( pur ) );
metaStore = new PurRepositoryMetaStore( this );
try {
metaStore.createNamespace( PentahoDefaults.NAMESPACE );
} catch ( MetaStoreException e ) {
LogChannel.GENERAL.logError( BaseMessages
.getString( PKG, "PurRepositoryMetastore.NamespaceCreateException.Message", PentahoDefaults.NAMESPACE ),
e );
}
this.user = new EEUserInfo( username, password, username, "test user", true );
this.jobDelegate = new JobDelegate( this, pur );
this.transDelegate = new TransDelegate( this, pur );
this.unifiedRepositoryLockService = new UnifiedRepositoryLockService( pur );
return;
}
try {
RepositoryConnectResult result = purRepositoryConnector.connect( username, password );
this.user = result.getUser();
this.connected = result.isSuccess();
this.securityProvider = result.getSecurityProvider();
this.securityManager = result.getSecurityManager();
IUnifiedRepository r = result.getUnifiedRepository();
try {
this.pur =
(IUnifiedRepository) Proxy
.newProxyInstance( r.getClass().getClassLoader(), new Class<?>[] { IUnifiedRepository.class },
new UnifiedRepositoryInvocationHandler<IUnifiedRepository>( r ) );
} catch ( Throwable th ) {
if ( log.isError() ) {
log.logError( "Failed to setup repository connection", th );
}
connected = false;
}
this.unifiedRepositoryLockService = new UnifiedRepositoryLockService( pur );
this.connectMessage = result.getConnectMessage();
this.purRepositoryServiceRegistry = result.repositoryServiceRegistry();
this.transDelegate = new TransDelegate( this, pur );
this.jobDelegate = new JobDelegate( this, pur );
} finally {
if ( connected ) {
LogChannel.GENERAL.logBasic( BaseMessages.getString( PKG, "PurRepositoryMetastore.Create.Message" ) );
metaStore = new PurRepositoryMetaStore( this );
// Create the default Pentaho namespace if it does not exist
try {
metaStore.createNamespace( PentahoDefaults.NAMESPACE );
LogChannel.GENERAL.logBasic( BaseMessages
.getString( PKG, "PurRepositoryMetastore.NamespaceCreateSuccess.Message", PentahoDefaults.NAMESPACE ) );
} catch ( MetaStoreNamespaceExistsException e ) {
// Ignore this exception, we only use it to save a call to check if the namespace exists, as the
// createNamespace()
// call will do the check for us and throw this exception.
} catch ( MetaStoreException e ) {
LogChannel.GENERAL.logError( BaseMessages
.getString( PKG, "PurRepositoryMetastore.NamespaceCreateException.Message", PentahoDefaults.NAMESPACE ),
e );
}
LogChannel.GENERAL.logBasic( BaseMessages.getString( PKG, "PurRepository.ConnectSuccess.Message" ) );
}
}
}
@Override public boolean isConnected() {
return connected;
}
@Override public void disconnect() {
connected = false;
metaStore = null;
purRepositoryConnector.disconnect();
}
@Override public int countNrJobEntryAttributes( ObjectId idJobentry, String code ) throws KettleException {
// implemented by RepositoryProxy
throw new UnsupportedOperationException();
}
@Override public int countNrStepAttributes( ObjectId idStep, String code ) throws KettleException {
// implemented by RepositoryProxy
throw new UnsupportedOperationException();
}
@Override
public RepositoryDirectoryInterface createRepositoryDirectory( final RepositoryDirectoryInterface parentDirectory,
final String directoryPath ) throws KettleException {
try {
RepositoryDirectoryInterface refreshedParentDir = findDirectory( parentDirectory.getPath() );
// update the passed in repository directory with the children recently loaded from the repo
parentDirectory.setChildren( refreshedParentDir.getChildren() );
String[] path = Const.splitPath( directoryPath, RepositoryDirectory.DIRECTORY_SEPARATOR );
RepositoryDirectoryInterface follow = refreshedParentDir;
for ( int level = 0; level < path.length; level++ ) {
RepositoryDirectoryInterface child = follow.findChild( path[level] );
if ( child == null ) {
// create this one
child = new RepositoryDirectory( follow, path[level] );
saveRepositoryDirectory( child );
// link this with the parent directory
follow.addSubdirectory( child );
}
follow = child;
}
return follow;
} catch ( Exception e ) {
throw new KettleException( "Unable to create directory with path [" + directoryPath + "]", e );
}
}
@Override public void saveRepositoryDirectory( final RepositoryDirectoryInterface dir ) throws KettleException {
try {
// id of root dir is null--check for it
if ( "/".equals( dir.getParent().getName() ) ) {
throw new KettleException( BaseMessages.getString( PKG, "PurRepository.FailedDirectoryCreation.Message" ) );
}
RepositoryFile
newFolder =
pur.createFolder( dir.getParent().getObjectId() != null ? dir.getParent().getObjectId().getId() : null,
new RepositoryFile.Builder( dir.getName() ).folder( true ).build(), null );
dir.setObjectId( new StringObjectId( newFolder.getId().toString() ) );
} catch ( Exception e ) {
throw new KettleException( "Unable to save repository directory with path [" + getPath( null, dir, null ) + "]",
e );
}
}
/**
* Determine if "baseFolder" is the same as "folder" or if "folder" is a descendant of "baseFolder"
*
* @param folder Folder to test for similarity / ancestory; Must not be null
* @param baseFolder Folder that may be the same or an ancestor; Must not be null
* @return True if folder is a descendant of baseFolder or False if not; False if either folder or baseFolder are null
*/
protected boolean isSameOrAncestorFolder( RepositoryFile folder, RepositoryFile baseFolder ) {
// If either folder is null, return false. We cannot do a proper comparison
if ( folder != null && baseFolder != null ) {
if (
// If the folders are equal
baseFolder.getId().equals( folder.getId() ) || (
// OR if the folders are NOT siblings AND the folder to move IS an ancestor to the users home folder
baseFolder.getPath().lastIndexOf( RepositoryDirectory.DIRECTORY_SEPARATOR )
!= folder.getPath().lastIndexOf( RepositoryDirectory.DIRECTORY_SEPARATOR )
&& baseFolder.getPath().startsWith( folder.getPath() ) ) ) {
return true;
}
}
return false;
}
/**
* Test to see if the folder is a user's home directory If it is an ancestor to a user's home directory, false will be
* returned. (It is not actually a user's home directory)
*
* @param folder The folder to test; Must not be null
* @return True if the directory is a users home directory and False if it is not; False if folder is null
*/
protected boolean isUserHomeDirectory( RepositoryFile folder ) {
if ( folder != null ) {
// Get the root of all home folders
RepositoryFile homeRootFolder = pur.getFile( ClientRepositoryPaths.getHomeFolderPath() );
if ( homeRootFolder != null ) {
// Strip the final RepositoryDirectory.DIRECTORY_SEPARATOR from the paths
String temp = homeRootFolder.getPath();
String
homeRootPath =
temp.endsWith( RepositoryDirectory.DIRECTORY_SEPARATOR )
&& temp.length() > RepositoryDirectory.DIRECTORY_SEPARATOR.length()
? temp.substring( 0, temp.length() - RepositoryDirectory.DIRECTORY_SEPARATOR.length() ) : temp;
temp = folder.getPath();
String
folderPath =
temp.endsWith( RepositoryDirectory.DIRECTORY_SEPARATOR )
&& temp.length() > RepositoryDirectory.DIRECTORY_SEPARATOR.length()
? temp.substring( 0, temp.length() - RepositoryDirectory.DIRECTORY_SEPARATOR.length() ) : temp;
// Is the folder in a user's home directory?
if ( folderPath.startsWith( homeRootPath ) ) {
if ( folderPath.equals( homeRootPath ) ) {
return false;
}
// If there is exactly one more RepositoryDirectory.DIRECTORY_SEPARATOR in folderPath than homeRootFolder,
// then the user is trying to delete another user's home directory
int folderPathDirCount = 0;
int homeRootPathDirCount = 0;
for ( int x = 0; x >= 0; folderPathDirCount++ ) {
x = folderPath.indexOf( RepositoryDirectory.DIRECTORY_SEPARATOR, x + 1 );
}
for ( int x = 0; x >= 0; homeRootPathDirCount++ ) {
x = homeRootPath.indexOf( RepositoryDirectory.DIRECTORY_SEPARATOR, x + 1 );
}
if ( folderPathDirCount == ( homeRootPathDirCount + 1 ) ) {
return true;
}
}
}
}
return false;
}
@Override
public void deleteRepositoryDirectory( final RepositoryDirectoryInterface dir ) throws KettleException {
deleteRepositoryDirectory( dir, false );
}
public void deleteRepositoryDirectory( final RepositoryDirectoryInterface dir, final boolean deleteHomeDirectories )
throws KettleException {
try {
// Fetch the folder to be deleted
RepositoryFile folder = pur.getFileById( dir.getObjectId().getId() );
// Fetch the user's home directory
RepositoryFile homeFolder = pur.getFile( ClientRepositoryPaths.getUserHomeFolderPath( user.getLogin() ) );
// Make sure the user is not trying to delete their own home directory
if ( isSameOrAncestorFolder( folder, homeFolder ) ) {
// Then throw an exception that the user cannot delete their own home directory
throw new KettleException( "You are not allowed to delete your home folder." );
}
if ( !deleteHomeDirectories && isUserHomeDirectory( folder ) ) {
throw new RepositoryObjectAccessException( "Cannot delete another users home directory",
RepositoryObjectAccessException.AccessExceptionType.USER_HOME_DIR );
}
pur.deleteFile( dir.getObjectId().getId(), null );
rootRef.clearRef();
} catch ( Exception e ) {
throw new KettleException( "Unable to delete directory with path [" + getPath( null, dir, null ) + "]", e );
}
}
@Override
public ObjectId renameRepositoryDirectory( final ObjectId dirId, final RepositoryDirectoryInterface newParent,
final String newName ) throws KettleException {
return renameRepositoryDirectory( dirId, newParent, newName, false );
}
public ObjectId renameRepositoryDirectory( final ObjectId dirId, final RepositoryDirectoryInterface newParent,
final String newName, final boolean renameHomeDirectories )
throws KettleException {
// dir ID is used to find orig obj; new parent is used as new parent (might be null meaning no change in parent);
// new name is used as new file name (might be null meaning no change in name)
String finalName = null;
String finalParentPath = null;
String interimFolderPath = null;
try {
RepositoryFile homeFolder = pur.getFile( ClientRepositoryPaths.getUserHomeFolderPath( user.getLogin() ) );
RepositoryFile folder = pur.getFileById( dirId.getId() );
// Make sure the user is not trying to move their own home directory
if ( isSameOrAncestorFolder( folder, homeFolder ) ) {
// Then throw an exception that the user cannot move their own home directory
throw new KettleException( "You are not allowed to move/rename your home folder." );
}
finalName = ( newName != null ? newName : folder.getName() );
interimFolderPath = getParentPath( folder.getPath() );
finalParentPath = ( newParent != null ? getPath( null, newParent, null ) : interimFolderPath );
if ( !renameHomeDirectories && isUserHomeDirectory( folder ) ) {
throw new RepositoryObjectAccessException( "Cannot move another users home directory",
RepositoryObjectAccessException.AccessExceptionType.USER_HOME_DIR );
}
pur.moveFile( dirId.getId(), finalParentPath + RepositoryFile.SEPARATOR + finalName, null );
rootRef.clearRef();
return dirId;
} catch ( Exception e ) {
throw new KettleException( "Unable to move/rename directory with id [" + dirId + "] to new parent ["
+ finalParentPath + "] and new name [" + finalName + "]", e );
}
}
protected RepositoryFileTree loadRepositoryFileTree( String path ) {
return pur.getTree( path, -1, null, true );
}
@Override
public RepositoryDirectoryInterface loadRepositoryDirectoryTree() throws KettleException {
// this method forces a reload of the repository directory tree structure
// a new rootRef will be obtained - this is a SoftReference which will be used
// by any calls to getRootDir()
RepositoryFileTree rootFileTree = loadRepositoryFileTree( ClientRepositoryPaths.getRootFolderPath() );
RepositoryDirectoryInterface rootDir = initRepositoryDirectoryTree( rootFileTree );
rootRef.setRef( rootDir );
return rootDir;
}
private RepositoryDirectoryInterface initRepositoryDirectoryTree( RepositoryFileTree repoTree )
throws KettleException {
RepositoryFile rootFolder = repoTree.getFile();
RepositoryDirectory rootDir = new RepositoryDirectory();
rootDir.setObjectId( new StringObjectId( rootFolder.getId().toString() ) );
loadRepositoryDirectory( rootDir, rootFolder, repoTree );
// Example: /etc
RepositoryDirectory etcDir = rootDir.findDirectory( ClientRepositoryPaths.getEtcFolderPath() );
RepositoryDirectory newRoot = new RepositoryDirectory();
newRoot.setObjectId( rootDir.getObjectId() );
newRoot.setVisible( false );
for ( int i = 0; i < rootDir.getNrSubdirectories(); i++ ) {
RepositoryDirectory childDir = rootDir.getSubdirectory( i );
// Don't show /etc
boolean isEtcChild = childDir.equals( etcDir );
if ( isEtcChild ) {
continue;
}
newRoot.addSubdirectory( childDir );
}
return newRoot;
}
private void loadRepositoryDirectory( final RepositoryDirectoryInterface parentDir, final RepositoryFile folder,
final RepositoryFileTree treeNode ) throws KettleException {
try {
List<RepositoryElementMetaInterface> fileChildren = new ArrayList<RepositoryElementMetaInterface>();
List<RepositoryFileTree> children = treeNode.getChildren();
if ( children != null ) {
for ( RepositoryFileTree child : children ) {
if ( child.getFile().isFolder() ) {
RepositoryDirectory dir = new RepositoryDirectory( parentDir, child.getFile().getName() );
dir.setObjectId( new StringObjectId( child.getFile().getId().toString() ) );
parentDir.addSubdirectory( dir );
loadRepositoryDirectory( dir, child.getFile(), child );
} else {
// a real file, like a Transformation or Job
RepositoryLock lock = unifiedRepositoryLockService.getLock( child.getFile() );
RepositoryObjectType objectType = getObjectType( child.getFile().getName() );
fileChildren.add( new EERepositoryObject( child, parentDir, null, objectType, null, lock, false ) );
}
}
parentDir.setRepositoryObjects( fileChildren );
}
} catch ( Exception e ) {
throw new KettleException( "Unable to load directory structure from repository", e );
}
}
@Override
public String[] getDirectoryNames( final ObjectId idDirectory ) throws KettleException {
try {
List<RepositoryFile> children = pur.getChildren( idDirectory.getId() );
List<String> childNames = new ArrayList<String>();
for ( RepositoryFile child : children ) {
if ( child.isFolder() ) {
childNames.add( child.getName() );
}
}
return childNames.toArray( new String[ 0 ] );
} catch ( Exception e ) {
throw new KettleException( "Unable to get list of object names from directory [" + idDirectory + "]", e );
}
}
@Override
public void deleteClusterSchema( ObjectId idCluster ) throws KettleException {
permanentlyDeleteSharedObject( idCluster );
removeFromSharedObjectCache( RepositoryObjectType.CLUSTER_SCHEMA, idCluster );
}
@Override
public void deleteJob( ObjectId idJob ) throws KettleException {
deleteFileById( idJob );
}
protected void permanentlyDeleteSharedObject( final ObjectId id ) throws KettleException {
try {
pur.deleteFile( id.getId(), true, null );
} catch ( Exception e ) {
throw new KettleException( "Unable to delete object with id [" + id + "]", e );
}
}
public void deleteFileById( final ObjectId id ) throws KettleException {
try {
pur.deleteFile( id.getId(), null );
rootRef.clearRef();
} catch ( Exception e ) {
throw new KettleException( "Unable to delete object with id [" + id + "]", e );
}
}
@Override
public void deletePartitionSchema( ObjectId idPartitionSchema ) throws KettleException {
permanentlyDeleteSharedObject( idPartitionSchema );
removeFromSharedObjectCache( RepositoryObjectType.PARTITION_SCHEMA, idPartitionSchema );
}
@Override
public void deleteSlave( ObjectId idSlave ) throws KettleException {
permanentlyDeleteSharedObject( idSlave );
removeFromSharedObjectCache( RepositoryObjectType.SLAVE_SERVER, idSlave );
}
@Override
public void deleteTransformation( ObjectId idTransformation ) throws KettleException {
deleteFileById( idTransformation );
rootRef.clearRef();
}
@Override
public boolean exists( final String name, final RepositoryDirectoryInterface repositoryDirectory,
final RepositoryObjectType objectType ) throws KettleException {
try {
String absPath = getPath( name, repositoryDirectory, objectType );
return pur.getFile( absPath ) != null;
} catch ( Exception e ) {
throw new KettleException( "Unable to verify if the repository element [" + name + "] exists in ", e );
}
}
private String getPath( final String name, final RepositoryDirectoryInterface repositoryDirectory,
final RepositoryObjectType objectType ) {
String path = null;
// need to check for null id since shared objects return a non-null repoDir (see
// partSchema.getRepositoryDirectory())
if ( repositoryDirectory != null && repositoryDirectory.getObjectId() != null ) {
path = repositoryDirectory.getPath();
}
// return the directory path
if ( objectType == null ) {
return path;
}
String sanitizedName = checkAndSanitize( name );
switch ( objectType ) {
case DATABASE: {
return getDatabaseMetaParentFolderPath() + RepositoryFile.SEPARATOR + sanitizedName
+ RepositoryObjectType.DATABASE.getExtension();
}
case TRANSFORMATION: {
// Check for null path
if ( path == null ) {
return null;
} else {
return path + ( path.endsWith( RepositoryFile.SEPARATOR ) ? "" : RepositoryFile.SEPARATOR ) + sanitizedName
+ RepositoryObjectType.TRANSFORMATION.getExtension();
}
}
case PARTITION_SCHEMA: {
return getPartitionSchemaParentFolderPath() + RepositoryFile.SEPARATOR + sanitizedName
+ RepositoryObjectType.PARTITION_SCHEMA.getExtension();
}
case SLAVE_SERVER: {
return getSlaveServerParentFolderPath() + RepositoryFile.SEPARATOR + sanitizedName
+ RepositoryObjectType.SLAVE_SERVER.getExtension();
}
case CLUSTER_SCHEMA: {
return getClusterSchemaParentFolderPath() + RepositoryFile.SEPARATOR + sanitizedName
+ RepositoryObjectType.CLUSTER_SCHEMA.getExtension();
}
case JOB: {
// Check for null path
if ( path == null ) {
return null;
} else {
return path + ( path.endsWith( RepositoryFile.SEPARATOR ) ? "" : RepositoryFile.SEPARATOR ) + sanitizedName
+ RepositoryObjectType.JOB.getExtension();
}
}
default: {
throw new UnsupportedOperationException( "not implemented" );
}
}
}
@Override
public ObjectId getClusterID( String name ) throws KettleException {
try {
return getObjectId( name, null, RepositoryObjectType.CLUSTER_SCHEMA, false );
} catch ( Exception e ) {
throw new KettleException( "Unable to get ID for cluster schema [" + name + "]", e );
}
}
@Override
public ObjectId[] getClusterIDs( boolean includeDeleted ) throws KettleException {
try {
List<RepositoryFile> children = getAllFilesOfType( null, RepositoryObjectType.CLUSTER_SCHEMA, includeDeleted );
List<ObjectId> ids = new ArrayList<ObjectId>();
for ( RepositoryFile file : children ) {
ids.add( new StringObjectId( file.getId().toString() ) );
}
return ids.toArray( new ObjectId[ 0 ] );
} catch ( Exception e ) {
throw new KettleException( "Unable to get all cluster schema IDs", e );
}
}
@Override
public String[] getClusterNames( boolean includeDeleted ) throws KettleException {
try {
List<RepositoryFile> children = getAllFilesOfType( null, RepositoryObjectType.CLUSTER_SCHEMA, includeDeleted );
List<String> names = new ArrayList<String>();
for ( RepositoryFile file : children ) {
names.add( file.getTitle() );
}
return names.toArray( new String[ 0 ] );
} catch ( Exception e ) {
throw new KettleException( "Unable to get all cluster schema names", e );
}
}
@Override
public ObjectId getDatabaseID( final String name ) throws KettleException {
try {
return getObjectId( name, null, RepositoryObjectType.DATABASE, false );
} catch ( Exception e ) {
throw new KettleException( "Unable to get ID for database [" + name + "]", e );
}
}
/**
* Copying the behavior of the original JCRRepository, this implementation returns IDs of deleted objects too.
*/
private ObjectId getObjectId( final String name, final RepositoryDirectoryInterface dir,
final RepositoryObjectType objectType, boolean includedDeleteFiles ) {
final String absPath = getPath( name, dir, objectType );
RepositoryFile file = pur.getFile( absPath );
if ( file != null ) {
// file exists
return new StringObjectId( file.getId().toString() );
} else if ( includedDeleteFiles ) {
switch ( objectType ) {
case DATABASE: {
// file either never existed or has been deleted
List<RepositoryFile>
deletedChildren =
pur.getDeletedFiles( getDatabaseMetaParentFolderPath(),
name + RepositoryObjectType.DATABASE.getExtension() );
if ( !deletedChildren.isEmpty() ) {
return new StringObjectId( deletedChildren.get( 0 ).getId().toString() );
} else {
return null;
}
}
case TRANSFORMATION: {
// file either never existed or has been deleted
List<RepositoryFile>
deletedChildren =
pur.getDeletedFiles( dir.getObjectId().getId(),
name + RepositoryObjectType.TRANSFORMATION.getExtension() );
if ( !deletedChildren.isEmpty() ) {
return new StringObjectId( deletedChildren.get( 0 ).getId().toString() );
} else {
return null;
}
}
case PARTITION_SCHEMA: {
// file either never existed or has been deleted
List<RepositoryFile>
deletedChildren =
pur.getDeletedFiles( getPartitionSchemaParentFolderPath(),
name + RepositoryObjectType.PARTITION_SCHEMA.getExtension() );
if ( !deletedChildren.isEmpty() ) {
return new StringObjectId( deletedChildren.get( 0 ).getId().toString() );
} else {
return null;
}
}
case SLAVE_SERVER: {
// file either never existed or has been deleted
List<RepositoryFile>
deletedChildren =
pur.getDeletedFiles( getSlaveServerParentFolderPath(),
name + RepositoryObjectType.SLAVE_SERVER.getExtension() );
if ( !deletedChildren.isEmpty() ) {
return new StringObjectId( deletedChildren.get( 0 ).getId().toString() );
} else {
return null;
}
}
case CLUSTER_SCHEMA: {
// file either never existed or has been deleted
List<RepositoryFile>
deletedChildren =
pur.getDeletedFiles( getClusterSchemaParentFolderPath(),
name + RepositoryObjectType.CLUSTER_SCHEMA.getExtension() );
if ( !deletedChildren.isEmpty() ) {
return new StringObjectId( deletedChildren.get( 0 ).getId().toString() );
} else {
return null;
}
}
case JOB: {
// file either never existed or has been deleted
List<RepositoryFile>
deletedChildren =
pur.getDeletedFiles( dir.getObjectId().getId(), name + RepositoryObjectType.JOB.getExtension() );
if ( !deletedChildren.isEmpty() ) {
return new StringObjectId( deletedChildren.get( 0 ).getId().toString() );
} else {
return null;
}
}
default: {
throw new UnsupportedOperationException( "not implemented" );
}
}
} else {
return null;
}
}
@Override
public ObjectId[] getDatabaseIDs( boolean includeDeleted ) throws KettleException {
try {
List<RepositoryFile> children = getAllFilesOfType( null, RepositoryObjectType.DATABASE, includeDeleted );
List<ObjectId> ids = new ArrayList<ObjectId>();
for ( RepositoryFile file : children ) {
ids.add( new StringObjectId( file.getId().toString() ) );
}
return ids.toArray( new ObjectId[ 0 ] );
} catch ( Exception e ) {
throw new KettleException( "Unable to get all database IDs", e );
}
}
protected List<RepositoryFile> getAllFilesOfType( final ObjectId dirId, final RepositoryObjectType objectType,
final boolean includeDeleted ) throws KettleException {
return getAllFilesOfType( dirId, Arrays.asList( new RepositoryObjectType[] { objectType } ), includeDeleted );
}
protected List<RepositoryFile> getAllFilesOfType( final ObjectId dirId, final List<RepositoryObjectType> objectTypes,
final boolean includeDeleted ) throws KettleException {
List<RepositoryFile> allChildren = new ArrayList<RepositoryFile>();
List<RepositoryFile> children = getAllFilesOfType( dirId, objectTypes );
allChildren.addAll( children );
if ( includeDeleted ) {
String dirPath = null;
if ( dirId != null ) {
// derive path using id
dirPath = pur.getFileById( dirId.getId() ).getPath();
}
List<RepositoryFile> deletedChildren = getAllDeletedFilesOfType( dirPath, objectTypes );
allChildren.addAll( deletedChildren );
Collections.sort( allChildren );
}
return allChildren;
}
protected List<RepositoryFile> getAllFilesOfType( final ObjectId dirId, final List<RepositoryObjectType> objectTypes )
throws KettleException {
Set<Serializable> parentFolderIds = new HashSet<>();
List<String> filters = new ArrayList<>();
for ( RepositoryObjectType objectType : objectTypes ) {
switch ( objectType ) {
case DATABASE: {
parentFolderIds.add( getDatabaseMetaParentFolderId() );
filters.add( "*" + RepositoryObjectType.DATABASE.getExtension() ); //$NON-NLS-1$
break;
}
case TRANSFORMATION: {
parentFolderIds.add( dirId.getId() );
filters.add( "*" + RepositoryObjectType.TRANSFORMATION.getExtension() ); //$NON-NLS-1$
break;
}
case PARTITION_SCHEMA: {
parentFolderIds.add( getPartitionSchemaParentFolderId() );
filters.add( "*" + RepositoryObjectType.PARTITION_SCHEMA.getExtension() ); //$NON-NLS-1$
break;
}
case SLAVE_SERVER: {
parentFolderIds.add( getSlaveServerParentFolderId() );
filters.add( "*" + RepositoryObjectType.SLAVE_SERVER.getExtension() ); //$NON-NLS-1$
break;
}
case CLUSTER_SCHEMA: {
parentFolderIds.add( getClusterSchemaParentFolderId() );
filters.add( "*" + RepositoryObjectType.CLUSTER_SCHEMA.getExtension() ); //$NON-NLS-1$
break;
}
case JOB: {
parentFolderIds.add( dirId.getId() );
filters.add( "*" + RepositoryObjectType.JOB.getExtension() ); //$NON-NLS-1$
break;
}
case TRANS_DATA_SERVICE: {
parentFolderIds.add( dirId.getId() );
filters.add( "*" + RepositoryObjectType.TRANS_DATA_SERVICE.getExtension() ); //$NON-NLS-1$
break;
}
default: {
throw new UnsupportedOperationException( "not implemented" );
}
}
}
StringBuilder mergedFilterBuf = new StringBuilder();
// build filter
int i = 0;
for ( String filter : filters ) {
if ( i++ > 0 ) {
mergedFilterBuf.append( " | " ); //$NON-NLS-1$
}
mergedFilterBuf.append( filter );
}
List<RepositoryFile> allFiles = new ArrayList<>();
for ( Serializable parentFolderId : parentFolderIds ) {
allFiles.addAll( pur.getChildren( parentFolderId, mergedFilterBuf.toString() ) );
}
Collections.sort( allFiles );
return allFiles;
}
protected List<RepositoryFile> getAllDeletedFilesOfType( final String dirPath,
final List<RepositoryObjectType> objectTypes )
throws KettleException {
Set<String> parentFolderPaths = new HashSet<>();
List<String> filters = new ArrayList<>();
for ( RepositoryObjectType objectType : objectTypes ) {
switch ( objectType ) {
case DATABASE: {
parentFolderPaths.add( getDatabaseMetaParentFolderPath() );
filters.add( "*" + RepositoryObjectType.DATABASE.getExtension() ); //$NON-NLS-1$
break;
}
case TRANSFORMATION: {
parentFolderPaths.add( dirPath );
filters.add( "*" + RepositoryObjectType.TRANSFORMATION.getExtension() ); //$NON-NLS-1$
break;
}
case PARTITION_SCHEMA: {
parentFolderPaths.add( getPartitionSchemaParentFolderPath() );
filters.add( "*" + RepositoryObjectType.PARTITION_SCHEMA.getExtension() ); //$NON-NLS-1$
break;
}
case SLAVE_SERVER: {
parentFolderPaths.add( getSlaveServerParentFolderPath() );
filters.add( "*" + RepositoryObjectType.SLAVE_SERVER.getExtension() ); //$NON-NLS-1$
break;
}
case CLUSTER_SCHEMA: {
parentFolderPaths.add( getClusterSchemaParentFolderPath() );
filters.add( "*" + RepositoryObjectType.CLUSTER_SCHEMA.getExtension() ); //$NON-NLS-1$
break;
}
case JOB: {
parentFolderPaths.add( dirPath );