Skip to content

Commit 107f259

Browse files
committed
continue to rough in
1 parent 1a0551f commit 107f259

File tree

17 files changed

+302
-48
lines changed

17 files changed

+302
-48
lines changed

lightblue-ldap-common/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!--
3+
Copyright 2014 Red Hat, Inc. and/or its affiliates.
4+
5+
This file is part of lightblue.
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>com.redhat.lightblue.ldap</groupId>
27+
<artifactId>lightblue-ldap-pom</artifactId>
28+
<version>0.0.1-SNAPSHOT</version>
29+
</parent>
30+
31+
<artifactId>lightblue-ldap-common</artifactId>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>com.redhat.lightblue</groupId>
36+
<artifactId>metadata</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>com.unboundid</groupId>
41+
<artifactId>unboundid-ldapsdk</artifactId>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.redhat.lightblue.common.ldap;
2+
3+
import com.unboundid.ldap.sdk.LDAPConnection;
4+
import com.unboundid.ldap.sdk.LDAPException;
5+
6+
7+
public interface DBResolver {
8+
9+
/**
10+
* Returns a {@link LDAPConnection} based on the backend definition
11+
*/
12+
LDAPConnection get(LdapDataStore store) throws LDAPException;
13+
14+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2014 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue.common.ldap;
20+
21+
import com.redhat.lightblue.metadata.DataStore;
22+
23+
public class LdapDataStore implements DataStore {
24+
25+
private static final long serialVersionUID = 7599798419158041647L;
26+
27+
public static final String BACKEND = "ldap";
28+
29+
private String database;
30+
31+
public String getBackend() {
32+
return BACKEND;
33+
}
34+
35+
public String getDatabase() {
36+
return database;
37+
}
38+
39+
public void setDatabase(String database) {
40+
this.database = database;
41+
}
42+
43+
public LdapDataStore(){}
44+
45+
public LdapDataStore(String database){
46+
this.database = database;
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
final int prime = 31;
52+
int result = 1;
53+
result = prime * result
54+
+ ((database == null) ? 0 : database.hashCode());
55+
return result;
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
if (this == obj) {
61+
return true;
62+
}
63+
if (obj == null) {
64+
return false;
65+
}
66+
if (getClass() != obj.getClass()) {
67+
return false;
68+
}
69+
LdapDataStore other = (LdapDataStore) obj;
70+
if (database == null) {
71+
if (other.database != null) {
72+
return false;
73+
}
74+
}
75+
else if (!database.equals(other.database)) {
76+
return false;
77+
}
78+
return true;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return "LdapDataStore [database=" + database + "]";
84+
}
85+
86+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.redhat.lightblue.common.ldap;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class LdapDataStoreTest {
8+
9+
@Test
10+
public void testGetBackend(){
11+
assertEquals(LdapDataStore.BACKEND, new LdapDataStore().getBackend());
12+
}
13+
14+
}

lightblue-ldap-config/pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@
4444
<artifactId>lightblue-ldap-metadata</artifactId>
4545
</dependency>
4646

47-
<dependency>
48-
<groupId>com.unboundid</groupId>
49-
<artifactId>unboundid-ldapsdk</artifactId>
50-
</dependency>
51-
52-
<dependency>
53-
<groupId>com.redhat.lightblue.ldap</groupId>
54-
<artifactId>lightblue-ldap-test</artifactId>
55-
<scope>test</scope>
47+
<dependency>
48+
<groupId>com.unboundid</groupId>
49+
<artifactId>unboundid-ldapsdk</artifactId>
5650
</dependency>
51+
52+
<dependency>
53+
<groupId>com.redhat.lightblue.ldap</groupId>
54+
<artifactId>lightblue-ldap-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
5757
</dependencies>
5858

5959
</project>

lightblue-ldap-config/src/main/java/com/redhat/lightblue/config/ldap/LdapControllerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
public class LdapControllerFactory implements ControllerFactory{
2828

2929
public CRUDController createController(ControllerConfiguration cfg, DataSourcesConfiguration ds) {
30-
return new LdapCRUDController();
30+
return new LdapCRUDController(
31+
new LdapDBResolver(ds.getDataSourcesByType(LdapDataSourceConfiguration.class)));
3132
}
3233

3334
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.redhat.lightblue.config.ldap;
2+
3+
import java.util.Map;
4+
5+
import com.redhat.lightblue.common.ldap.DBResolver;
6+
import com.redhat.lightblue.common.ldap.LdapDataStore;
7+
import com.unboundid.ldap.sdk.LDAPConnection;
8+
import com.unboundid.ldap.sdk.LDAPException;
9+
10+
public class LdapDBResolver implements DBResolver{
11+
12+
private final Map<String, LdapDataSourceConfiguration> ldapDataSources;
13+
14+
public LdapDBResolver(Map<String, LdapDataSourceConfiguration> ldapDataSources){
15+
this.ldapDataSources = ldapDataSources;
16+
}
17+
18+
public LDAPConnection get(LdapDataStore store) throws LDAPException {
19+
return get(store.getDatabase());
20+
}
21+
22+
public LDAPConnection get(String database) throws LDAPException{
23+
LdapDataSourceConfiguration cnf = ldapDataSources.get(database);
24+
if(cnf == null){
25+
throw new IllegalArgumentException("No database for " + database);
26+
}
27+
return cnf.getLdapConnection();
28+
}
29+
30+
}

lightblue-ldap-config/src/main/java/com/redhat/lightblue/config/ldap/LdapDataSourceConfiguration.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,36 @@ private JsonNode parseJsonNode(JsonNode node, String key, boolean required){
140140
return parsedNode;
141141
}
142142

143+
@Override
144+
public int hashCode() {
145+
final int prime = 31;
146+
int result = 1;
147+
result = prime * result
148+
+ ((databaseName == null) ? 0 : databaseName.hashCode());
149+
return result;
150+
}
151+
152+
@Override
153+
public boolean equals(Object obj) {
154+
if (this == obj) {
155+
return true;
156+
}
157+
if (obj == null) {
158+
return false;
159+
}
160+
if (getClass() != obj.getClass()) {
161+
return false;
162+
}
163+
LdapDataSourceConfiguration other = (LdapDataSourceConfiguration) obj;
164+
if (databaseName == null) {
165+
if (other.databaseName != null) {
166+
return false;
167+
}
168+
}
169+
else if (!databaseName.equals(other.databaseName)) {
170+
return false;
171+
}
172+
return true;
173+
}
174+
143175
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.redhat.lightblue.config.ldap;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.mockito.Mockito.mock;
5+
6+
import org.junit.Test;
7+
8+
import com.redhat.lightblue.config.DataSourcesConfiguration;
9+
10+
public class LdapControllerFactoryTest {
11+
12+
@Test
13+
public void testCreateController(){
14+
assertNotNull(new LdapControllerFactory().createController(null, mock(DataSourcesConfiguration.class)));
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.redhat.lightblue.config.ldap;
2+
3+
import java.util.HashMap;
4+
5+
import org.junit.Test;
6+
7+
import com.unboundid.ldap.sdk.LDAPException;
8+
9+
public class LdapDBResolverTest {
10+
11+
@Test(expected = IllegalArgumentException.class)
12+
public void testGet_UnknownDatabase() throws LDAPException{
13+
LdapDBResolver resolver = new LdapDBResolver(new HashMap<String, LdapDataSourceConfiguration>());
14+
resolver.get("Does Not Exist");
15+
}
16+
17+
}

0 commit comments

Comments
 (0)