forked from zaproxy/zaproxy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zaproxy#2430 from thc202/mem-leak-db-listeners
Fix memory leak on session creation/loading
- Loading branch information
Showing
5 changed files
with
206 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Zed Attack Proxy (ZAP) and its related class files. | ||
* | ||
* ZAP is an HTTP/HTTPS proxy for assessing web application security. | ||
* | ||
* Copyright 2016 The ZAP Development Team | ||
* | ||
* 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.parosproxy.paros.db; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
/** | ||
* An abstract implementation of {@link Database}, it allows to manage {@link DatabaseListener DatabaseListeners}. | ||
* | ||
* @since TODO add version | ||
*/ | ||
public abstract class AbstractDatabase implements Database { | ||
|
||
protected final Logger logger = Logger.getLogger(getClass()); | ||
|
||
private final List<DatabaseListener> databaseListeners; | ||
|
||
public AbstractDatabase() { | ||
databaseListeners = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Gets the database listeners added. | ||
* | ||
* @return the database listeners | ||
* @see #addDatabaseListener(DatabaseListener) | ||
*/ | ||
protected List<DatabaseListener> getDatabaseListeners() { | ||
return databaseListeners; | ||
} | ||
|
||
@Override | ||
public void addDatabaseListener(DatabaseListener listener) { | ||
databaseListeners.add(listener); | ||
} | ||
|
||
@Override | ||
public void removeDatabaseListener(DatabaseListener listener) { | ||
databaseListeners.remove(listener); | ||
} | ||
|
||
@Override | ||
public void close(boolean compact) { | ||
close(compact, true); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* <p> | ||
* Also, removes the database listeners added previously. | ||
*/ | ||
@Override | ||
public void close(boolean compact, boolean cleanup) { | ||
removeDatabaseListeners(); | ||
} | ||
|
||
/** | ||
* Removes all database listeners added. | ||
* | ||
* @see #removeDatabaseListener(DatabaseListener) | ||
*/ | ||
protected void removeDatabaseListeners() { | ||
databaseListeners.clear(); | ||
} | ||
|
||
/** | ||
* Notifies the given listeners that the given database server was opened. | ||
* | ||
* @param listeners the listeners that will be notified | ||
* @param databaseServer the database server that was opened | ||
* @throws DatabaseException if an error occurred while notifying the database listeners. | ||
*/ | ||
protected void notifyListenersDatabaseOpen(Collection<DatabaseListener> listeners, DatabaseServer databaseServer) | ||
throws DatabaseException { | ||
for (DatabaseListener databaseListener : listeners) { | ||
try { | ||
databaseListener.databaseOpen(databaseServer); | ||
} catch (DatabaseUnsupportedException e) { | ||
logger.error(e.getMessage(), e); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Notifies the database listeners added that the given database server was opened. | ||
* | ||
* @param databaseServer the database server that was opened | ||
* @throws DatabaseException if an error occurred while notifying the database listeners. | ||
*/ | ||
protected void notifyListenersDatabaseOpen(DatabaseServer databaseServer) throws DatabaseException { | ||
notifyListenersDatabaseOpen(databaseListeners, databaseServer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.