Skip to content
This repository was archived by the owner on Nov 24, 2024. It is now read-only.

Commit 48b5a36

Browse files
authored
Update codebase (#4)
Changes: * drop legacy SISU, use latest * drop legacy test helper class, use vanilla junit * apply language level changes, where applicable * fix javadoc where applicable * Drop legacy plx Xml * Make classes thread-safe, mark them as singletons
1 parent 7a20924 commit 48b5a36

File tree

10 files changed

+137
-149
lines changed

10 files changed

+137
-149
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ target/
33
.classpath
44
.settings/
55
bin
6+
*.iml
7+

pom.xml

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>plexus-cipher</artifactId>
12-
<version>1.9-SNAPSHOT</version>
12+
<version>2.0-SNAPSHOT</version>
1313

1414
<name>Plexus Cipher: encryption/decryption Component</name>
1515

@@ -32,16 +32,37 @@
3232

3333
<properties>
3434
<javaVersion>7</javaVersion>
35+
<sisuVersion>0.3.4</sisuVersion>
3536
<project.build.outputTimestamp>2020-01-20T18:52:37Z</project.build.outputTimestamp>
3637
</properties>
37-
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>javax.inject</groupId>
42+
<artifactId>javax.inject</artifactId>
43+
<version>1</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.eclipse.sisu</groupId>
47+
<artifactId>org.eclipse.sisu.inject</artifactId>
48+
<version>${sisuVersion}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.13.2</version>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
3859
<build>
3960
<pluginManagement>
4061
<plugins>
4162
<plugin>
4263
<groupId>org.apache.maven.plugins</groupId>
4364
<artifactId>maven-surefire-plugin</artifactId>
44-
<version>2.22.1</version>
65+
<version>2.22.2</version>
4566
</plugin>
4667
<plugin>
4768
<groupId>org.eclipse.m2e</groupId>
@@ -106,7 +127,7 @@
106127
<plugin>
107128
<groupId>org.eclipse.sisu</groupId>
108129
<artifactId>sisu-maven-plugin</artifactId>
109-
<version>0.3.4</version>
130+
<version>${sisuVersion}</version>
110131
<executions>
111132
<execution>
112133
<goals>
@@ -119,19 +140,4 @@
119140
</plugins>
120141
</build>
121142

122-
<dependencies>
123-
<dependency>
124-
<groupId>org.sonatype.sisu</groupId>
125-
<artifactId>sisu-inject-bean</artifactId>
126-
<version>2.6.0</version>
127-
<scope>provided</scope>
128-
</dependency>
129-
<dependency>
130-
<groupId>junit</groupId>
131-
<artifactId>junit</artifactId>
132-
<version>4.13.1</version>
133-
<scope>test</scope>
134-
</dependency>
135-
</dependencies>
136-
137143
</project>

src/main/java/org/sonatype/plexus/components/cipher/Base64.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class Base64
9595
* The value of undefined encodings is <code>-1</code>.
9696
* </p>
9797
*/
98-
private static byte[] base64Alphabet = new byte[BASELENGTH];
98+
private static final byte[] base64Alphabet = new byte[BASELENGTH];
9999

100100
/**
101101
* <p/>
@@ -110,7 +110,7 @@ public class Base64
110110
* For example, <code>lookUpBase64Alphabet[62] </code> returns <code>'+'</code>.
111111
* </p>
112112
*/
113-
private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
113+
private static final byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
114114

115115
// Populating the lookup and character arrays
116116
static {
Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
@@ -15,35 +15,38 @@
1515
import java.security.Provider;
1616
import java.security.Security;
1717
import java.util.HashSet;
18-
import java.util.Iterator;
1918
import java.util.Set;
2019
import java.util.regex.Matcher;
2120
import java.util.regex.Pattern;
2221

23-
import javax.enterprise.inject.Typed;
2422
import javax.inject.Named;
23+
import javax.inject.Singleton;
24+
25+
import org.eclipse.sisu.Typed;
2526

2627
/**
28+
* Default implementation of {@link PlexusCipher}. This class is thread safe.
29+
*
2730
* @author Oleg Gusakov
2831
*/
32+
@Singleton
2933
@Named( "default" )
3034
@Typed( PlexusCipher.class )
3135
public class DefaultPlexusCipher
3236
implements PlexusCipher
3337
{
34-
3538
private static final Pattern ENCRYPTED_STRING_PATTERN = Pattern.compile( ".*?[^\\\\]?\\{(.*?[^\\\\])\\}.*" );
3639

3740
private final PBECipher _cipher;
3841

3942
// ---------------------------------------------------------------
4043
public DefaultPlexusCipher()
41-
throws PlexusCipherException
4244
{
4345
_cipher = new PBECipher();
4446
}
4547

4648
// ---------------------------------------------------------------
49+
@Override
4750
public String encrypt( final String str, final String passPhrase )
4851
throws PlexusCipherException
4952
{
@@ -56,13 +59,15 @@ public String encrypt( final String str, final String passPhrase )
5659
}
5760

5861
// ---------------------------------------------------------------
62+
@Override
5963
public String encryptAndDecorate( final String str, final String passPhrase )
6064
throws PlexusCipherException
6165
{
6266
return decorate( encrypt( str, passPhrase ) );
6367
}
6468

6569
// ---------------------------------------------------------------
70+
@Override
6671
public String decrypt( final String str, final String passPhrase )
6772
throws PlexusCipherException
6873
{
@@ -75,6 +80,7 @@ public String decrypt( final String str, final String passPhrase )
7580
}
7681

7782
// ---------------------------------------------------------------
83+
@Override
7884
public String decryptDecorated( final String str, final String passPhrase )
7985
throws PlexusCipherException
8086
{
@@ -92,6 +98,7 @@ public String decryptDecorated( final String str, final String passPhrase )
9298
}
9399

94100
// ----------------------------------------------------------------------------
101+
@Override
95102
public boolean isEncryptedString( final String str )
96103
{
97104
if ( str == null || str.length() < 1 )
@@ -105,7 +112,7 @@ public boolean isEncryptedString( final String str )
105112
}
106113

107114
// ----------------------------------------------------------------------------
108-
// -------------------
115+
@Override
109116
public String unDecorate( final String str )
110117
throws PlexusCipherException
111118
{
@@ -122,75 +129,67 @@ public String unDecorate( final String str )
122129
}
123130

124131
// ----------------------------------------------------------------------------
125-
// -------------------
132+
@Override
126133
public String decorate( final String str )
127134
{
128135
return ENCRYPTED_STRING_DECORATION_START + ( str == null ? "" : str ) + ENCRYPTED_STRING_DECORATION_STOP;
129136
}
130137

131138
// ---------------------------------------------------------------
132-
// ---------------------------------------------------------------
133-
// ***************************************************************
139+
134140
/**
135141
* Exploratory part. This method returns all available services types
136142
*/
137143
public static String[] getServiceTypes()
138144
{
139-
Set result = new HashSet();
145+
Set<String> result = new HashSet<>();
140146

141147
// All all providers
142148
Provider[] providers = Security.getProviders();
143-
for ( int i = 0; i < providers.length; i++ )
144-
{
149+
for (Provider provider : providers) {
145150
// Get services provided by each provider
146-
Set keys = providers[i].keySet();
147-
for ( Iterator it = keys.iterator(); it.hasNext(); )
148-
{
149-
String key = (String) it.next();
150-
key = key.split( " " )[0];
151-
152-
if ( key.startsWith( "Alg.Alias." ) )
153-
{
151+
Set<Object> keys = provider.keySet();
152+
for (Object o : keys) {
153+
String key = (String) o;
154+
key = key.split(" ")[0];
155+
156+
if (key.startsWith("Alg.Alias.")) {
154157
// Strip the alias
155-
key = key.substring( 10 );
158+
key = key.substring(10);
156159
}
157-
int ix = key.indexOf( '.' );
158-
result.add( key.substring( 0, ix ) );
160+
int ix = key.indexOf('.');
161+
result.add(key.substring(0, ix));
159162
}
160163
}
161-
return (String[]) result.toArray( new String[result.size()] );
164+
return result.toArray( new String[result.size()] );
162165
}
163166

164167
/**
165168
* This method returns the available implementations for a service type
166169
*/
167170
public static String[] getCryptoImpls( final String serviceType )
168171
{
169-
Set result = new HashSet();
172+
Set<String> result = new HashSet<>();
170173

171174
// All all providers
172175
Provider[] providers = Security.getProviders();
173-
for ( int i = 0; i < providers.length; i++ )
174-
{
176+
for (Provider provider : providers) {
175177
// Get services provided by each provider
176-
Set keys = providers[i].keySet();
177-
for ( Iterator it = keys.iterator(); it.hasNext(); )
178-
{
179-
String key = (String) it.next();
180-
key = key.split( " " )[0];
181-
182-
if ( key.startsWith( serviceType + "." ) )
183-
{
184-
result.add( key.substring( serviceType.length() + 1 ) );
178+
Set<Object> keys = provider.keySet();
179+
for (Object o : keys) {
180+
String key = (String) o;
181+
key = key.split(" ")[0];
182+
183+
if (key.startsWith(serviceType + ".")) {
184+
result.add(key.substring(serviceType.length() + 1));
185185
}
186-
else if ( key.startsWith( "Alg.Alias." + serviceType + "." ) )
187-
{
186+
else if (key.startsWith("Alg.Alias." + serviceType + ".")) {
188187
// This is an alias
189-
result.add( key.substring( serviceType.length() + 11 ) );
188+
result.add(key.substring(serviceType.length() + 11));
190189
}
191190
}
192191
}
193-
return (String[]) result.toArray( new String[result.size()] );
192+
return result.toArray( new String[result.size()] );
194193
}
195194

196195
// ---------------------------------------------------------------
@@ -201,26 +200,18 @@ public static void main( final String[] args )
201200
String[] serviceTypes = getServiceTypes();
202201
if ( serviceTypes != null )
203202
{
204-
for ( int i = 0; i < serviceTypes.length; i++ )
205-
{
206-
String serviceType = serviceTypes[i];
207-
String[] serviceProviders = getCryptoImpls( serviceType );
208-
if ( serviceProviders != null )
209-
{
210-
System.out.println( serviceType + ": provider list" );
211-
for ( int j = 0; j < serviceProviders.length; j++ )
212-
{
213-
String provider = serviceProviders[j];
214-
System.out.println( " " + provider );
203+
for (String serviceType : serviceTypes) {
204+
String[] serviceProviders = getCryptoImpls(serviceType);
205+
if (serviceProviders != null) {
206+
System.out.println(serviceType + ": provider list");
207+
for (String provider : serviceProviders) {
208+
System.out.println(" " + provider);
215209
}
216210
}
217-
else
218-
{
219-
System.out.println( serviceType + ": does not have any providers in this environment" );
211+
else {
212+
System.out.println(serviceType + ": does not have any providers in this environment");
220213
}
221214
}
222215
}
223216
}
224-
// ---------------------------------------------------------------
225-
// ---------------------------------------------------------------
226217
}

0 commit comments

Comments
 (0)