2
0
Fork 0

WIP: left over open files for long...

This commit is contained in:
Willem 2017-06-10 02:22:01 +02:00
parent 0a2398c5c1
commit 76aa74990e
165 changed files with 4299 additions and 3373 deletions

View file

@ -0,0 +1,119 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.vasc.backend.ldap;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import net.forwardfire.vasc.backend.AbstractVascBackendLocal;
import net.forwardfire.vasc.backend.VascBackendAccessDataKey;
import net.forwardfire.vasc.backend.data.HashMapVascBackendAccessDataKey;
/**
* LdapVascBackend provides abstract data access for ldap.
*
* @author Willem Cazander
* @version 1.0 Sep 4, 2008
*/
abstract public class AbstractLdapVascBackend extends AbstractVascBackendLocal<HashMap<String,Serializable>> {
private Logger logger = Logger.getLogger(AbstractLdapVascBackend.class.getName());
private LdapConnectionProvider ldapConnectionProvider = null;
private String baseDN = null;
private final Map<String,Class<?>> keyMapType = new HashMap<String,Class<?>>(5);
abstract protected void startBackendLdap();
/**
* @see net.forwardfire.vasc.backend.AbstractVascBackendLocal#startBackendLocal()
*/
@Override
protected final void startBackendLocal() {
requireNonEmpty(getBaseDN(), "baseDN requires value.");
requireNonEmpty(keyMapType.keySet(), "No primary keys defined.");
startBackendLdap();
}
protected void safeDisconnect(LDAPConnection connection) {
if (connection==null) {
return;
}
try {
connection.disconnect();
} catch (LDAPException e) {
logger.warning("Error while disconnecting: "+e.getMessage());
}
}
protected final void addKeyMapType(String field,Class<?> fieldType) {
requireNonEmpty(field,"field has to have value");
requireNonNull(fieldType,"field has to have value");
keyMapType.put(field, fieldType);
}
protected final Map<String,Class<?>> getKeyMapType() {
return keyMapType;
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#createVascBackendAccessDataKey()
*/
@Override
public final VascBackendAccessDataKey<HashMap<String, Serializable>> createVascBackendAccessDataKey() {
return new HashMapVascBackendAccessDataKey(keyMapType);
}
/**
* @return the ldapConnectionProvider.
*/
public final LdapConnectionProvider getLdapConnectionProvider() {
return ldapConnectionProvider;
}
/**
* @param ldapConnectionProvider the ldapConnectionProvider to set.
*/
public final void setLdapConnectionProvider(LdapConnectionProvider ldapConnectionProvider) {
this.ldapConnectionProvider = ldapConnectionProvider;
}
/**
* @return the baseDN.
*/
public final String getBaseDN() {
return baseDN;
}
/**
* @param baseDN the baseDN to set.
*/
public final void setBaseDN(String baseDN) {
this.baseDN = baseDN;
}
}

View file

@ -22,25 +22,18 @@
package net.forwardfire.vasc.backend.ldap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import net.forwardfire.vasc.backend.AbstractVascBackend;
import net.forwardfire.vasc.backend.DefaultVascBackendResult;
import net.forwardfire.vasc.backend.VascBackendException;
import net.forwardfire.vasc.backend.VascBackendResult;
import net.forwardfire.vasc.backend.VascBackendState;
import net.forwardfire.vasc.backend.VascEntryFieldValue;
import net.forwardfire.vasc.backend.VascEntryRecordCreator;
import net.forwardfire.vasc.backend.data.HashMapVascEntryFieldValue;
import net.forwardfire.vasc.backend.data.HashMapVascEntryRecordCreator;
import net.forwardfire.vasc.backend.VascBackendAccessDataRecord;
import net.forwardfire.vasc.backend.crud.VascBackendCrud;
import net.forwardfire.vasc.backend.data.HashMapVascBackendAccessDataRecord;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
@ -54,91 +47,39 @@ import com.novell.ldap.LDAPSearchResults;
* @author Willem Cazander
* @version 1.0 Sep 4, 2008
*/
public class LdapVascBackend extends AbstractVascBackend<HashMap<String,Object>> {
public class LdapVascBackendCrud extends AbstractLdapVascBackend implements VascBackendCrud<HashMap<String,Serializable>,HashMap<String,Serializable>> {
private Logger logger = Logger.getLogger(LdapVascBackend.class.getName());
private LdapConnectionProvider ldapConnectionProvider = null;
private String baseDN = null;
private Logger logger = Logger.getLogger(LdapVascBackendCrud.class.getName());
private String keyAttribute = null;
private String ldapFilter = null;
private String createObjectClass = null;
private int timeLimit = 10000;
private boolean referralFollowing = true;
/**
* @return the ldapConnectionProvider.
*/
public LdapConnectionProvider getLdapConnectionProvider() {
return ldapConnectionProvider;
}
/**
* @param ldapConnectionProvider the ldapConnectionProvider to set.
*/
public void setLdapConnectionProvider(LdapConnectionProvider ldapConnectionProvider) {
this.ldapConnectionProvider = ldapConnectionProvider;
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#execute()
*/
@Override
public VascBackendResult<HashMap<String,Object>> execute(VascBackendState state) throws VascBackendException {
LdapConnectionProvider prov = getLdapConnectionProvider();
LDAPConnection connection = prov.getLdapConnection();
List<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>(50);
try {
LDAPSearchConstraints cons = new LDAPSearchConstraints();
cons.setBatchSize( 0 );
cons.setTimeLimit(getTimeLimit() ) ;
cons.setReferralFollowing(isReferralFollowing());
connection.setConstraints(cons);
protected void startBackendLdap() {
}
@Override
public VascBackendAccessDataRecord<HashMap<String, Serializable>, HashMap<String, Serializable>> createVascBackendAccessDataRecord() {
return new HashMapVascBackendAccessDataRecord(getKeyMapType());
}
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = baseDN;
LDAPSearchResults searchResults = connection.search(
searchBase, // object to read
searchScope, // scope - read single object
getLdapFilter(), // search filter
null, // return all attributes
false); // return attrs and values
while (searchResults.hasMore()) {
LDAPEntry entry = searchResults.next();
HashMap<String,Object> map = new HashMap<String,Object>(10);
LDAPAttributeSet attributeSet = entry.getAttributeSet();
Iterator<LDAPAttribute> i = attributeSet.iterator();
while (i.hasNext()) {
LDAPAttribute attr = i.next();
String[] s = attr.getStringValueArray();
if (s.length==1) {
map.put(attr.getName(), attr.getStringValue());
} else {
List<String> multiValue = new ArrayList<String>(s.length);
for (String ss:s) {
multiValue.add(ss);
}
map.put(attr.getName(), multiValue );
}
}
result.add(map);
}
} catch (LDAPException e) {
throw new VascBackendException(e);
} finally {
safeDisconnect(connection);
}
return new DefaultVascBackendResult<HashMap<String,Object>>(result);
@Override
public HashMap<String, Serializable> fetch(HashMap<String, Serializable> recordPK) throws VascBackendException {
return null;
}
@Override
public HashMap<String, Serializable> newRecord() throws VascBackendException {
return new HashMap<String,Serializable>();
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#merge(java.lang.Object)
* @see net.forwardfire.vasc.backend.crud.VascBackendCrud#merge(java.lang.Object)
*/
@Override
public HashMap<String,Object> merge(HashMap<String,Object> map) throws VascBackendException {
public HashMap<String,Serializable> merge(HashMap<String,Serializable> map) throws VascBackendException {
LdapConnectionProvider prov = getLdapConnectionProvider();
LDAPConnection connection = prov.getLdapConnection();
try {
@ -149,7 +90,7 @@ public class LdapVascBackend extends AbstractVascBackend<HashMap<String,Object>>
cons.setReferralFollowing(isReferralFollowing());
connection.setConstraints(cons);
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = baseDN;
String searchBase = getBaseDN();
String filter = "(&"+ldapFilter+"("+keyAttribute+"="+keyValue+"))";
LDAPSearchResults searchResults = connection.search(
searchBase, // object to read
@ -216,15 +157,15 @@ public class LdapVascBackend extends AbstractVascBackend<HashMap<String,Object>>
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#persist(java.lang.Object)
* @see net.forwardfire.vasc.backend.crud.VascBackendCrud#persist(java.lang.Object)
*/
@Override
public void persist(HashMap<String,Object> map) throws VascBackendException {
public HashMap<String,Serializable> persist(HashMap<String,Serializable> map) throws VascBackendException {
LdapConnectionProvider prov = getLdapConnectionProvider();
LDAPConnection connection = prov.getLdapConnection();
try {
String keyValue = (String)map.get(keyAttribute);
LDAPEntry entry = new LDAPEntry(keyAttribute+"="+keyValue+","+baseDN);
LDAPEntry entry = new LDAPEntry(keyAttribute+"="+keyValue+","+getBaseDN());
LDAPAttribute ob = new LDAPAttribute("objectClass");
String[] obs = createObjectClass.split(",");
@ -248,19 +189,20 @@ public class LdapVascBackend extends AbstractVascBackend<HashMap<String,Object>>
} finally {
safeDisconnect(connection);
}
return fetch(map);
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#delete(java.lang.Object)
* @see net.forwardfire.vasc.backend.crud.VascBackendCrud#delete(java.lang.Object)
*/
@Override
public void delete(HashMap<String,Object> map) throws VascBackendException {
public void delete(HashMap<String,Serializable> map) throws VascBackendException {
LdapConnectionProvider prov = getLdapConnectionProvider();
LDAPConnection connection = prov.getLdapConnection();
try {
String keyValue = (String)map.get(keyAttribute);
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = baseDN;
String searchBase = getBaseDN();
String filter = "(&"+ldapFilter+"("+keyAttribute+"="+keyValue+"))";
LDAPSearchResults searchResults = connection.search(
searchBase, // object to read
@ -282,47 +224,6 @@ public class LdapVascBackend extends AbstractVascBackend<HashMap<String,Object>>
}
}
private void safeDisconnect(LDAPConnection connection) {
if (connection==null) {
return;
}
try {
connection.disconnect();
} catch (LDAPException e) {
logger.warning("Error while disconnecting: "+e.getMessage());
}
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#provideVascEntryRecordCreator()
*/
@Override
public VascEntryRecordCreator provideVascEntryRecordCreator() {
return new HashMapVascEntryRecordCreator();
}
/**
* @see net.forwardfire.vasc.backend.VascBackend#provideVascEntryFieldValue()
*/
@Override
public VascEntryFieldValue provideVascEntryFieldValue() {
return new HashMapVascEntryFieldValue();
}
/**
* @return the baseDN.
*/
public String getBaseDN() {
return baseDN;
}
/**
* @param baseDN the baseDN to set.
*/
public void setBaseDN(String baseDN) {
this.baseDN = baseDN;
}
/**
* @return the keyAttribute.
*/

View file

@ -0,0 +1,220 @@
/*
* Copyright 2007-2012 forwardfire.net All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.forwardfire.vasc.backend.ldap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import net.forwardfire.vasc.backend.VascBackendException;
import net.forwardfire.vasc.backend.VascBackendAccessDataRecord;
import net.forwardfire.vasc.backend.crud.VascBackendCrud;
import net.forwardfire.vasc.backend.data.HashMapVascBackendAccessDataRecord;
import net.forwardfire.vasc.backend.list.DefaultVascBackendListResult;
import net.forwardfire.vasc.backend.list.VascBackendList;
import net.forwardfire.vasc.backend.list.VascBackendListFeature;
import net.forwardfire.vasc.backend.list.VascBackendListResult;
import net.forwardfire.vasc.backend.list.VascBackendListRequest;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPModification;
import com.novell.ldap.LDAPSearchConstraints;
import com.novell.ldap.LDAPSearchResults;
/**
* LdapVascBackend provides abstract data access for ldap.
*
* @author Willem Cazander
* @version 1.0 Sep 4, 2008
*/
public class LdapVascBackendList extends AbstractLdapVascBackend implements VascBackendList<HashMap<String,Serializable>,HashMap<String,Serializable>> {
private Logger logger = Logger.getLogger(LdapVascBackendList.class.getName());
private final EnumSet<VascBackendListFeature> features = EnumSet.noneOf(VascBackendListFeature.class);
private final List<String> rowActions = new ArrayList<String>();
private String keyAttribute = null;
private String ldapFilter = null;
private String createObjectClass = null;
private int timeLimit = 10000;
private boolean referralFollowing = true;
@Override
protected void startBackendLdap() {
// TODO Auto-generated method stub
}
@Override
public VascBackendAccessDataRecord<HashMap<String, Serializable>, HashMap<String, Serializable>> createVascBackendAccessDataRecord() {
return new HashMapVascBackendAccessDataRecord(getKeyMapType());
}
@Override
public EnumSet<VascBackendListFeature> getSupportedFeatures() {
return features;
}
@Override
public List<String> getSupportedRowActions() {
return rowActions;
}
@Override
public void executeRowAction(VascBackendListRequest request,List<HashMap<String, Serializable>> recordPKs, String actionName) throws VascBackendException {
}
/**
* @see net.forwardfire.vasc.backend.crud.VascBackendCrud#execute()
*/
@Override
public VascBackendListResult<HashMap<String,Serializable>> execute(VascBackendListRequest state) throws VascBackendException {
LdapConnectionProvider prov = getLdapConnectionProvider();
LDAPConnection connection = prov.getLdapConnection();
List<HashMap<String,Serializable>> result = new ArrayList<HashMap<String,Serializable>>(50);
try {
LDAPSearchConstraints cons = new LDAPSearchConstraints();
cons.setBatchSize( 0 );
cons.setTimeLimit(getTimeLimit() ) ;
cons.setReferralFollowing(isReferralFollowing());
connection.setConstraints(cons);
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = getBaseDN();
LDAPSearchResults searchResults = connection.search(
searchBase, // object to read
searchScope, // scope - read single object
getLdapFilter(), // search filter
null, // return all attributes
false); // return attrs and values
while (searchResults.hasMore()) {
LDAPEntry entry = searchResults.next();
HashMap<String,Serializable> map = new HashMap<String,Serializable>(10);
LDAPAttributeSet attributeSet = entry.getAttributeSet();
Iterator<LDAPAttribute> i = attributeSet.iterator();
while (i.hasNext()) {
LDAPAttribute attr = i.next();
String[] s = attr.getStringValueArray();
if (s.length==1) {
map.put(attr.getName(), attr.getStringValue());
} else {
List<String> multiValue = new ArrayList<String>(s.length);
for (String ss:s) {
multiValue.add(ss);
}
map.put(attr.getName(), (Serializable)multiValue );
}
}
result.add(map);
}
} catch (LDAPException e) {
throw new VascBackendException(e);
} finally {
safeDisconnect(connection);
}
return new DefaultVascBackendListResult<HashMap<String,Serializable>>(result);
}
/**
* @return the keyAttribute.
*/
public String getKeyAttribute() {
return keyAttribute;
}
/**
* @param keyAttribute the keyAttribute to set.
*/
public void setKeyAttribute(String keyAttribute) {
this.keyAttribute = keyAttribute;
}
/**
* @return the ldapFilter.
*/
public String getLdapFilter() {
return ldapFilter;
}
/**
* @param ldapFilter the ldapFilter to set.
*/
public void setLdapFilter(String ldapFilter) {
this.ldapFilter = ldapFilter;
}
/**
* @return the createObjectClass.
*/
public String getCreateObjectClass() {
return createObjectClass;
}
/**
* @param createObjectClass the createObjectClass to set.
*/
public void setCreateObjectClass(String createObjectClass) {
this.createObjectClass = createObjectClass;
}
/**
* @return the timeLimit.
*/
public int getTimeLimit() {
return timeLimit;
}
/**
* @param timeLimit the timeLimit to set.
*/
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
/**
* @return the referralFollowing.
*/
public boolean isReferralFollowing() {
return referralFollowing;
}
/**
* @param referralFollowing the referralFollowing to set.
*/
public void setReferralFollowing(boolean referralFollowing) {
this.referralFollowing = referralFollowing;
}
}