This project has retired. For details please refer to its
Attic page.
DefaultCassandraArchivaManager xref
1 package org.apache.archiva.metadata.repository.cassandra;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.archiva.metadata.repository.cassandra.model.ColumnNames.*;
23
24 import me.prettyprint.cassandra.model.BasicColumnDefinition;
25 import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
26 import me.prettyprint.cassandra.serializers.StringSerializer;
27 import me.prettyprint.cassandra.service.CassandraHostConfigurator;
28 import me.prettyprint.cassandra.service.ThriftKsDef;
29 import me.prettyprint.hector.api.Cluster;
30 import me.prettyprint.hector.api.HConsistencyLevel;
31 import me.prettyprint.hector.api.Keyspace;
32 import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
33 import me.prettyprint.hector.api.ddl.ColumnIndexType;
34 import me.prettyprint.hector.api.ddl.ComparatorType;
35 import me.prettyprint.hector.api.factory.HFactory;
36 import org.apache.archiva.metadata.repository.RepositorySessionFactoryBean;
37 import org.apache.commons.lang.StringUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.ApplicationContext;
42 import org.springframework.stereotype.Service;
43
44 import javax.annotation.PostConstruct;
45 import javax.annotation.PreDestroy;
46 import javax.inject.Inject;
47 import java.util.ArrayList;
48 import java.util.List;
49
50
51
52
53
54
55
56 @Service("archivaEntityManagerFactory#cassandra")
57 public class DefaultCassandraArchivaManager
58 implements CassandraArchivaManager
59 {
60
61 private Logger logger = LoggerFactory.getLogger( getClass() );
62
63 @Inject
64 private ApplicationContext applicationContext;
65
66 private static final String CLUSTER_NAME = "archiva";
67
68 private static final String KEYSPACE_NAME = "ArchivaKeySpace";
69
70 private boolean started;
71
72 private Cluster cluster;
73
74 private Keyspace keyspace;
75
76
77 private String repositoryFamilyName = "repository";
78
79 private String namespaceFamilyName = "namespace";
80
81 private String projectFamilyName = PROJECT.toString();
82
83 private String projectVersionMetadataFamilyName = "projectversionmetadata";
84
85 private String artifactMetadataFamilyName = "artifactmetadata";
86
87 private String metadataFacetFamilyName = "metadatafacet";
88
89 private String mailingListFamilyName = "mailinglist";
90
91 private String licenseFamilyName = "license";
92
93 private String dependencyFamilyName = "dependency";
94
95 @Value("${cassandra.host}")
96 private String cassandraHost;
97
98 @Value("${cassandra.port}")
99 private String cassandraPort;
100
101 @Value("${cassandra.maxActive}")
102 private int maxActive;
103
104 @Value("${cassandra.readConsistencyLevel}")
105 private String readConsistencyLevel;
106
107 @Value("${cassandra.writeConsistencyLevel}")
108 private String writeConsistencyLevel;
109
110 @Value("${cassandra.replicationFactor}")
111 private int replicationFactor;
112
113 @Value("${cassandra.keyspace.name}")
114 private String keyspaceName;
115
116 @Value("${cassandra.cluster.name}")
117 private String clusterName;
118
119 @Inject
120 private RepositorySessionFactoryBean repositorySessionFactoryBean;
121
122 @PostConstruct
123 public void initialize()
124 {
125
126 if ( !StringUtils.equals( repositorySessionFactoryBean.getId(), "cassandra" ) )
127 {
128 return;
129 }
130 final CassandraHostConfigurator configurator =
131 new CassandraHostConfigurator( cassandraHost + ":" + cassandraPort );
132 configurator.setMaxActive( maxActive );
133
134
135 cluster = HFactory.getOrCreateCluster( clusterName, configurator );
136
137 final ConfigurableConsistencyLevel consistencyLevelPolicy = new ConfigurableConsistencyLevel();
138 consistencyLevelPolicy.setDefaultReadConsistencyLevel( HConsistencyLevel.valueOf( readConsistencyLevel ) );
139 consistencyLevelPolicy.setDefaultWriteConsistencyLevel( HConsistencyLevel.valueOf( writeConsistencyLevel ) );
140 keyspace = HFactory.createKeyspace( keyspaceName, cluster, consistencyLevelPolicy );
141
142 List<ColumnFamilyDefinition> cfds = new ArrayList<>();
143
144
145 {
146
147 final ColumnFamilyDefinition namespace =
148 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
149 getNamespaceFamilyName(),
150 ComparatorType.UTF8TYPE );
151 cfds.add( namespace );
152
153
154
155 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
156 nameColumn.setName( StringSerializer.get().toByteBuffer( NAME.toString() ) );
157 nameColumn.setIndexName( NAME.toString() );
158 nameColumn.setIndexType( ColumnIndexType.KEYS );
159 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
160 namespace.addColumnDefinition( nameColumn );
161
162 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
163 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( REPOSITORY_NAME.toString() ) );
164 repositoryIdColumn.setIndexName( REPOSITORY_NAME.toString() );
165 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
166 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
167 namespace.addColumnDefinition( repositoryIdColumn );
168 }
169
170
171 {
172 final ColumnFamilyDefinition repository =
173 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
174 getRepositoryFamilyName(),
175 ComparatorType.UTF8TYPE );
176
177 cfds.add( repository );
178
179 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
180 nameColumn.setName( StringSerializer.get().toByteBuffer( REPOSITORY_NAME.toString() ) );
181 nameColumn.setIndexName( REPOSITORY_NAME.toString() );
182 nameColumn.setIndexType( ColumnIndexType.KEYS );
183 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
184 repository.addColumnDefinition( nameColumn );
185 }
186
187
188 {
189
190 final ColumnFamilyDefinition project = HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
191 getProjectFamilyName(),
192 ComparatorType.UTF8TYPE );
193 cfds.add( project );
194
195
196
197 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
198 projectIdColumn.setName( StringSerializer.get().toByteBuffer( PROJECT_ID.toString() ) );
199 projectIdColumn.setIndexName( PROJECT_ID.toString() );
200 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
201 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
202 project.addColumnDefinition( projectIdColumn );
203
204 BasicColumnDefinition repositoryIdColumn = new BasicColumnDefinition();
205 repositoryIdColumn.setName( StringSerializer.get().toByteBuffer( REPOSITORY_NAME.toString() ) );
206 repositoryIdColumn.setIndexName( REPOSITORY_NAME.toString() );
207 repositoryIdColumn.setIndexType( ColumnIndexType.KEYS );
208 repositoryIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
209 project.addColumnDefinition( repositoryIdColumn );
210
211 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
212 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( NAMESPACE_ID.toString() ) );
213 namespaceIdColumn.setIndexName( NAMESPACE_ID.toString() );
214 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
215 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
216 project.addColumnDefinition( namespaceIdColumn );
217 }
218
219
220 {
221
222 final ColumnFamilyDefinition projectVersionMetadataModel =
223 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
224 getProjectVersionMetadataFamilyName(),
225 ComparatorType.UTF8TYPE );
226 cfds.add( projectVersionMetadataModel );
227
228
229
230 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
231 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( NAMESPACE_ID.toString() ) );
232 namespaceIdColumn.setIndexName( NAMESPACE_ID.toString() );
233 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
234 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
235 projectVersionMetadataModel.addColumnDefinition( namespaceIdColumn );
236
237 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
238 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( REPOSITORY_NAME.toString() ) );
239 repositoryNameColumn.setIndexName( REPOSITORY_NAME.toString() );
240 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
241 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
242 projectVersionMetadataModel.addColumnDefinition( repositoryNameColumn );
243
244 BasicColumnDefinition idColumn = new BasicColumnDefinition();
245 idColumn.setName( StringSerializer.get().toByteBuffer( ID.toString() ) );
246 idColumn.setIndexName( ID.toString() );
247 idColumn.setIndexType( ColumnIndexType.KEYS );
248 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
249 projectVersionMetadataModel.addColumnDefinition( idColumn );
250
251 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
252 projectIdColumn.setName( StringSerializer.get().toByteBuffer( PROJECT_ID.toString() ) );
253 projectIdColumn.setIndexName( PROJECT_ID.toString() );
254 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
255 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
256 projectVersionMetadataModel.addColumnDefinition( projectIdColumn );
257
258 }
259
260
261 {
262
263 final ColumnFamilyDefinition artifactMetadataModel =
264 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
265 getArtifactMetadataFamilyName(),
266 ComparatorType.UTF8TYPE );
267 cfds.add( artifactMetadataModel );
268
269
270
271 BasicColumnDefinition idColumn = new BasicColumnDefinition();
272 idColumn.setName( StringSerializer.get().toByteBuffer( ID.toString() ) );
273 idColumn.setIndexName( ID.toString() );
274 idColumn.setIndexType( ColumnIndexType.KEYS );
275 idColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
276 artifactMetadataModel.addColumnDefinition( idColumn );
277
278 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
279 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( REPOSITORY_NAME.toString() ) );
280 repositoryNameColumn.setIndexName( REPOSITORY_NAME.toString() );
281 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
282 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
283 artifactMetadataModel.addColumnDefinition( repositoryNameColumn );
284
285 BasicColumnDefinition namespaceIdColumn = new BasicColumnDefinition();
286 namespaceIdColumn.setName( StringSerializer.get().toByteBuffer( NAMESPACE_ID.toString() ) );
287 namespaceIdColumn.setIndexName( NAMESPACE_ID.toString() );
288 namespaceIdColumn.setIndexType( ColumnIndexType.KEYS );
289 namespaceIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
290 artifactMetadataModel.addColumnDefinition( namespaceIdColumn );
291
292 BasicColumnDefinition projectColumn = new BasicColumnDefinition();
293 projectColumn.setName( StringSerializer.get().toByteBuffer( PROJECT.toString() ) );
294 projectColumn.setIndexName( PROJECT.toString() );
295 projectColumn.setIndexType( ColumnIndexType.KEYS );
296 projectColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
297 artifactMetadataModel.addColumnDefinition( projectColumn );
298
299 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
300 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( PROJECT_VERSION.toString() ) );
301 projectVersionColumn.setIndexName( PROJECT_VERSION.toString() );
302 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
303 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
304 artifactMetadataModel.addColumnDefinition( projectVersionColumn );
305
306 BasicColumnDefinition versionColumn = new BasicColumnDefinition();
307 versionColumn.setName( StringSerializer.get().toByteBuffer( VERSION.toString() ) );
308 versionColumn.setIndexName( VERSION.toString() );
309 versionColumn.setIndexType( ColumnIndexType.KEYS );
310 versionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
311 artifactMetadataModel.addColumnDefinition( versionColumn );
312
313 BasicColumnDefinition whenGatheredColumn = new BasicColumnDefinition();
314 whenGatheredColumn.setName( StringSerializer.get().toByteBuffer( WHEN_GATHERED.toString() ) );
315 whenGatheredColumn.setIndexName( WHEN_GATHERED.toString() );
316 whenGatheredColumn.setIndexType( ColumnIndexType.KEYS );
317 whenGatheredColumn.setValidationClass( ComparatorType.LONGTYPE.getClassName() );
318 artifactMetadataModel.addColumnDefinition( whenGatheredColumn );
319
320 BasicColumnDefinition sha1Column = new BasicColumnDefinition();
321 sha1Column.setName( StringSerializer.get().toByteBuffer( SHA1.toString() ) );
322 sha1Column.setIndexName( SHA1.toString() );
323 sha1Column.setIndexType( ColumnIndexType.KEYS );
324 sha1Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
325 artifactMetadataModel.addColumnDefinition( sha1Column );
326
327 BasicColumnDefinition md5Column = new BasicColumnDefinition();
328 md5Column.setName( StringSerializer.get().toByteBuffer( MD5.toString() ) );
329 md5Column.setIndexName( MD5.toString() );
330 md5Column.setIndexType( ColumnIndexType.KEYS );
331 md5Column.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
332 artifactMetadataModel.addColumnDefinition( md5Column );
333
334
335 }
336
337
338 {
339 final ColumnFamilyDefinition metadataFacetModel =
340 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
341 getMetadataFacetFamilyName(),
342 ComparatorType.UTF8TYPE );
343 cfds.add( metadataFacetModel );
344
345
346
347 BasicColumnDefinition facetIdColumn = new BasicColumnDefinition();
348 facetIdColumn.setName( StringSerializer.get().toByteBuffer( FACET_ID.toString() ) );
349 facetIdColumn.setIndexName( FACET_ID.toString() );
350 facetIdColumn.setIndexType( ColumnIndexType.KEYS );
351 facetIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
352 metadataFacetModel.addColumnDefinition( facetIdColumn );
353
354 BasicColumnDefinition repositoryNameColumn = new BasicColumnDefinition();
355 repositoryNameColumn.setName( StringSerializer.get().toByteBuffer( REPOSITORY_NAME.toString() ) );
356 repositoryNameColumn.setIndexName( REPOSITORY_NAME.toString() );
357 repositoryNameColumn.setIndexType( ColumnIndexType.KEYS );
358 repositoryNameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
359 metadataFacetModel.addColumnDefinition( repositoryNameColumn );
360
361 BasicColumnDefinition nameColumn = new BasicColumnDefinition();
362 nameColumn.setName( StringSerializer.get().toByteBuffer( NAME.toString() ) );
363 nameColumn.setIndexName( NAME.toString() );
364 nameColumn.setIndexType( ColumnIndexType.KEYS );
365 nameColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
366 metadataFacetModel.addColumnDefinition( nameColumn );
367
368 BasicColumnDefinition namespaceColumn = new BasicColumnDefinition();
369 namespaceColumn.setName( StringSerializer.get().toByteBuffer( NAMESPACE_ID.toString() ) );
370 namespaceColumn.setIndexName( NAMESPACE_ID.toString() );
371 namespaceColumn.setIndexType( ColumnIndexType.KEYS );
372 namespaceColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
373 metadataFacetModel.addColumnDefinition( namespaceColumn );
374
375 BasicColumnDefinition projectIdColumn = new BasicColumnDefinition();
376 projectIdColumn.setName( StringSerializer.get().toByteBuffer( PROJECT_ID.toString() ) );
377 projectIdColumn.setIndexName( PROJECT_ID.toString() );
378 projectIdColumn.setIndexType( ColumnIndexType.KEYS );
379 projectIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
380 metadataFacetModel.addColumnDefinition( projectIdColumn );
381
382 BasicColumnDefinition projectVersionColumn = new BasicColumnDefinition();
383 projectVersionColumn.setName( StringSerializer.get().toByteBuffer( PROJECT_VERSION.toString() ) );
384 projectVersionColumn.setIndexName( PROJECT_VERSION.toString() );
385 projectVersionColumn.setIndexType( ColumnIndexType.KEYS );
386 projectVersionColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
387 metadataFacetModel.addColumnDefinition( projectVersionColumn );
388
389 }
390
391
392 {
393 final ColumnFamilyDefinition mailingListCf =
394 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
395 getMailingListFamilyName(),
396 ComparatorType.UTF8TYPE );
397
398 BasicColumnDefinition projectVersionMetadataModel_key = new BasicColumnDefinition();
399 projectVersionMetadataModel_key.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataModel.key" ) );
400 projectVersionMetadataModel_key.setIndexName( "projectVersionMetadataModel_key" );
401 projectVersionMetadataModel_key.setIndexType( ColumnIndexType.KEYS );
402 projectVersionMetadataModel_key.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
403 mailingListCf.addColumnDefinition( projectVersionMetadataModel_key );
404
405 cfds.add( mailingListCf );
406
407
408
409 }
410
411
412 {
413 final ColumnFamilyDefinition licenseCf =
414 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
415 getLicenseFamilyName(),
416 ComparatorType.UTF8TYPE );
417
418 BasicColumnDefinition projectVersionMetadataModel_key = new BasicColumnDefinition();
419 projectVersionMetadataModel_key.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataModel.key" ) );
420 projectVersionMetadataModel_key.setIndexName( "projectVersionMetadataModel_key" );
421 projectVersionMetadataModel_key.setIndexType( ColumnIndexType.KEYS );
422 projectVersionMetadataModel_key.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
423 licenseCf.addColumnDefinition( projectVersionMetadataModel_key );
424
425 cfds.add( licenseCf );
426
427
428
429 }
430
431
432 {
433 final ColumnFamilyDefinition dependencyCf =
434 HFactory.createColumnFamilyDefinition( keyspace.getKeyspaceName(),
435 getDependencyFamilyName(),
436 ComparatorType.UTF8TYPE );
437 cfds.add( dependencyCf );
438
439
440
441 BasicColumnDefinition groupIdColumn = new BasicColumnDefinition();
442 groupIdColumn.setName( StringSerializer.get().toByteBuffer( GROUP_ID.toString() ) );
443 groupIdColumn.setIndexName( "groupIdIdx" );
444 groupIdColumn.setIndexType( ColumnIndexType.KEYS );
445 groupIdColumn.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
446 dependencyCf.addColumnDefinition( groupIdColumn );
447
448 BasicColumnDefinition projectVersionMetadataModel_key = new BasicColumnDefinition();
449 projectVersionMetadataModel_key.setName( StringSerializer.get().toByteBuffer( "projectVersionMetadataModel.key" ) );
450 projectVersionMetadataModel_key.setIndexName( "projectVersionMetadataModel_key" );
451 projectVersionMetadataModel_key.setIndexType( ColumnIndexType.KEYS );
452 projectVersionMetadataModel_key.setValidationClass( ComparatorType.UTF8TYPE.getClassName() );
453 dependencyCf.addColumnDefinition( projectVersionMetadataModel_key );
454
455 }
456
457
458 {
459 if ( cluster.describeKeyspace( keyspaceName ) == null )
460 {
461 logger.info( "Creating Archiva Cassandra '" + keyspaceName + "' keyspace." );
462 cluster.addKeyspace( HFactory.createKeyspaceDefinition( keyspaceName,
463 ThriftKsDef.DEF_STRATEGY_CLASS,
464 replicationFactor,
465 cfds )
466 );
467 }
468 }
469
470 }
471
472 @Override
473 public void start()
474 {
475 }
476
477 @PreDestroy
478 @Override
479 public void shutdown()
480 {
481 }
482
483
484 @Override
485 public boolean started()
486 {
487 return started;
488 }
489
490
491 @Override
492 public Keyspace getKeyspace()
493 {
494 return keyspace;
495 }
496
497 @Override
498 public Cluster getCluster()
499 {
500 return cluster;
501 }
502
503 @Override
504 public String getRepositoryFamilyName()
505 {
506 return repositoryFamilyName;
507 }
508
509 @Override
510 public String getNamespaceFamilyName()
511 {
512 return namespaceFamilyName;
513 }
514
515 @Override
516 public String getProjectFamilyName()
517 {
518 return projectFamilyName;
519 }
520
521 @Override
522 public String getProjectVersionMetadataFamilyName()
523 {
524 return projectVersionMetadataFamilyName;
525 }
526
527 @Override
528 public String getArtifactMetadataFamilyName()
529 {
530 return artifactMetadataFamilyName;
531 }
532
533 @Override
534 public String getMetadataFacetFamilyName()
535 {
536 return metadataFacetFamilyName;
537 }
538
539 @Override
540 public String getMailingListFamilyName()
541 {
542 return mailingListFamilyName;
543 }
544
545 @Override
546 public String getLicenseFamilyName()
547 {
548 return licenseFamilyName;
549 }
550
551 @Override
552 public String getDependencyFamilyName()
553 {
554 return dependencyFamilyName;
555 }
556 }