Example usage for java.lang.ref WeakReference WeakReference

List of usage examples for java.lang.ref WeakReference WeakReference

Introduction

In this page you can find the example usage for java.lang.ref WeakReference WeakReference.

Prototype

public WeakReference(T referent) 

Source Link

Document

Creates a new weak reference that refers to the given object.

Usage

From source file:org.eclipse.hono.service.amqp.AmqpServiceBase.java

/**
 * Processes a peer's AMQP <em>open</em> frame.
 * <p>//from  www .  ja  v a  2s . c o  m
 * This default implementation
 * <ol>
 * <li>adds a unique connection identifier to the connection's attachments
 * under key {@link Constants#KEY_CONNECTION_ID}</li>
 * <li>invokes {@link #processDesiredCapabilities(ProtonConnection, Symbol[])}</li>
 * <li>sets a timer that closes the connection once the client's token
 * has expired</li>
 * <li>sends the AMQP <em>open</em> frame to the peer</li>
 * </ol>
 * 
 * @param connection The connection to open.
 */
protected void processRemoteOpen(final ProtonConnection connection) {

    final HonoUser clientPrincipal = Constants.getClientPrincipal(connection);
    LOG.debug("client [container: {}, user: {}] connected", connection.getRemoteContainer(),
            clientPrincipal.getName());
    // attach an ID so that we can later inform downstream components when connection is closed
    connection.attachments().set(Constants.KEY_CONNECTION_ID, String.class, UUID.randomUUID().toString());
    processDesiredCapabilities(connection, connection.getRemoteDesiredCapabilities());
    final Duration delay = Duration.between(Instant.now(), clientPrincipal.getExpirationTime());
    final WeakReference<ProtonConnection> conRef = new WeakReference<>(connection);
    vertx.setTimer(delay.toMillis(), timerId -> {
        if (conRef.get() != null) {
            closeExpiredConnection(conRef.get());
        }
    });
    connection.open();
}

From source file:com.pdfread.standardreader.net.AsyncHttpClient.java

protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
        String contentType, AsyncHttpResponseHandler responseHandler, Context context) {

    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }// w w w  .jav  a2  s  . c  om

    Future<?> request = threadPool
            .submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));

    if (context != null) {
        // Add request to request map
        List<WeakReference<Future<?>>> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList<WeakReference<Future<?>>>();
            requestMap.put(context, requestList);
        }

        requestList.add(new WeakReference<Future<?>>(request));

        // TODO: Remove dead weakrefs from requestLists?
    }
}

From source file:com.google.blockly.model.BlockFactory.java

/**
 * Updates the list of options used by dropdowns in select block types. These fields must be
 * derived from the prototype blocks loaded via JSON (via {@link #obtainBlock}), and where
 * {@link FieldDropdown#setOptions(FieldDropdown.Options)} has not been called. An instance of
 * the block must already exist, usually the prototype loaded via JSON.
 *
 * @param blockType The name for the type of block containing such fields
 * @param fieldName The name of the field within the block.
 * @param optionList The list of {@link FieldDropdown.Option}s used to to set for the
 *                   referenced dropdowns.
 *///from w ww. jav a  2  s.  co  m
public void updateDropdownOptions(String blockType, String fieldName, List<FieldDropdown.Option> optionList) {
    BlockTypeFieldName key = new BlockTypeFieldName(blockType, fieldName);
    WeakReference<FieldDropdown.Options> sharedOptionsRef = mDropdownOptions.get(key);
    FieldDropdown.Options sharedOptions = sharedOptionsRef == null ? null : sharedOptionsRef.get();
    if (sharedOptions == null) {
        sharedOptions = new FieldDropdown.Options(optionList);
        mDropdownOptions.put(key, new WeakReference<>(sharedOptions));
    } else {
        sharedOptions.updateOptions(optionList);
    }
}

From source file:com.xiao.smartband.http.AsyncHttpClient.java

private void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
        String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }/* w w w  . ja  v a2s.c om*/
    Future<?> request = threadPool
            .submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));

    if (context != null) {
        // Add request to request map
        List<WeakReference<Future<?>>> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList<WeakReference<Future<?>>>();
            requestMap.put(context, requestList);
        }

        requestList.add(new WeakReference<Future<?>>(request));

        // TODO: Remove dead weakrefs from requestLists?
    }
}

From source file:com.ytrain.mutrain.utils.asynchttp.AsyncHttpClient.java

protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
        String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }/*from  ww  w. ja  v  a 2  s. c  om*/

    Future<?> request = threadPool
            .submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));

    if (context != null) {

        List<WeakReference<Future<?>>> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList<WeakReference<Future<?>>>();
            requestMap.put(context, requestList);
        }

        requestList.add(new WeakReference<Future<?>>(request));

    }
}

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.link_and_brush.LinkAndBrushChartPanel.java

/**
 * Add a {@link LinkAndBrushSelectionListener}. The listener is saved as a {@link WeakReference}
 * . Thus listener must not be hidden (anonymous) classes!
 *///from w w w . j  a v  a2 s .c o  m
public void addLinkAndBrushSelectionListener(LinkAndBrushSelectionListener l) {
    listeners.add(new WeakReference<LinkAndBrushSelectionListener>(l));
}

From source file:com.mustardgrain.solr.SolrClient.java

private void startAliveCheckExecutor() {
    // double-checked locking, but it's OK because we don't *do* anything
    // with aliveCheckExecutor
    // if it's not null.
    if (aliveCheckExecutor == null) {
        synchronized (this) {
            if (aliveCheckExecutor == null) {
                aliveCheckExecutor = Executors
                        .newSingleThreadScheduledExecutor(new SolrjNamedThreadFactory("aliveCheckExecutor"));
                aliveCheckExecutor.scheduleAtFixedRate(getAliveCheckRunner(new WeakReference<SolrClient>(this)),
                        this.interval, this.interval, TimeUnit.MILLISECONDS);
            }/* w  ww . j  a  v a 2 s. c  o  m*/
        }
    }
}

From source file:org.apache.solr.client.solrj.impl.LBHttpSolrClient.java

private void startAliveCheckExecutor() {
    // double-checked locking, but it's OK because we don't *do* anything with aliveCheckExecutor
    // if it's not null.
    if (aliveCheckExecutor == null) {
        synchronized (this) {
            if (aliveCheckExecutor == null) {
                aliveCheckExecutor = Executors
                        .newSingleThreadScheduledExecutor(new SolrjNamedThreadFactory("aliveCheckExecutor"));
                aliveCheckExecutor.scheduleAtFixedRate(getAliveCheckRunner(new WeakReference<>(this)),
                        this.interval, this.interval, TimeUnit.MILLISECONDS);
            }//from w  w w .j  a  va 2s . c  o  m
        }
    }
}

From source file:net.java.sip.communicator.plugin.notificationwiring.NotificationManager.java

/**
 * Implements CallListener.incomingCallReceived. When a call is received
 * plays the ring phone sound to the user and gathers caller information
 * that may be used by a user-specified command (incomingCall event
 * trigger).//from  w w w . j  a  v  a 2s. c  om
 *
 * @param ev the <tt>CallEvent</tt>
 */
public void incomingCallReceived(CallEvent ev) {
    try {
        Call call = ev.getSourceCall();
        CallPeer peer = call.getCallPeers().next();
        Map<String, String> peerInfo = new HashMap<String, String>();
        String peerName = peer.getDisplayName();

        peerInfo.put("caller.uri", peer.getURI());
        peerInfo.put("caller.address", peer.getAddress());
        peerInfo.put("caller.name", peerName);
        peerInfo.put("caller.id", peer.getPeerID());

        /*
         * The loopCondition will stay with the notification sound until the
         * latter is stopped. If by any chance the sound fails to stop by
         * the time the call is no longer referenced, do try to stop it
         * then. That's why the loopCondition will weakly reference the
         * call.
         */
        final WeakReference<Call> weakCall = new WeakReference<Call>(call);
        NotificationData notification = fireNotification(INCOMING_CALL, "",
                ev.isDesktopStreaming()
                        ? NotificationWiringActivator.getResources()
                                .getI18NString("service.gui.INCOMING_SCREEN_SHARE", new String[] { peerName })
                        : NotificationWiringActivator.getResources().getI18NString("service.gui.INCOMING_CALL",
                                new String[] { peerName }),
                peerInfo, new Callable<Boolean>() {
                    public Boolean call() {
                        Call call = weakCall.get();

                        if (call == null)
                            return false;

                        /*
                         * INCOMING_CALL should be played for a Call
                         * only while there is a CallPeer in the
                         * INCOMING_CALL state.
                         */
                        Iterator<? extends CallPeer> peerIter = call.getCallPeers();
                        boolean loop = false;

                        while (peerIter.hasNext()) {
                            CallPeer peer = peerIter.next();

                            if (CallPeerState.INCOMING_CALL.equals(peer.getState())) {
                                loop = true;
                                break;
                            }
                        }
                        return loop;
                    }
                });

        if (notification != null)
            callNotifications.put(call, notification);

        call.addCallChangeListener(this);

        peer.addCallPeerListener(this);
        peer.addCallPeerSecurityListener(this);
        peer.addCallPeerConferenceListener(this);
    } catch (Throwable t) {
        if (t instanceof ThreadDeath)
            throw (ThreadDeath) t;
        else {
            logger.error("An error occurred while trying to notify" + " about an incoming call", t);
        }
    }
}

From source file:cn.xdf.thinkutils.http2.AsyncHttpClient.java

protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest,
        String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
    if (contentType != null) {
        uriRequest.addHeader("Content-Type", contentType);
    }// w  w  w  .java 2  s .c  o m

    Future<?> request = threadPool
            .submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));
    if (context != null) {
        // Add request to request map
        List<WeakReference<Future<?>>> requestList = requestMap.get(context);
        if (requestList == null) {
            requestList = new LinkedList<WeakReference<Future<?>>>();
            requestMap.put(context, requestList);
        }
        requestList.add(new WeakReference<Future<?>>(request));
        // TODO: Remove dead weakrefs from requestLists?
    }

}