This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 88d9ee8568a0b453c8765ed62fad3d7213e2a620 Author: Benoit Tellier <[email protected]> AuthorDate: Thu Nov 7 09:02:00 2019 +0700 PROTOCOLS-37 [Refactoring] Get rid of FutureResponse class --- .../james/protocols/api/future/FutureResponse.java | 68 ---------- .../protocols/api/future/FutureResponseImpl.java | 149 --------------------- 2 files changed, 217 deletions(-) diff --git a/protocols/api/src/main/java/org/apache/james/protocols/api/future/FutureResponse.java b/protocols/api/src/main/java/org/apache/james/protocols/api/future/FutureResponse.java deleted file mode 100644 index 02701f1..0000000 --- a/protocols/api/src/main/java/org/apache/james/protocols/api/future/FutureResponse.java +++ /dev/null @@ -1,68 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you 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.apache.james.protocols.api.future; - -import org.apache.james.protocols.api.Response; - -/** - * An special {@link Response} which allows to populate it in an async fashion. It also allows to register listeners which will get notified once the - * {@link FutureResponse} is ready - * - * - */ -public interface FutureResponse extends Response { - - /** - * Add a {@link ResponseListener} which will get notified once {@link #isReady()} is true - * - * @param listener - */ - void addListener(ResponseListener listener); - - /** - * Remote a {@link ResponseListener} - * - * @param listener - */ - void removeListener(ResponseListener listener); - - /** - * Return <code>true</code> once the {@link FutureResponse} is ready and calling any of the get methods will not block any more. - * - * @return ready - */ - boolean isReady(); - - - /** - * Listener which will get notified once the {@link FutureResponse#isReady()} returns <code>true</code> - * - * - */ - interface ResponseListener { - - /** - * The {@link FutureResponse} is ready for processing - * - * @param response - */ - void onResponse(FutureResponse response); - } -} diff --git a/protocols/api/src/main/java/org/apache/james/protocols/api/future/FutureResponseImpl.java b/protocols/api/src/main/java/org/apache/james/protocols/api/future/FutureResponseImpl.java deleted file mode 100644 index eed41c6..0000000 --- a/protocols/api/src/main/java/org/apache/james/protocols/api/future/FutureResponseImpl.java +++ /dev/null @@ -1,149 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you 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.apache.james.protocols.api.future; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.james.protocols.api.AbstractResponse; -import org.apache.james.protocols.api.Response; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * {@link FutureResponse} implementation which wraps a {@link AbstractResponse} implementation - * - * - */ -public class FutureResponseImpl implements FutureResponse { - - private final Logger logger; - - public FutureResponseImpl() { - this(LoggerFactory.getLogger(FutureResponseImpl.class)); - } - - public FutureResponseImpl(Logger logger) { - this.logger = logger; - } - - protected Response response; - private List<ResponseListener> listeners; - private int waiters; - - protected final synchronized void checkReady() { - while (!isReady()) { - try { - waiters++; - wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } finally { - waiters--; - } - } - } - - @Override - public synchronized void addListener(ResponseListener listener) { - if (isReady()) { - listener.onResponse(this); - } else { - if (listeners == null) { - listeners = new ArrayList<>(); - } - listeners.add(listener); - } - } - - @Override - public synchronized void removeListener(ResponseListener listener) { - if (!isReady()) { - if (listeners != null) { - listeners.remove(listener); - } - } - } - - @Override - public synchronized boolean isReady() { - return response != null; - } - - @Override - public List<CharSequence> getLines() { - checkReady(); - return response.getLines(); - } - - - @Override - public String getRetCode() { - checkReady(); - return response.getRetCode(); - } - - - @Override - public boolean isEndSession() { - checkReady(); - return response.isEndSession(); - } - - @Override - public synchronized String toString() { - checkReady(); - return response.toString(); - } - - /** - * Set the {@link Response} which will be used to notify the registered - * {@link ResponseListener}'. After this method is called all waiting - * threads will get notified and {@link #isReady()} will return <code>true<code>. - * - * @param response - */ - public void setResponse(Response response) { - boolean fire = false; - synchronized (this) { - if (!isReady()) { - this.response = response; - fire = listeners != null; - - if (waiters > 0) { - notifyAll(); - } - } - } - - if (fire) { - for (ResponseListener listener : listeners) { - try { - listener.onResponse(this); - } catch (Throwable e) { - logger.warn("An exception was thrown by the listener {}", listener, e); - } - } - listeners = null; - - } - } - -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
